File: test_xrayToExplicitResourceManagement.html

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (345 lines) | stat: -rw-r--r-- 15,776 bytes parent folder | download | duplicates (11)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html lang="en">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1929055
-->

<head>
  <meta charset="UTF-8">
  <title>Test for Bug 1929055</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin" />
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
  <script>
    var { AppConstants } = SpecialPowers.ChromeUtils.importESModule(
      "resource://gre/modules/AppConstants.sys.mjs"
    );
    const isExplicitResourceManagementEnabled = AppConstants.ENABLE_EXPLICIT_RESOURCE_MANAGEMENT;

    async function go() {
      SimpleTest.waitForExplicitFinish();

      let simpleConstructors = [
        'DisposableStack',
        'AsyncDisposableStack',
      ];

      const errorObjectClasses = [
        'SuppressedError',
      ]

      simpleConstructors = simpleConstructors.concat(errorObjectClasses);

      const iwin = document.getElementById('ifr').contentWindow;

      if (!isExplicitResourceManagementEnabled) {
        for (let c of simpleConstructors) {
          is(iwin[c], undefined, "Constructors should not be exposed: " + c);
        }
        SimpleTest.finish();
        return;
      }

      await SpecialPowers.pushPrefEnv({
        set: [["javascript.options.experimental.explicit_resource_management", true]],
      });

      let global = Cu.getGlobalForObject.bind(Cu);

      // Copied from js/xpconnect/tests/chrome/test_xrayToJS.xhtml
      // ==== BEGIN ===

      // Test constructors that can be instantiated with zero arguments, or with
      // a fixed set of arguments provided using `...rest`.
      for (let c of simpleConstructors) {
        var args = [];
        if (typeof c === 'object') {
          args = c.args;
          c = c.name;
        }
        ok(iwin[c], "Constructors appear: " + c);
        is(iwin[c], Cu.unwaiveXrays(iwin.wrappedJSObject[c]),
          "we end up with the appropriate constructor: " + c);
        is(Cu.unwaiveXrays(Cu.waiveXrays(new iwin[c](...args)).constructor), iwin[c],
          "constructor property is set up right: " + c);
        let expectedProto = Cu.isOpaqueWrapper(new iwin[c](...args)) ?
          iwin.Object.prototype : iwin[c].prototype;
        is(Object.getPrototypeOf(new iwin[c](...args)), expectedProto,
          "prototype is correct: " + c);
        is(global(new iwin[c](...args)), iwin, "Got the right global: " + c);
      }

      var gPrototypeProperties = {};
      var gConstructorProperties = {};
      // Properties which cannot be invoked if callable without potentially
      // rendering the object useless.
      var gStatefulProperties = {};

      function testProtoCallables(protoCallables, xray, xrayProto, localProto, callablesExcluded) {
        // Handle undefined callablesExcluded.
        let dontCall = callablesExcluded ?? [];
        for (let name of protoCallables) {
          info("Running tests for property: " + name);
          // Test both methods and getter properties.
          function lookupCallable(obj) {
            let desc = null;
            do {
              desc = Object.getOwnPropertyDescriptor(obj, name);
              if (desc) {
                break;
              }
              obj = Object.getPrototypeOf(obj);
            } while (obj);
            return desc ? (desc.get || desc.value) : undefined;
          };
          ok(xrayProto.hasOwnProperty(name), `proto should have the property '${name}' as own`);
          ok(!xray.hasOwnProperty(name), `instance should not have the property '${name}' as own`);
          let method = lookupCallable(xrayProto);
          is(typeof method, 'function', "Methods from Xrays are functions");
          is(global(method), window, "Methods from Xrays are local");
          ok(method instanceof Function, "instanceof works on methods from Xrays");
          is(lookupCallable(xrayProto), method, "Holder caching works properly");
          is(lookupCallable(xray), method, "Proto props resolve on the instance");
          let local = lookupCallable(localProto);
          is(method.length, local.length, "Function.length identical");
          if (!method.length && !dontCall.includes(name)) {
            is(method.call(xray) + "", local.call(xray) + "",
              "Xray and local method results stringify identically");

            // If invoking this method returns something non-Xrayable (opaque), the
            // stringification is going to return [object Object].
            // This happens for set[@@iterator] and other Iterator objects.
            let callable = lookupCallable(xray.wrappedJSObject);
            if (!Cu.isOpaqueWrapper(method.call(xray)) && callable) {
              is(method.call(xray) + "",
                callable.call(xray.wrappedJSObject) + "",
                "Xray and waived method results stringify identically");
            }
          }
        }
      }

      function testCtorCallables(ctorCallables, xrayCtor, localCtor) {
        for (let name of ctorCallables) {
          // Don't try to test Function.prototype, since that is in fact a callable
          // but doesn't really do the things we expect callables to do here
          // (e.g. it's in the wrong global, since it gets Xrayed itself).
          if (name == "prototype" && localCtor.name == "Function") {
            continue;
          }
          info(`Running tests for property: ${localCtor.name}.${name}`);
          // Test both methods and getter properties.
          function lookupCallable(obj) {
            let desc = null;
            do {
              desc = Object.getOwnPropertyDescriptor(obj, name);
              obj = Object.getPrototypeOf(obj);
            } while (!desc);
            return desc.get || desc.value;
          };

          ok(xrayCtor.hasOwnProperty(name), "ctor should have the property as own");
          let method = lookupCallable(xrayCtor);
          is(typeof method, 'function', "Methods from ctor Xrays are functions");
          is(global(method), window, "Methods from ctor Xrays are local");
          ok(method instanceof Function,
            "instanceof works on methods from ctor Xrays");
          is(lookupCallable(xrayCtor), method,
            "Holder caching works properly on ctors");
          let local = lookupCallable(localCtor);
          is(method.length, local.length,
            "Function.length identical for method from ctor");
          // Don't try to do the return-value check on Date.now(), since there is
          // absolutely no reason it should return the same value each time.
          //
          // Also don't try to do the return-value check on Regexp.lastMatch and
          // Regexp["$&"] (which are aliases), because they get state off the global
          // they live in, as far as I can tell, so testing them over Xrays will be
          // wrong: on the Xray they will actaully get the lastMatch of _our_
          // global, not the Xrayed one.
          if (!method.length &&
            !(localCtor.name == "Date" && name == "now") &&
            !(localCtor.name == "RegExp" && (name == "lastMatch" || name == "$&"))) {
            is(method.call(xrayCtor) + "", local.call(xrayCtor) + "",
              "Xray and local method results stringify identically on constructors");
            is(method.call(xrayCtor) + "",
              lookupCallable(xrayCtor.wrappedJSObject).call(xrayCtor.wrappedJSObject) + "",
              "Xray and waived method results stringify identically");
          }
        }
      }

      function filterOut(array, props) {
        return array.filter(p => !props.includes(p));
      }

      function propertyIsGetter(obj, name) {
        return !!Object.getOwnPropertyDescriptor(obj, name).get;
      }

      function constructorProps(arr) {
        // Some props live on all constructors
        return arr.concat(["prototype", "length", "name"]);
      }

      // Sort an array that may contain symbols as well as strings.
      function sortProperties(arr) {
        function sortKey(prop) {
          return typeof prop + ":" + prop.toString();
        }
        arr.sort((a, b) => sortKey(a) < sortKey(b) ? -1 : +1);
      }

      function testXray(classname, xray, xray2, propsToSkip, ctorPropsToSkip = []) {
        propsToSkip = propsToSkip || [];
        let xrayProto = Object.getPrototypeOf(xray);
        let localProto = window[classname].prototype;
        let desiredProtoProps = Object.getOwnPropertyNames(localProto).sort();

        is(desiredProtoProps.toSource(),
          gPrototypeProperties[classname].filter(id => typeof id === "string").toSource(),
          "A property on the " + classname +
          " prototype has changed! You need a security audit from an XPConnect peer");
        is(Object.getOwnPropertySymbols(localProto).map(uneval).sort().toSource(),
          gPrototypeProperties[classname].filter(id => typeof id !== "string").map(uneval).sort().toSource(),
          "A symbol-keyed property on the " + classname +
          " prototype has been changed! You need a security audit from an XPConnect peer");

        let protoProps = filterOut(desiredProtoProps, propsToSkip);
        let protoCallables = protoProps.filter(name => propertyIsGetter(localProto, name, classname) ||
          typeof localProto[name] == 'function' &&
          name != 'constructor');
        let callablesExcluded = gStatefulProperties[classname];
        ok(!!protoCallables.length, "Need something to test");
        is(xrayProto, iwin[classname].prototype, "Xray proto is correct");
        is(xrayProto, xray.__proto__, "Proto accessors agree");
        var protoProto = classname == "Object" ? null : iwin.Object.prototype;
        is(Object.getPrototypeOf(xrayProto), protoProto, "proto proto is correct");
        testProtoCallables(protoCallables, xray, xrayProto, localProto, callablesExcluded);
        is(Object.getOwnPropertyNames(xrayProto).sort().toSource(),
          protoProps.toSource(), "getOwnPropertyNames works");
        is(Object.getOwnPropertySymbols(xrayProto).map(uneval).sort().toSource(),
          gPrototypeProperties[classname].filter(id => typeof id !== "string" && !propsToSkip.includes(id))
            .map(uneval).sort().toSource(),
          "getOwnPropertySymbols works");

        is(xrayProto.constructor, iwin[classname], "constructor property works");

        xrayProto.expando = 42;
        is(xray.expando, 42, "Xrayed instances see proto expandos");
        is(xray2.expando, 42, "Xrayed instances see proto expandos");

        // Now test constructors
        let localCtor = window[classname];
        let xrayCtor = xrayProto.constructor;
        // We already checked that this is the same as iwin[classname]

        let desiredCtorProps =
          Object.getOwnPropertyNames(localCtor).sort();
        is(desiredCtorProps.toSource(),
          gConstructorProperties[classname].filter(id => typeof id === "string").toSource(),
          "A property on the " + classname +
          " constructor has changed! You need a security audit from an XPConnect peer");
        let desiredCtorSymbols =
          Object.getOwnPropertySymbols(localCtor).map(uneval).sort()
        is(desiredCtorSymbols.toSource(),
          gConstructorProperties[classname].filter(id => typeof id !== "string").map(uneval).sort().toSource(),
          "A symbol-keyed property on the " + classname +
          " constructor has been changed! You need a security audit from an XPConnect peer");

        let ctorProps = filterOut(desiredCtorProps, ctorPropsToSkip);
        let ctorSymbols = filterOut(desiredCtorSymbols, ctorPropsToSkip.map(uneval));
        let ctorCallables = ctorProps.filter(name => propertyIsGetter(localCtor, name, classname) ||
          typeof localCtor[name] == 'function');
        testCtorCallables(ctorCallables, xrayCtor, localCtor);
        is(Object.getOwnPropertyNames(xrayCtor).sort().toSource(),
          ctorProps.toSource(), "getOwnPropertyNames works on Xrayed ctors");
        is(Object.getOwnPropertySymbols(xrayCtor).map(uneval).sort().toSource(),
          ctorSymbols.toSource(), "getOwnPropertySymbols works on Xrayed ctors");
      }

      // ==== END ===

      gPrototypeProperties.DisposableStack = [
        "adopt", "constructor", "defer", "dispose", "disposed", "move", "use",
        Symbol.toStringTag, Symbol.dispose
      ];
      gStatefulProperties.DisposableStack = ["dispose", Symbol.dispose, "move"];
      gConstructorProperties.DisposableStack = constructorProps([]);

      gPrototypeProperties.AsyncDisposableStack = [
        "adopt", "constructor", "defer", "disposeAsync", "disposed", "move", "use",
        Symbol.toStringTag, Symbol.asyncDispose
      ];
      gStatefulProperties.AsyncDisposableStack = ["disposeAsync", Symbol.asyncDispose, "move"];
      gConstructorProperties.AsyncDisposableStack = constructorProps([]);

      // Sort all the lists so we don't need to mutate them later (or copy them
      // again to sort them).
      for (let c of Object.keys(gPrototypeProperties))
        sortProperties(gPrototypeProperties[c]);
      for (let c of Object.keys(gConstructorProperties))
        sortProperties(gConstructorProperties[c]);

      function testDisposableStack() {
        testXray("DisposableStack", new iwin.DisposableStack(), new iwin.DisposableStack());
      }

      function testAsyncDisposableStack() {
        testXray("AsyncDisposableStack", new iwin.AsyncDisposableStack(), new iwin.AsyncDisposableStack());
      }

      function testSuppressedError() {
        const c = errorObjectClasses[0];
        const args = ['error', 'suppressed', 'some message'];
        var e = new iwin.SuppressedError(...args);

        // Copied from js/xpconnect/tests/chrome/test_xrayToJS.xhtml
        // ==== BEGIN ===

        is(Object.getPrototypeOf(e).name, c, "Prototype has correct name");
        is(Object.getPrototypeOf(Object.getPrototypeOf(e)), iwin.Error.prototype, "Dependent prototype set up correctly");
        is(e.name, c, "Exception name inherited correctly");

        function testProperty(name, criterion, goodReplacement, faultyReplacement) {
          ok(criterion(e[name]), name + " property is correct: " + e[name]);
          e.wrappedJSObject[name] = goodReplacement;
          is(e[name], goodReplacement, name + " property ok after replacement: " + goodReplacement);
          e.wrappedJSObject[name] = faultyReplacement;
          is(e[name], name == 'message' ? "" : undefined, name + " property skipped after suspicious replacement");
        }
        testProperty('message', x => x == 'some message', 'some other message', 42);
        testProperty('fileName', x => x == '', 'otherFilename.html', new iwin.Object());
        testProperty('columnNumber', x => x == 1, 99, 99.5);
        testProperty('lineNumber', x => x == 0, 50, 'foo');

        // ==== END ===

        e.wrappedJSObject.error = 42;
        is(e.wrappedJSObject.error, 42, "errors is a plain data property");
        is(e.error, 42, "error visible over Xrays");

        e.wrappedJSObject.suppressed = 43;
        is(e.wrappedJSObject.suppressed, 43, "suppressed is a plain data property");
        is(e.suppressed, 43, "suppressed visible over Xrays");
      }

      testDisposableStack();

      testAsyncDisposableStack();

      testSuppressedError();

      await SpecialPowers.popPrefEnv();
      SimpleTest.finish();
    }
  </script>
</head>

<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1929055">Mozilla Bug 1929055</a>

  <iframe id="ifr" onload="go();" src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html" />
</body>

</html>