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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// Copyright 2015 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 "base/strings/string_number_conversions.h"
#include "content/browser/notifications/notification_id_generator.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
namespace {
const int kRenderProcessId = 42;
const int64_t kPersistentNotificationId = 430;
const int kNonPersistentNotificationId = 5400;
const char kExampleTag[] = "example";
class TestBrowserContextConfigurableIncognito : public TestBrowserContext {
public:
TestBrowserContextConfigurableIncognito() {}
~TestBrowserContextConfigurableIncognito() override {}
void set_incognito(bool incognito) { incognito_ = incognito; }
// TestBrowserContext implementation.
bool IsOffTheRecord() const override { return incognito_; }
private:
bool incognito_ = false;
DISALLOW_COPY_AND_ASSIGN(TestBrowserContextConfigurableIncognito);
};
class NotificationIdGeneratorTest : public ::testing::Test {
public:
NotificationIdGeneratorTest()
: generator_(&browser_context_, kRenderProcessId) {}
void SetUp() override {
}
protected:
GURL origin() const { return GURL("https://example.com"); }
TestBrowserContextConfigurableIncognito* browser_context() {
return &browser_context_;
}
NotificationIdGenerator* generator() { return &generator_; }
private:
TestBrowserContextConfigurableIncognito browser_context_;
NotificationIdGenerator generator_;
};
// -----------------------------------------------------------------------------
// Persistent and non-persistent notifications
//
// Tests that cover logic common to both persistent and non-persistent
// notifications: different browser contexts, Incognito mode or not,
// Two calls to the generator with exactly the same information should result
// in exactly the same notification ids being generated.
TEST_F(NotificationIdGeneratorTest, DeterministicGeneration) {
// Persistent notifications.
EXPECT_EQ(
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId));
EXPECT_EQ(
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_EQ(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId));
EXPECT_EQ(
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId));
}
// Uniqueness of notification ids will be impacted by the browser context.
TEST_F(NotificationIdGeneratorTest, DifferentBrowserContexts) {
TestBrowserContextConfigurableIncognito second_browser_context;
NotificationIdGenerator second_generator(&second_browser_context,
kRenderProcessId);
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId),
second_generator.GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId));
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId),
second_generator.GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
second_generator.GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId));
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId),
second_generator.GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId));
}
// Uniqueness of notification ids will be impacted by the fact whether the
// browser context is in Incognito mode.
TEST_F(NotificationIdGeneratorTest, DifferentIncognitoStates) {
ASSERT_FALSE(browser_context()->IsOffTheRecord());
// Persistent notifications.
std::string normal_persistent_notification_id =
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId);
browser_context()->set_incognito(true);
ASSERT_TRUE(browser_context()->IsOffTheRecord());
std::string incognito_persistent_notification_id =
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId);
EXPECT_NE(normal_persistent_notification_id,
incognito_persistent_notification_id);
browser_context()->set_incognito(false);
// Non-persistent notifications.
ASSERT_FALSE(browser_context()->IsOffTheRecord());
std::string normal_non_persistent_notification_id =
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId);
browser_context()->set_incognito(true);
ASSERT_TRUE(browser_context()->IsOffTheRecord());
std::string incognito_non_persistent_notification_id =
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId);
EXPECT_NE(normal_non_persistent_notification_id,
incognito_non_persistent_notification_id);
}
// The origin of the notification will impact the generated notification id.
TEST_F(NotificationIdGeneratorTest, DifferentOrigins) {
GURL different_origin("https://example2.com");
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
different_origin, kExampleTag, kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
different_origin, kExampleTag, kNonPersistentNotificationId));
}
// The tag, when non-empty, will impact the generated notification id.
TEST_F(NotificationIdGeneratorTest, DifferentTags) {
const std::string& different_tag = std::string(kExampleTag) + "2";
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin(), different_tag, kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin(), different_tag, kNonPersistentNotificationId));
}
// The persistent or non-persistent notification id will impact the generated
// notification id when the tag is empty.
TEST_F(NotificationIdGeneratorTest, DifferentIds) {
NotificationIdGenerator second_generator(browser_context(),
kRenderProcessId + 1);
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId + 1));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId + 1));
// Non-persistent when a tag is being used.
EXPECT_EQ(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
second_generator.GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId));
}
// Using a numeric tag that could resemble a persistent notification id should
// not be equal to a notification without a tag, but with that id.
TEST_F(NotificationIdGeneratorTest, NumericTagAmbiguity) {
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin(),
base::IntToString(kPersistentNotificationId),
kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(),
base::IntToString(kNonPersistentNotificationId),
kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId));
}
// Using port numbers and a tag which, when concatenated, could end up being
// equal to each other if origins stop ending with slashes.
TEST_F(NotificationIdGeneratorTest, OriginPortAmbiguity) {
GURL origin_805("https://example.com:805");
GURL origin_8051("https://example.com:8051");
// Persistent notifications.
EXPECT_NE(
generator()->GenerateForPersistentNotification(
origin_805, "17", kPersistentNotificationId),
generator()->GenerateForPersistentNotification(
origin_8051, "7", kPersistentNotificationId));
// Non-persistent notifications.
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin_805, "17", kNonPersistentNotificationId),
generator()->GenerateForNonPersistentNotification(
origin_8051, "7", kNonPersistentNotificationId));
}
// -----------------------------------------------------------------------------
// Persistent notifications
//
// Tests covering the logic specific to persistent notifications. This kind of
// notification does not care about the renderer process that created them.
TEST_F(NotificationIdGeneratorTest, PersistentDifferentRenderProcessIds) {
NotificationIdGenerator second_generator(browser_context(),
kRenderProcessId + 1);
EXPECT_EQ(
generator()->GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId),
second_generator.GenerateForPersistentNotification(
origin(), kExampleTag, kPersistentNotificationId));
EXPECT_EQ(
generator()->GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId),
second_generator.GenerateForPersistentNotification(
origin(), "" /* tag */, kPersistentNotificationId));
}
// -----------------------------------------------------------------------------
// Non-persistent notifications
//
// Tests covering the logic specific to non-persistent notifications. This kind
// of notification cares about the renderer process they were created by when
// the notification does not have a tag, since multiple renderers would restart
// the count for non-persistent notification ids.
TEST_F(NotificationIdGeneratorTest, NonPersistentDifferentRenderProcessIds) {
NotificationIdGenerator second_generator(browser_context(),
kRenderProcessId + 1);
EXPECT_EQ(
generator()->GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId),
second_generator.GenerateForNonPersistentNotification(
origin(), kExampleTag, kNonPersistentNotificationId));
EXPECT_NE(
generator()->GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId),
second_generator.GenerateForNonPersistentNotification(
origin(), "" /* tag */, kNonPersistentNotificationId));
}
// Concatenation of the render process id and the non-persistent notification
// id should not result in the generation of a duplicated notification id.
TEST_F(NotificationIdGeneratorTest, NonPersistentRenderProcessIdAmbiguity) {
NotificationIdGenerator generator_rpi_5(browser_context(), 5);
NotificationIdGenerator generator_rpi_51(browser_context(), 51);
EXPECT_NE(
generator_rpi_5.GenerateForNonPersistentNotification(
origin(), "" /* tag */, 1337 /* non_persistent_notification_id */),
generator_rpi_51.GenerateForNonPersistentNotification(
origin(), "" /* tag */, 337 /* non_persistent_notification_id */));
}
} // namespace
} // namespace content
|