Bug 1249438 P3 Add a gtest that registers duplicate service worker registrations. r=baku
authorBen Kelly <ben@wanderview.com>
Tue, 29 Mar 2016 07:20:48 -0700
changeset 290935 8b7a2b89ff39d5943ebc6b439f769a74dd38f3d3
parent 290934 af6b4f4f55cb0c237c66be379dbd8344725f9136
child 290936 11c3335d77fb751128c456daa204385d69678484
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1249438
milestone48.0a1
Bug 1249438 P3 Add a gtest that registers duplicate service worker registrations. r=baku
dom/workers/test/gtest/TestReadWrite.cpp
--- a/dom/workers/test/gtest/TestReadWrite.cpp
+++ b/dom/workers/test/gtest/TestReadWrite.cpp
@@ -365,17 +365,17 @@ TEST(ServiceWorkerRegistrar, TestVersion
 
   ASSERT_STREQ("", suffix1.get());
   ASSERT_STREQ("scope 1", cInfo1.spec().get());
   ASSERT_STREQ("scope 1", data[1].scope().get());
   ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
   ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
 }
 
-TEST(ServiceWorkerRegistrar, TestDedupe)
+TEST(ServiceWorkerRegistrar, TestDedupeRead)
 {
   nsAutoCString buffer("3" "\n");
 
   // unique entries
   buffer.Append("^appId=123&inBrowser=1\n");
   buffer.Append("spec 0\nscope 0\ncurrentWorkerURL 0\ncacheName 0\n");
   buffer.Append(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
 
@@ -428,14 +428,66 @@ TEST(ServiceWorkerRegistrar, TestDedupe)
 
   ASSERT_STREQ("", suffix1.get());
   ASSERT_STREQ("scope 1", cInfo1.spec().get());
   ASSERT_STREQ("scope 1", data[1].scope().get());
   ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
   ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
 }
 
+TEST(ServiceWorkerRegistrar, TestDedupeWrite)
+{
+  {
+    RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
+
+    for (int i = 0; i < 10; ++i) {
+      ServiceWorkerRegistrationData reg;
+
+      reg.scope() = NS_LITERAL_CSTRING("scope write dedupe");
+      reg.currentWorkerURL() = nsPrintfCString("currentWorkerURL write %d", i);
+      reg.cacheName() =
+        NS_ConvertUTF8toUTF16(nsPrintfCString("cacheName write %d", i));
+
+      nsAutoCString spec;
+      spec.AppendPrintf("spec write dedupe/%d", i);
+      reg.principal() =
+        mozilla::ipc::ContentPrincipalInfo(mozilla::PrincipalOriginAttributes(0, false), spec);
+
+      swr->TestRegisterServiceWorker(reg);
+    }
+
+    nsresult rv = swr->TestWriteData();
+    ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
+  }
+
+  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
+
+  nsresult rv = swr->TestReadData();
+  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
+
+  // Duplicate entries should be removed.
+  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
+  ASSERT_EQ((uint32_t)1, data.Length()) << "1 entry should be found";
+
+  ASSERT_EQ(data[0].principal().type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo);
+  const mozilla::ipc::ContentPrincipalInfo& cInfo = data[0].principal();
+
+  mozilla::PrincipalOriginAttributes attrs(0, false);
+  nsAutoCString suffix, expectSuffix;
+  attrs.CreateSuffix(expectSuffix);
+  cInfo.attrs().CreateSuffix(suffix);
+
+  // Last entry passed to RegisterServiceWorkerInternal() should overwrite
+  // previous values.  So expect "9" in values here.
+  ASSERT_STREQ(expectSuffix.get(), suffix.get());
+  ASSERT_STREQ("scope write dedupe", cInfo.spec().get());
+  ASSERT_STREQ("scope write dedupe", data[0].scope().get());
+  ASSERT_STREQ("currentWorkerURL write 9", data[0].currentWorkerURL().get());
+  ASSERT_STREQ("cacheName write 9",
+               NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
+}
+
 int main(int argc, char** argv) {
   ::testing::InitGoogleTest(&argc, argv);
 
   int rv = RUN_ALL_TESTS();
   return rv;
 }