summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/push_messaging/push_messaging_app_identifier_unittest.cc
blob: 5b7c649c2e4db64d9010cbc7c02acaa7ecc3cbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2014 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 "chrome/browser/push_messaging/push_messaging_app_identifier.h"

#include <stdint.h>

#include "base/time/time.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

void ExpectAppIdentifiersEqual(const PushMessagingAppIdentifier& a,
                               const PushMessagingAppIdentifier& b) {
  EXPECT_EQ(a.app_id(), b.app_id());
  EXPECT_EQ(a.origin(), b.origin());
  EXPECT_EQ(a.service_worker_registration_id(),
            b.service_worker_registration_id());
  EXPECT_EQ(a.expiration_time(), b.expiration_time());
}

base::Time kExpirationTime =
    base::Time::FromDeltaSinceWindowsEpoch(base::Seconds(1));

}  // namespace

class PushMessagingAppIdentifierTest : public testing::Test {
 protected:
  PushMessagingAppIdentifier GenerateId(
      const GURL& origin,
      int64_t service_worker_registration_id) {
    // To bypass DCHECK in PushMessagingAppIdentifier::Generate, we just use it
    // to generate app_id, and then use private constructor.
    std::string app_id = PushMessagingAppIdentifier::Generate(
                             GURL("https://www.example.com/"), 1)
                             .app_id();
    return PushMessagingAppIdentifier(app_id, origin,
                                      service_worker_registration_id);
  }

  void SetUp() override {
    original_ = PushMessagingAppIdentifier::Generate(
        GURL("https://www.example.com/"), 1);
    same_origin_and_sw_ = PushMessagingAppIdentifier::Generate(
        GURL("https://www.example.com"), 1);
    different_origin_ = PushMessagingAppIdentifier::Generate(
        GURL("https://foobar.example.com/"), 1);
    different_sw_ = PushMessagingAppIdentifier::Generate(
        GURL("https://www.example.com/"), 42);
    with_et_ = PushMessagingAppIdentifier::Generate(
        GURL("https://www.example.com/"), 1, kExpirationTime);
    different_et_ = PushMessagingAppIdentifier::Generate(
        GURL("https://www.example.com/"), 1,
        kExpirationTime + base::Seconds(100));
  }

  Profile* profile() { return &profile_; }

  PushMessagingAppIdentifier original_;
  PushMessagingAppIdentifier same_origin_and_sw_;
  PushMessagingAppIdentifier different_origin_;
  PushMessagingAppIdentifier different_sw_;
  PushMessagingAppIdentifier different_et_;
  PushMessagingAppIdentifier with_et_;

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
};

TEST_F(PushMessagingAppIdentifierTest, ConstructorValidity) {
  // The following two are valid:
  EXPECT_FALSE(GenerateId(GURL("https://www.example.com/"), 1).is_null());
  EXPECT_FALSE(GenerateId(GURL("https://www.example.com"), 1).is_null());
  // The following four are invalid and will DCHECK in Generate:
  EXPECT_FALSE(GenerateId(GURL(""), 1).is_null());
  EXPECT_FALSE(GenerateId(GURL("foo"), 1).is_null());
  EXPECT_FALSE(GenerateId(GURL("https://www.example.com/foo"), 1).is_null());
  EXPECT_FALSE(GenerateId(GURL("https://www.example.com/#foo"), 1).is_null());
  // The following one is invalid and will DCHECK in Generate and be null:
  EXPECT_TRUE(GenerateId(GURL("https://www.example.com/"), -1).is_null());
}

TEST_F(PushMessagingAppIdentifierTest, UniqueGuids) {
  EXPECT_NE(
      PushMessagingAppIdentifier::Generate(GURL("https://www.example.com/"), 1)
          .app_id(),
      PushMessagingAppIdentifier::Generate(GURL("https://www.example.com/"), 1)
          .app_id());
}

TEST_F(PushMessagingAppIdentifierTest, FindInvalidAppId) {
  // These calls to FindByAppId should not DCHECK.
  EXPECT_TRUE(PushMessagingAppIdentifier::FindByAppId(profile(), "").is_null());
  EXPECT_TRUE(PushMessagingAppIdentifier::FindByAppId(
                  profile(), "amhfneadkjmnlefnpidcijoldiibcdnd")
                  .is_null());
}

TEST_F(PushMessagingAppIdentifierTest, PersistAndFind) {
  ASSERT_TRUE(
      PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id())
          .is_null());

  const auto identifier = PushMessagingAppIdentifier::FindByServiceWorker(
      profile(), original_.origin(),
      original_.service_worker_registration_id());

  ASSERT_TRUE(identifier.is_null());

  // Test basic PersistToPrefs round trips.
  original_.PersistToPrefs(profile());
  {
    PushMessagingAppIdentifier found_by_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id());
    EXPECT_FALSE(found_by_app_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_app_id);
  }
  {
    PushMessagingAppIdentifier found_by_origin_and_swr_id =
        PushMessagingAppIdentifier::FindByServiceWorker(
            profile(), original_.origin(),
            original_.service_worker_registration_id());
    EXPECT_FALSE(found_by_origin_and_swr_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_origin_and_swr_id);
  }
}

TEST_F(PushMessagingAppIdentifierTest, FindLegacy) {
  const std::string legacy_app_id("wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F");
  ASSERT_TRUE(PushMessagingAppIdentifier::FindByAppId(profile(), legacy_app_id)
                  .is_null());

  const auto identifier = PushMessagingAppIdentifier::FindByServiceWorker(
      profile(), original_.origin(),
      original_.service_worker_registration_id());

  ASSERT_TRUE(identifier.is_null());

  // Create a legacy preferences entry (the test happens to use PersistToPrefs
  // since that currently works, but it's ok to change the behavior of
  // PersistToPrefs; if so, this test can just do a raw DictionaryPrefUpdate).
  original_.app_id_ = legacy_app_id;
  original_.PersistToPrefs(profile());

  // Test that legacy entries can be read back from prefs.
  {
    PushMessagingAppIdentifier found_by_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id());
    EXPECT_FALSE(found_by_app_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_app_id);
  }
  {
    PushMessagingAppIdentifier found_by_origin_and_swr_id =
        PushMessagingAppIdentifier::FindByServiceWorker(
            profile(), original_.origin(),
            original_.service_worker_registration_id());
    EXPECT_FALSE(found_by_origin_and_swr_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_origin_and_swr_id);
  }
}

TEST_F(PushMessagingAppIdentifierTest, PersistOverwritesSameOriginAndSW) {
  original_.PersistToPrefs(profile());

  // Test that PersistToPrefs overwrites when same origin and Service Worker.
  ASSERT_NE(original_.app_id(), same_origin_and_sw_.app_id());
  ASSERT_EQ(original_.origin(), same_origin_and_sw_.origin());
  ASSERT_EQ(original_.service_worker_registration_id(),
            same_origin_and_sw_.service_worker_registration_id());
  same_origin_and_sw_.PersistToPrefs(profile());
  {
    PushMessagingAppIdentifier found_by_original_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id());
    EXPECT_TRUE(found_by_original_app_id.is_null());
  }
  {
    PushMessagingAppIdentifier found_by_soas_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(),
                                                same_origin_and_sw_.app_id());
    EXPECT_FALSE(found_by_soas_app_id.is_null());
    ExpectAppIdentifiersEqual(same_origin_and_sw_, found_by_soas_app_id);
  }
  {
    PushMessagingAppIdentifier found_by_original_origin_and_swr_id =
        PushMessagingAppIdentifier::FindByServiceWorker(
            profile(), original_.origin(),
            original_.service_worker_registration_id());
    EXPECT_FALSE(found_by_original_origin_and_swr_id.is_null());
    ExpectAppIdentifiersEqual(same_origin_and_sw_,
                              found_by_original_origin_and_swr_id);
  }
}

TEST_F(PushMessagingAppIdentifierTest, PersistDoesNotOverwriteDifferent) {
  original_.PersistToPrefs(profile());

  // Test that PersistToPrefs doesn't overwrite when different origin or SW.
  ASSERT_NE(original_.app_id(), different_origin_.app_id());
  ASSERT_NE(original_.app_id(), different_sw_.app_id());
  different_origin_.PersistToPrefs(profile());
  different_sw_.PersistToPrefs(profile());
  {
    PushMessagingAppIdentifier found_by_original_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id());
    EXPECT_FALSE(found_by_original_app_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_original_app_id);
  }
  {
    PushMessagingAppIdentifier found_by_original_origin_and_swr_id =
        PushMessagingAppIdentifier::FindByServiceWorker(
            profile(), original_.origin(),
            original_.service_worker_registration_id());
    EXPECT_FALSE(found_by_original_origin_and_swr_id.is_null());
    ExpectAppIdentifiersEqual(original_, found_by_original_origin_and_swr_id);
  }
}

TEST_F(PushMessagingAppIdentifierTest, DeleteFromPrefs) {
  original_.PersistToPrefs(profile());
  different_origin_.PersistToPrefs(profile());
  different_sw_.PersistToPrefs(profile());

  // Test DeleteFromPrefs. Deleted app identifier should be deleted.
  original_.DeleteFromPrefs(profile());
  {
    PushMessagingAppIdentifier found_by_original_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), original_.app_id());
    EXPECT_TRUE(found_by_original_app_id.is_null());
  }
  {
    PushMessagingAppIdentifier found_by_original_origin_and_swr_id =
        PushMessagingAppIdentifier::FindByServiceWorker(
            profile(), original_.origin(),
            original_.service_worker_registration_id());
    EXPECT_TRUE(found_by_original_origin_and_swr_id.is_null());
  }
}

TEST_F(PushMessagingAppIdentifierTest, GetAll) {
  original_.PersistToPrefs(profile());
  different_origin_.PersistToPrefs(profile());
  different_sw_.PersistToPrefs(profile());

  original_.DeleteFromPrefs(profile());

  // Test GetAll. Non-deleted app identifiers should all be listed.
  std::vector<PushMessagingAppIdentifier> all_app_identifiers =
      PushMessagingAppIdentifier::GetAll(profile());
  EXPECT_EQ(2u, all_app_identifiers.size());
  // Order is unspecified.
  bool contained_different_origin = false;
  bool contained_different_sw = false;
  for (const PushMessagingAppIdentifier& app_identifier : all_app_identifiers) {
    if (app_identifier.app_id() == different_origin_.app_id()) {
      ExpectAppIdentifiersEqual(different_origin_, app_identifier);
      contained_different_origin = true;
    } else {
      ExpectAppIdentifiersEqual(different_sw_, app_identifier);
      contained_different_sw = true;
    }
  }
  EXPECT_TRUE(contained_different_origin);
  EXPECT_TRUE(contained_different_sw);
}

TEST_F(PushMessagingAppIdentifierTest, PersistWithExpirationTime) {
  ASSERT_TRUE(with_et_.expiration_time());
  ASSERT_TRUE(different_et_.expiration_time());
  ASSERT_EQ(with_et_.origin(), different_et_.origin());
  ASSERT_EQ(with_et_.service_worker_registration_id(),
            different_et_.service_worker_registration_id());
  ASSERT_FALSE(kExpirationTime.is_null());

  different_et_.PersistToPrefs(profile());

  // Test PersistToPrefs and FindByAppId, whether expiration time is saved
  // properly
  std::vector<PushMessagingAppIdentifier> all_app_identifiers =
      PushMessagingAppIdentifier::GetAll(profile());
  EXPECT_EQ(1u, all_app_identifiers.size());
  {
    PushMessagingAppIdentifier found_by_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(),
                                                different_et_.app_id());
    // Check whether expiration time was saved
    ExpectAppIdentifiersEqual(found_by_app_id, different_et_);
  }
  with_et_.PersistToPrefs(profile());
  {
    all_app_identifiers = PushMessagingAppIdentifier::GetAll(profile());
    EXPECT_EQ(1u, all_app_identifiers.size());
  }
  {
    PushMessagingAppIdentifier found_by_with_et_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(), with_et_.app_id());
    EXPECT_FALSE(found_by_with_et_app_id.is_null());
    EXPECT_EQ(found_by_with_et_app_id.expiration_time(), kExpirationTime);
    ExpectAppIdentifiersEqual(found_by_with_et_app_id, with_et_);
  }
  {
    PushMessagingAppIdentifier found_by_different_et_app_id =
        PushMessagingAppIdentifier::FindByAppId(profile(),
                                                different_et_.app_id());
    EXPECT_TRUE(found_by_different_et_app_id.is_null());
  }
}