Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Bug - 634379 sandbox tests #109

Closed
wants to merge 8 commits into from
278 changes: 278 additions & 0 deletions packages/api-utils/tests/sandbox/all.js
@@ -0,0 +1,278 @@
const { Cc, Ci, Cu } = require("chrome");

function Sandboxed(principal) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this isn't called simply Sandbox? Sandboxed is a strange name for a constructor, since it is an adjective, and constructor names are typically nouns.

let sandbox = Cu.Sandbox(principal || "http://www.mozilla.com/");
return Cu.evalInSandbox("({" +
" create: function create(prototype, descriptor) {" +
" return Object.create(prototype, descriptor);" +
" }," +
" getPrototypeOf: function getPrototypeOf(object) {" +
" return Object.getPrototypeOf(object);" +
" }," +
" defineProperty: function defineProperty(object, name, descriptor) {" +
" return Object.defineProperty(object, name, descriptor);" +
" }," +
" defineProperties: function defineProperties(object, map) {" +
" return Object.defineProperties(object, map);" +
" }," +
" keys: function keys(object) {" +
" return Object.keys(object);" +
" }," +
" getOwnPropertyNames: function getOwnPropertyNames(object) {" +
" return Object.getOwnPropertyNames(object);" +
" }," +
" getOwnPropertyDescriptor: function getOwnPropertyDescriptor(value, key) {" +
" return Object.getOwnPropertyDescriptor(value, key);" +
" }," +
"})", sandbox);
};

exports.Assert = require("./asserts").create(Object);
exports["test inheritence"] = function (assert) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: inheritence -> inheritance

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it'd make sense to separate these two lines with a line of whitespace, since they aren't particularly connected.

function Type() {}
let prototype = Type.prototype;
let sandbox = Sandboxed();
let properties = { a: { value: "a" }, b: { get: function() "b" } };

let f1 = Object.create(Type.prototype, properties);
let f2 = Object.create(Type.prototype, properties);

assert.equal(Type.prototype, prototype, "prototype did not changed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: changed -> change

assert.equal(f1.constructor, Type, "consturctor is a Type function");
assert.equal(f2.constructor, Type, "sandbox: consturctor is a Type function");
assert.equal(Object.getPrototypeOf(f1), Type.prototype,
"prototype is `Type.prototype`");
assert.equal(sandbox.getPrototypeOf(f1), Type.prototype,
"`getPrototypeOf` in sandbox returs `Type.prototype`");
assert.equal(Object.getPrototypeOf(f2), Type.prototype,
"sandbox: prototype is `Type.prototype`");
assert.equal(sandbox.getPrototypeOf(f2), Type.prototype,
"getPrototypeOf in sandbox returns `Type.prototype`");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: costurctor -> constructor
Nit: returs -> returns

Nit: since f1 and f2 are created using the same prototype and property descriptor map, asserting "prototype is Type.prototype" and "getPrototypeOf in sandbox returns Type.prototype" for one of them is sufficient (and if it wasn't, then at least the test descriptions should be consistent, except that they should identify the object being tested, so it's possible to distinguish between them while still seeing that they test the same thing).

};

exports["test writable / non-writable properties"] = function (assert) {
let sandbox = Sandboxed();
let prototype = Object.create(Object.prototype, {
a: { value: "a", writable: false },
b: { value: "b", writable: true },
c: { value: "c" }
});
let properties = {
d: { value: "d", writable: false },
e: { value: "e", writable: true },
f: { value: "f" }
};

let f1 = Object.create(prototype, properties);
let f2 = sandbox.create(prototype, properties);

assert.equal(f1.a, f2.a, "property `a` values match");
assert.equal(f1.b, f2.b, "property `b` values match");
assert.equal(f1.c, f2.c, "property `c` values match");
assert.equal(f1.d, f2.d, "property `d` values match");
assert.equal(f1.e, f2.e, "property `e` values match");
assert.equal(f1.f, f2.f, "property `f` values match");

assert.equal(Object.getOwnPropertyDescriptor(f1, "a"),
Object.getOwnPropertyDescriptor(f2, "a"),
"proprety `a` descriptors are undefined");
assert.equal(Object.getOwnPropertyDescriptor(f1, "b"),
Object.getOwnPropertyDescriptor(f2, "b"),
"proprety `c` descriptors are undefined");
assert.equal(Object.getOwnPropertyDescriptor(f1, "c"),
Object.getOwnPropertyDescriptor(f2, "c"),
"proprety `c` descriptors are undefined");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: proprety -> property

c -> b for the second of these three asserts.

Also, this is checking that the property descriptors match, not that they are undefined.



assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "d"),
Object.getOwnPropertyDescriptor(f2, "d"),
"property `d` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "e"),
Object.getOwnPropertyDescriptor(f2, "e"),
"property `e` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "f"),
Object.getOwnPropertyDescriptor(f2, "f"),
"property `f` descriptors match");

assert.nonWritable(f1, "a", "property `a` is non-writable");
assert.nonWritable(f2, "a", "sandbox: property `a` is non-writable");
assert.nonWritable(f1, "c", "property `c` is non-writable");
assert.nonWritable(f2, "c", "sandbox: property `d` is non-writable");
assert.nonWritable(f1, "d", "property `i` is non-writable");
assert.nonWritable(f2, "d", "sandbox: property `a` is non-writable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d -> c
i -> d
a (the third occurrence) -> d

assert.nonWritable(f1, "f", "property `f` is non-writable");
assert.nonWritable(f2, "f", "sandbox: property `f` is non-writable");

f1.b = f2.b = f1.e = f2.e = "<value>";

assert.equal(f1.b, f2.b, "property `b` values are the same after set");
assert.equal(f1.e, f2.e, "property `e` values are the same after set");
assert.equal(f1.b, f2.e, "all writable propeperties changed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: propeperties -> properties

};

exports["test configurable / non-configurable properties"] = function (assert) {
let sandbox = Sandboxed();
let properties = {
a: { value: "a", configurable: false },
b: { value: "b", configurable: true },
c: { value: "c" },
d: { get: function() "d", configurable: false },
e: { get: function() "e", configurable: true },
f: { get: function() "f" }
};

let override = { value: "<override>" };

let f1 = Object.create(Object.prototype, properties);
let f2 = sandbox.create(Object.prototype, properties);

assert.equal(f1.a, f2.a, "property `a` values match");
assert.equal(f1.b, f2.b, "property `b` values match");
assert.equal(f1.c, f2.c, "property `c` values match");
assert.equal(f1.d, f2.d, "property `d` values match");
assert.equal(f1.e, f2.e, "property `e` values match");
assert.equal(f1.f, f2.f, "property `f` values match");

assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "a"),
Object.getOwnPropertyDescriptor(f2, "a"),
"property `a` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "b"),
Object.getOwnPropertyDescriptor(f2, "b"),
"property `b` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "c"),
Object.getOwnPropertyDescriptor(f2, "c"),
"property `c` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "d"),
Object.getOwnPropertyDescriptor(f2, "d"),
"property `d` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "e"),
Object.getOwnPropertyDescriptor(f2, "e"),
"property `e` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "f"),
Object.getOwnPropertyDescriptor(f2, "f"),
"property `f` descriptors match");

assert.nonConfigurable(f1, "a", "property `a` is non-configurable");
assert.nonConfigurable(f2, "a",
"sandbox: property `a` is non-configurable");
assert.nonConfigurable(f1, "c",
"property `c` defaults to non-configurable");
assert.nonConfigurable(f2, "c",
"sandbox: property `d` defaults to non-configurable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d -> c

assert.nonConfigurable(f1, "d", "property `d` is non-configurable");
assert.nonConfigurable(f2, "d",
"sandbox: property `d` is non-configurable");
assert.nonConfigurable(f1, "f", "property `f` defaults to non-configurable");
assert.nonConfigurable(f2, "f",
"sandbox: property `f` defaults to non-configurable");

Object.defineProperty(f1, "b", override);
Object.defineProperty(f2, "b", override);
Object.defineProperty(f1, "e", override);
Object.defineProperty(f2, "e", override);

assert.equal(f1.b, f2.b, "property `b` values are the same after redefine");
assert.equal(f1.e, f2.e, "property `e` values are the same after redifine");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: redifine -> redefine

assert.equal(f1.b, f2.e, "all propeperties redefined to same");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: propeperties -> properties


delete f1.b;
delete f2.b;
delete f1.e;
delete f2.e;

assert.ok(!('b' in f1), "property `b` was deleted");
assert.ok(!('b' in f2), "sandbox: property `b` was deleted");
assert.ok(!('e' in f1), "property `e` was deleted");
assert.ok(!('e' in f2), "sandbox: property `e` was deleted");
};


exports["test enumerable / non-enumerable properties"] = function (assert) {
let sandbox = Sandboxed();
let properties = {
a: { value: "a", enumerable: false },
b: { value: "b", enumerable: true },
c: { value: "c" },
d: { get: function() "d", enumerable: false },
e: { get: function() "e", enumerable: true },
f: { get: function() "f" }
};

let f1 = Object.create(Object.prototype, properties);
let f2 = sandbox.create(Object.prototype, properties);

assert.equal(f1.a, f2.a, "property `a` values match");
assert.equal(f1.b, f2.b, "property `b` values match");
assert.equal(f1.c, f2.c, "property `c` values match");
assert.equal(f1.d, f2.d, "property `d` values match");
assert.equal(f1.e, f2.e, "property `e` values match");
assert.equal(f1.f, f2.f, "property `f` values match");

assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "a"),
Object.getOwnPropertyDescriptor(f2, "a"),
"property `a` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "b"),
Object.getOwnPropertyDescriptor(f2, "b"),
"property `b` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "c"),
Object.getOwnPropertyDescriptor(f2, "c"),
"property `c` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "d"),
Object.getOwnPropertyDescriptor(f2, "d"),
"property `d` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "e"),
Object.getOwnPropertyDescriptor(f2, "e"),
"property `e` descriptors match");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "f"),
Object.getOwnPropertyDescriptor(f2, "f"),
"property `f` descriptors match");

assert.nonEnumerable(f1, "a", "property `a` is non-enumerable");
assert.nonEnumerable(f2, "a",
"sandbox: property `a` is non-enumerable");
assert.nonEnumerable(f1, "c",
"property `c` defaults to non-enumerable");
assert.nonEnumerable(f2, "c",
"sandbox: property `d` defaults to non-enumerable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d -> c

assert.nonEnumerable(f1, "d", "property `d` is non-enumerable");
assert.nonEnumerable(f2, "d",
"sandbox: property `d` is non-enumerable");
assert.nonEnumerable(f1, "f", "property `f` defaults to non-enumerable");
assert.nonEnumerable(f2, "f",
"sandbox: property `f` defaults to non-enumerable");
};

exports["property names / keys"] = function (assert) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, these tests aren't being run, although it isn't clear why.

let sandbox = Sandboxed();
let properties = {
a: { value: "a", enumerable: false },
b: { value: "b", enumerable: true },
c: { value: "c" },
d: { get: function() "d", enumerable: false },
e: { get: function() "e", enumerable: true },
f: { get: function() "f" }
};

let f1 = Object.create(Object.prototype, properties);
let f2 = sandbox.create(Object.prototype, properties);

assert.equal(Object.keys(f1).length, 6, "6 own properties");
assert.equal(Object.keys(f2).length, 6,
"sandbox: 6 own properties");
assert.equal(sandbox.keys(f1).length, 6,
"`getOwnPropertyNames` from sandbox reports 6 own properties");
assert.equal(sandbox.keys(f2).length, 6,
"`getOwnPropertyNames` from sandbox reports 6 own properties");

assert.equal(Object.keys(f1).length, 4, "4 own enumerable properties");
assert.equal(Object.keys(f2).length, 4,
"sandbox: 6 own enumerable properties");
assert.equal(sandbox.keys(f1).length, 4,
"`keys` from sandbox reports 4 own properties");
assert.equal(sandbox.keys(f2).length, 4,
"`keys` from sandbox reports 4 own properties");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Erm, these are the exact same tests, although their descriptions are different.


};

if (module == require.main)
require("test").run(exports);