|
| 1 | +package io.objectbox; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | + |
| 8 | +import io.objectbox.exception.FileCorruptException; |
| 9 | +import io.objectbox.exception.PagesCorruptException; |
| 10 | +import io.objectbox.model.ValidateOnOpenMode; |
| 11 | +import org.greenrobot.essentials.io.IoUtils; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertNotNull; |
| 17 | +import static org.junit.Assert.assertThrows; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | +import static org.junit.Assert.fail; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests validation (and recovery) options on opening a store. |
| 23 | + */ |
| 24 | +public class BoxStoreValidationTest extends AbstractObjectBoxTest { |
| 25 | + |
| 26 | + private BoxStoreBuilder builder; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected BoxStore createBoxStore() { |
| 30 | + // Standard setup of store not required |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setUpBuilder() { |
| 36 | + BoxStore.clearDefaultStore(); |
| 37 | + builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void validateOnOpen() { |
| 42 | + // Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time) |
| 43 | + byte[] model = createTestModel(null); |
| 44 | + long id = buildNotCorruptedDatabase(model); |
| 45 | + |
| 46 | + // Then re-open database with validation and ensure db is operational |
| 47 | + builder = new BoxStoreBuilder(model).directory(boxStoreDir); |
| 48 | + builder.entity(new TestEntity_()); |
| 49 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 50 | + store = builder.build(); |
| 51 | + assertNotNull(getTestEntityBox().get(id)); |
| 52 | + getTestEntityBox().put(new TestEntity(0)); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Test |
| 57 | + public void validateOnOpenCorruptFile() throws IOException { |
| 58 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 59 | + prepareBadDataFile(dir, "corrupt-pageno-in-branch-data.mdb"); |
| 60 | + |
| 61 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 62 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 63 | + |
| 64 | + @SuppressWarnings("resource") |
| 65 | + FileCorruptException ex = assertThrows(PagesCorruptException.class, () -> builder.build()); |
| 66 | + assertEquals("Validating pages failed (page not found)", ex.getMessage()); |
| 67 | + |
| 68 | + // Clean up |
| 69 | + deleteAllFiles(dir); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void usePreviousCommitWithCorruptFile() throws IOException { |
| 74 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 75 | + prepareBadDataFile(dir, "corrupt-pageno-in-branch-data.mdb"); |
| 76 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 77 | + builder.validateOnOpen(ValidateOnOpenMode.Full).usePreviousCommit(); |
| 78 | + store = builder.build(); |
| 79 | + String diagnoseString = store.diagnose(); |
| 80 | + assertTrue(diagnoseString.contains("entries=2")); |
| 81 | + store.validate(0, true); |
| 82 | + store.close(); |
| 83 | + assertTrue(store.deleteAllFiles()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void usePreviousCommitAfterFileCorruptException() throws IOException { |
| 88 | + File dir = prepareTempDir("object-store-test-corrupted"); |
| 89 | + prepareBadDataFile(dir, "corrupt-pageno-in-branch-data.mdb"); |
| 90 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 91 | + builder.validateOnOpen(ValidateOnOpenMode.Full); |
| 92 | + try { |
| 93 | + store = builder.build(); |
| 94 | + fail("Should have thrown"); |
| 95 | + } catch (PagesCorruptException e) { |
| 96 | + builder.usePreviousCommit(); |
| 97 | + store = builder.build(); |
| 98 | + } |
| 99 | + |
| 100 | + String diagnoseString = store.diagnose(); |
| 101 | + assertTrue(diagnoseString.contains("entries=2")); |
| 102 | + store.validate(0, true); |
| 103 | + store.close(); |
| 104 | + assertTrue(store.deleteAllFiles()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void validateOnOpenKv() { |
| 109 | + // Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time) |
| 110 | + byte[] model = createTestModel(null); |
| 111 | + long id = buildNotCorruptedDatabase(model); |
| 112 | + |
| 113 | + // Then re-open database with validation and ensure db is operational |
| 114 | + builder = new BoxStoreBuilder(model).directory(boxStoreDir); |
| 115 | + builder.entity(new TestEntity_()); |
| 116 | + builder.validateOnOpenKv(); |
| 117 | + store = builder.build(); |
| 118 | + assertNotNull(getTestEntityBox().get(id)); |
| 119 | + getTestEntityBox().put(new TestEntity(0)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void validateOnOpenKvCorruptFile() throws IOException { |
| 124 | + File dir = prepareTempDir("obx-store-validate-kv-corrupted"); |
| 125 | + prepareBadDataFile(dir, "corrupt-keysize0-data.mdb"); |
| 126 | + |
| 127 | + builder = BoxStoreBuilder.createDebugWithoutModel().directory(dir); |
| 128 | + builder.validateOnOpenKv(); |
| 129 | + |
| 130 | + @SuppressWarnings("resource") |
| 131 | + FileCorruptException ex = assertThrows(FileCorruptException.class, () -> builder.build()); |
| 132 | + assertEquals("KV validation failed; key is empty (KV pair number: 1, key size: 0, data size: 112)", |
| 133 | + ex.getMessage()); |
| 134 | + |
| 135 | + // Clean up |
| 136 | + deleteAllFiles(dir); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns the id of the inserted test entity. |
| 141 | + */ |
| 142 | + private long buildNotCorruptedDatabase(byte[] model) { |
| 143 | + builder = new BoxStoreBuilder(model).directory(boxStoreDir); |
| 144 | + builder.entity(new TestEntity_()); |
| 145 | + store = builder.build(); |
| 146 | + |
| 147 | + TestEntity object = new TestEntity(0); |
| 148 | + object.setSimpleString("hello hello"); |
| 149 | + long id = getTestEntityBox().put(object); |
| 150 | + store.close(); |
| 151 | + return id; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Copies the given file from resources to the given directory as "data.mdb". |
| 156 | + */ |
| 157 | + private void prepareBadDataFile(File dir, String resourceName) throws IOException { |
| 158 | + assertTrue(dir.mkdir()); |
| 159 | + File badDataFile = new File(dir, "data.mdb"); |
| 160 | + try (InputStream badIn = getClass().getResourceAsStream(resourceName)) { |
| 161 | + assertNotNull(badIn); |
| 162 | + try (FileOutputStream badOut = new FileOutputStream(badDataFile)) { |
| 163 | + IoUtils.copyAllBytes(badIn, badOut); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments