summaryrefslogtreecommitdiffstats
path: root/remote/shared/webdriver/test/xpcshell/test_Capabilities.js
blob: e88c0126c9d3ad43ec3b6d05a002da4d3664c5fb (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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

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

const { AppInfo } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/AppInfo.sys.mjs"
);
const { error } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/Errors.sys.mjs"
);
const {
  Capabilities,
  PageLoadStrategy,
  Proxy,
  Timeouts,
  UnhandledPromptBehavior,
} = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/Capabilities.sys.mjs"
);

add_task(function test_Timeouts_ctor() {
  let ts = new Timeouts();
  equal(ts.implicit, 0);
  equal(ts.pageLoad, 300000);
  equal(ts.script, 30000);
});

add_task(function test_Timeouts_toString() {
  equal(new Timeouts().toString(), "[object Timeouts]");
});

add_task(function test_Timeouts_toJSON() {
  let ts = new Timeouts();
  deepEqual(ts.toJSON(), { implicit: 0, pageLoad: 300000, script: 30000 });
});

add_task(function test_Timeouts_fromJSON() {
  let json = {
    implicit: 0,
    pageLoad: 2.0,
    script: Number.MAX_SAFE_INTEGER,
  };
  let ts = Timeouts.fromJSON(json);
  equal(ts.implicit, json.implicit);
  equal(ts.pageLoad, json.pageLoad);
  equal(ts.script, json.script);
});

add_task(function test_Timeouts_fromJSON_unrecognised_field() {
  let json = {
    sessionId: "foobar",
  };
  try {
    Timeouts.fromJSON(json);
  } catch (e) {
    equal(e.name, error.InvalidArgumentError.name);
    equal(e.message, "Unrecognised timeout: sessionId");
  }
});

add_task(function test_Timeouts_fromJSON_invalid_types() {
  for (let value of [null, [], {}, false, "10", 2.5]) {
    Assert.throws(
      () => Timeouts.fromJSON({ implicit: value }),
      /InvalidArgumentError/
    );
  }
});

add_task(function test_Timeouts_fromJSON_bounds() {
  for (let value of [-1, Number.MAX_SAFE_INTEGER + 1]) {
    Assert.throws(
      () => Timeouts.fromJSON({ script: value }),
      /InvalidArgumentError/
    );
  }
});

add_task(function test_PageLoadStrategy() {
  equal(PageLoadStrategy.None, "none");
  equal(PageLoadStrategy.Eager, "eager");
  equal(PageLoadStrategy.Normal, "normal");
});

add_task(function test_Proxy_ctor() {
  let p = new Proxy();
  let props = [
    "proxyType",
    "httpProxy",
    "sslProxy",
    "socksProxy",
    "socksVersion",
    "proxyAutoconfigUrl",
  ];
  for (let prop of props) {
    ok(prop in p, `${prop} in ${JSON.stringify(props)}`);
    equal(p[prop], null);
  }
});

add_task(function test_Proxy_init() {
  let p = new Proxy();

  // no changed made, and 5 (system) is default
  equal(p.init(), false);
  equal(Preferences.get("network.proxy.type"), 5);

  // pac
  p.proxyType = "pac";
  p.proxyAutoconfigUrl = "http://localhost:1234";
  ok(p.init());

  equal(Preferences.get("network.proxy.type"), 2);
  equal(
    Preferences.get("network.proxy.autoconfig_url"),
    "http://localhost:1234"
  );

  // direct
  p = new Proxy();
  p.proxyType = "direct";
  ok(p.init());
  equal(Preferences.get("network.proxy.type"), 0);

  // autodetect
  p = new Proxy();
  p.proxyType = "autodetect";
  ok(p.init());
  equal(Preferences.get("network.proxy.type"), 4);

  // system
  p = new Proxy();
  p.proxyType = "system";
  ok(p.init());
  equal(Preferences.get("network.proxy.type"), 5);

  // manual
  for (let proxy of ["http", "ssl", "socks"]) {
    p = new Proxy();
    p.proxyType = "manual";
    p.noProxy = ["foo", "bar"];
    p[`${proxy}Proxy`] = "foo";
    p[`${proxy}ProxyPort`] = 42;
    if (proxy === "socks") {
      p[`${proxy}Version`] = 4;
    }

    ok(p.init());
    equal(Preferences.get("network.proxy.type"), 1);
    equal(Preferences.get("network.proxy.no_proxies_on"), "foo, bar");
    equal(Preferences.get(`network.proxy.${proxy}`), "foo");
    equal(Preferences.get(`network.proxy.${proxy}_port`), 42);
    if (proxy === "socks") {
      equal(Preferences.get(`network.proxy.${proxy}_version`), 4);
    }
  }

  // empty no proxy should reset default exclustions
  p = new Proxy();
  p.proxyType = "manual";
  p.noProxy = [];
  ok(p.init());
  equal(Preferences.get("network.proxy.no_proxies_on"), "");
});

add_task(function test_Proxy_toString() {
  equal(new Proxy().toString(), "[object Proxy]");
});

add_task(function test_Proxy_toJSON() {
  let p = new Proxy();
  deepEqual(p.toJSON(), {});

  // autoconfig url
  p = new Proxy();
  p.proxyType = "pac";
  p.proxyAutoconfigUrl = "foo";
  deepEqual(p.toJSON(), { proxyType: "pac", proxyAutoconfigUrl: "foo" });

  // manual proxy
  p = new Proxy();
  p.proxyType = "manual";
  deepEqual(p.toJSON(), { proxyType: "manual" });

  for (let proxy of ["httpProxy", "sslProxy", "socksProxy"]) {
    let expected = { proxyType: "manual" };

    p = new Proxy();
    p.proxyType = "manual";

    if (proxy == "socksProxy") {
      p.socksVersion = 5;
      expected.socksVersion = 5;
    }

    // without port
    p[proxy] = "foo";
    expected[proxy] = "foo";
    deepEqual(p.toJSON(), expected);

    // with port
    p[proxy] = "foo";
    p[`${proxy}Port`] = 0;
    expected[proxy] = "foo:0";
    deepEqual(p.toJSON(), expected);

    p[`${proxy}Port`] = 42;
    expected[proxy] = "foo:42";
    deepEqual(p.toJSON(), expected);

    // add brackets for IPv6 address as proxy hostname
    p[proxy] = "2001:db8::1";
    p[`${proxy}Port`] = 42;
    expected[proxy] = "foo:42";
    expected[proxy] = "[2001:db8::1]:42";
    deepEqual(p.toJSON(), expected);
  }

  // noProxy: add brackets for IPv6 address
  p = new Proxy();
  p.proxyType = "manual";
  p.noProxy = ["2001:db8::1"];
  let expected = { proxyType: "manual", noProxy: "[2001:db8::1]" };
  deepEqual(p.toJSON(), expected);
});

add_task(function test_Proxy_fromJSON() {
  let p = new Proxy();
  deepEqual(p, Proxy.fromJSON(undefined));
  deepEqual(p, Proxy.fromJSON(null));

  for (let typ of [true, 42, "foo", []]) {
    Assert.throws(() => Proxy.fromJSON(typ), /InvalidArgumentError/);
  }

  // must contain a valid proxyType
  Assert.throws(() => Proxy.fromJSON({}), /InvalidArgumentError/);
  Assert.throws(
    () => Proxy.fromJSON({ proxyType: "foo" }),
    /InvalidArgumentError/
  );

  // autoconfig url
  for (let url of [true, 42, [], {}]) {
    Assert.throws(
      () => Proxy.fromJSON({ proxyType: "pac", proxyAutoconfigUrl: url }),
      /InvalidArgumentError/
    );
  }

  p = new Proxy();
  p.proxyType = "pac";
  p.proxyAutoconfigUrl = "foo";
  deepEqual(p, Proxy.fromJSON({ proxyType: "pac", proxyAutoconfigUrl: "foo" }));

  // manual proxy
  p = new Proxy();
  p.proxyType = "manual";
  deepEqual(p, Proxy.fromJSON({ proxyType: "manual" }));

  for (let proxy of ["httpProxy", "sslProxy", "socksProxy"]) {
    let manual = { proxyType: "manual" };

    // invalid hosts
    for (let host of [
      true,
      42,
      [],
      {},
      null,
      "http://foo",
      "foo:-1",
      "foo:65536",
      "foo/test",
      "foo#42",
      "foo?foo=bar",
      "2001:db8::1",
    ]) {
      manual[proxy] = host;
      Assert.throws(() => Proxy.fromJSON(manual), /InvalidArgumentError/);
    }

    p = new Proxy();
    p.proxyType = "manual";
    if (proxy == "socksProxy") {
      manual.socksVersion = 5;
      p.socksVersion = 5;
    }

    let host_map = {
      "foo:1": { hostname: "foo", port: 1 },
      "foo:21": { hostname: "foo", port: 21 },
      "foo:80": { hostname: "foo", port: 80 },
      "foo:443": { hostname: "foo", port: 443 },
      "foo:65535": { hostname: "foo", port: 65535 },
      "127.0.0.1:42": { hostname: "127.0.0.1", port: 42 },
      "[2001:db8::1]:42": { hostname: "2001:db8::1", port: "42" },
    };

    // valid proxy hosts with port
    for (let host in host_map) {
      manual[proxy] = host;

      p[`${proxy}`] = host_map[host].hostname;
      p[`${proxy}Port`] = host_map[host].port;

      deepEqual(p, Proxy.fromJSON(manual));
    }

    // Without a port the default port of the scheme is used
    for (let host of ["foo", "foo:"]) {
      manual[proxy] = host;

      // For socks no default port is available
      p[proxy] = `foo`;
      if (proxy === "socksProxy") {
        p[`${proxy}Port`] = null;
      } else {
        let default_ports = { httpProxy: 80, sslProxy: 443 };

        p[`${proxy}Port`] = default_ports[proxy];
      }

      deepEqual(p, Proxy.fromJSON(manual));
    }
  }

  // missing required socks version
  Assert.throws(
    () => Proxy.fromJSON({ proxyType: "manual", socksProxy: "foo:1234" }),
    /InvalidArgumentError/
  );

  // Bug 1703805: Since Firefox 90 ftpProxy is no longer supported
  Assert.throws(
    () => Proxy.fromJSON({ proxyType: "manual", ftpProxy: "foo:21" }),
    /InvalidArgumentError/
  );

  // noProxy: invalid settings
  for (let noProxy of [true, 42, {}, null, "foo", [true], [42], [{}], [null]]) {
    Assert.throws(
      () => Proxy.fromJSON({ proxyType: "manual", noProxy }),
      /InvalidArgumentError/
    );
  }

  // noProxy: valid settings
  p = new Proxy();
  p.proxyType = "manual";
  for (let noProxy of [[], ["foo"], ["foo", "bar"], ["127.0.0.1"]]) {
    let manual = { proxyType: "manual", noProxy };
    p.noProxy = noProxy;
    deepEqual(p, Proxy.fromJSON(manual));
  }

  // noProxy: IPv6 needs brackets removed
  p = new Proxy();
  p.proxyType = "manual";
  p.noProxy = ["2001:db8::1"];
  let manual = { proxyType: "manual", noProxy: ["[2001:db8::1]"] };
  deepEqual(p, Proxy.fromJSON(manual));
});

add_task(function test_UnhandledPromptBehavior() {
  equal(UnhandledPromptBehavior.Accept, "accept");
  equal(UnhandledPromptBehavior.AcceptAndNotify, "accept and notify");
  equal(UnhandledPromptBehavior.Dismiss, "dismiss");
  equal(UnhandledPromptBehavior.DismissAndNotify, "dismiss and notify");
  equal(UnhandledPromptBehavior.Ignore, "ignore");
});

add_task(function test_Capabilities_ctor() {
  let caps = new Capabilities();
  ok(caps.has("browserName"));
  ok(caps.has("browserVersion"));
  ok(caps.has("platformName"));
  ok(["linux", "mac", "windows", "android"].includes(caps.get("platformName")));
  equal(PageLoadStrategy.Normal, caps.get("pageLoadStrategy"));
  equal(false, caps.get("acceptInsecureCerts"));
  ok(caps.get("timeouts") instanceof Timeouts);
  ok(caps.get("proxy") instanceof Proxy);
  equal(caps.get("setWindowRect"), !AppInfo.isAndroid);
  equal(caps.get("strictFileInteractability"), false);
  equal(caps.get("webSocketUrl"), null);

  equal(false, caps.get("moz:accessibilityChecks"));
  ok(caps.has("moz:buildID"));
  ok(caps.has("moz:debuggerAddress"));
  ok(caps.has("moz:platformVersion"));
  ok(caps.has("moz:processID"));
  ok(caps.has("moz:profile"));
  equal(false, caps.get("moz:useNonSpecCompliantPointerOrigin"));
  equal(true, caps.get("moz:webdriverClick"));
});

add_task(function test_Capabilities_toString() {
  equal("[object Capabilities]", new Capabilities().toString());
});

add_task(function test_Capabilities_toJSON() {
  let caps = new Capabilities();
  let json = caps.toJSON();

  equal(caps.get("browserName"), json.browserName);
  equal(caps.get("browserVersion"), json.browserVersion);
  equal(caps.get("platformName"), json.platformName);
  equal(caps.get("pageLoadStrategy"), json.pageLoadStrategy);
  equal(caps.get("acceptInsecureCerts"), json.acceptInsecureCerts);
  deepEqual(caps.get("proxy").toJSON(), json.proxy);
  deepEqual(caps.get("timeouts").toJSON(), json.timeouts);
  equal(caps.get("setWindowRect"), json.setWindowRect);
  equal(caps.get("strictFileInteractability"), json.strictFileInteractability);
  equal(caps.get("webSocketUrl"), json.webSocketUrl);

  equal(caps.get("moz:accessibilityChecks"), json["moz:accessibilityChecks"]);
  equal(caps.get("moz:buildID"), json["moz:buildID"]);
  equal(caps.get("moz:debuggerAddress"), json["moz:debuggerAddress"]);
  equal(caps.get("moz:platformVersion"), json["moz:platformVersion"]);
  equal(caps.get("moz:processID"), json["moz:processID"]);
  equal(caps.get("moz:profile"), json["moz:profile"]);
  equal(
    caps.get("moz:useNonSpecCompliantPointerOrigin"),
    json["moz:useNonSpecCompliantPointerOrigin"]
  );
  equal(caps.get("moz:webdriverClick"), json["moz:webdriverClick"]);
});

add_task(function test_Capabilities_fromJSON() {
  const { fromJSON } = Capabilities;

  // plain
  for (let typ of [{}, null, undefined]) {
    ok(fromJSON(typ).has("browserName"));
  }
  for (let typ of [true, 42, "foo", []]) {
    Assert.throws(() => fromJSON(typ), /InvalidArgumentError/);
  }

  // matching
  let caps = new Capabilities();

  caps = fromJSON({ acceptInsecureCerts: true });
  equal(true, caps.get("acceptInsecureCerts"));
  caps = fromJSON({ acceptInsecureCerts: false });
  equal(false, caps.get("acceptInsecureCerts"));
  Assert.throws(
    () => fromJSON({ acceptInsecureCerts: "foo" }),
    /InvalidArgumentError/
  );

  for (let strategy of Object.values(PageLoadStrategy)) {
    caps = fromJSON({ pageLoadStrategy: strategy });
    equal(strategy, caps.get("pageLoadStrategy"));
  }
  Assert.throws(
    () => fromJSON({ pageLoadStrategy: "foo" }),
    /InvalidArgumentError/
  );
  Assert.throws(
    () => fromJSON({ pageLoadStrategy: null }),
    /InvalidArgumentError/
  );

  let proxyConfig = { proxyType: "manual" };
  caps = fromJSON({ proxy: proxyConfig });
  equal("manual", caps.get("proxy").proxyType);

  let timeoutsConfig = { implicit: 123 };
  caps = fromJSON({ timeouts: timeoutsConfig });
  equal(123, caps.get("timeouts").implicit);

  if (!AppInfo.isAndroid) {
    caps = fromJSON({ setWindowRect: true });
    equal(true, caps.get("setWindowRect"));
    Assert.throws(
      () => fromJSON({ setWindowRect: false }),
      /InvalidArgumentError/
    );
  } else {
    Assert.throws(
      () => fromJSON({ setWindowRect: true }),
      /InvalidArgumentError/
    );
  }

  caps = fromJSON({ strictFileInteractability: false });
  equal(false, caps.get("strictFileInteractability"));
  caps = fromJSON({ strictFileInteractability: true });
  equal(true, caps.get("strictFileInteractability"));

  caps = fromJSON({ webSocketUrl: true });
  equal(true, caps.get("webSocketUrl"));
  Assert.throws(
    () => fromJSON({ webSocketUrl: false }),
    /InvalidArgumentError/
  );
  Assert.throws(
    () => fromJSON({ webSocketUrl: "foo" }),
    /InvalidArgumentError/
  );

  caps = fromJSON({ "moz:accessibilityChecks": true });
  equal(true, caps.get("moz:accessibilityChecks"));
  caps = fromJSON({ "moz:accessibilityChecks": false });
  equal(false, caps.get("moz:accessibilityChecks"));
  Assert.throws(
    () => fromJSON({ "moz:accessibilityChecks": "foo" }),
    /InvalidArgumentError/
  );
  Assert.throws(
    () => fromJSON({ "moz:accessibilityChecks": 1 }),
    /InvalidArgumentError/
  );

  // capability is always populated with null if remote agent is not listening
  caps = fromJSON({});
  equal(null, caps.get("moz:debuggerAddress"));
  caps = fromJSON({ "moz:debuggerAddress": "foo" });
  equal(null, caps.get("moz:debuggerAddress"));
  caps = fromJSON({ "moz:debuggerAddress": true });
  equal(null, caps.get("moz:debuggerAddress"));

  caps = fromJSON({ "moz:useNonSpecCompliantPointerOrigin": false });
  equal(false, caps.get("moz:useNonSpecCompliantPointerOrigin"));
  caps = fromJSON({ "moz:useNonSpecCompliantPointerOrigin": true });
  equal(true, caps.get("moz:useNonSpecCompliantPointerOrigin"));
  Assert.throws(
    () => fromJSON({ "moz:useNonSpecCompliantPointerOrigin": "foo" }),
    /InvalidArgumentError/
  );
  Assert.throws(
    () => fromJSON({ "moz:useNonSpecCompliantPointerOrigin": 1 }),
    /InvalidArgumentError/
  );

  caps = fromJSON({ "moz:webdriverClick": true });
  equal(true, caps.get("moz:webdriverClick"));
  caps = fromJSON({ "moz:webdriverClick": false });
  equal(false, caps.get("moz:webdriverClick"));
  Assert.throws(
    () => fromJSON({ "moz:webdriverClick": "foo" }),
    /InvalidArgumentError/
  );
  Assert.throws(
    () => fromJSON({ "moz:webdriverClick": 1 }),
    /InvalidArgumentError/
  );
});

// use Proxy.toJSON to test marshal
add_task(function test_marshal() {
  let proxy = new Proxy();

  // drop empty fields
  deepEqual({}, proxy.toJSON());
  proxy.proxyType = "manual";
  deepEqual({ proxyType: "manual" }, proxy.toJSON());
  proxy.proxyType = null;
  deepEqual({}, proxy.toJSON());
  proxy.proxyType = undefined;
  deepEqual({}, proxy.toJSON());

  // iterate over object literals
  proxy.proxyType = { foo: "bar" };
  deepEqual({ proxyType: { foo: "bar" } }, proxy.toJSON());

  // iterate over complex object that implement toJSON
  proxy.proxyType = new Proxy();
  deepEqual({}, proxy.toJSON());
  proxy.proxyType.proxyType = "manual";
  deepEqual({ proxyType: { proxyType: "manual" } }, proxy.toJSON());

  // drop objects with no entries
  proxy.proxyType = { foo: {} };
  deepEqual({}, proxy.toJSON());
  proxy.proxyType = { foo: new Proxy() };
  deepEqual({}, proxy.toJSON());
});