Index: content/browser/background_fetch/background_fetch_registration_id_unittest.cc |
diff --git a/content/browser/background_fetch/background_fetch_registration_id_unittest.cc b/content/browser/background_fetch/background_fetch_registration_id_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab2afe725651ab1e8b6a77330036011f142c84b8 |
--- /dev/null |
+++ b/content/browser/background_fetch/background_fetch_registration_id_unittest.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/background_fetch/background_fetch_registration_id.h" |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+#include "url/origin.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+const char kTestOrigin[] = "https://www.example.com"; |
+const int64_t kTestServiceWorkerRegistrationId = 9001; |
+const int64_t kTestLongServiceWorkerRegistrationId = 14294967296; |
+const char kTestTag[] = "test_tag"; |
+const char kExpectedSerialized[] = "0#9001#https://www.example.com#test_tag"; |
+const char kExpectedLongServiceWorkerIdSerialized[] = |
+ "0#14294967296#https://www.example.com#test_tag"; |
+ |
+} // anonymous namespace |
+ |
+TEST(BackgroundFetchRegistrationIdTest, SerializeTest) { |
+ BackgroundFetchRegistrationId registration_id( |
+ kTestServiceWorkerRegistrationId, url::Origin(GURL(kTestOrigin)), |
+ kTestTag); |
+ std::string serialized_id = registration_id.Serialize(); |
+ EXPECT_EQ(kExpectedSerialized, serialized_id); |
+} |
+ |
+TEST(BackgroundFetchRegistrationIdTest, SerializeLongServiceWorkerIdTest) { |
+ BackgroundFetchRegistrationId registration_id( |
+ kTestLongServiceWorkerRegistrationId, url::Origin(GURL(kTestOrigin)), |
+ kTestTag); |
+ std::string serialized_id = registration_id.Serialize(); |
+ EXPECT_EQ(kExpectedLongServiceWorkerIdSerialized, serialized_id); |
+} |
+ |
+TEST(BackgroundFetchRegistrationIdTest, DeserializeSuccessTest) { |
+ BackgroundFetchRegistrationId registration_id; |
+ ASSERT_TRUE(BackgroundFetchRegistrationId::Deserialize( |
+ kExpectedLongServiceWorkerIdSerialized, ®istration_id)); |
+ |
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize()); |
+ EXPECT_EQ(kTestLongServiceWorkerRegistrationId, |
+ registration_id.service_worker_registration_id()); |
+ EXPECT_EQ(kTestTag, registration_id.tag()); |
+} |
+ |
+TEST(BackgroundFetchRegistrationIdTest, |
+ DeserializeLongServiceWorkerSuccessTest) { |
+ BackgroundFetchRegistrationId registration_id; |
+ ASSERT_TRUE(BackgroundFetchRegistrationId::Deserialize(kExpectedSerialized, |
+ ®istration_id)); |
+ |
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize()); |
+ EXPECT_EQ(kTestServiceWorkerRegistrationId, |
+ registration_id.service_worker_registration_id()); |
+ EXPECT_EQ(kTestTag, registration_id.tag()); |
+} |
+ |
+TEST(BackgroundFetchRegistrationIdTest, DeserializeComplexTagTest) { |
+ BackgroundFetchRegistrationId registration_id; |
+ std::string silly_tag("0#9001#https://www.example.com#this#is#a#stupid#tag"); |
+ ASSERT_TRUE( |
+ BackgroundFetchRegistrationId::Deserialize(silly_tag, ®istration_id)); |
+ |
+ EXPECT_EQ(kTestOrigin, registration_id.origin().Serialize()); |
+ EXPECT_EQ(kTestServiceWorkerRegistrationId, |
+ registration_id.service_worker_registration_id()); |
+ EXPECT_EQ("this#is#a#stupid#tag", registration_id.tag()); |
+} |
+ |
+TEST(BackgroundFetchRegistrationIdTest, DeserializeFailureTest) { |
+ BackgroundFetchRegistrationId registration_id; |
+ |
+ // Should fail: Invalid version. |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "1#1#https://www.example.com#tag", ®istration_id)); |
+ |
+ // Should fail: Invalid service worker registration id. |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "0#abc#https://www.example.com#tag", ®istration_id)); |
+ |
+ // Should fail: Too large service worker registration id. |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "0#11111111111111111111111111111111111#https://www.example.com#tag", |
+ ®istration_id)); |
+ |
+ // Should fail: Invalid origin. |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "0#1#www.example.com#tag", ®istration_id)); |
+ |
+ // Should fail: Empty origin. |
+ EXPECT_FALSE( |
+ BackgroundFetchRegistrationId::Deserialize("0#1##tag", ®istration_id)); |
+ |
+ // Should fail: Not enough fields. |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "0#1#https://www.example.com", ®istration_id)); |
+ |
+ // Should fail: Invalid tag |
+ EXPECT_FALSE(BackgroundFetchRegistrationId::Deserialize( |
+ "0#1#https://www.example.com#", ®istration_id)); |
+} |
+ |
+} // namespace content |