Bug 1156277 - Fix intermittent failures in test_IE_cookies.js by randomizing and clearing cookies. r=gijs, a=test-only
authorMarco Bonardo <mbonardo@mozilla.com>
Fri, 28 Aug 2015 14:19:06 +0200
changeset 288998 a9438a2e6b2771b18d2c06432727dd79df9fec4b
parent 288997 ddb0b7c40d6a0a0b0b122d7cfe7b0790e0c4311b
child 288999 98983a6fd50d6eb8166116c12d980694be24101e
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs, test-only
bugs1156277
milestone42.0a2
Bug 1156277 - Fix intermittent failures in test_IE_cookies.js by randomizing and clearing cookies. r=gijs, a=test-only
browser/components/migration/tests/unit/test_IE_cookies.js
--- a/browser/components/migration/tests/unit/test_IE_cookies.js
+++ b/browser/components/migration/tests/unit/test_IE_cookies.js
@@ -3,57 +3,99 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 
 add_task(function* () {
   let migrator = MigrationUtils.getMigrator("ie");
   // Sanity check for the source.
   Assert.ok(migrator.sourceExists);
 
   const BOOL = ctypes.bool;
   const LPCTSTR = ctypes.char16_t.ptr;
+  const DWORD = ctypes.uint32_t;
+  const LPDWORD = DWORD.ptr;
 
   let wininet = ctypes.open("Wininet");
-  do_register_cleanup(() => {
-    try {
-      wininet.close();
-    } catch (ex) {}
-  });
 
   /*
-  BOOL InternetSetCookie(
+  BOOL InternetSetCookieW(
     _In_  LPCTSTR lpszUrl,
     _In_  LPCTSTR lpszCookieName,
     _In_  LPCTSTR lpszCookieData
   );
   */
   let setIECookie = wininet.declare("InternetSetCookieW",
                                     ctypes.default_abi,
                                     BOOL,
                                     LPCTSTR,
                                     LPCTSTR,
                                     LPCTSTR);
 
-  let expiry = new Date();
-  expiry.setDate(expiry.getDate() + 7);
+  /*
+  BOOL InternetGetCookieW(
+    _In_    LPCTSTR lpszUrl,
+    _In_    LPCTSTR lpszCookieName,
+    _Out_   LPCTSTR  lpszCookieData,
+    _Inout_ LPDWORD lpdwSize
+  );
+  */
+  let getIECookie = wininet.declare("InternetGetCookieW",
+                                    ctypes.default_abi,
+                                    BOOL,
+                                    LPCTSTR,
+                                    LPCTSTR,
+                                    LPCTSTR,
+                                    LPDWORD);
+
+  // We need to randomize the cookie to avoid clashing with other cookies
+  // that might have been set by previous tests and not properly cleared.
+  let date = (new Date()).getDate();
   const COOKIE = {
-    host: "mycookietest.com",
+    get host() {
+      return new URL(this.href).host;
+    },
+    href: `http://mycookietest.${Math.random()}.com`,
     name: "testcookie",
     value: "testvalue",
-    expiry
+    expiry: new Date(new Date().setDate(date + 2))
   };
+  let data = ctypes.char16_t.array()(256);
+  let sizeRef = DWORD(256).address();
 
-  // Sanity check.
+  do_register_cleanup(() => {
+    // Remove the cookie.
+    try {
+      let expired = new Date(new Date().setDate(date - 2));
+      let rv = setIECookie(COOKIE.href, COOKIE.name,
+                           `; expires=${expired.toUTCString()}`);
+      Assert.ok(rv, "Expired the IE cookie");
+      Assert.ok(!getIECookie(COOKIE.href, COOKIE.name, data, sizeRef),
+      "The cookie has been properly removed");
+    } catch (ex) {}
+
+    // Close the library.
+    try {
+      wininet.close();
+    } catch (ex) {}
+  });
+
+  // Create the persistent cookie in IE.
+  let value = `${COOKIE.value}; expires=${COOKIE.expiry.toUTCString()}`;
+  let rv = setIECookie(COOKIE.href, COOKIE.name, value);
+  Assert.ok(rv, "Added a persistent IE cookie: " + value);
+
+  // Sanity check the cookie has been created.
+  Assert.ok(getIECookie(COOKIE.href, COOKIE.name, data, sizeRef),
+            "Found the added persistent IE cookie");
+  do_print("Found cookie: " + data.readString());
+  Assert.equal(data.readString(), `${COOKIE.name}=${COOKIE.value}`,
+            "Found the expected cookie");
+
+  // Sanity check that there are no cookies.
   Assert.equal(Services.cookies.countCookiesFromHost(COOKIE.host), 0,
                "There are no cookies initially");
 
-  // Create the persistent cookie in IE.
-  let value = COOKIE.name + " = " + COOKIE.value +"; expires = " +
-              COOKIE.expiry.toUTCString();
-  let rv = setIECookie(new URL("http://" + COOKIE.host).href, null, value);
-  Assert.ok(rv, "Added a persistent IE cookie");
-
   // Migrate cookies.
   yield promiseMigration(migrator, MigrationUtils.resourceTypes.COOKIES);
 
   Assert.equal(Services.cookies.countCookiesFromHost(COOKIE.host), 1,
                "Migrated the expected number of cookies");
 
   // Now check the cookie details.
   let enumerator = Services.cookies.getCookiesFromHost(COOKIE.host);