Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // Ideally this would be an xpcshell test, but Troubleshoot relies on things |
michael@0 | 6 | // that aren't initialized outside of a XUL app environment like AddonManager |
michael@0 | 7 | // and the "@mozilla.org/xre/app-info;1" component. |
michael@0 | 8 | |
michael@0 | 9 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | Components.utils.import("resource://gre/modules/Troubleshoot.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | function test() { |
michael@0 | 13 | waitForExplicitFinish(); |
michael@0 | 14 | function doNextTest() { |
michael@0 | 15 | if (!tests.length) { |
michael@0 | 16 | finish(); |
michael@0 | 17 | return; |
michael@0 | 18 | } |
michael@0 | 19 | tests.shift()(doNextTest); |
michael@0 | 20 | } |
michael@0 | 21 | doNextTest(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | registerCleanupFunction(function () { |
michael@0 | 25 | // Troubleshoot.jsm is imported into the global scope -- the window -- above. |
michael@0 | 26 | // If it's not deleted, it outlives the test and is reported as a leak. |
michael@0 | 27 | delete window.Troubleshoot; |
michael@0 | 28 | }); |
michael@0 | 29 | |
michael@0 | 30 | let tests = [ |
michael@0 | 31 | |
michael@0 | 32 | function snapshotSchema(done) { |
michael@0 | 33 | Troubleshoot.snapshot(function (snapshot) { |
michael@0 | 34 | try { |
michael@0 | 35 | validateObject(snapshot, SNAPSHOT_SCHEMA); |
michael@0 | 36 | ok(true, "The snapshot should conform to the schema."); |
michael@0 | 37 | } |
michael@0 | 38 | catch (err) { |
michael@0 | 39 | ok(false, err); |
michael@0 | 40 | } |
michael@0 | 41 | done(); |
michael@0 | 42 | }); |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | function modifiedPreferences(done) { |
michael@0 | 46 | let prefs = [ |
michael@0 | 47 | "javascript.troubleshoot", |
michael@0 | 48 | "troubleshoot.foo", |
michael@0 | 49 | "javascript.print_to_filename", |
michael@0 | 50 | "network.proxy.troubleshoot", |
michael@0 | 51 | ]; |
michael@0 | 52 | prefs.forEach(function (p) { |
michael@0 | 53 | Services.prefs.setBoolPref(p, true); |
michael@0 | 54 | is(Services.prefs.getBoolPref(p), true, "The pref should be set: " + p); |
michael@0 | 55 | }); |
michael@0 | 56 | Troubleshoot.snapshot(function (snapshot) { |
michael@0 | 57 | let p = snapshot.modifiedPreferences; |
michael@0 | 58 | is(p["javascript.troubleshoot"], true, |
michael@0 | 59 | "The pref should be present because it's whitelisted " + |
michael@0 | 60 | "but not blacklisted."); |
michael@0 | 61 | ok(!("troubleshoot.foo" in p), |
michael@0 | 62 | "The pref should be absent because it's not in the whitelist."); |
michael@0 | 63 | ok(!("javascript.print_to_filename" in p), |
michael@0 | 64 | "The pref should be absent because it's blacklisted."); |
michael@0 | 65 | ok(!("network.proxy.troubleshoot" in p), |
michael@0 | 66 | "The pref should be absent because it's blacklisted."); |
michael@0 | 67 | prefs.forEach(function (p) Services.prefs.deleteBranch(p)); |
michael@0 | 68 | done(); |
michael@0 | 69 | }); |
michael@0 | 70 | }, |
michael@0 | 71 | ]; |
michael@0 | 72 | |
michael@0 | 73 | // This is inspired by JSON Schema, or by the example on its Wikipedia page |
michael@0 | 74 | // anyway. |
michael@0 | 75 | const SNAPSHOT_SCHEMA = { |
michael@0 | 76 | type: "object", |
michael@0 | 77 | required: true, |
michael@0 | 78 | properties: { |
michael@0 | 79 | application: { |
michael@0 | 80 | required: true, |
michael@0 | 81 | type: "object", |
michael@0 | 82 | properties: { |
michael@0 | 83 | name: { |
michael@0 | 84 | required: true, |
michael@0 | 85 | type: "string", |
michael@0 | 86 | }, |
michael@0 | 87 | version: { |
michael@0 | 88 | required: true, |
michael@0 | 89 | type: "string", |
michael@0 | 90 | }, |
michael@0 | 91 | userAgent: { |
michael@0 | 92 | required: true, |
michael@0 | 93 | type: "string", |
michael@0 | 94 | }, |
michael@0 | 95 | vendor: { |
michael@0 | 96 | type: "string", |
michael@0 | 97 | }, |
michael@0 | 98 | supportURL: { |
michael@0 | 99 | type: "string", |
michael@0 | 100 | }, |
michael@0 | 101 | }, |
michael@0 | 102 | }, |
michael@0 | 103 | crashes: { |
michael@0 | 104 | required: false, |
michael@0 | 105 | type: "object", |
michael@0 | 106 | properties: { |
michael@0 | 107 | pending: { |
michael@0 | 108 | required: true, |
michael@0 | 109 | type: "number", |
michael@0 | 110 | }, |
michael@0 | 111 | submitted: { |
michael@0 | 112 | required: true, |
michael@0 | 113 | type: "array", |
michael@0 | 114 | items: { |
michael@0 | 115 | type: "object", |
michael@0 | 116 | properties: { |
michael@0 | 117 | id: { |
michael@0 | 118 | required: true, |
michael@0 | 119 | type: "string", |
michael@0 | 120 | }, |
michael@0 | 121 | date: { |
michael@0 | 122 | required: true, |
michael@0 | 123 | type: "number", |
michael@0 | 124 | }, |
michael@0 | 125 | pending: { |
michael@0 | 126 | required: true, |
michael@0 | 127 | type: "boolean", |
michael@0 | 128 | }, |
michael@0 | 129 | }, |
michael@0 | 130 | }, |
michael@0 | 131 | }, |
michael@0 | 132 | }, |
michael@0 | 133 | }, |
michael@0 | 134 | extensions: { |
michael@0 | 135 | required: true, |
michael@0 | 136 | type: "array", |
michael@0 | 137 | items: { |
michael@0 | 138 | type: "object", |
michael@0 | 139 | properties: { |
michael@0 | 140 | name: { |
michael@0 | 141 | required: true, |
michael@0 | 142 | type: "string", |
michael@0 | 143 | }, |
michael@0 | 144 | version: { |
michael@0 | 145 | required: true, |
michael@0 | 146 | type: "string", |
michael@0 | 147 | }, |
michael@0 | 148 | id: { |
michael@0 | 149 | required: true, |
michael@0 | 150 | type: "string", |
michael@0 | 151 | }, |
michael@0 | 152 | isActive: { |
michael@0 | 153 | required: true, |
michael@0 | 154 | type: "boolean", |
michael@0 | 155 | }, |
michael@0 | 156 | }, |
michael@0 | 157 | }, |
michael@0 | 158 | }, |
michael@0 | 159 | modifiedPreferences: { |
michael@0 | 160 | required: true, |
michael@0 | 161 | type: "object", |
michael@0 | 162 | }, |
michael@0 | 163 | graphics: { |
michael@0 | 164 | required: true, |
michael@0 | 165 | type: "object", |
michael@0 | 166 | properties: { |
michael@0 | 167 | numTotalWindows: { |
michael@0 | 168 | required: true, |
michael@0 | 169 | type: "number", |
michael@0 | 170 | }, |
michael@0 | 171 | numAcceleratedWindows: { |
michael@0 | 172 | required: true, |
michael@0 | 173 | type: "number", |
michael@0 | 174 | }, |
michael@0 | 175 | windowLayerManagerType: { |
michael@0 | 176 | type: "string", |
michael@0 | 177 | }, |
michael@0 | 178 | windowLayerManagerRemote: { |
michael@0 | 179 | type: "boolean", |
michael@0 | 180 | }, |
michael@0 | 181 | numAcceleratedWindowsMessage: { |
michael@0 | 182 | type: "array", |
michael@0 | 183 | }, |
michael@0 | 184 | adapterDescription: { |
michael@0 | 185 | type: "string", |
michael@0 | 186 | }, |
michael@0 | 187 | adapterVendorID: { |
michael@0 | 188 | type: "string", |
michael@0 | 189 | }, |
michael@0 | 190 | adapterDeviceID: { |
michael@0 | 191 | type: "string", |
michael@0 | 192 | }, |
michael@0 | 193 | adapterRAM: { |
michael@0 | 194 | type: "string", |
michael@0 | 195 | }, |
michael@0 | 196 | adapterDrivers: { |
michael@0 | 197 | type: "string", |
michael@0 | 198 | }, |
michael@0 | 199 | driverVersion: { |
michael@0 | 200 | type: "string", |
michael@0 | 201 | }, |
michael@0 | 202 | driverDate: { |
michael@0 | 203 | type: "string", |
michael@0 | 204 | }, |
michael@0 | 205 | adapterDescription2: { |
michael@0 | 206 | type: "string", |
michael@0 | 207 | }, |
michael@0 | 208 | adapterVendorID2: { |
michael@0 | 209 | type: "string", |
michael@0 | 210 | }, |
michael@0 | 211 | adapterDeviceID2: { |
michael@0 | 212 | type: "string", |
michael@0 | 213 | }, |
michael@0 | 214 | adapterRAM2: { |
michael@0 | 215 | type: "string", |
michael@0 | 216 | }, |
michael@0 | 217 | adapterDrivers2: { |
michael@0 | 218 | type: "string", |
michael@0 | 219 | }, |
michael@0 | 220 | driverVersion2: { |
michael@0 | 221 | type: "string", |
michael@0 | 222 | }, |
michael@0 | 223 | driverDate2: { |
michael@0 | 224 | type: "string", |
michael@0 | 225 | }, |
michael@0 | 226 | isGPU2Active: { |
michael@0 | 227 | type: "boolean", |
michael@0 | 228 | }, |
michael@0 | 229 | direct2DEnabled: { |
michael@0 | 230 | type: "boolean", |
michael@0 | 231 | }, |
michael@0 | 232 | directWriteEnabled: { |
michael@0 | 233 | type: "boolean", |
michael@0 | 234 | }, |
michael@0 | 235 | directWriteVersion: { |
michael@0 | 236 | type: "string", |
michael@0 | 237 | }, |
michael@0 | 238 | clearTypeParameters: { |
michael@0 | 239 | type: "string", |
michael@0 | 240 | }, |
michael@0 | 241 | webglRenderer: { |
michael@0 | 242 | type: "string", |
michael@0 | 243 | }, |
michael@0 | 244 | info: { |
michael@0 | 245 | type: "object", |
michael@0 | 246 | }, |
michael@0 | 247 | failures: { |
michael@0 | 248 | type: "array", |
michael@0 | 249 | items: { |
michael@0 | 250 | type: "string", |
michael@0 | 251 | }, |
michael@0 | 252 | }, |
michael@0 | 253 | direct2DEnabledMessage: { |
michael@0 | 254 | type: "array", |
michael@0 | 255 | }, |
michael@0 | 256 | webglRendererMessage: { |
michael@0 | 257 | type: "array", |
michael@0 | 258 | }, |
michael@0 | 259 | }, |
michael@0 | 260 | }, |
michael@0 | 261 | javaScript: { |
michael@0 | 262 | required: true, |
michael@0 | 263 | type: "object", |
michael@0 | 264 | properties: { |
michael@0 | 265 | incrementalGCEnabled: { |
michael@0 | 266 | type: "boolean", |
michael@0 | 267 | }, |
michael@0 | 268 | }, |
michael@0 | 269 | }, |
michael@0 | 270 | accessibility: { |
michael@0 | 271 | required: true, |
michael@0 | 272 | type: "object", |
michael@0 | 273 | properties: { |
michael@0 | 274 | isActive: { |
michael@0 | 275 | required: true, |
michael@0 | 276 | type: "boolean", |
michael@0 | 277 | }, |
michael@0 | 278 | forceDisabled: { |
michael@0 | 279 | type: "number", |
michael@0 | 280 | }, |
michael@0 | 281 | }, |
michael@0 | 282 | }, |
michael@0 | 283 | libraryVersions: { |
michael@0 | 284 | required: true, |
michael@0 | 285 | type: "object", |
michael@0 | 286 | properties: { |
michael@0 | 287 | NSPR: { |
michael@0 | 288 | required: true, |
michael@0 | 289 | type: "object", |
michael@0 | 290 | properties: { |
michael@0 | 291 | minVersion: { |
michael@0 | 292 | required: true, |
michael@0 | 293 | type: "string", |
michael@0 | 294 | }, |
michael@0 | 295 | version: { |
michael@0 | 296 | required: true, |
michael@0 | 297 | type: "string", |
michael@0 | 298 | }, |
michael@0 | 299 | }, |
michael@0 | 300 | }, |
michael@0 | 301 | NSS: { |
michael@0 | 302 | required: true, |
michael@0 | 303 | type: "object", |
michael@0 | 304 | properties: { |
michael@0 | 305 | minVersion: { |
michael@0 | 306 | required: true, |
michael@0 | 307 | type: "string", |
michael@0 | 308 | }, |
michael@0 | 309 | version: { |
michael@0 | 310 | required: true, |
michael@0 | 311 | type: "string", |
michael@0 | 312 | }, |
michael@0 | 313 | }, |
michael@0 | 314 | }, |
michael@0 | 315 | NSSUTIL: { |
michael@0 | 316 | required: true, |
michael@0 | 317 | type: "object", |
michael@0 | 318 | properties: { |
michael@0 | 319 | minVersion: { |
michael@0 | 320 | required: true, |
michael@0 | 321 | type: "string", |
michael@0 | 322 | }, |
michael@0 | 323 | version: { |
michael@0 | 324 | required: true, |
michael@0 | 325 | type: "string", |
michael@0 | 326 | }, |
michael@0 | 327 | }, |
michael@0 | 328 | }, |
michael@0 | 329 | NSSSSL: { |
michael@0 | 330 | required: true, |
michael@0 | 331 | type: "object", |
michael@0 | 332 | properties: { |
michael@0 | 333 | minVersion: { |
michael@0 | 334 | required: true, |
michael@0 | 335 | type: "string", |
michael@0 | 336 | }, |
michael@0 | 337 | version: { |
michael@0 | 338 | required: true, |
michael@0 | 339 | type: "string", |
michael@0 | 340 | }, |
michael@0 | 341 | }, |
michael@0 | 342 | }, |
michael@0 | 343 | NSSSMIME: { |
michael@0 | 344 | required: true, |
michael@0 | 345 | type: "object", |
michael@0 | 346 | properties: { |
michael@0 | 347 | minVersion: { |
michael@0 | 348 | required: true, |
michael@0 | 349 | type: "string", |
michael@0 | 350 | }, |
michael@0 | 351 | version: { |
michael@0 | 352 | required: true, |
michael@0 | 353 | type: "string", |
michael@0 | 354 | }, |
michael@0 | 355 | }, |
michael@0 | 356 | }, |
michael@0 | 357 | }, |
michael@0 | 358 | }, |
michael@0 | 359 | userJS: { |
michael@0 | 360 | required: true, |
michael@0 | 361 | type: "object", |
michael@0 | 362 | properties: { |
michael@0 | 363 | exists: { |
michael@0 | 364 | required: true, |
michael@0 | 365 | type: "boolean", |
michael@0 | 366 | }, |
michael@0 | 367 | }, |
michael@0 | 368 | }, |
michael@0 | 369 | experiments: { |
michael@0 | 370 | type: "array", |
michael@0 | 371 | }, |
michael@0 | 372 | }, |
michael@0 | 373 | }; |
michael@0 | 374 | |
michael@0 | 375 | /** |
michael@0 | 376 | * Throws an Error if obj doesn't conform to schema. That way you get a nice |
michael@0 | 377 | * error message and a stack to help you figure out what went wrong, which you |
michael@0 | 378 | * wouldn't get if this just returned true or false instead. There's still |
michael@0 | 379 | * room for improvement in communicating validation failures, however. |
michael@0 | 380 | * |
michael@0 | 381 | * @param obj The object to validate. |
michael@0 | 382 | * @param schema The schema that obj should conform to. |
michael@0 | 383 | */ |
michael@0 | 384 | function validateObject(obj, schema) { |
michael@0 | 385 | if (obj === undefined && !schema.required) |
michael@0 | 386 | return; |
michael@0 | 387 | if (typeof(schema.type) != "string") |
michael@0 | 388 | throw schemaErr("'type' must be a string", schema); |
michael@0 | 389 | if (objType(obj) != schema.type) |
michael@0 | 390 | throw validationErr("Object is not of the expected type", obj, schema); |
michael@0 | 391 | let validatorFnName = "validateObject_" + schema.type; |
michael@0 | 392 | if (!(validatorFnName in this)) |
michael@0 | 393 | throw schemaErr("Validator function not defined for type", schema); |
michael@0 | 394 | this[validatorFnName](obj, schema); |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | function validateObject_object(obj, schema) { |
michael@0 | 398 | if (typeof(schema.properties) != "object") |
michael@0 | 399 | // Don't care what obj's properties are. |
michael@0 | 400 | return; |
michael@0 | 401 | // First check that all the schema's properties match the object. |
michael@0 | 402 | for (let prop in schema.properties) |
michael@0 | 403 | validateObject(obj[prop], schema.properties[prop]); |
michael@0 | 404 | // Now check that the object doesn't have any properties not in the schema. |
michael@0 | 405 | for (let prop in obj) |
michael@0 | 406 | if (!(prop in schema.properties)) |
michael@0 | 407 | throw validationErr("Object has property not in schema", obj, schema); |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | function validateObject_array(array, schema) { |
michael@0 | 411 | if (typeof(schema.items) != "object") |
michael@0 | 412 | // Don't care what the array's elements are. |
michael@0 | 413 | return; |
michael@0 | 414 | array.forEach(function (elt) validateObject(elt, schema.items)); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | function validateObject_string(str, schema) {} |
michael@0 | 418 | function validateObject_boolean(bool, schema) {} |
michael@0 | 419 | function validateObject_number(num, schema) {} |
michael@0 | 420 | |
michael@0 | 421 | function validationErr(msg, obj, schema) { |
michael@0 | 422 | return new Error("Validation error: " + msg + |
michael@0 | 423 | ": object=" + JSON.stringify(obj) + |
michael@0 | 424 | ", schema=" + JSON.stringify(schema)); |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | function schemaErr(msg, schema) { |
michael@0 | 428 | return new Error("Schema error: " + msg + ": " + JSON.stringify(schema)); |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | function objType(obj) { |
michael@0 | 432 | let type = typeof(obj); |
michael@0 | 433 | if (type != "object") |
michael@0 | 434 | return type; |
michael@0 | 435 | if (Array.isArray(obj)) |
michael@0 | 436 | return "array"; |
michael@0 | 437 | if (obj === null) |
michael@0 | 438 | return "null"; |
michael@0 | 439 | return type; |
michael@0 | 440 | } |