Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.healthreport; |
michael@0 | 5 | |
michael@0 | 6 | import java.io.File; |
michael@0 | 7 | import java.io.IOException; |
michael@0 | 8 | |
michael@0 | 9 | import org.json.JSONException; |
michael@0 | 10 | import org.json.JSONObject; |
michael@0 | 11 | import org.mozilla.gecko.background.helpers.FakeProfileTestCase; |
michael@0 | 12 | |
michael@0 | 13 | public class TestProfileInformationCache extends FakeProfileTestCase { |
michael@0 | 14 | |
michael@0 | 15 | public final void testInitState() throws IOException { |
michael@0 | 16 | MockProfileInformationCache cache = new MockProfileInformationCache(this.fakeProfileDirectory.getAbsolutePath()); |
michael@0 | 17 | assertFalse(cache.isInitialized()); |
michael@0 | 18 | assertFalse(cache.needsWrite()); |
michael@0 | 19 | |
michael@0 | 20 | try { |
michael@0 | 21 | cache.isBlocklistEnabled(); |
michael@0 | 22 | fail("Should throw fetching isBlocklistEnabled."); |
michael@0 | 23 | } catch (IllegalStateException e) { |
michael@0 | 24 | // Great! |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | cache.beginInitialization(); |
michael@0 | 28 | assertFalse(cache.isInitialized()); |
michael@0 | 29 | assertTrue(cache.needsWrite()); |
michael@0 | 30 | |
michael@0 | 31 | try { |
michael@0 | 32 | cache.isBlocklistEnabled(); |
michael@0 | 33 | fail("Should throw fetching isBlocklistEnabled."); |
michael@0 | 34 | } catch (IllegalStateException e) { |
michael@0 | 35 | // Great! |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | cache.completeInitialization(); |
michael@0 | 39 | assertTrue(cache.isInitialized()); |
michael@0 | 40 | assertFalse(cache.needsWrite()); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | public final MockProfileInformationCache makeCache(final String suffix) { |
michael@0 | 44 | File subdir = new File(this.fakeProfileDirectory.getAbsolutePath() + File.separator + suffix); |
michael@0 | 45 | subdir.mkdir(); |
michael@0 | 46 | return new MockProfileInformationCache(subdir.getAbsolutePath()); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | public final void testPersisting() throws IOException { |
michael@0 | 50 | MockProfileInformationCache cache = makeCache("testPersisting"); |
michael@0 | 51 | // We start with no file. |
michael@0 | 52 | assertFalse(cache.getFile().exists()); |
michael@0 | 53 | |
michael@0 | 54 | // Partially populate. Note that this doesn't happen in live code, but |
michael@0 | 55 | // apparently we can end up with null add-ons JSON on disk, so this |
michael@0 | 56 | // reproduces that scenario. |
michael@0 | 57 | cache.beginInitialization(); |
michael@0 | 58 | cache.setBlocklistEnabled(true); |
michael@0 | 59 | cache.setTelemetryEnabled(true); |
michael@0 | 60 | cache.setProfileCreationTime(1234L); |
michael@0 | 61 | cache.completeInitialization(); |
michael@0 | 62 | |
michael@0 | 63 | assertTrue(cache.getFile().exists()); |
michael@0 | 64 | |
michael@0 | 65 | // But reading this from disk won't work, because we were only partially |
michael@0 | 66 | // initialized. We want to start over. |
michael@0 | 67 | cache = makeCache("testPersisting"); |
michael@0 | 68 | assertFalse(cache.isInitialized()); |
michael@0 | 69 | assertFalse(cache.restoreUnlessInitialized()); |
michael@0 | 70 | assertFalse(cache.isInitialized()); |
michael@0 | 71 | |
michael@0 | 72 | // Now fully populate, and try again... |
michael@0 | 73 | cache.beginInitialization(); |
michael@0 | 74 | cache.setBlocklistEnabled(true); |
michael@0 | 75 | cache.setTelemetryEnabled(true); |
michael@0 | 76 | cache.setProfileCreationTime(1234L); |
michael@0 | 77 | cache.setJSONForAddons(new JSONObject()); |
michael@0 | 78 | cache.completeInitialization(); |
michael@0 | 79 | assertTrue(cache.getFile().exists()); |
michael@0 | 80 | |
michael@0 | 81 | // ... and this time we succeed. |
michael@0 | 82 | cache = makeCache("testPersisting"); |
michael@0 | 83 | assertFalse(cache.isInitialized()); |
michael@0 | 84 | assertTrue(cache.restoreUnlessInitialized()); |
michael@0 | 85 | assertTrue(cache.isInitialized()); |
michael@0 | 86 | assertTrue(cache.isBlocklistEnabled()); |
michael@0 | 87 | assertTrue(cache.isTelemetryEnabled()); |
michael@0 | 88 | assertEquals(1234L, cache.getProfileCreationTime()); |
michael@0 | 89 | |
michael@0 | 90 | // Mutate. |
michael@0 | 91 | cache.beginInitialization(); |
michael@0 | 92 | assertFalse(cache.isInitialized()); |
michael@0 | 93 | cache.setBlocklistEnabled(false); |
michael@0 | 94 | cache.setProfileCreationTime(2345L); |
michael@0 | 95 | cache.completeInitialization(); |
michael@0 | 96 | assertTrue(cache.isInitialized()); |
michael@0 | 97 | |
michael@0 | 98 | cache = makeCache("testPersisting"); |
michael@0 | 99 | assertFalse(cache.isInitialized()); |
michael@0 | 100 | assertTrue(cache.restoreUnlessInitialized()); |
michael@0 | 101 | |
michael@0 | 102 | assertTrue(cache.isInitialized()); |
michael@0 | 103 | assertFalse(cache.isBlocklistEnabled()); |
michael@0 | 104 | assertTrue(cache.isTelemetryEnabled()); |
michael@0 | 105 | assertEquals(2345L, cache.getProfileCreationTime()); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | public final void testVersioning() throws JSONException, IOException { |
michael@0 | 109 | MockProfileInformationCache cache = makeCache("testVersioning"); |
michael@0 | 110 | final int currentVersion = ProfileInformationCache.FORMAT_VERSION; |
michael@0 | 111 | final JSONObject json = cache.toJSON(); |
michael@0 | 112 | assertEquals(currentVersion, json.getInt("version")); |
michael@0 | 113 | |
michael@0 | 114 | // Initialize enough that we can re-load it. |
michael@0 | 115 | cache.beginInitialization(); |
michael@0 | 116 | cache.setJSONForAddons(new JSONObject()); |
michael@0 | 117 | cache.completeInitialization(); |
michael@0 | 118 | cache.writeJSON(json); |
michael@0 | 119 | assertTrue(cache.restoreUnlessInitialized()); |
michael@0 | 120 | cache.beginInitialization(); // So that we'll need to read again. |
michael@0 | 121 | json.put("version", currentVersion + 1); |
michael@0 | 122 | cache.writeJSON(json); |
michael@0 | 123 | |
michael@0 | 124 | // We can't restore a future version. |
michael@0 | 125 | assertFalse(cache.restoreUnlessInitialized()); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | public void testRestoreInvalidJSON() throws Exception { |
michael@0 | 129 | final MockProfileInformationCache cache = makeCache("invalid"); |
michael@0 | 130 | |
michael@0 | 131 | final JSONObject invalidJSON = new JSONObject(); |
michael@0 | 132 | invalidJSON.put("blocklist", true); |
michael@0 | 133 | invalidJSON.put("telemetry", false); |
michael@0 | 134 | invalidJSON.put("profileCreated", 1234567L); |
michael@0 | 135 | cache.writeJSON(invalidJSON); |
michael@0 | 136 | assertFalse(cache.restoreUnlessInitialized()); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | private JSONObject getValidCacheJSON() throws Exception { |
michael@0 | 140 | final JSONObject json = new JSONObject(); |
michael@0 | 141 | json.put("blocklist", true); |
michael@0 | 142 | json.put("telemetry", false); |
michael@0 | 143 | json.put("profileCreated", 1234567L); |
michael@0 | 144 | json.put("addons", new JSONObject()); |
michael@0 | 145 | json.put("version", ProfileInformationCache.FORMAT_VERSION); |
michael@0 | 146 | return json; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | public void testRestoreImplicitV1() throws Exception { |
michael@0 | 150 | assertTrue(ProfileInformationCache.FORMAT_VERSION > 1); |
michael@0 | 151 | |
michael@0 | 152 | final MockProfileInformationCache cache = makeCache("implicitV1"); |
michael@0 | 153 | final JSONObject json = getValidCacheJSON(); |
michael@0 | 154 | json.remove("version"); |
michael@0 | 155 | cache.writeJSON(json); |
michael@0 | 156 | // Can't restore v1 (which is implicitly set) since it is not the current version. |
michael@0 | 157 | assertFalse(cache.restoreUnlessInitialized()); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | public void testRestoreOldVersion() throws Exception { |
michael@0 | 161 | final int oldVersion = 1; |
michael@0 | 162 | assertTrue(ProfileInformationCache.FORMAT_VERSION > oldVersion); |
michael@0 | 163 | |
michael@0 | 164 | final MockProfileInformationCache cache = makeCache("oldVersion"); |
michael@0 | 165 | final JSONObject json = getValidCacheJSON(); |
michael@0 | 166 | json.put("version", oldVersion); |
michael@0 | 167 | cache.writeJSON(json); |
michael@0 | 168 | assertFalse(cache.restoreUnlessInitialized()); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | public void testRestoreCurrentVersion() throws Exception { |
michael@0 | 172 | final MockProfileInformationCache cache = makeCache("currentVersion"); |
michael@0 | 173 | final JSONObject json = getValidCacheJSON(); |
michael@0 | 174 | cache.writeJSON(json); |
michael@0 | 175 | cache.beginInitialization(); |
michael@0 | 176 | cache.setTelemetryEnabled(true); |
michael@0 | 177 | cache.completeInitialization(); |
michael@0 | 178 | assertEquals(ProfileInformationCache.FORMAT_VERSION, cache.readJSON().getInt("version")); |
michael@0 | 179 | } |
michael@0 | 180 | } |