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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function matches_always(perm, uris) {
uris.forEach((uri) => {
do_check_true(perm.matchesURI(uri, true), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
do_check_true(perm.matchesURI(uri, false), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
});
}
function matches_weak(perm, uris) {
uris.forEach((uri) => {
do_check_false(perm.matchesURI(uri, true), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
do_check_true(perm.matchesURI(uri, false), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
});
}
function matches_never(perm, uris) {
uris.forEach((uri) => {
do_check_false(perm.matchesURI(uri, true), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
do_check_false(perm.matchesURI(uri, false), "perm: " + perm.principal.origin + ", URI: " + uri.spec);
});
}
function mk_permission(uri, isAppPermission = false) {
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
// Get the permission from the principal!
let attrs = {appId: 1000};
let principal =
secMan.createCodebasePrincipal(uri, isAppPermission ? attrs : {});
pm.addFromPrincipal(principal, "test/matchesuri", pm.ALLOW_ACTION);
let permission = pm.getPermissionObject(principal, "test/matchesuri", true);
return permission;
}
function run_test() {
// initialize the permission manager service
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let fileprefix = "file:///";
if (Services.appinfo.OS == "WINNT") {
// Windows rejects files if they don't have a drive. See Bug 1180870
fileprefix += "c:/";
}
// Add some permissions
let uri0 = NetUtil.newURI("http://google.com:9091/just/a/path", null, null);
let uri1 = NetUtil.newURI("http://hangouts.google.com:9091/some/path", null, null);
let uri2 = NetUtil.newURI("http://google.com:9091/", null, null);
let uri3 = NetUtil.newURI("http://google.org:9091/", null, null);
let uri4 = NetUtil.newURI("http://deeper.hangouts.google.com:9091/", null, null);
let uri5 = NetUtil.newURI("https://google.com/just/a/path", null, null);
let uri6 = NetUtil.newURI("https://hangouts.google.com", null, null);
let uri7 = NetUtil.newURI("https://google.com/", null, null);
let fileuri1 = NetUtil.newURI(fileprefix + "a/file/path", null, null);
let fileuri2 = NetUtil.newURI(fileprefix + "a/file/path/deeper", null, null);
let fileuri3 = NetUtil.newURI(fileprefix + "a/file/otherpath", null, null);
{
let perm = mk_permission(uri0);
matches_always(perm, [uri0, uri2]);
matches_weak(perm, [uri1, uri4]);
matches_never(perm, [uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri1);
matches_always(perm, [uri1]);
matches_weak(perm, [uri4]);
matches_never(perm, [uri0, uri2, uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri2);
matches_always(perm, [uri0, uri2]);
matches_weak(perm, [uri1, uri4]);
matches_never(perm, [uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri3);
matches_always(perm, [uri3]);
matches_weak(perm, []);
matches_never(perm, [uri1, uri2, uri4, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri4);
matches_always(perm, [uri4]);
matches_weak(perm, []);
matches_never(perm, [uri1, uri2, uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri5);
matches_always(perm, [uri5, uri7]);
matches_weak(perm, [uri6]);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri6);
matches_always(perm, [uri6]);
matches_weak(perm, []);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, uri5, uri7, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(uri7);
matches_always(perm, [uri5, uri7]);
matches_weak(perm, [uri6]);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, fileuri1, fileuri2, fileuri3]);
}
{
let perm = mk_permission(fileuri1);
matches_always(perm, [fileuri1]);
matches_weak(perm, []);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, uri5, uri6, uri7, fileuri2, fileuri3]);
}
{
let perm = mk_permission(fileuri2);
matches_always(perm, [fileuri2]);
matches_weak(perm, []);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, uri5, uri6, uri7, fileuri1, fileuri3]);
}
{
let perm = mk_permission(fileuri3);
matches_always(perm, [fileuri3]);
matches_weak(perm, []);
matches_never(perm, [uri0, uri1, uri2, uri3, uri4, uri5, uri6, uri7, fileuri1, fileuri2]);
}
// Clean up!
pm.removeAll();
}
|