summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_schemas_allowed_contexts.js
blob: 16743049d0300c01990892a3057071327c250ee0 (plain)
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
"use strict";

const { Schemas } = ChromeUtils.importESModule(
  "resource://gre/modules/Schemas.sys.mjs"
);

const global = this;

let schemaJson = [
  {
    namespace: "noAllowedContexts",
    properties: {
      prop1: { type: "object" },
      prop2: { type: "object", allowedContexts: ["test_zero", "test_one"] },
      prop3: { type: "number", value: 1 },
      prop4: { type: "number", value: 1, allowedContexts: ["numeric_one"] },
    },
  },
  {
    namespace: "defaultContexts",
    defaultContexts: ["test_two"],
    properties: {
      prop1: { type: "object" },
      prop2: { type: "object", allowedContexts: ["test_three"] },
      prop3: { type: "number", value: 1 },
      prop4: { type: "number", value: 1, allowedContexts: ["numeric_two"] },
    },
  },
  {
    namespace: "withAllowedContexts",
    allowedContexts: ["test_four"],
    properties: {
      prop1: { type: "object" },
      prop2: { type: "object", allowedContexts: ["test_five"] },
      prop3: { type: "number", value: 1 },
      prop4: { type: "number", value: 1, allowedContexts: ["numeric_three"] },
    },
  },
  {
    namespace: "withAllowedContextsAndDefault",
    allowedContexts: ["test_six"],
    defaultContexts: ["test_seven"],
    properties: {
      prop1: { type: "object" },
      prop2: { type: "object", allowedContexts: ["test_eight"] },
      prop3: { type: "number", value: 1 },
      prop4: { type: "number", value: 1, allowedContexts: ["numeric_four"] },
    },
  },
  {
    namespace: "with_submodule",
    defaultContexts: ["test_nine"],
    types: [
      {
        id: "subtype",
        type: "object",
        functions: [
          {
            name: "noAllowedContexts",
            type: "function",
            parameters: [],
          },
          {
            name: "allowedContexts",
            allowedContexts: ["test_ten"],
            type: "function",
            parameters: [],
          },
        ],
      },
    ],
    properties: {
      prop1: { $ref: "subtype" },
      prop2: { $ref: "subtype", allowedContexts: ["test_eleven"] },
    },
  },
];

add_task(async function testRestrictions() {
  let url = "data:," + JSON.stringify(schemaJson);
  await Schemas.load(url);
  let results = {};
  let localWrapper = {
    manifestVersion: 2,
    cloneScope: global,
    shouldInject(ns, name, allowedContexts) {
      name = ns ? ns + "." + name : name;
      results[name] = allowedContexts.join(",");
      return true;
    },
    getImplementation() {
      // The actual implementation is not significant for this test.
      // Let's take this opportunity to see if schema generation is free of
      // exceptions even when somehow getImplementation does not return an
      // implementation.
    },
  };

  let root = {};
  Schemas.inject(root, localWrapper);

  function verify(path, expected) {
    let obj = root;
    for (let thing of path.split(".")) {
      try {
        obj = obj[thing];
      } catch (e) {
        // Blech.
      }
    }

    let result = results[path];
    equal(result, expected, path);
  }

  verify("noAllowedContexts", "");
  verify("noAllowedContexts.prop1", "");
  verify("noAllowedContexts.prop2", "test_zero,test_one");
  verify("noAllowedContexts.prop3", "");
  verify("noAllowedContexts.prop4", "numeric_one");

  verify("defaultContexts", "");
  verify("defaultContexts.prop1", "test_two");
  verify("defaultContexts.prop2", "test_three");
  verify("defaultContexts.prop3", "test_two");
  verify("defaultContexts.prop4", "numeric_two");

  verify("withAllowedContexts", "test_four");
  verify("withAllowedContexts.prop1", "");
  verify("withAllowedContexts.prop2", "test_five");
  verify("withAllowedContexts.prop3", "");
  verify("withAllowedContexts.prop4", "numeric_three");

  verify("withAllowedContextsAndDefault", "test_six");
  verify("withAllowedContextsAndDefault.prop1", "test_seven");
  verify("withAllowedContextsAndDefault.prop2", "test_eight");
  verify("withAllowedContextsAndDefault.prop3", "test_seven");
  verify("withAllowedContextsAndDefault.prop4", "numeric_four");

  verify("with_submodule", "");
  verify("with_submodule.prop1", "test_nine");
  verify("with_submodule.prop1.noAllowedContexts", "test_nine");
  verify("with_submodule.prop1.allowedContexts", "test_ten");
  verify("with_submodule.prop2", "test_eleven");
  // Note: test_nine inherits allowed contexts from the namespace, not from
  // submodule. There is no "defaultContexts" for submodule types to not
  // complicate things.
  verify("with_submodule.prop1.noAllowedContexts", "test_nine");
  verify("with_submodule.prop1.allowedContexts", "test_ten");

  // This is a constant, so it does not matter that getImplementation does not
  // return an implementation since the API injector should take care of it.
  equal(root.noAllowedContexts.prop3, 1);

  Assert.throws(
    () => root.noAllowedContexts.prop1,
    /undefined/,
    "Should throw when the implementation is absent."
  );
});