Bug 1502435 - Update tests to include checks for canary clientId. r=jchen, a=pascalc
authorPetru Lingurar <petru.lingurar@softvision.ro>
Wed, 31 Oct 2018 01:14:57 +0200
changeset 492974 3acb6478c0ab
parent 492973 6166ae37d035
child 492975 805f775f35e2
push id1844
push userryanvm@gmail.com
push dateMon, 05 Nov 2018 15:21:08 +0000
treeherdermozilla-release@805f775f35e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen, pascalc
bugs1502435
milestone63.0.2
Bug 1502435 - Update tests to include checks for canary clientId. r=jchen, a=pascalc
mobile/android/app/src/test/java/org/mozilla/gecko/TestGeckoProfile.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testUnifiedTelemetryClientId.java
--- a/mobile/android/app/src/test/java/org/mozilla/gecko/TestGeckoProfile.java
+++ b/mobile/android/app/src/test/java/org/mozilla/gecko/TestGeckoProfile.java
@@ -26,16 +26,17 @@ import static org.junit.Assert.*;
 /**
  * Unit test methods of the GeckoProfile class.
  */
 @RunWith(RobolectricTestRunner.class)
 public class TestGeckoProfile {
     private static final String PROFILE_NAME = "profileName";
 
     private static final String CLIENT_ID_JSON_ATTR = "clientID";
+    private static final String CLIENT_ID_CANARY = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";
     private static final String PROFILE_CREATION_DATE_JSON_ATTR = "created";
 
     @Rule
     public TemporaryFolder dirContainingProfile = new TemporaryFolder();
 
     private File profileDir;
     private GeckoProfile profile;
 
@@ -47,19 +48,22 @@ public class TestGeckoProfile {
         final Context context = RuntimeEnvironment.application;
         profileDir = dirContainingProfile.newFolder();
         profile = GeckoProfile.get(context, PROFILE_NAME, profileDir);
 
         clientIdFile = new File(profileDir, "datareporting/state.json");
         timesFile = new File(profileDir, "times.json");
     }
 
-    public void assertValidClientId(final String clientId) {
+    private void assertValidClientId(final String clientId) throws IllegalStateException {
         // This isn't the method we use in the main GeckoProfile code, but it should be equivalent.
         UUID.fromString(clientId); // assert: will throw if null or invalid UUID.
+        if (CLIENT_ID_CANARY.equals(clientId)) {
+            throw new IllegalArgumentException("Canary client id: " + clientId);
+        }
     }
 
     @Test
     public void testGetDir() {
         assertEquals("Profile dir argument during construction and returned value are equal",
                 profileDir, profile.getDir());
     }
 
@@ -72,16 +76,25 @@ public class TestGeckoProfile {
         assertValidClientId(clientId);
         assertTrue("client ID file exists", clientIdFile.exists());
 
         assertEquals("Returned client ID is the same as the one previously returned", clientId, profile.getClientId());
         assertEquals("clientID file format matches expectations", clientId, readClientIdFromFile(clientIdFile));
     }
 
     @Test
+    public void testIfHadCanaryClientIdForFreshProfile() throws Exception {
+         assertFalse("client ID file does not exist", clientIdFile.exists());
+
+        // Asking for getClientId() will create a new one if none existed
+        profile.getClientId();
+        assertEquals(false, profile.getIfHadCanaryClientId());
+    }
+
+    @Test
     public void testGetClientIdFileAlreadyExists() throws Exception {
         final String validClientId = "905de1c0-0ea6-4a43-95f9-6170035f5a82";
         assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
         writeClientIdToFile(clientIdFile, validClientId);
 
         final String clientIdFromProfile = profile.getClientId();
         assertEquals("Client ID from method matches ID written to disk", validClientId, clientIdFromProfile);
     }
@@ -121,16 +134,57 @@ public class TestGeckoProfile {
         FileUtils.writeStringToFile(clientIdFile, "clientID: \"" + validClientId + "\"");
 
         final String clientIdForInvalidFormat = profile.getClientId();
         assertValidClientId(clientIdForInvalidFormat);
         assertNotEquals("Created new ID when file format was invalid", validClientId, clientIdForInvalidFormat);
     }
 
     @Test
+    public void testGetClientIdForCanaryId() throws Exception {
+        assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
+        writeClientIdToFile(clientIdFile, CLIENT_ID_CANARY);
+
+        final String clientIdForCanaryId = profile.getClientId();
+        assertValidClientId(clientIdForCanaryId);
+        assertNotEquals(CLIENT_ID_CANARY, clientIdForCanaryId);
+    }
+
+    @Test
+    public void testCanaryFlagForValidId() throws Exception {
+        assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
+        final String generatedValidClientId = UUID.randomUUID().toString();
+        writeClientIdToFile(clientIdFile, generatedValidClientId);
+        createCanaryIdFlag();
+
+        final boolean hasCanaryFlagForValidId = profile.getIfHadCanaryClientId();
+        assertEquals(false, hasCanaryFlagForValidId);
+    }
+
+    @Test
+    public void testCanaryIdFlagForInvalidId() throws Exception {
+        assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
+        writeClientIdToFile(clientIdFile, "invalid");
+        createCanaryIdFlag();
+
+        final boolean hasCanaryFlagForInvalidId = profile.getIfHadCanaryClientId();
+        assertEquals(false, hasCanaryFlagForInvalidId);
+    }
+
+    @Test
+    public void testCanaryIdFlagForCanaryId() throws Exception {
+        assertTrue("Created the parent dirs of the client ID file", clientIdFile.getParentFile().mkdirs());
+        writeClientIdToFile(clientIdFile, CLIENT_ID_CANARY);
+        createCanaryIdFlag();
+
+        final boolean hasCanaryFlagForCanaryId = profile.getIfHadCanaryClientId();
+        assertEquals(true, hasCanaryFlagForCanaryId);
+    }
+
+    @Test
     public void testEnsureParentDirs() {
         final File grandParentDir = new File(profileDir, "grandParent");
         final File parentDir = new File(grandParentDir, "parent");
         final File childFile = new File(parentDir, "child");
 
         // Assert initial state.
         assertFalse("Topmost parent dir should not exist yet", grandParentDir.exists());
         assertFalse("Bottommost parent dir should not exist yet", parentDir.exists());
@@ -166,16 +220,17 @@ public class TestGeckoProfile {
                 null,
                 "",
                 "a",
                 "anInvalidClientId",
                 "905de1c0-0ea6-4a43-95f9-6170035f5a820", // too long (last section)
                 "905de1c0-0ea6-4a43-95f9-6170035f5a8", // too short (last section)
                 "05de1c0-0ea6-4a43-95f9-6170035f5a82", // too short (first section)
                 "905de1c0-0ea6-4a43-95f9-6170035f5a8!", // contains a symbol
+                CLIENT_ID_CANARY,
         };
         for (final String invalidClientId : invalidClientIds) {
             assertFalse("Client ID, " + invalidClientId + ", is invalid", GeckoProfile.isClientIdValid(invalidClientId));
         }
 
         // We generate client IDs using UUID - better make sure they're valid.
         for (int i = 0; i < 30; ++i) {
             final String generatedClientId = UUID.randomUUID().toString();
@@ -226,9 +281,13 @@ public class TestGeckoProfile {
         return obj.getString(CLIENT_ID_JSON_ATTR);
     }
 
     private void writeClientIdToFile(final File file, final String clientId) throws Exception {
         final JSONObject obj = new JSONObject();
         obj.put(CLIENT_ID_JSON_ATTR, clientId);
         FileUtils.writeJSONObjectToFile(file, obj);
     }
+
+    private void createCanaryIdFlag() throws Exception{
+        profile.getClientId();
+    }
 }
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testUnifiedTelemetryClientId.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testUnifiedTelemetryClientId.java
@@ -10,16 +10,17 @@ import android.util.Log;
 import org.mozilla.gecko.GeckoProfile;
 
 import java.io.File;
 import java.io.IOException;
 
 public class testUnifiedTelemetryClientId extends JavascriptBridgeTest {
     private static final String TEST_JS = "testUnifiedTelemetryClientId.js";
     private static final String CLIENT_ID_PATH = "datareporting/state.json";
+    private static final String CLIENT_ID_CANARY = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";
 
     private GeckoProfile profile;
     private File profileDir;
     private File[] filesToDeleteOnReset;
 
     public void setUp() throws Exception {
         super.setUp();
         profile = getTestProfile();
@@ -133,16 +134,17 @@ public class testUnifiedTelemetryClientI
         fAssertEquals("Same client ID retrieved from Java", clientIdFromJS, clientIdFromJavaAgain);
     }
 
     private String getClientIdFromJava() throws IOException {
         // This assumes implementation details: it assumes the client ID
         // file is created when Java attempts to retrieve it if it does not exist.
         final String clientId = profile.getClientId();
         fAssertNotNull("Returned client ID is not null", clientId);
+        fAssertNotEquals("Client ID is not the canary id", clientId, CLIENT_ID_CANARY);
         fAssertTrue("Client ID file exists after getClientId call", getClientIdFile().exists());
         return clientId;
     }
 
     private String getClientIdFromJS() {
         return getBlockingFromJsString("clientId");
     }