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
639
640
641
642
643
644
645
646
647
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=2 ts=2 sts=2
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Test added with bug 460086 to test the behavior of the new API that was added
* to remove all traces of visiting a site.
*/
// Globals
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
"resource://testing-common/PlacesTestUtils.jsm");
const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
const COOKIE_NAME = "testcookie";
const COOKIE_PATH = "/";
const LOGIN_USERNAME = "username";
const LOGIN_PASSWORD = "password";
const LOGIN_USERNAME_FIELD = "username_field";
const LOGIN_PASSWORD_FIELD = "password_field";
const PERMISSION_TYPE = "test-perm";
const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION;
const PREFERENCE_NAME = "test-pref";
// Utility Functions
/**
* Creates an nsIURI object for the given string representation of a URI.
*
* @param aURIString
* The spec of the URI to create.
* @returns an nsIURI representing aURIString.
*/
function uri(aURIString)
{
return Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).
newURI(aURIString, null, null);
}
/**
* Asynchronously check a url is visited.
*
* @param aURI
* The URI.
*
* @return {Promise}
* @resolves When the check has been added successfully.
* @rejects JavaScript exception.
*/
function promiseIsURIVisited(aURI)
{
let deferred = Promise.defer();
PlacesUtils.asyncHistory.isURIVisited(aURI, function(unused, aIsVisited) {
deferred.resolve(aIsVisited);
});
return deferred.promise;
}
/**
* Add a cookie to the cookie service.
*
* @param aDomain
*/
function add_cookie(aDomain)
{
check_cookie_exists(aDomain, false);
let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false,
COOKIE_EXPIRY, {});
check_cookie_exists(aDomain, true);
}
/**
* Checks to ensure that a cookie exists or not for a domain.
*
* @param aDomain
* The domain to check for the cookie.
* @param aExists
* True if the cookie should exist, false otherwise.
*/
function check_cookie_exists(aDomain, aExists)
{
let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
let cookie = {
host: aDomain,
name: COOKIE_NAME,
path: COOKIE_PATH
}
let checker = aExists ? do_check_true : do_check_false;
checker(cm.cookieExists(cookie));
}
/**
* Adds a disabled host to the login manager.
*
* @param aHost
* The host to add to the list of disabled hosts.
*/
function add_disabled_host(aHost)
{
check_disabled_host(aHost, false);
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
lm.setLoginSavingEnabled(aHost, false);
check_disabled_host(aHost, true);
}
/**
* Checks to see if a host is disabled for storing logins or not.
*
* @param aHost
* The host to check if it is disabled.
* @param aIsDisabled
* True if the host should be disabled, false otherwise.
*/
function check_disabled_host(aHost, aIsDisabled)
{
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
let checker = aIsDisabled ? do_check_false : do_check_true;
checker(lm.getLoginSavingEnabled(aHost));
}
/**
* Adds a login for the specified host to the login manager.
*
* @param aHost
* The host to add the login for.
*/
function add_login(aHost)
{
check_login_exists(aHost, false);
let login = Cc["@mozilla.org/login-manager/loginInfo;1"].
createInstance(Ci.nsILoginInfo);
login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD,
LOGIN_USERNAME_FIELD, LOGIN_PASSWORD_FIELD);
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
lm.addLogin(login);
check_login_exists(aHost, true);
}
/**
* Checks to see if a login exists for a host.
*
* @param aHost
* The host to check for the test login.
* @param aExists
* True if the login should exist, false otherwise.
*/
function check_login_exists(aHost, aExists)
{
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
let count = { value: 0 };
lm.findLogins(count, aHost, "", null);
do_check_eq(count.value, aExists ? 1 : 0);
}
/**
* Adds a permission for the specified URI to the permission manager.
*
* @param aURI
* The URI to add the test permission for.
*/
function add_permission(aURI)
{
check_permission_exists(aURI, false);
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let principal = ssm.createCodebasePrincipal(aURI, {});
pm.addFromPrincipal(principal, PERMISSION_TYPE, PERMISSION_VALUE);
check_permission_exists(aURI, true);
}
/**
* Checks to see if a permission exists for the given URI.
*
* @param aURI
* The URI to check if a permission exists.
* @param aExists
* True if the permission should exist, false otherwise.
*/
function check_permission_exists(aURI, aExists)
{
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let principal = ssm.createCodebasePrincipal(aURI, {});
let perm = pm.testExactPermissionFromPrincipal(principal, PERMISSION_TYPE);
let checker = aExists ? do_check_eq : do_check_neq;
checker(perm, PERMISSION_VALUE);
}
/**
* Adds a content preference for the specified URI.
*
* @param aURI
* The URI to add a preference for.
*/
function add_preference(aURI)
{
let deferred = Promise.defer();
let cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService2);
cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, {
handleCompletion: () => deferred.resolve()
});
return deferred.promise;
}
/**
* Checks to see if a content preference exists for the given URI.
*
* @param aURI
* The URI to check if a preference exists.
*/
function preference_exists(aURI)
{
let deferred = Promise.defer();
let cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService2);
let exists = false;
cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, {
handleResult: () => exists = true,
handleCompletion: () => deferred.resolve(exists)
});
return deferred.promise;
}
// Test Functions
// History
function* test_history_cleared_with_direct_match()
{
const TEST_URI = uri("http://mozilla.org/foo");
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield PlacesTestUtils.addVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
do_check_false(yield promiseIsURIVisited(TEST_URI));
}
function* test_history_cleared_with_subdomain()
{
const TEST_URI = uri("http://www.mozilla.org/foo");
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield PlacesTestUtils.addVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
do_check_false(yield promiseIsURIVisited(TEST_URI));
}
function* test_history_not_cleared_with_uri_contains_domain()
{
const TEST_URI = uri("http://ilovemozilla.org/foo");
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield PlacesTestUtils.addVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
do_check_true(yield promiseIsURIVisited(TEST_URI));
// Clear history since we left something there from this test.
yield PlacesTestUtils.clearHistory();
}
// Cookie Service
function* test_cookie_cleared_with_direct_match()
{
const TEST_DOMAIN = "mozilla.org";
add_cookie(TEST_DOMAIN);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_cookie_exists(TEST_DOMAIN, false);
}
function* test_cookie_cleared_with_subdomain()
{
const TEST_DOMAIN = "www.mozilla.org";
add_cookie(TEST_DOMAIN);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_cookie_exists(TEST_DOMAIN, false);
}
function* test_cookie_not_cleared_with_uri_contains_domain()
{
const TEST_DOMAIN = "ilovemozilla.org";
add_cookie(TEST_DOMAIN);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_cookie_exists(TEST_DOMAIN, true);
}
// Login Manager
function* test_login_manager_disabled_hosts_cleared_with_direct_match()
{
const TEST_HOST = "http://mozilla.org";
add_disabled_host(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_disabled_host(TEST_HOST, false);
}
function* test_login_manager_disabled_hosts_cleared_with_subdomain()
{
const TEST_HOST = "http://www.mozilla.org";
add_disabled_host(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_disabled_host(TEST_HOST, false);
}
function* test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain()
{
const TEST_HOST = "http://ilovemozilla.org";
add_disabled_host(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_disabled_host(TEST_HOST, true);
// Reset state
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
lm.setLoginSavingEnabled(TEST_HOST, true);
check_disabled_host(TEST_HOST, false);
}
function* test_login_manager_logins_cleared_with_direct_match()
{
const TEST_HOST = "http://mozilla.org";
add_login(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_login_exists(TEST_HOST, false);
}
function* test_login_manager_logins_cleared_with_subdomain()
{
const TEST_HOST = "http://www.mozilla.org";
add_login(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_login_exists(TEST_HOST, false);
}
function* test_login_manager_logins_not_cleared_with_uri_contains_domain()
{
const TEST_HOST = "http://ilovemozilla.org";
add_login(TEST_HOST);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_login_exists(TEST_HOST, true);
let lm = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
lm.removeAllLogins();
check_login_exists(TEST_HOST, false);
}
// Permission Manager
function* test_permission_manager_cleared_with_direct_match()
{
const TEST_URI = uri("http://mozilla.org");
add_permission(TEST_URI);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_permission_exists(TEST_URI, false);
}
function* test_permission_manager_cleared_with_subdomain()
{
const TEST_URI = uri("http://www.mozilla.org");
add_permission(TEST_URI);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_permission_exists(TEST_URI, false);
}
function* test_permission_manager_not_cleared_with_uri_contains_domain()
{
const TEST_URI = uri("http://ilovemozilla.org");
add_permission(TEST_URI);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_permission_exists(TEST_URI, true);
// Reset state
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
pm.removeAll();
check_permission_exists(TEST_URI, false);
}
function waitForPurgeNotification() {
let deferred = Promise.defer();
let observer = {
observe: function(aSubject, aTopic, aData)
{
Services.obs.removeObserver(observer, "browser:purge-domain-data");
// test_storage_cleared needs this extra executeSoon because
// the DOMStorage clean-up is also listening to this same observer
// which is run synchronously.
Services.tm.mainThread.dispatch(function() {
deferred.resolve();
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
}
};
Services.obs.addObserver(observer, "browser:purge-domain-data", false);
return deferred.promise;
}
// Content Preferences
function* test_content_preferences_cleared_with_direct_match()
{
const TEST_URI = uri("http://mozilla.org");
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
function* test_content_preferences_cleared_with_subdomain()
{
const TEST_URI = uri("http://www.mozilla.org");
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
function* test_content_preferences_not_cleared_with_uri_contains_domain()
{
const TEST_URI = uri("http://ilovemozilla.org");
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield waitForPurgeNotification();
do_check_true(yield preference_exists(TEST_URI));
// Reset state
yield ForgetAboutSite.removeDataFromDomain("ilovemozilla.org");
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
function push_registration_exists(aURL, ps)
{
return new Promise(resolve => {
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let principal = ssm.createCodebasePrincipalFromOrigin(aURL);
return ps.getSubscription(aURL, principal, (status, record) => {
if (!Components.isSuccessCode(status)) {
resolve(false);
} else {
resolve(!!record);
}
});
});
}
// Push
function* test_push_cleared()
{
let ps;
try {
ps = Cc["@mozilla.org/push/Service;1"].
getService(Ci.nsIPushService);
} catch (e) {
// No push service, skip test.
return;
}
do_get_profile();
setPrefs();
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
const userAgentID = 'bd744428-f125-436a-b6d0-dd0c9845837f';
const channelID = '0ef2ad4a-6c49-41ad-af6e-95d2425276bf';
let db = PushServiceWebSocket.newPushDB();
try {
PushService.init({
serverURI: "wss://push.example.org/",
db,
makeWebSocket(uriObj) {
return new MockWebSocket(uriObj, {
onHello(request) {
this.serverSendMsg(JSON.stringify({
messageType: 'hello',
status: 200,
uaid: userAgentID,
}));
},
});
}
});
const TEST_URL = "https://www.mozilla.org/scope/";
do_check_false(yield push_registration_exists(TEST_URL, ps));
yield db.put({
channelID,
pushEndpoint: 'https://example.org/update/clear-success',
scope: TEST_URL,
version: 1,
originAttributes: '',
quota: Infinity,
});
do_check_true(yield push_registration_exists(TEST_URL, ps));
let promisePurgeNotification = waitForPurgeNotification();
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield promisePurgeNotification;
do_check_false(yield push_registration_exists(TEST_URL, ps));
} finally {
yield PushService._shutdownService();
}
}
// Cache
function* test_cache_cleared()
{
// Because this test is asynchronous, it should be the last test
do_check_true(tests[tests.length - 1] == arguments.callee);
// NOTE: We could be more extensive with this test and actually add an entry
// to the cache, and then make sure it is gone. However, we trust that
// the API is well tested, and that when we get the observer
// notification, we have actually cleared the cache.
// This seems to happen asynchronously...
let os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
let observer = {
observe: function(aSubject, aTopic, aData)
{
os.removeObserver(observer, "cacheservice:empty-cache");
// Shutdown the download manager.
Services.obs.notifyObservers(null, "quit-application", null);
do_test_finished();
}
};
os.addObserver(observer, "cacheservice:empty-cache", false);
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
do_test_pending();
}
function* test_storage_cleared()
{
function getStorageForURI(aURI)
{
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let principal = ssm.createCodebasePrincipal(aURI, {});
let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"].
getService(Ci.nsIDOMStorageManager);
return dsm.createStorage(null, principal, "");
}
let s = [
getStorageForURI(uri("http://mozilla.org")),
getStorageForURI(uri("http://my.mozilla.org")),
getStorageForURI(uri("http://ilovemozilla.org")),
];
for (let i = 0; i < s.length; ++i) {
let storage = s[i];
storage.setItem("test", "value" + i);
do_check_eq(storage.length, 1);
do_check_eq(storage.key(0), "test");
do_check_eq(storage.getItem("test"), "value" + i);
}
yield ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield waitForPurgeNotification();
do_check_eq(s[0].getItem("test"), null);
do_check_eq(s[0].length, 0);
do_check_eq(s[1].getItem("test"), null);
do_check_eq(s[1].length, 0);
do_check_eq(s[2].getItem("test"), "value2");
do_check_eq(s[2].length, 1);
}
var tests = [
// History
test_history_cleared_with_direct_match,
test_history_cleared_with_subdomain,
test_history_not_cleared_with_uri_contains_domain,
// Cookie Service
test_cookie_cleared_with_direct_match,
test_cookie_cleared_with_subdomain,
test_cookie_not_cleared_with_uri_contains_domain,
// Login Manager
test_login_manager_disabled_hosts_cleared_with_direct_match,
test_login_manager_disabled_hosts_cleared_with_subdomain,
test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain,
test_login_manager_logins_cleared_with_direct_match,
test_login_manager_logins_cleared_with_subdomain,
test_login_manager_logins_not_cleared_with_uri_contains_domain,
// Permission Manager
test_permission_manager_cleared_with_direct_match,
test_permission_manager_cleared_with_subdomain,
test_permission_manager_not_cleared_with_uri_contains_domain,
// Content Preferences
test_content_preferences_cleared_with_direct_match,
test_content_preferences_cleared_with_subdomain,
test_content_preferences_not_cleared_with_uri_contains_domain,
// Push
test_push_cleared,
// Storage
test_storage_cleared,
// Cache
test_cache_cleared,
];
function run_test()
{
for (let i = 0; i < tests.length; i++)
add_task(tests[i]);
run_next_test();
}
|