1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
7 #include "chrome/test/base/testing_profile.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 void ExpectAppIdentifiersEqual(const PushMessagingAppIdentifier
& a
,
14 const PushMessagingAppIdentifier
& b
) {
15 EXPECT_EQ(a
.app_id(), b
.app_id());
16 EXPECT_EQ(a
.origin(), b
.origin());
17 EXPECT_EQ(a
.service_worker_registration_id(),
18 b
.service_worker_registration_id());
23 class PushMessagingAppIdentifierTest
: public testing::Test
{
25 PushMessagingAppIdentifier
GenerateId(
27 int64_t service_worker_registration_id
) {
28 // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it
29 // to generate app_id, and then use private constructor.
30 std::string app_id
= PushMessagingAppIdentifier::Generate(
31 GURL("https://www.example.com/"), 1).app_id();
32 return PushMessagingAppIdentifier(app_id
, origin
,
33 service_worker_registration_id
);
36 void SetUp() override
{
37 original_
= PushMessagingAppIdentifier::Generate(
38 GURL("https://www.example.com/"), 1);
39 same_origin_and_sw_
= PushMessagingAppIdentifier::Generate(
40 GURL("https://www.example.com"), 1);
41 different_origin_
= PushMessagingAppIdentifier::Generate(
42 GURL("https://foobar.example.com/"), 1);
43 different_sw_
= PushMessagingAppIdentifier::Generate(
44 GURL("https://www.example.com/"), 42);
47 Profile
* profile() { return &profile_
; }
49 PushMessagingAppIdentifier original_
;
50 PushMessagingAppIdentifier same_origin_and_sw_
;
51 PushMessagingAppIdentifier different_origin_
;
52 PushMessagingAppIdentifier different_sw_
;
55 content::TestBrowserThreadBundle thread_bundle_
;
56 TestingProfile profile_
;
59 TEST_F(PushMessagingAppIdentifierTest
, ConstructorValidity
) {
60 // The following two are valid:
61 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/"), 1).is_null());
62 EXPECT_FALSE(GenerateId(GURL("https://www.example.com"), 1).is_null());
63 // The following four are invalid and will DCHECK in Generate:
64 EXPECT_FALSE(GenerateId(GURL(""), 1).is_null());
65 EXPECT_FALSE(GenerateId(GURL("foo"), 1).is_null());
66 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/foo"), 1).is_null());
67 EXPECT_FALSE(GenerateId(GURL("https://www.example.com/#foo"), 1).is_null());
68 // The following one is invalid and will DCHECK in Generate and be null:
69 EXPECT_TRUE(GenerateId(GURL("https://www.example.com/"), -1).is_null());
72 TEST_F(PushMessagingAppIdentifierTest
, UniqueGuids
) {
73 EXPECT_NE(PushMessagingAppIdentifier::Generate(
74 GURL("https://www.example.com/"), 1).app_id(),
75 PushMessagingAppIdentifier::Generate(
76 GURL("https://www.example.com/"), 1).app_id());
79 TEST_F(PushMessagingAppIdentifierTest
, FindInvalidAppId
) {
80 // These calls to FindByAppId should not DCHECK.
81 EXPECT_TRUE(PushMessagingAppIdentifier::FindByAppId(profile(), "").is_null());
82 EXPECT_TRUE(PushMessagingAppIdentifier::FindByAppId(
83 profile(), "amhfneadkjmnlefnpidcijoldiibcdnd").is_null());
86 TEST_F(PushMessagingAppIdentifierTest
, PersistAndFind
) {
87 ASSERT_TRUE(PushMessagingAppIdentifier::FindByAppId(
88 profile(), original_
.app_id()).is_null());
89 ASSERT_TRUE(PushMessagingAppIdentifier::FindByServiceWorker(
90 profile(), original_
.origin(), original_
.service_worker_registration_id())
93 // Test basic PersistToPrefs round trips.
94 original_
.PersistToPrefs(profile());
96 PushMessagingAppIdentifier found_by_app_id
=
97 PushMessagingAppIdentifier::FindByAppId(profile(), original_
.app_id());
98 EXPECT_FALSE(found_by_app_id
.is_null());
99 ExpectAppIdentifiersEqual(original_
, found_by_app_id
);
102 PushMessagingAppIdentifier found_by_origin_and_swr_id
=
103 PushMessagingAppIdentifier::FindByServiceWorker(profile(),
104 original_
.origin(), original_
.service_worker_registration_id());
105 EXPECT_FALSE(found_by_origin_and_swr_id
.is_null());
106 ExpectAppIdentifiersEqual(original_
, found_by_origin_and_swr_id
);
110 TEST_F(PushMessagingAppIdentifierTest
, FindLegacy
) {
111 const std::string
legacy_app_id("wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F");
112 ASSERT_TRUE(PushMessagingAppIdentifier::FindByAppId(profile(),
113 legacy_app_id
).is_null());
114 ASSERT_TRUE(PushMessagingAppIdentifier::FindByServiceWorker(
115 profile(), original_
.origin(), original_
.service_worker_registration_id())
118 // Create a legacy preferences entry (the test happens to use PersistToPrefs
119 // since that currently works, but it's ok to change the behavior of
120 // PersistToPrefs; if so, this test can just do a raw DictionaryPrefUpdate).
121 original_
.app_id_
= legacy_app_id
;
122 original_
.PersistToPrefs(profile());
124 // Test that legacy entries can be read back from prefs.
126 PushMessagingAppIdentifier found_by_app_id
=
127 PushMessagingAppIdentifier::FindByAppId(profile(), original_
.app_id());
128 EXPECT_FALSE(found_by_app_id
.is_null());
129 ExpectAppIdentifiersEqual(original_
, found_by_app_id
);
132 PushMessagingAppIdentifier found_by_origin_and_swr_id
=
133 PushMessagingAppIdentifier::FindByServiceWorker(profile(),
134 original_
.origin(), original_
.service_worker_registration_id());
135 EXPECT_FALSE(found_by_origin_and_swr_id
.is_null());
136 ExpectAppIdentifiersEqual(original_
, found_by_origin_and_swr_id
);
140 TEST_F(PushMessagingAppIdentifierTest
, PersistOverwritesSameOriginAndSW
) {
141 original_
.PersistToPrefs(profile());
143 // Test that PersistToPrefs overwrites when same origin and Service Worker.
144 ASSERT_NE(original_
.app_id(), same_origin_and_sw_
.app_id());
145 ASSERT_EQ(original_
.origin(), same_origin_and_sw_
.origin());
146 ASSERT_EQ(original_
.service_worker_registration_id(),
147 same_origin_and_sw_
.service_worker_registration_id());
148 same_origin_and_sw_
.PersistToPrefs(profile());
150 PushMessagingAppIdentifier found_by_original_app_id
=
151 PushMessagingAppIdentifier::FindByAppId(profile(), original_
.app_id());
152 EXPECT_TRUE(found_by_original_app_id
.is_null());
155 PushMessagingAppIdentifier found_by_soas_app_id
=
156 PushMessagingAppIdentifier::FindByAppId(profile(),
157 same_origin_and_sw_
.app_id());
158 EXPECT_FALSE(found_by_soas_app_id
.is_null());
159 ExpectAppIdentifiersEqual(same_origin_and_sw_
, found_by_soas_app_id
);
162 PushMessagingAppIdentifier found_by_original_origin_and_swr_id
=
163 PushMessagingAppIdentifier::FindByServiceWorker(profile(),
164 original_
.origin(), original_
.service_worker_registration_id());
165 EXPECT_FALSE(found_by_original_origin_and_swr_id
.is_null());
166 ExpectAppIdentifiersEqual(same_origin_and_sw_
,
167 found_by_original_origin_and_swr_id
);
171 TEST_F(PushMessagingAppIdentifierTest
, PersistDoesNotOverwriteDifferent
) {
172 original_
.PersistToPrefs(profile());
174 // Test that PersistToPrefs doesn't overwrite when different origin or SW.
175 ASSERT_NE(original_
.app_id(), different_origin_
.app_id());
176 ASSERT_NE(original_
.app_id(), different_sw_
.app_id());
177 different_origin_
.PersistToPrefs(profile());
178 different_sw_
.PersistToPrefs(profile());
180 PushMessagingAppIdentifier found_by_original_app_id
=
181 PushMessagingAppIdentifier::FindByAppId(profile(), original_
.app_id());
182 EXPECT_FALSE(found_by_original_app_id
.is_null());
183 ExpectAppIdentifiersEqual(original_
, found_by_original_app_id
);
186 PushMessagingAppIdentifier found_by_original_origin_and_swr_id
=
187 PushMessagingAppIdentifier::FindByServiceWorker(profile(),
188 original_
.origin(), original_
.service_worker_registration_id());
189 EXPECT_FALSE(found_by_original_origin_and_swr_id
.is_null());
190 ExpectAppIdentifiersEqual(original_
, found_by_original_origin_and_swr_id
);
194 TEST_F(PushMessagingAppIdentifierTest
, DeleteFromPrefs
) {
195 original_
.PersistToPrefs(profile());
196 different_origin_
.PersistToPrefs(profile());
197 different_sw_
.PersistToPrefs(profile());
199 // Test DeleteFromPrefs. Deleted app identifier should be deleted.
200 original_
.DeleteFromPrefs(profile());
202 PushMessagingAppIdentifier found_by_original_app_id
=
203 PushMessagingAppIdentifier::FindByAppId(profile(), original_
.app_id());
204 EXPECT_TRUE(found_by_original_app_id
.is_null());
207 PushMessagingAppIdentifier found_by_original_origin_and_swr_id
=
208 PushMessagingAppIdentifier::FindByServiceWorker(profile(),
209 original_
.origin(), original_
.service_worker_registration_id());
210 EXPECT_TRUE(found_by_original_origin_and_swr_id
.is_null());
214 TEST_F(PushMessagingAppIdentifierTest
, GetAll
) {
215 original_
.PersistToPrefs(profile());
216 different_origin_
.PersistToPrefs(profile());
217 different_sw_
.PersistToPrefs(profile());
219 original_
.DeleteFromPrefs(profile());
221 // Test GetAll. Non-deleted app identifiers should all be listed.
222 std::vector
<PushMessagingAppIdentifier
> all_app_identifiers
=
223 PushMessagingAppIdentifier::GetAll(profile());
224 EXPECT_EQ(2u, all_app_identifiers
.size());
225 // Order is unspecified.
226 bool contained_different_origin
= false;
227 bool contained_different_sw
= false;
228 for (const PushMessagingAppIdentifier
& app_identifier
: all_app_identifiers
) {
229 if (app_identifier
.app_id() == different_origin_
.app_id()) {
230 ExpectAppIdentifiersEqual(different_origin_
, app_identifier
);
231 contained_different_origin
= true;
233 ExpectAppIdentifiersEqual(different_sw_
, app_identifier
);
234 contained_different_sw
= true;
237 EXPECT_TRUE(contained_different_origin
);
238 EXPECT_TRUE(contained_different_sw
);