Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/test/gtest/TestReadWrite.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "gtest/gtest.h"
8
#include "mozilla/BasePrincipal.h"
9
#include "mozilla/dom/ServiceWorkerRegistrar.h"
10
#include "mozilla/dom/ServiceWorkerRegistrarTypes.h"
11
#include "mozilla/ipc/PBackgroundSharedTypes.h"
12
13
#include "nsAppDirectoryServiceDefs.h"
14
#include "nsIFile.h"
15
#include "nsIOutputStream.h"
16
#include "nsNetUtil.h"
17
#include "nsPrintfCString.h"
18
#include "nsIServiceWorkerManager.h"
19
20
#include "prtime.h"
21
22
using namespace mozilla::dom;
23
using namespace mozilla::ipc;
24
25
class ServiceWorkerRegistrarTest : public ServiceWorkerRegistrar
26
{
27
public:
28
  ServiceWorkerRegistrarTest()
29
0
  {
30
0
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
31
0
    nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
32
0
                                       getter_AddRefs(mProfileDir));
33
0
    MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
34
#else
35
    NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
36
                           getter_AddRefs(mProfileDir));
37
#endif
38
0
    MOZ_DIAGNOSTIC_ASSERT(mProfileDir);
39
0
  }
40
41
0
  nsresult TestReadData() { return ReadData(); }
42
0
  nsresult TestWriteData() { return WriteData(mData); }
43
0
  void TestDeleteData() { DeleteData(); }
44
45
  void TestRegisterServiceWorker(const ServiceWorkerRegistrationData& aData)
46
0
  {
47
0
    RegisterServiceWorkerInternal(aData);
48
0
  }
49
50
0
  nsTArray<ServiceWorkerRegistrationData>& TestGetData() { return mData; }
51
};
52
53
already_AddRefed<nsIFile>
54
GetFile()
55
0
{
56
0
  nsCOMPtr<nsIFile> file;
57
0
  nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
58
0
                                       getter_AddRefs(file));
59
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
60
0
    return nullptr;
61
0
  }
62
0
63
0
  file->Append(NS_LITERAL_STRING(SERVICEWORKERREGISTRAR_FILE));
64
0
  return file.forget();
65
0
}
66
67
bool
68
CreateFile(const nsACString& aData)
69
0
{
70
0
  nsCOMPtr<nsIFile> file = GetFile();
71
0
72
0
  nsCOMPtr<nsIOutputStream> stream;
73
0
  nsresult rv = NS_NewLocalFileOutputStream(getter_AddRefs(stream), file);
74
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
75
0
    return false;
76
0
  }
77
0
78
0
  uint32_t count;
79
0
  rv = stream->Write(aData.Data(), aData.Length(), &count);
80
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
81
0
    return false;
82
0
  }
83
0
84
0
  if (count != aData.Length()) {
85
0
    return false;
86
0
  }
87
0
88
0
  return true;
89
0
}
90
91
TEST(ServiceWorkerRegistrar, TestNoFile)
92
0
{
93
0
  nsCOMPtr<nsIFile> file = GetFile();
94
0
  ASSERT_TRUE(file) << "GetFile must return a nsIFIle";
95
0
96
0
  bool exists;
97
0
  nsresult rv = file->Exists(&exists);
98
0
  ASSERT_EQ(NS_OK, rv) << "nsIFile::Exists cannot fail";
99
0
100
0
  if (exists) {
101
0
    rv = file->Remove(false);
102
0
    ASSERT_EQ(NS_OK, rv) << "nsIFile::Remove cannot fail";
103
0
  }
104
0
105
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
106
0
107
0
  rv = swr->TestReadData();
108
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
109
0
110
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
111
0
  ASSERT_EQ((uint32_t)0, data.Length()) << "No data should be found in an empty file";
112
0
}
113
114
TEST(ServiceWorkerRegistrar, TestEmptyFile)
115
0
{
116
0
  ASSERT_TRUE(CreateFile(EmptyCString())) << "CreateFile should not fail";
117
0
118
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
119
0
120
0
  nsresult rv = swr->TestReadData();
121
0
  ASSERT_NE(NS_OK, rv) << "ReadData() should fail if the file is empty";
122
0
123
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
124
0
  ASSERT_EQ((uint32_t)0, data.Length()) << "No data should be found in an empty file";
125
0
}
126
127
TEST(ServiceWorkerRegistrar, TestRightVersionFile)
128
0
{
129
0
  ASSERT_TRUE(CreateFile(NS_LITERAL_CSTRING(SERVICEWORKERREGISTRAR_VERSION "\n"))) << "CreateFile should not fail";
130
0
131
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
132
0
133
0
  nsresult rv = swr->TestReadData();
134
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail when the version is correct";
135
0
136
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
137
0
  ASSERT_EQ((uint32_t)0, data.Length()) << "No data should be found in an empty file";
138
0
}
139
140
TEST(ServiceWorkerRegistrar, TestWrongVersionFile)
141
0
{
142
0
  ASSERT_TRUE(CreateFile(NS_LITERAL_CSTRING(SERVICEWORKERREGISTRAR_VERSION "bla\n"))) << "CreateFile should not fail";
143
0
144
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
145
0
146
0
  nsresult rv = swr->TestReadData();
147
0
  ASSERT_NE(NS_OK, rv) << "ReadData() should fail when the version is not correct";
148
0
149
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
150
0
  ASSERT_EQ((uint32_t)0, data.Length()) << "No data should be found in an empty file";
151
0
}
152
153
TEST(ServiceWorkerRegistrar, TestReadData)
154
0
{
155
0
  nsAutoCString buffer(SERVICEWORKERREGISTRAR_VERSION "\n");
156
0
157
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
158
0
  buffer.AppendLiteral("https://scope_0.org\ncurrentWorkerURL 0\n");
159
0
  buffer.Append(SERVICEWORKERREGISTRAR_TRUE "\n");
160
0
  buffer.AppendLiteral("cacheName 0\n");
161
0
  buffer.AppendInt(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS, 16);
162
0
  buffer.AppendLiteral("\n");
163
0
  buffer.AppendInt(0);
164
0
  buffer.AppendLiteral("\n");
165
0
  buffer.AppendInt(0);
166
0
  buffer.AppendLiteral("\n");
167
0
  buffer.AppendInt(0);
168
0
  buffer.AppendLiteral("\n");
169
0
  buffer.Append(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
170
0
171
0
  buffer.AppendLiteral("\n");
172
0
  buffer.AppendLiteral("https://scope_1.org\ncurrentWorkerURL 1\n");
173
0
  buffer.Append(SERVICEWORKERREGISTRAR_FALSE "\n");
174
0
  buffer.AppendLiteral("cacheName 1\n");
175
0
  buffer.AppendInt(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_ALL, 16);
176
0
  buffer.AppendLiteral("\n");
177
0
  PRTime ts = PR_Now();
178
0
  buffer.AppendInt(ts);
179
0
  buffer.AppendLiteral("\n");
180
0
  buffer.AppendInt(ts);
181
0
  buffer.AppendLiteral("\n");
182
0
  buffer.AppendInt(ts);
183
0
  buffer.AppendLiteral("\n");
184
0
  buffer.Append(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
185
0
186
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
187
0
188
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
189
0
190
0
  nsresult rv = swr->TestReadData();
191
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
192
0
193
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
194
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
195
0
196
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
197
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
198
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
199
0
200
0
  nsAutoCString suffix0;
201
0
  cInfo0.attrs().CreateSuffix(suffix0);
202
0
203
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
204
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
205
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
206
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
207
0
  ASSERT_TRUE(data[0].currentWorkerHandlesFetch());
208
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
209
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
210
0
            data[0].updateViaCache());
211
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
212
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
213
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
214
0
215
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
216
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
217
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
218
0
219
0
  nsAutoCString suffix1;
220
0
  cInfo1.attrs().CreateSuffix(suffix1);
221
0
222
0
  ASSERT_STREQ("", suffix1.get());
223
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
224
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
225
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
226
0
  ASSERT_FALSE(data[1].currentWorkerHandlesFetch());
227
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
228
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_ALL,
229
0
            data[1].updateViaCache());
230
0
  ASSERT_EQ((int64_t)ts, data[1].currentWorkerInstalledTime());
231
0
  ASSERT_EQ((int64_t)ts, data[1].currentWorkerActivatedTime());
232
0
  ASSERT_EQ((int64_t)ts, data[1].lastUpdateTime());
233
0
}
234
235
TEST(ServiceWorkerRegistrar, TestDeleteData)
236
0
{
237
0
  ASSERT_TRUE(CreateFile(NS_LITERAL_CSTRING("Foobar"))) << "CreateFile should not fail";
238
0
239
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
240
0
241
0
  swr->TestDeleteData();
242
0
243
0
  nsCOMPtr<nsIFile> file = GetFile();
244
0
245
0
  bool exists;
246
0
  nsresult rv = file->Exists(&exists);
247
0
  ASSERT_EQ(NS_OK, rv) << "nsIFile::Exists cannot fail";
248
0
249
0
  ASSERT_FALSE(exists) << "The file should not exist after a DeleteData().";
250
0
}
251
252
TEST(ServiceWorkerRegistrar, TestWriteData)
253
0
{
254
0
  {
255
0
    RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
256
0
257
0
    for (int i = 0; i < 10; ++i) {
258
0
      ServiceWorkerRegistrationData reg;
259
0
260
0
      reg.scope() = nsPrintfCString("https://scope_write_%d.org", i);
261
0
      reg.currentWorkerURL() = nsPrintfCString("currentWorkerURL write %d", i);
262
0
      reg.currentWorkerHandlesFetch() = true;
263
0
      reg.cacheName() =
264
0
        NS_ConvertUTF8toUTF16(nsPrintfCString("cacheName write %d", i));
265
0
      reg.updateViaCache() =
266
0
        nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS;
267
0
268
0
      reg.currentWorkerInstalledTime() = PR_Now();
269
0
      reg.currentWorkerActivatedTime() = PR_Now();
270
0
      reg.lastUpdateTime() = PR_Now();
271
0
272
0
      nsAutoCString spec;
273
0
      spec.AppendPrintf("spec write %d", i);
274
0
      reg.principal() =
275
0
        mozilla::ipc::ContentPrincipalInfo(mozilla::OriginAttributes(i, i % 2),
276
0
                                           spec, spec);
277
0
278
0
      swr->TestRegisterServiceWorker(reg);
279
0
    }
280
0
281
0
    nsresult rv = swr->TestWriteData();
282
0
    ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
283
0
  }
284
0
285
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
286
0
287
0
  nsresult rv = swr->TestReadData();
288
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
289
0
290
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
291
0
  ASSERT_EQ((uint32_t)10, data.Length()) << "10 entries should be found";
292
0
293
0
  for (int i = 0; i < 10; ++i) {
294
0
    nsAutoCString test;
295
0
296
0
    ASSERT_EQ(data[i].principal().type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo);
297
0
    const mozilla::ipc::ContentPrincipalInfo& cInfo = data[i].principal();
298
0
299
0
    mozilla::OriginAttributes attrs(i, i % 2);
300
0
    nsAutoCString suffix, expectSuffix;
301
0
    attrs.CreateSuffix(expectSuffix);
302
0
    cInfo.attrs().CreateSuffix(suffix);
303
0
304
0
    ASSERT_STREQ(expectSuffix.get(), suffix.get());
305
0
306
0
    test.AppendPrintf("https://scope_write_%d.org", i);
307
0
    ASSERT_STREQ(test.get(), cInfo.spec().get());
308
0
309
0
    test.Truncate();
310
0
    test.AppendPrintf("https://scope_write_%d.org", i);
311
0
    ASSERT_STREQ(test.get(), data[i].scope().get());
312
0
313
0
    test.Truncate();
314
0
    test.AppendPrintf("currentWorkerURL write %d", i);
315
0
    ASSERT_STREQ(test.get(), data[i].currentWorkerURL().get());
316
0
317
0
    ASSERT_EQ(true, data[i].currentWorkerHandlesFetch());
318
0
319
0
    test.Truncate();
320
0
    test.AppendPrintf("cacheName write %d", i);
321
0
    ASSERT_STREQ(test.get(), NS_ConvertUTF16toUTF8(data[i].cacheName()).get());
322
0
323
0
    ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
324
0
              data[i].updateViaCache());
325
0
326
0
    ASSERT_NE((int64_t)0, data[i].currentWorkerInstalledTime());
327
0
    ASSERT_NE((int64_t)0, data[i].currentWorkerActivatedTime());
328
0
    ASSERT_NE((int64_t)0, data[i].lastUpdateTime());
329
0
  }
330
0
}
331
332
TEST(ServiceWorkerRegistrar, TestVersion2Migration)
333
0
{
334
0
  nsAutoCString buffer("2" "\n");
335
0
336
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
337
0
  buffer.AppendLiteral("spec 0\nhttps://scope_0.org\nscriptSpec 0\ncurrentWorkerURL 0\nactiveCache 0\nwaitingCache 0\n");
338
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
339
0
340
0
  buffer.AppendLiteral("\n");
341
0
  buffer.AppendLiteral("spec 1\nhttps://scope_1.org\nscriptSpec 1\ncurrentWorkerURL 1\nactiveCache 1\nwaitingCache 1\n");
342
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
343
0
344
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
345
0
346
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
347
0
348
0
  nsresult rv = swr->TestReadData();
349
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
350
0
351
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
352
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
353
0
354
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
355
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
356
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
357
0
358
0
  nsAutoCString suffix0;
359
0
  cInfo0.attrs().CreateSuffix(suffix0);
360
0
361
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
362
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
363
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
364
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
365
0
  ASSERT_EQ(true, data[0].currentWorkerHandlesFetch());
366
0
  ASSERT_STREQ("activeCache 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
367
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
368
0
            data[0].updateViaCache());
369
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
370
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
371
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
372
0
373
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
374
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
375
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
376
0
377
0
  nsAutoCString suffix1;
378
0
  cInfo1.attrs().CreateSuffix(suffix1);
379
0
380
0
  ASSERT_STREQ("", suffix1.get());
381
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
382
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
383
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
384
0
  ASSERT_EQ(true, data[1].currentWorkerHandlesFetch());
385
0
  ASSERT_STREQ("activeCache 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
386
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
387
0
            data[1].updateViaCache());
388
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
389
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
390
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
391
0
}
392
393
TEST(ServiceWorkerRegistrar, TestVersion3Migration)
394
0
{
395
0
  nsAutoCString buffer("3" "\n");
396
0
397
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
398
0
  buffer.AppendLiteral("spec 0\nhttps://scope_0.org\ncurrentWorkerURL 0\ncacheName 0\n");
399
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
400
0
401
0
  buffer.AppendLiteral("\n");
402
0
  buffer.AppendLiteral("spec 1\nhttps://scope_1.org\ncurrentWorkerURL 1\ncacheName 1\n");
403
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
404
0
405
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
406
0
407
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
408
0
409
0
  nsresult rv = swr->TestReadData();
410
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
411
0
412
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
413
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
414
0
415
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
416
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
417
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
418
0
419
0
  nsAutoCString suffix0;
420
0
  cInfo0.attrs().CreateSuffix(suffix0);
421
0
422
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
423
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
424
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
425
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
426
0
  ASSERT_EQ(true, data[0].currentWorkerHandlesFetch());
427
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
428
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
429
0
            data[0].updateViaCache());
430
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
431
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
432
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
433
0
434
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
435
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
436
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
437
0
438
0
  nsAutoCString suffix1;
439
0
  cInfo1.attrs().CreateSuffix(suffix1);
440
0
441
0
  ASSERT_STREQ("", suffix1.get());
442
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
443
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
444
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
445
0
  ASSERT_EQ(true, data[1].currentWorkerHandlesFetch());
446
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
447
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
448
0
            data[1].updateViaCache());
449
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
450
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
451
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
452
0
}
453
454
TEST(ServiceWorkerRegistrar, TestVersion4Migration)
455
0
{
456
0
  nsAutoCString buffer("4" "\n");
457
0
458
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
459
0
  buffer.AppendLiteral("https://scope_0.org\ncurrentWorkerURL 0\ncacheName 0\n");
460
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
461
0
462
0
  buffer.AppendLiteral("\n");
463
0
  buffer.AppendLiteral("https://scope_1.org\ncurrentWorkerURL 1\ncacheName 1\n");
464
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
465
0
466
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
467
0
468
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
469
0
470
0
  nsresult rv = swr->TestReadData();
471
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
472
0
473
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
474
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
475
0
476
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
477
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
478
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
479
0
480
0
  nsAutoCString suffix0;
481
0
  cInfo0.attrs().CreateSuffix(suffix0);
482
0
483
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
484
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
485
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
486
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
487
0
  // default is true
488
0
  ASSERT_EQ(true, data[0].currentWorkerHandlesFetch());
489
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
490
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
491
0
            data[0].updateViaCache());
492
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
493
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
494
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
495
0
496
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
497
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
498
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
499
0
500
0
  nsAutoCString suffix1;
501
0
  cInfo1.attrs().CreateSuffix(suffix1);
502
0
503
0
  ASSERT_STREQ("", suffix1.get());
504
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
505
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
506
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
507
0
  // default is true
508
0
  ASSERT_EQ(true, data[1].currentWorkerHandlesFetch());
509
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
510
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
511
0
            data[1].updateViaCache());
512
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
513
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
514
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
515
0
}
516
517
TEST(ServiceWorkerRegistrar, TestVersion5Migration)
518
0
{
519
0
  nsAutoCString buffer("5" "\n");
520
0
521
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
522
0
  buffer.AppendLiteral("https://scope_0.org\ncurrentWorkerURL 0\n");
523
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TRUE "\n");
524
0
  buffer.AppendLiteral("cacheName 0\n");
525
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
526
0
527
0
  buffer.AppendLiteral("\n");
528
0
  buffer.AppendLiteral("https://scope_1.org\ncurrentWorkerURL 1\n");
529
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_FALSE "\n");
530
0
  buffer.AppendLiteral("cacheName 1\n");
531
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
532
0
533
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
534
0
535
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
536
0
537
0
  nsresult rv = swr->TestReadData();
538
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
539
0
540
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
541
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
542
0
543
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
544
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
545
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
546
0
547
0
  nsAutoCString suffix0;
548
0
  cInfo0.attrs().CreateSuffix(suffix0);
549
0
550
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
551
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
552
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
553
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
554
0
  ASSERT_TRUE(data[0].currentWorkerHandlesFetch());
555
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
556
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
557
0
            data[0].updateViaCache());
558
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
559
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
560
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
561
0
562
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
563
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
564
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
565
0
566
0
  nsAutoCString suffix1;
567
0
  cInfo1.attrs().CreateSuffix(suffix1);
568
0
569
0
  ASSERT_STREQ("", suffix1.get());
570
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
571
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
572
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
573
0
  ASSERT_FALSE(data[1].currentWorkerHandlesFetch());
574
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
575
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
576
0
            data[1].updateViaCache());
577
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
578
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
579
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
580
0
}
581
582
TEST(ServiceWorkerRegistrar, TestVersion6Migration)
583
0
{
584
0
  nsAutoCString buffer("6" "\n");
585
0
586
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
587
0
  buffer.AppendLiteral("https://scope_0.org\ncurrentWorkerURL 0\n");
588
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TRUE "\n");
589
0
  buffer.AppendLiteral("cacheName 0\n");
590
0
  buffer.AppendInt(nsIRequest::LOAD_NORMAL, 16);
591
0
  buffer.AppendLiteral("\n");
592
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
593
0
594
0
  buffer.AppendLiteral("\n");
595
0
  buffer.AppendLiteral("https://scope_1.org\ncurrentWorkerURL 1\n");
596
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_FALSE "\n");
597
0
  buffer.AppendLiteral("cacheName 1\n");
598
0
  buffer.AppendInt(nsIRequest::VALIDATE_ALWAYS, 16);
599
0
  buffer.AppendLiteral("\n");
600
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
601
0
602
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
603
0
604
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
605
0
606
0
  nsresult rv = swr->TestReadData();
607
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
608
0
609
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
610
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
611
0
612
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
613
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
614
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
615
0
616
0
  nsAutoCString suffix0;
617
0
  cInfo0.attrs().CreateSuffix(suffix0);
618
0
619
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
620
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
621
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
622
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
623
0
  ASSERT_TRUE(data[0].currentWorkerHandlesFetch());
624
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
625
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_ALL,
626
0
            data[0].updateViaCache());
627
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
628
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
629
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
630
0
631
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
632
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
633
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
634
0
635
0
  nsAutoCString suffix1;
636
0
  cInfo1.attrs().CreateSuffix(suffix1);
637
0
638
0
  ASSERT_STREQ("", suffix1.get());
639
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
640
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
641
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
642
0
  ASSERT_FALSE(data[1].currentWorkerHandlesFetch());
643
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
644
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
645
0
            data[1].updateViaCache());
646
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
647
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
648
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
649
0
}
650
651
TEST(ServiceWorkerRegistrar, TestVersion7Migration)
652
0
{
653
0
  nsAutoCString buffer("7" "\n");
654
0
655
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
656
0
  buffer.AppendLiteral("https://scope_0.org\ncurrentWorkerURL 0\n");
657
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TRUE "\n");
658
0
  buffer.AppendLiteral("cacheName 0\n");
659
0
  buffer.AppendInt(nsIRequest::LOAD_NORMAL, 16);
660
0
  buffer.AppendLiteral("\n");
661
0
  buffer.AppendInt(0);
662
0
  buffer.AppendLiteral("\n");
663
0
  buffer.AppendInt(0);
664
0
  buffer.AppendLiteral("\n");
665
0
  buffer.AppendInt(0);
666
0
  buffer.AppendLiteral("\n");
667
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
668
0
669
0
  buffer.AppendLiteral("\n");
670
0
  buffer.AppendLiteral("https://scope_1.org\ncurrentWorkerURL 1\n");
671
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_FALSE "\n");
672
0
  buffer.AppendLiteral("cacheName 1\n");
673
0
  buffer.AppendInt(nsIRequest::VALIDATE_ALWAYS, 16);
674
0
  buffer.AppendLiteral("\n");
675
0
  PRTime ts = PR_Now();
676
0
  buffer.AppendInt(ts);
677
0
  buffer.AppendLiteral("\n");
678
0
  buffer.AppendInt(ts);
679
0
  buffer.AppendLiteral("\n");
680
0
  buffer.AppendInt(ts);
681
0
  buffer.AppendLiteral("\n");
682
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
683
0
684
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
685
0
686
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
687
0
688
0
  nsresult rv = swr->TestReadData();
689
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
690
0
691
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
692
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
693
0
694
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
695
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
696
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
697
0
698
0
  nsAutoCString suffix0;
699
0
  cInfo0.attrs().CreateSuffix(suffix0);
700
0
701
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
702
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
703
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
704
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
705
0
  ASSERT_TRUE(data[0].currentWorkerHandlesFetch());
706
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
707
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_ALL,
708
0
            data[0].updateViaCache());
709
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
710
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
711
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
712
0
713
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
714
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
715
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
716
0
717
0
  nsAutoCString suffix1;
718
0
  cInfo1.attrs().CreateSuffix(suffix1);
719
0
720
0
  ASSERT_STREQ("", suffix1.get());
721
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
722
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
723
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
724
0
  ASSERT_FALSE(data[1].currentWorkerHandlesFetch());
725
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
726
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
727
0
            data[1].updateViaCache());
728
0
  ASSERT_EQ((int64_t)ts, data[1].currentWorkerInstalledTime());
729
0
  ASSERT_EQ((int64_t)ts, data[1].currentWorkerActivatedTime());
730
0
  ASSERT_EQ((int64_t)ts, data[1].lastUpdateTime());
731
0
}
732
733
TEST(ServiceWorkerRegistrar, TestDedupeRead)
734
0
{
735
0
  nsAutoCString buffer("3" "\n");
736
0
737
0
  // unique entries
738
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
739
0
  buffer.AppendLiteral("spec 0\nhttps://scope_0.org\ncurrentWorkerURL 0\ncacheName 0\n");
740
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
741
0
742
0
  buffer.AppendLiteral("\n");
743
0
  buffer.AppendLiteral("spec 1\nhttps://scope_1.org\ncurrentWorkerURL 1\ncacheName 1\n");
744
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
745
0
746
0
  // dupe entries
747
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
748
0
  buffer.AppendLiteral("spec 1\nhttps://scope_0.org\ncurrentWorkerURL 0\ncacheName 0\n");
749
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
750
0
751
0
  buffer.AppendLiteral("^appId=123&inBrowser=1\n");
752
0
  buffer.AppendLiteral("spec 2\nhttps://scope_0.org\ncurrentWorkerURL 0\ncacheName 0\n");
753
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
754
0
755
0
  buffer.AppendLiteral("\n");
756
0
  buffer.AppendLiteral("spec 3\nhttps://scope_1.org\ncurrentWorkerURL 1\ncacheName 1\n");
757
0
  buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR "\n");
758
0
759
0
  ASSERT_TRUE(CreateFile(buffer)) << "CreateFile should not fail";
760
0
761
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
762
0
763
0
  nsresult rv = swr->TestReadData();
764
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
765
0
766
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
767
0
  ASSERT_EQ((uint32_t)2, data.Length()) << "2 entries should be found";
768
0
769
0
  const mozilla::ipc::PrincipalInfo& info0 = data[0].principal();
770
0
  ASSERT_EQ(info0.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
771
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo0 = data[0].principal();
772
0
773
0
  nsAutoCString suffix0;
774
0
  cInfo0.attrs().CreateSuffix(suffix0);
775
0
776
0
  ASSERT_STREQ("^appId=123&inBrowser=1", suffix0.get());
777
0
  ASSERT_STREQ("https://scope_0.org", cInfo0.spec().get());
778
0
  ASSERT_STREQ("https://scope_0.org", data[0].scope().get());
779
0
  ASSERT_STREQ("currentWorkerURL 0", data[0].currentWorkerURL().get());
780
0
  ASSERT_EQ(true, data[0].currentWorkerHandlesFetch());
781
0
  ASSERT_STREQ("cacheName 0", NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
782
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
783
0
            data[0].updateViaCache());
784
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
785
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
786
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
787
0
788
0
  const mozilla::ipc::PrincipalInfo& info1 = data[1].principal();
789
0
  ASSERT_EQ(info1.type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) << "First principal must be content";
790
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo1 = data[1].principal();
791
0
792
0
  nsAutoCString suffix1;
793
0
  cInfo1.attrs().CreateSuffix(suffix1);
794
0
795
0
  ASSERT_STREQ("", suffix1.get());
796
0
  ASSERT_STREQ("https://scope_1.org", cInfo1.spec().get());
797
0
  ASSERT_STREQ("https://scope_1.org", data[1].scope().get());
798
0
  ASSERT_STREQ("currentWorkerURL 1", data[1].currentWorkerURL().get());
799
0
  ASSERT_EQ(true, data[1].currentWorkerHandlesFetch());
800
0
  ASSERT_STREQ("cacheName 1", NS_ConvertUTF16toUTF8(data[1].cacheName()).get());
801
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
802
0
            data[1].updateViaCache());
803
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerInstalledTime());
804
0
  ASSERT_EQ((int64_t)0, data[1].currentWorkerActivatedTime());
805
0
  ASSERT_EQ((int64_t)0, data[1].lastUpdateTime());
806
0
}
807
808
TEST(ServiceWorkerRegistrar, TestDedupeWrite)
809
0
{
810
0
  {
811
0
    RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
812
0
813
0
    for (int i = 0; i < 10; ++i) {
814
0
      ServiceWorkerRegistrationData reg;
815
0
816
0
      reg.scope() = NS_LITERAL_CSTRING("https://scope_write.dedupe");
817
0
      reg.currentWorkerURL() = nsPrintfCString("currentWorkerURL write %d", i);
818
0
      reg.currentWorkerHandlesFetch() = true;
819
0
      reg.cacheName() =
820
0
        NS_ConvertUTF8toUTF16(nsPrintfCString("cacheName write %d", i));
821
0
      reg.updateViaCache() =
822
0
        nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS;
823
0
824
0
      nsAutoCString spec;
825
0
      spec.AppendPrintf("spec write dedupe/%d", i);
826
0
      reg.principal() =
827
0
        mozilla::ipc::ContentPrincipalInfo(mozilla::OriginAttributes(0, false),
828
0
                                           spec, spec);
829
0
830
0
      swr->TestRegisterServiceWorker(reg);
831
0
    }
832
0
833
0
    nsresult rv = swr->TestWriteData();
834
0
    ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
835
0
  }
836
0
837
0
  RefPtr<ServiceWorkerRegistrarTest> swr = new ServiceWorkerRegistrarTest;
838
0
839
0
  nsresult rv = swr->TestReadData();
840
0
  ASSERT_EQ(NS_OK, rv) << "ReadData() should not fail";
841
0
842
0
  // Duplicate entries should be removed.
843
0
  const nsTArray<ServiceWorkerRegistrationData>& data = swr->TestGetData();
844
0
  ASSERT_EQ((uint32_t)1, data.Length()) << "1 entry should be found";
845
0
846
0
  ASSERT_EQ(data[0].principal().type(), mozilla::ipc::PrincipalInfo::TContentPrincipalInfo);
847
0
  const mozilla::ipc::ContentPrincipalInfo& cInfo = data[0].principal();
848
0
849
0
  mozilla::OriginAttributes attrs(0, false);
850
0
  nsAutoCString suffix, expectSuffix;
851
0
  attrs.CreateSuffix(expectSuffix);
852
0
  cInfo.attrs().CreateSuffix(suffix);
853
0
854
0
  // Last entry passed to RegisterServiceWorkerInternal() should overwrite
855
0
  // previous values.  So expect "9" in values here.
856
0
  ASSERT_STREQ(expectSuffix.get(), suffix.get());
857
0
  ASSERT_STREQ("https://scope_write.dedupe", cInfo.spec().get());
858
0
  ASSERT_STREQ("https://scope_write.dedupe", data[0].scope().get());
859
0
  ASSERT_STREQ("currentWorkerURL write 9", data[0].currentWorkerURL().get());
860
0
  ASSERT_EQ(true, data[0].currentWorkerHandlesFetch());
861
0
  ASSERT_STREQ("cacheName write 9",
862
0
               NS_ConvertUTF16toUTF8(data[0].cacheName()).get());
863
0
  ASSERT_EQ(nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS,
864
0
            data[0].updateViaCache());
865
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerInstalledTime());
866
0
  ASSERT_EQ((int64_t)0, data[0].currentWorkerActivatedTime());
867
0
  ASSERT_EQ((int64_t)0, data[0].lastUpdateTime());
868
0
}
869
870
0
int main(int argc, char** argv) {
871
0
  ::testing::InitGoogleTest(&argc, argv);
872
0
873
0
  int rv = RUN_ALL_TESTS();
874
0
  return rv;
875
0
}