summaryrefslogtreecommitdiffstats
path: root/src/tests/tests/storage.js
blob: cb4042dd5fe28fa7ccb60250b35746395de3605a (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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* globals badger:false, constants:false */

(function () {

const DOMAIN = "example.com",
  SUBDOMAIN = "widgets." + DOMAIN,
  SUBSUBDOMAIN = "cdn." + SUBDOMAIN;

let storage = badger.storage,
  actionMap,
  snitchMap;

QUnit.module("Storage", {
  before: (assert) => {
    // can't initialize globally above
    // as they get initialized too early when run by Selenium
    actionMap = storage.getStore('action_map');
    snitchMap = storage.getStore('snitch_map');

    assert.notOk(actionMap.getItem(DOMAIN),
      "test domain is not yet in action_map");
    assert.notOk(snitchMap.getItem(DOMAIN),
      "test domain is not yet in snitch_map");
  }
});

QUnit.test("testGetBadgerStorage", function (assert) {
  assert.ok(actionMap.updateObject instanceof Function, "actionMap is a pbstorage");
});

QUnit.test("test BadgerStorage methods", function (assert) {
  actionMap.setItem('foo', 'bar');
  assert.equal(actionMap.getItem('foo'), 'bar');
  assert.ok(actionMap.hasItem('foo'));
  actionMap.deleteItem('foo');
  assert.notOk(actionMap.hasItem('foo'));
});

QUnit.test("test user override of default action for domain", function (assert) {
  badger.saveAction("allow", "pbtest.org");
  assert.equal(storage.getAction("pbtest.org"), constants.USER_ALLOW);
  badger.saveAction("block", "pbtest.org");
  assert.equal(storage.getAction("pbtest.org"), constants.USER_BLOCK);
  badger.saveAction("allow", "pbtest.org");
  assert.equal(storage.getAction("pbtest.org"), constants.USER_ALLOW);
  storage.revertUserAction("pbtest.org");
  assert.equal(storage.getAction("pbtest.org"), constants.NO_TRACKING);
});

QUnit.test("settings map merging", (assert) => {
  let settings_map = storage.getStore('settings_map');

  // overwrite settings with test values
  settings_map.setItem('disabledSites', ['example.com']);
  settings_map.setItem('showCounter', true);

  // merge settings
  settings_map.merge({
    disabledSites: ['www.nytimes.com'],
    showCounter: false,
  });

  // verify
  assert.deepEqual(
    settings_map.getItem('disabledSites'),
    ['example.com', 'www.nytimes.com'],
    "disabled site lists are combined when merging settings"
  );
  assert.ok(!settings_map.getItem('showCounter'), "other settings are overwritten");
});

// previously:
// https://github.com/EFForg/privacybadger/pull/1911#issuecomment-379896911
QUnit.test("action map merge copies/breaks references", (assert) => {
  let data = {
    dnt: false,
    heuristicAction: '',
    nextUpdateTime: 100,
    userAction: 'user_block'
  };

  actionMap.merge({[DOMAIN]: data});
  assert.deepEqual(
    actionMap.getItem(DOMAIN),
    data,
    "test domain was imported");

  // set a property on the original object
  data.userAction = "user_allow";

  // this should not affect data in storage
  assert.equal(actionMap.getItem(DOMAIN).userAction,
    "user_block",
    "already imported data should be left alone " +
    "when modifying object used for import");
});

QUnit.test("action map merge only updates user action", (assert) => {
  actionMap.setItem(DOMAIN,
    {dnt: false, heuristicAction: '', nextUpdateTime: 100, userAction: ''});
  assert.equal(actionMap.getItem(DOMAIN).nextUpdateTime, 100);

  let newValue = {dnt: true, heuristicAction: constants.BLOCK,
    nextUpdateTime: 99, userAction: constants.USER_BLOCK};
  actionMap.merge({[DOMAIN]: newValue});
  assert.equal(actionMap.getItem(DOMAIN).userAction,
    constants.USER_BLOCK,
    "userAction should be merged if it's set");
  assert.equal(actionMap.getItem(DOMAIN).heuristicAction, '',
    'heuristicAction should never be overwritten');

  newValue.userAction = '';
  actionMap.merge({[DOMAIN]: newValue});
  assert.equal(actionMap.getItem(DOMAIN).userAction,
    constants.USER_BLOCK,
    'blank userAction should not overwrite anything');
});

QUnit.test("action map merge creates new entry if necessary", (assert) => {
  assert.notOk(actionMap.hasItem('newsite.com'));

  let newValue = {dnt: false, heuristicAction: constants.BLOCK,
    nextUpdateTime: 100, userAction: ''};
  actionMap.merge({'newsite.com': newValue});
  assert.notOk(actionMap.hasItem('newsite.com'),
    'action map entry should not be created for heuristicAction alone');

  newValue.userAction = constants.USER_BLOCK;
  actionMap.merge({'newsite.com': newValue});
  assert.ok(actionMap.hasItem('newsite.com'),
    'action map entry should be created if userAction is set');

  actionMap.deleteItem('newsite.com');

  newValue.userAction = '';
  newValue.dnt = true;
  actionMap.merge({'newsite.com': newValue});
  assert.ok(actionMap.hasItem('newsite.com'),
    'action map entry should be created if DNT is set');
});

QUnit.test("action map merge updates with latest DNT info", (assert) => {
  actionMap.setItem(DOMAIN,
    {dnt: false, heuristicAction: '', nextUpdateTime: 100, userAction: ''});

  // DNT should not be merged if nextUpdateTime is earlier
  let newValue = {dnt: true, heuristicAction: '', nextUpdateTime: 99, userAction: ''};
  actionMap.merge({[DOMAIN]: newValue});
  assert.equal(actionMap.getItem(DOMAIN).nextUpdateTime, 100,
    'nextUpdateTime should not be changed to an earlier time');
  assert.notOk(actionMap.getItem(DOMAIN).dnt,
    'DNT value should not be updated by out-of-date information');

  // DNT should be merged if it's more up-to-date
  newValue.nextUpdateTime = 101;
  actionMap.merge({[DOMAIN]: newValue});
  assert.equal(actionMap.getItem(DOMAIN).nextUpdateTime, 101,
    'nextUpdateTime should be updated to later time');
  assert.ok(actionMap.getItem(DOMAIN).dnt,
    'DNT value should be updated with more recent information');
});

QUnit.test("action map merge handles missing nextUpdateTime", (assert) => {
  let newValue = {
    dnt: true,
    heuristicAction: '',
    userAction: ''
  };

  assert.notOk(newValue.hasOwnProperty('nextUpdateTime'),
    "nextUpdateTime is indeed missing from the import");

  // new DNT domain should be imported
  actionMap.merge({[DOMAIN]: newValue});
  assert.deepEqual(
    actionMap.getItem(DOMAIN),
    Object.assign({ nextUpdateTime: 0 }, newValue),
    "test domain was imported and nextUpdateTime got initialized");

  // existing DNT domain should be left alone
  // as we don't know how fresh the import is
  newValue.dnt = false;
  actionMap.merge({[DOMAIN]: newValue});
  assert.ok(actionMap.getItem(DOMAIN).dnt,
    "existing data should be left alone " +
    "when unable to determine recency of new data");

  // now set the timestamp and try again
  newValue.nextUpdateTime = 200;
  actionMap.merge({[DOMAIN]: newValue});
  assert.notOk(actionMap.getItem(DOMAIN).dnt,
    "DNT got overriden now that new data seems fresher");
});

QUnit.test("action map merge handles missing userAction", (assert) => {
  let newValue = {
    heuristicAction: 'allow',
    dnt: true,
    nextUpdateTime: 100
  };

  // import and check that userAction got initialized
  actionMap.merge({[DOMAIN]: newValue});
  assert.deepEqual(
    actionMap.getItem(DOMAIN),
    Object.assign({ userAction: '' }, newValue),
    "test domain was imported and userAction got initialized");
});

QUnit.test("action map merge handles missing dnt", (assert) => {
  let newValue = {
    heuristicAction: 'block',
    userAction: 'user_allow'
  };

  // import and check that userAction got initialized
  actionMap.merge({[DOMAIN]: newValue});
  assert.deepEqual(
    actionMap.getItem(DOMAIN),
    Object.assign({ dnt: false, nextUpdateTime: 0 }, newValue),
    "test domain was imported and DNT got initialized");
});

QUnit.test("action map merge handles subdomains correctly", (assert) => {
  actionMap.setItem('testsite.com',
    {dnt: false, heuristicAction: '', nextUpdateTime: 100, userAction: ''});

  let newValue = {dnt: true, heuristicAction: '', nextUpdateTime: 100, userAction: ''};

  actionMap.merge({'s1.testsite.com': newValue});
  assert.ok(actionMap.hasItem('s1.testsite.com'),
    'Subdomains should be merged if they honor DNT');

  newValue.dnt = false;
  actionMap.merge({'s2.testsite.com': newValue});
  assert.notOk(actionMap.hasItem('s2.testsite.com'),
    "Subdomains should not be merged if they don't honor DNT");
});

QUnit.test("snitch map merging", (assert) => {
  snitchMap.merge({[DOMAIN]: ['firstparty.org']});
  assert.ok(snitchMap.getItem(DOMAIN).indexOf('firstparty.org') > -1);

  // Check to make sure existing and new domain are present
  snitchMap.merge({[DOMAIN]: ['firstparty2.org']});
  assert.ok(snitchMap.getItem(DOMAIN).indexOf('firstparty.org') > -1);
  assert.ok(snitchMap.getItem(DOMAIN).indexOf('firstparty2.org') > -1);

  // Verify 'block' status is triggered once TRACKING_THRESHOLD is hit
  assert.equal(actionMap.getItem(DOMAIN).heuristicAction, "allow");
  snitchMap.merge({[DOMAIN]: ["firstparty3.org"]});
  assert.equal(actionMap.getItem(DOMAIN).heuristicAction, "block");
});

QUnit.test("blocking cascades", (assert) => {
  // mark domain for blocking
  storage.setupHeuristicAction(DOMAIN, constants.BLOCK);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.BLOCK,
    "domain is marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.BLOCK,
    "domain is marked for blocking"
  );

  // check that subdomain inherits blocking
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.NO_TRACKING,
    "subdomain is not marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.BLOCK,
    "subdomain is marked for blocking (via parent domain)"
  );

  // check that subsubdomain inherits blocking
  assert.equal(
    storage.getAction(SUBSUBDOMAIN),
    constants.NO_TRACKING,
    "subsubdomain is not marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBSUBDOMAIN),
    constants.BLOCK,
    "subsubdomain is marked for blocking (via grandparent domain)"
  );
});

QUnit.test("DNT does not cascade", (assert) => {
  storage.setupDNT(DOMAIN);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.DNT,
    "domain is marked as DNT directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.DNT,
    "domain is marked as DNT"
  );

  // check that subdomain does not inherit DNT
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.NO_TRACKING,
    "subdomain is not marked as DNT directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.NO_TRACKING,
    "subdomain is not marked as DNT (via parent domain)"
  );
});

QUnit.test("DNT does not return as an action if user has chosen not to", (assert) => {
  let settings_map = storage.getStore('settings_map');
  settings_map.setItem("checkForDNTPolicy", false);
  storage.setupDNT(DOMAIN);

  assert.equal(
    storage.getAction(DOMAIN),
    constants.NO_TRACKING,
    "domain is marked as DNT directly, but returns as NO_TRACKING because user has disabled DNT"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.NO_TRACKING,
    "domain is marked as DNT, but returns as NO_TRACKING because user has disabled DNT"
  );
});

QUnit.test("blocking still cascades after domain declares DNT", (assert) => {
  storage.setupHeuristicAction(DOMAIN, constants.BLOCK);
  storage.setupDNT(DOMAIN);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN, true),
    constants.BLOCK,
    "domain is marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.DNT,
    "domain is marked as DNT"
  );

  // check that subdomain inherits blocking
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.NO_TRACKING,
    "subdomain is not marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.BLOCK,
    "subdomain is marked for blocking (via parent domain)"
  );
});

QUnit.test("cascading doesn't work the other way", (assert) => {
  // mark subdomain for blocking
  storage.setupHeuristicAction(SUBDOMAIN, constants.BLOCK);

  // check subdomain itself
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.BLOCK,
    "subdomain is marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.BLOCK,
    "subdomain is marked for blocking"
  );

  // check that parent domain does not inherit blocking
  assert.equal(
    storage.getAction(DOMAIN),
    constants.NO_TRACKING,
    "domain is not marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.NO_TRACKING,
    "domain is not marked for blocking"
  );
});

QUnit.test("blocking overrules allowing", (assert) => {
  // mark domain for blocking
  storage.setupHeuristicAction(DOMAIN, constants.BLOCK);
  // mark subsubdomain as "allow" (not-yet-over-the-threshold tracker)
  storage.setupHeuristicAction(SUBSUBDOMAIN, constants.ALLOW);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.BLOCK,
    "domain is marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.BLOCK,
    "domain is marked for blocking"
  );

  // check that subsubdomain inherits blocking
  assert.equal(
    storage.getAction(SUBSUBDOMAIN),
    constants.ALLOW,
    "subdomain is marked as 'allow' directly"
  );
  assert.equal(
    storage.getBestAction(SUBSUBDOMAIN),
    constants.BLOCK,
    "subsubdomain is marked for blocking (via grandparent domain)"
  );
});

QUnit.test("cookieblocking overrules blocking", (assert) => {
  // mark domain for cookieblocking
  storage.setupHeuristicAction(DOMAIN, constants.COOKIEBLOCK);
  // mark subdomain for blocking
  storage.setupHeuristicAction(SUBDOMAIN, constants.BLOCK);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.COOKIEBLOCK,
    "domain is marked for cookieblocking directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.COOKIEBLOCK,
    "domain is marked for cookieblocking"
  );

  // check that subdomain inherits cookieblocking
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.BLOCK,
    "subdomain is marked for blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.COOKIEBLOCK,
    "subdomain is marked for cookieblocking (via parent domain)"
  );
});

QUnit.test("user actions overrule everything else", (assert) => {
  storage.setupUserAction(DOMAIN, constants.USER_BLOCK);
  storage.setupHeuristicAction(SUBDOMAIN, constants.COOKIEBLOCK);
  storage.setupDNT(SUBSUBDOMAIN);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.USER_BLOCK,
    "domain is marked as userblock directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.USER_BLOCK,
    "domain is marked as userblock"
  );

  // check subdomain
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.COOKIEBLOCK,
    "subdomain is marked for cookie blocking directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.USER_BLOCK,
    "subdomain is marked as userblock"
  );

  // check subsubdomain
  assert.equal(
    storage.getAction(SUBSUBDOMAIN),
    constants.DNT,
    "subsubdomain is marked as DNT directly"
  );
  assert.equal(
    storage.getBestAction(SUBSUBDOMAIN),
    constants.USER_BLOCK,
    "subsubdomain is marked as userblock"
  );
});

// all three user actions are equally important
// but the one closest to the FQDN being checked should win
QUnit.test("specificity of rules of equal priority", (assert) => {
  storage.setupUserAction(DOMAIN, constants.USER_BLOCK);
  storage.setupUserAction(SUBDOMAIN, constants.USER_ALLOW);
  storage.setupUserAction(SUBSUBDOMAIN, constants.USER_COOKIEBLOCK);

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    constants.USER_BLOCK,
    "domain is marked as userblock directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.USER_BLOCK,
    "domain is marked as userblock"
  );

  // check subdomain
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.USER_ALLOW,
    "subdomain is marked as userallow directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.USER_ALLOW,
    "subdomain is marked as userallow"
  );

  // check subsubdomain
  assert.equal(
    storage.getAction(SUBSUBDOMAIN),
    constants.USER_COOKIEBLOCK,
    "subsubdomain is marked as usercookieblock directly"
  );
  assert.equal(
    storage.getBestAction(SUBSUBDOMAIN),
    constants.USER_COOKIEBLOCK,
    "subsubdomain is marked as usercookieblock"
  );
});

QUnit.test("unexpected heuristic actions are ignored", (assert) => {
  storage.setupHeuristicAction(DOMAIN, "foo");
  storage.setupHeuristicAction(SUBDOMAIN, constants.ALLOW);
  storage.setupHeuristicAction(SUBSUBDOMAIN, "bar");

  // check domain itself
  assert.equal(
    storage.getAction(DOMAIN),
    "foo",
    "domain is marked as 'foo' directly"
  );
  assert.equal(
    storage.getBestAction(DOMAIN),
    constants.NO_TRACKING,
    "best action for domain is 'no tracking'"
  );

  // check subdomain
  assert.equal(
    storage.getAction(SUBDOMAIN),
    constants.ALLOW,
    "subdomain is marked as 'allow' directly"
  );
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.ALLOW,
    "best action for subdomain is 'allow'"
  );

  // check subsubdomain
  assert.equal(
    storage.getAction(SUBSUBDOMAIN),
    "bar",
    "subsubdomain is marked as 'bar' directly"
  );
  assert.equal(
    storage.getBestAction(SUBSUBDOMAIN),
    constants.ALLOW,
    "best action for subsubdomain is 'allow'"
  );
});

function checkCookieblocking(assert) {
  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.NO_TRACKING,
    "subdomain is not yet (cookie)blocked"
  );
  assert.ok(
    storage.wouldGetCookieblocked(SUBDOMAIN),
    "subdomain would get cookieblocked if blocked"
  );

  // block the subdomain
  badger.heuristicBlocking.blocklistOrigin(DOMAIN, SUBDOMAIN);

  assert.equal(
    storage.getBestAction(SUBDOMAIN),
    constants.COOKIEBLOCK,
    "subdomain is cookieblocked"
  );
  assert.ok(
    storage.wouldGetCookieblocked(SUBDOMAIN),
    "subdomain would get/is cookieblocked"
  );
}

QUnit.test("checking cookieblock potential for yellowlisted subdomain", (assert) => {
  assert.notOk(
    storage.wouldGetCookieblocked(SUBDOMAIN),
    "subdomain wouldn't get cookieblocked if blocked"
  );

  // add subdomain to yellowlist
  storage.getStore('cookieblock_list').setItem(SUBDOMAIN, true);

  checkCookieblocking(assert);
});

QUnit.test("checking cookieblock potential for subdomain with yellowlisted base domain", (assert) => {
  assert.notOk(
    storage.wouldGetCookieblocked(SUBDOMAIN),
    "subdomain wouldn't get cookieblocked if blocked"
  );

  // add base domain to yellowlist
  storage.getStore('cookieblock_list').setItem(DOMAIN, true);

  checkCookieblocking(assert);
});

}());