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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared'
// notifications behave as expected.
var test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function continue_test()
{
do_run_generator(test_generator);
}
function do_run_test() {
// Set up a profile.
let profile = do_get_profile();
let pm = Services.perms;
let now = Number(Date.now());
let permType = "test/expiration-perm";
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let uri = NetUtil.newURI("http://example.com");
let principal = ssm.createCodebasePrincipal(uri, {});
let observer = new permission_observer(test_generator, now, permType);
Services.obs.addObserver(observer, "perm-changed", false);
// Add a permission, to test the 'add' notification. Note that we use
// do_execute_soon() so that we can use our generator to continue the test
// where we left off.
do_execute_soon(function() {
pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 100000);
});
yield;
// Alter a permission, to test the 'changed' notification.
do_execute_soon(function() {
pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 200000);
});
yield;
// Remove a permission, to test the 'deleted' notification.
do_execute_soon(function() {
pm.removeFromPrincipal(principal, permType);
});
yield;
// Clear permissions, to test the 'cleared' notification.
do_execute_soon(function() {
pm.removeAll();
});
yield;
Services.obs.removeObserver(observer, "perm-changed");
do_check_eq(observer.adds, 1);
do_check_eq(observer.changes, 1);
do_check_eq(observer.deletes, 1);
do_check_true(observer.cleared);
do_finish_generator_test(test_generator);
}
function permission_observer(generator, now, type) {
// Set up our observer object.
this.generator = generator;
this.pm = Services.perms;
this.now = now;
this.type = type;
this.adds = 0;
this.changes = 0;
this.deletes = 0;
this.cleared = false;
}
permission_observer.prototype = {
observe: function(subject, topic, data) {
do_check_eq(topic, "perm-changed");
// "deleted" means a permission was deleted. aPermission is the deleted permission.
// "added" means a permission was added. aPermission is the added permission.
// "changed" means a permission was altered. aPermission is the new permission.
// "cleared" means the entire permission list was cleared. aPermission is null.
if (data == "added") {
var perm = subject.QueryInterface(Ci.nsIPermission);
this.adds++;
switch (this.adds) {
case 1:
do_check_eq(this.type, perm.type);
do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
do_check_eq(this.now + 100000, perm.expireTime);
break;
default:
do_throw("too many add notifications posted.");
}
} else if (data == "changed") {
let perm = subject.QueryInterface(Ci.nsIPermission);
this.changes++;
switch (this.changes) {
case 1:
do_check_eq(this.type, perm.type);
do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
do_check_eq(this.now + 200000, perm.expireTime);
break;
default:
do_throw("too many change notifications posted.");
}
} else if (data == "deleted") {
var perm = subject.QueryInterface(Ci.nsIPermission);
this.deletes++;
switch (this.deletes) {
case 1:
do_check_eq(this.type, perm.type);
break;
default:
do_throw("too many delete notifications posted.");
}
} else if (data == "cleared") {
// only clear once: at the end
do_check_false(this.cleared);
do_check_eq(do_count_enumerator(Services.perms.enumerator), 0);
this.cleared = true;
} else {
do_throw("unexpected data '" + data + "'!");
}
// Continue the test.
do_run_generator(this.generator);
},
};
|