summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_load_invalid_entries.js
blob: decbce81a07dc66dc76d7e7c8a9fb7d03c23f1a5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
var DEBUG_TEST = false;

function run_test() {
  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
  // Setup a profile directory.
  var dir = do_get_profile();

  // We need to execute a pm method to be sure that the DB is fully
  // initialized.
  var pm = Services.perms;
  Assert.equal(pm.all.length, 0, "No cookies");

  // Get the db file.
  var file = dir.clone();
  file.append("permissions.sqlite");

  var storage = Services.storage;

  // Create database.
  var connection = storage.openDatabase(file);
  // The file should now exist.
  Assert.ok(file.exists());

  connection.schemaVersion = 3;
  connection.executeSimpleSQL("DROP TABLE moz_hosts");
  connection.executeSimpleSQL(
    "CREATE TABLE moz_hosts (" +
      " id INTEGER PRIMARY KEY" +
      ",host TEXT" +
      ",type TEXT" +
      ",permission INTEGER" +
      ",expireType INTEGER" +
      ",expireTime INTEGER" +
      ",appId INTEGER" +
      ",isInBrowserElement INTEGER" +
      ")"
  );

  // Now we can inject garbadge in the database.
  var garbadge = [
    // Regular entry.
    {
      host: "42",
      type: "0",
      permission: 1,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // Special values in host (some being invalid).
    {
      host: "scheme:file",
      type: "1",
      permission: 0,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },
    {
      host: "192.168.0.1",
      type: "2",
      permission: 0,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },
    {
      host: "2001:0db8:0000:0000:0000:ff00:0042:8329",
      type: "3",
      permission: 0,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },
    {
      host: "::1",
      type: "4",
      permission: 0,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // Permission is UNKNOWN_ACTION.
    {
      host: "42",
      type: "5",
      permission: Ci.nsIPermissionManager.UNKNOWN_ACTION,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // Permission is out of range.
    {
      host: "42",
      type: "6",
      permission: 100,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },
    {
      host: "42",
      type: "7",
      permission: -100,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // ExpireType is out of range.
    {
      host: "42",
      type: "8",
      permission: 1,
      expireType: -100,
      expireTime: 0,
      isInBrowserElement: 0,
    },
    {
      host: "42",
      type: "9",
      permission: 1,
      expireType: 100,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // ExpireTime is at 0 with ExpireType = Time.
    {
      host: "42",
      type: "10",
      permission: 1,
      expireType: Ci.nsIPermissionManager.EXPIRE_TIME,
      expireTime: 0,
      isInBrowserElement: 0,
    },

    // ExpireTime has a value with ExpireType != Time
    {
      host: "42",
      type: "11",
      permission: 1,
      expireType: Ci.nsIPermissionManager.EXPIRE_SESSION,
      expireTime: 1000,
      isInBrowserElement: 0,
    },
    {
      host: "42",
      type: "12",
      permission: 1,
      expireType: Ci.nsIPermissionManager.EXPIRE_NEVER,
      expireTime: 1000,
      isInBrowserElement: 0,
    },

    // ExpireTime is negative.
    {
      host: "42",
      type: "13",
      permission: 1,
      expireType: Ci.nsIPermissionManager.EXPIRE_TIME,
      expireTime: -1,
      isInBrowserElement: 0,
    },

    // IsInBrowserElement is negative or higher than 1.
    {
      host: "42",
      type: "15",
      permission: 1,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: -1,
    },
    {
      host: "42",
      type: "16",
      permission: 1,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 10,
    },

    // This insertion should be the last one. It is used to make sure we always
    // load it regardless of the previous entries validities.
    {
      host: "example.org",
      type: "test-load-invalid-entries",
      permission: Ci.nsIPermissionManager.ALLOW_ACTION,
      expireType: 0,
      expireTime: 0,
      isInBrowserElement: 0,
    },
  ];

  for (var i = 0; i < garbadge.length; ++i) {
    if (DEBUG_TEST) {
      dump("\n value #" + i + "\n\n");
    }
    var data = garbadge[i];
    connection.executeSimpleSQL(
      "INSERT INTO moz_hosts " +
        " (id, host, type, permission, expireType, expireTime, isInBrowserElement, appId) " +
        "VALUES (" +
        i +
        ", '" +
        data.host +
        "', '" +
        data.type +
        "', " +
        data.permission +
        ", " +
        data.expireType +
        ", " +
        data.expireTime +
        ", " +
        data.isInBrowserElement +
        ", 0)"
    );
  }

  // This will force the permission-manager to reload the data.
  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");

  // Let's do something in order to be sure the DB is read.
  Assert.greater(pm.all.length, 0);

  // The schema should be upgraded to 11, and a 'modificationTime' column should
  // exist with all records having a value of 0.
  Assert.equal(connection.schemaVersion, 12);

  let select = connection.createStatement(
    "SELECT modificationTime FROM moz_perms"
  );
  let numMigrated = 0;
  while (select.executeStep()) {
    let thisModTime = select.getInt64(0);
    Assert.ok(
      thisModTime > 0,
      "new modifiedTime field is correct (but it's not 0!)"
    );
    numMigrated += 1;
  }
  // check we found at least 1 record that was migrated.
  Assert.greater(
    numMigrated,
    0,
    "we found at least 1 record that was migrated"
  );

  // This permission should always be there.
  let ssm = Services.scriptSecurityManager;
  let uri = NetUtil.newURI("http://example.org");
  let principal = ssm.createContentPrincipal(uri, {});
  Assert.equal(
    pm.testPermissionFromPrincipal(principal, "test-load-invalid-entries"),
    Ci.nsIPermissionManager.ALLOW_ACTION
  );
}