js/xpconnect/tests/chrome/test_cloneInto.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/chrome/test_cloneInto.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     1.7 +<window title="Mozilla Bug 503926"
     1.8 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     1.9 +  <script type="application/javascript"
    1.10 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.11 +
    1.12 +  <!-- test results are displayed in the html:body -->
    1.13 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.14 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=964293"
    1.15 +     target="_blank">Cu.cloneInto()</a>
    1.16 +  </body>
    1.17 +
    1.18 +  <!-- test code goes here -->
    1.19 +  <script type="application/javascript">
    1.20 +  <![CDATA[
    1.21 +
    1.22 +  const Cu = Components.utils;
    1.23 +  const Ci = Components.interfaces;
    1.24 +
    1.25 +  const TypedArrayThings = [
    1.26 +    'Int8Array',
    1.27 +    'Uint8Array',
    1.28 +    'Uint8ClampedArray',
    1.29 +    'Int16Array',
    1.30 +    'Uint16Array',
    1.31 +    'Int32Array',
    1.32 +    'Uint32Array',
    1.33 +    'Float32Array',
    1.34 +    'Float64Array',
    1.35 +  ];
    1.36 +
    1.37 +  function getType(a) {
    1.38 +    if (a === null || a === undefined)
    1.39 +      return 'null';
    1.40 +
    1.41 +    if (Array.isArray(a))
    1.42 +      return 'array';
    1.43 +
    1.44 +    if (a instanceof Ci.nsIDOMFile)
    1.45 +      return 'file';
    1.46 +
    1.47 +    if (a instanceof Ci.nsIDOMBlob)
    1.48 +      return 'blob';
    1.49 +
    1.50 +    if (TypedArrayThings.indexOf(a.constructor.name) !== -1)
    1.51 +      return a.constructor.name;
    1.52 +
    1.53 +    if (typeof a == 'object')
    1.54 +      return 'object';
    1.55 +
    1.56 +    if (typeof a == 'function')
    1.57 +      return 'function';
    1.58 +
    1.59 +    return 'primitive';
    1.60 +  }
    1.61 +
    1.62 +  function compare(a, b) {
    1.63 +    is (getType(a), getType(b), 'Type matches');
    1.64 +
    1.65 +    var type = getType(a);
    1.66 +    if (type == 'array') {
    1.67 +      is (a.length, b.length, 'Array.length matches');
    1.68 +      for (var i = 0; i < a.length; ++i) {
    1.69 +        compare(a[i], b[i]);
    1.70 +      }
    1.71 +
    1.72 +      return;
    1.73 +    }
    1.74 +
    1.75 +    if (type == 'file' || type == 'blob') {
    1.76 +      ok ( a === b, 'They should match');
    1.77 +      return;
    1.78 +    }
    1.79 +
    1.80 +    if (type == 'object') {
    1.81 +      ok ( a !== b, 'They should not match');
    1.82 +
    1.83 +      var aProps = [];
    1.84 +      for (var p in a) aProps.push(p);
    1.85 +
    1.86 +      var bProps = [];
    1.87 +      for (var p in b) bProps.push(p);
    1.88 +
    1.89 +      is (aProps.length, bProps.length, 'Props match');
    1.90 +      is (aProps.sort().toSource(), bProps.sort().toSource(), 'Props match - using toSource()');
    1.91 +
    1.92 +      for (var p in a) {
    1.93 +        compare(a[p], b[p]);
    1.94 +      }
    1.95 +
    1.96 +      return;
    1.97 +    }
    1.98 +
    1.99 +    if (type == 'function') {
   1.100 +      ok ( a !== b, 'They should not match');
   1.101 +      return;
   1.102 +    }
   1.103 +
   1.104 +    if (type != 'null') {
   1.105 +      is (a.toSource(), b.toSource(), 'Matching using toSource()');
   1.106 +    }
   1.107 +  }
   1.108 +
   1.109 +  var sandboxOptions = {
   1.110 +    wantXrays: true,
   1.111 +    wantExportHelpers: true,
   1.112 +  };
   1.113 +  var sandbox = new Cu.Sandbox(window, sandboxOptions);
   1.114 +  // The second sandbox is for testing the exportHelper version of the cloneInto
   1.115 +  var sandbox2 = new Cu.Sandbox("http://example.com", sandboxOptions);
   1.116 +  sandbox.sandbox2 = sandbox2;
   1.117 +
   1.118 +  function cloneAndTest(test, cloneOptions) {
   1.119 +    var output = sandbox.test = Cu.cloneInto(test, sandbox, cloneOptions);
   1.120 +    compare(test, output);
   1.121 +
   1.122 +    sandbox.cloneOptions = cloneOptions;
   1.123 +    output = Cu.evalInSandbox('cloneInto(test, sandbox2, cloneOptions)', sandbox);
   1.124 +    compare(test, output);
   1.125 +  }
   1.126 +
   1.127 +  var tests = [
   1.128 +    1,
   1.129 +    null,
   1.130 +    true,
   1.131 +    'hello world',
   1.132 +    [1, 2, 3],
   1.133 +    { a: 1, b: 2 },
   1.134 +    { blob: new Blob([]), file: new File(new Blob([])) },
   1.135 +    new Date(),
   1.136 +    { a: 1, b: {}, c: [1, 2, 3, { d: new Blob([]) } ], e: 'hello world' },
   1.137 +  ];
   1.138 +
   1.139 +  for (var i = 0; i < tests.length; ++i) {
   1.140 +    cloneAndTest(tests[i]);
   1.141 +  }
   1.142 +
   1.143 +  try {
   1.144 +    var output = Cu.cloneInto({ a: function() {} }, sandbox);
   1.145 +    ok(false, 'Function should not be cloned by default');
   1.146 +  } catch(e) {
   1.147 +    ok(true, 'Function should not be cloned by default');
   1.148 +  }
   1.149 +
   1.150 +  try {
   1.151 +    sandbox2.sandbox = sandbox;
   1.152 +    Cu.evalInSandbox('cloneInto({}, sandbox)', sandbox2);
   1.153 +    ok(false, 'CloneInto should only work on less privileged target scopes.');
   1.154 +  } catch(e) {
   1.155 +    ok(/denied|insecure/.test(e),
   1.156 +       'CloneInto should only work on less privileged target scopes.');
   1.157 +  }
   1.158 +
   1.159 +  var test = { a: function() { return 42; } }
   1.160 +  cloneAndTest(test, { cloneFunctions: true });
   1.161 +  ]]>
   1.162 +  </script>
   1.163 +</window>

mercurial