addon-sdk/source/test/test-api-utils.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const apiUtils = require("sdk/deprecated/api-utils");
     7 exports.testValidateOptionsEmpty = function (assert) {
     8   let val = apiUtils.validateOptions(null, {});
    10   assert.deepEqual(val, {});
    12   val = apiUtils.validateOptions(null, { foo: {} });
    13   assert.deepEqual(val, {});
    15   val = apiUtils.validateOptions({}, {});
    16   assert.deepEqual(val, {});
    18   val = apiUtils.validateOptions({}, { foo: {} });
    19   assert.deepEqual(val, {});
    20 };
    22 exports.testValidateOptionsNonempty = function (assert) {
    23   let val = apiUtils.validateOptions({ foo: 123 }, {});
    24   assert.deepEqual(val, {});
    26   val = apiUtils.validateOptions({ foo: 123, bar: 456 },
    27                                  { foo: {}, bar: {}, baz: {} });
    29   assert.deepEqual(val, { foo: 123, bar: 456 });
    30 };
    32 exports.testValidateOptionsMap = function (assert) {
    33   let val = apiUtils.validateOptions({ foo: 3, bar: 2 }, {
    34     foo: { map: function (v) v * v },
    35     bar: { map: function (v) undefined }
    36   });
    37   assert.deepEqual(val, { foo: 9, bar: undefined });
    38 };
    40 exports.testValidateOptionsMapException = function (assert) {
    41   let val = apiUtils.validateOptions({ foo: 3 }, {
    42     foo: { map: function () { throw new Error(); }}
    43   });
    44   assert.deepEqual(val, { foo: 3 });
    45 };
    47 exports.testValidateOptionsOk = function (assert) {
    48   let val = apiUtils.validateOptions({ foo: 3, bar: 2, baz: 1 }, {
    49     foo: { ok: function (v) v },
    50     bar: { ok: function (v) v }
    51   });
    52   assert.deepEqual(val, { foo: 3, bar: 2 });
    54   assert.throws(
    55     function () apiUtils.validateOptions({ foo: 2, bar: 2 }, {
    56       bar: { ok: function (v) v > 2 }
    57     }),
    58     /^The option "bar" is invalid/,
    59     "ok should raise exception on invalid option"
    60   );
    62   assert.throws(
    63     function () apiUtils.validateOptions(null, { foo: { ok: function (v) v }}),
    64     /^The option "foo" is invalid/,
    65     "ok should raise exception on invalid option"
    66   );
    67 };
    69 exports.testValidateOptionsIs = function (assert) {
    70   let opts = {
    71     array: [],
    72     boolean: true,
    73     func: function () {},
    74     nul: null,
    75     number: 1337,
    76     object: {},
    77     string: "foo",
    78     undef1: undefined
    79   };
    80   let requirements = {
    81     array: { is: ["array"] },
    82     boolean: { is: ["boolean"] },
    83     func: { is: ["function"] },
    84     nul: { is: ["null"] },
    85     number: { is: ["number"] },
    86     object: { is: ["object"] },
    87     string: { is: ["string"] },
    88     undef1: { is: ["undefined"] },
    89     undef2: { is: ["undefined"] }
    90   };
    91   let val = apiUtils.validateOptions(opts, requirements);
    92   assert.deepEqual(val, opts);
    94   assert.throws(
    95     function () apiUtils.validateOptions(null, {
    96       foo: { is: ["object", "number"] }
    97     }),
    98     /^The option "foo" must be one of the following types: object, number/,
    99     "Invalid type should raise exception"
   100   );
   101 };
   103 exports.testValidateOptionsIsWithExportedValue = function (assert) {
   104   let { string, number, boolean, object } = apiUtils;
   106   let opts = {
   107     boolean: true,
   108     number: 1337,
   109     object: {},
   110     string: "foo"
   111   };
   112   let requirements = {
   113     string: { is: string },
   114     number: { is: number },
   115     boolean: { is: boolean },
   116     object: { is: object }
   117   };
   118   let val = apiUtils.validateOptions(opts, requirements);
   119   assert.deepEqual(val, opts);
   121   // Test the types are optional by default
   122   val = apiUtils.validateOptions({foo: 'bar'}, requirements);
   123   assert.deepEqual(val, {});
   124 };
   126 exports.testValidateOptionsIsWithEither = function (assert) {
   127   let { string, number, boolean, either } = apiUtils;
   128   let text = { is: either(string, number) };
   130   let requirements = {
   131     text: text,
   132     boolOrText: { is: either(text, boolean) }
   133   };
   135   let val = apiUtils.validateOptions({text: 12}, requirements);
   136   assert.deepEqual(val, {text: 12});
   138   val = apiUtils.validateOptions({text: "12"}, requirements);
   139   assert.deepEqual(val, {text: "12"});
   141   val = apiUtils.validateOptions({boolOrText: true}, requirements);
   142   assert.deepEqual(val, {boolOrText: true});
   144   val = apiUtils.validateOptions({boolOrText: "true"}, requirements);
   145   assert.deepEqual(val, {boolOrText: "true"});
   147   val = apiUtils.validateOptions({boolOrText: 1}, requirements);
   148   assert.deepEqual(val, {boolOrText: 1});
   150   assert.throws(
   151     () => apiUtils.validateOptions({text: true}, requirements),
   152     /^The option "text" must be one of the following types/,
   153     "Invalid type should raise exception"
   154   );
   156   assert.throws(
   157     () => apiUtils.validateOptions({boolOrText: []}, requirements),
   158     /^The option "boolOrText" must be one of the following types/,
   159     "Invalid type should raise exception"
   160   );
   161 };
   163 exports.testValidateOptionsWithRequiredAndOptional = function (assert) {
   164   let { string, number, required, optional } = apiUtils;
   166   let opts = {
   167     number: 1337,
   168     string: "foo"
   169   };
   171   let requirements = {
   172     string: required(string),
   173     number: number
   174   };
   176   let val = apiUtils.validateOptions(opts, requirements);
   177   assert.deepEqual(val, opts);
   179   val = apiUtils.validateOptions({string: "foo"}, requirements);
   180   assert.deepEqual(val, {string: "foo"});
   182   assert.throws(
   183     () => apiUtils.validateOptions({number: 10}, requirements),
   184     /^The option "string" must be one of the following types/,
   185     "Invalid type should raise exception"
   186   );
   188   // Makes string optional
   189   requirements.string = optional(requirements.string);
   191   val = apiUtils.validateOptions({number: 10}, requirements),
   192   assert.deepEqual(val, {number: 10});
   194 };
   198 exports.testValidateOptionsWithExportedValue = function (assert) {
   199   let { string, number, boolean, object } = apiUtils;
   201   let opts = {
   202     boolean: true,
   203     number: 1337,
   204     object: {},
   205     string: "foo"
   206   };
   207   let requirements = {
   208     string: string,
   209     number: number,
   210     boolean: boolean,
   211     object: object
   212   };
   213   let val = apiUtils.validateOptions(opts, requirements);
   214   assert.deepEqual(val, opts);
   216   // Test the types are optional by default
   217   val = apiUtils.validateOptions({foo: 'bar'}, requirements);
   218   assert.deepEqual(val, {});
   219 };
   222 exports.testValidateOptionsMapIsOk = function (assert) {
   223   let [map, is, ok] = [false, false, false];
   224   let val = apiUtils.validateOptions({ foo: 1337 }, {
   225     foo: {
   226       map: function (v) v.toString(),
   227       is: ["string"],
   228       ok: function (v) v.length > 0
   229     }
   230   });
   231   assert.deepEqual(val, { foo: "1337" });
   233   let requirements = {
   234     foo: {
   235       is: ["object"],
   236       ok: function () assert.fail("is should have caused us to throw by now")
   237     }
   238   };
   239   assert.throws(
   240     function () apiUtils.validateOptions(null, requirements),
   241     /^The option "foo" must be one of the following types: object/,
   242     "is should be used before ok is called"
   243   );
   244 };
   246 exports.testValidateOptionsErrorMsg = function (assert) {
   247   assert.throws(
   248     function () apiUtils.validateOptions(null, {
   249       foo: { ok: function (v) v, msg: "foo!" }
   250     }),
   251     /^foo!/,
   252     "ok should raise exception with customized message"
   253   );
   254 };
   256 exports.testValidateMapWithMissingKey = function (assert) {
   257   let val = apiUtils.validateOptions({ }, {
   258     foo: {
   259       map: function (v) v || "bar"
   260     }
   261   });
   262   assert.deepEqual(val, { foo: "bar" });
   264   val = apiUtils.validateOptions({ }, {
   265     foo: {
   266       map: function (v) { throw "bar" }
   267     }
   268   });
   269   assert.deepEqual(val, { });
   270 };
   272 exports.testValidateMapWithMissingKeyAndThrown = function (assert) {
   273   let val = apiUtils.validateOptions({}, {
   274     bar: {
   275       map: function(v) { throw "bar" }
   276     },
   277     baz: {
   278       map: function(v) "foo"
   279     }
   280   });
   281   assert.deepEqual(val, { baz: "foo" });
   282 };
   284 exports.testAddIterator = function testAddIterator (assert) {
   285   let obj = {};
   286   let keys = ["foo", "bar", "baz"];
   287   let vals = [1, 2, 3];
   288   let keysVals = [["foo", 1], ["bar", 2], ["baz", 3]];
   289   apiUtils.addIterator(
   290     obj,
   291     function keysValsGen() {
   292       for each (let keyVal in keysVals)
   293         yield keyVal;
   294     }
   295   );
   297   let keysItr = [];
   298   for (let key in obj)
   299     keysItr.push(key);
   301   assert.equal(keysItr.length, keys.length,
   302                    "the keys iterator returns the correct number of items");
   303   for (let i = 0; i < keys.length; i++)
   304     assert.equal(keysItr[i], keys[i], "the key is correct");
   306   let valsItr = [];
   307   for each (let val in obj)
   308     valsItr.push(val);
   309   assert.equal(valsItr.length, vals.length,
   310                    "the vals iterator returns the correct number of items");
   311   for (let i = 0; i < vals.length; i++)
   312     assert.equal(valsItr[i], vals[i], "the val is correct");
   314 };
   316 require('test').run(exports);

mercurial