Bug 1355471 - Test timeout field before value's typing; r?whimboo draft
authorAndreas Tolfsen <ato@mozilla.com>
Tue, 11 Apr 2017 14:57:01 +0100
changeset 560492 753feb54427f982aa83bd11ddddc57a54133c4d3
parent 560424 f914d40a48009c5acd1093e9939cc0ec035696dd
child 623716 2dfd62bce7a43b5f03c25d0d6976cfff8e6479eb
push id53435
push userbmo:ato@mozilla.com
push dateTue, 11 Apr 2017 14:01:43 +0000
reviewerswhimboo
bugs1355471
milestone55.0a1
Bug 1355471 - Test timeout field before value's typing; r?whimboo The error message returned when unmarshalling the timeout configuration object with invalid input is misleading, because it checks the typing of the value before the field name. This patch changes Marionette to run the type assertion for the value after each case in the switch statement has been evaluated, ensuring that the field is valid before asserting its value. It also adds a few unit tests to verify this behaviour. Fixes: https://github.com/mozilla/geckodriver/issues/633 MozReview-Commit-ID: LVjTyUacD0s
testing/marionette/session.js
testing/marionette/test_session.js
--- a/testing/marionette/session.js
+++ b/testing/marionette/session.js
@@ -48,33 +48,31 @@ session.Timeouts = class {
     };
   }
 
   static fromJSON (json) {
     assert.object(json);
     let t = new session.Timeouts();
 
     for (let [typ, ms] of Object.entries(json)) {
-      assert.positiveInteger(ms);
-
       switch (typ) {
         case "implicit":
-          t.implicit = ms;
+          t.implicit = assert.positiveInteger(ms);
           break;
 
         case "script":
-          t.script = ms;
+          t.script = assert.positiveInteger(ms);
           break;
 
         case "pageLoad":
-          t.pageLoad = ms;
+          t.pageLoad = assert.positiveInteger(ms);
           break;
 
         default:
-          throw new InvalidArgumentError();
+          throw new InvalidArgumentError("Unrecognised timeout: " + typ);
       }
     }
 
     return t;
   }
 };
 
 /** Enum of page loading strategies. */
--- a/testing/marionette/test_session.js
+++ b/testing/marionette/test_session.js
@@ -43,16 +43,53 @@ add_test(function test_Timeouts_fromJSON
   let ts = session.Timeouts.fromJSON(json);
   equal(ts.implicit, json.implicit);
   equal(ts.pageLoad, json.pageLoad);
   equal(ts.script, json.script);
 
   run_next_test();
 });
 
+add_test(function test_Timeouts_fromJSON_unrecognised_field() {
+  let json = {
+    sessionId: "foobar",
+    script: 42,
+  };
+  try {
+    session.Timeouts.fromJSON(json);
+  } catch (e) {
+    equal(e.name, InvalidArgumentError.name);
+    equal(e.message, "Unrecognised timeout: sessionId");
+  }
+
+  run_next_test();
+});
+
+add_test(function test_Timeouts_fromJSON_invalid_type() {
+  try {
+    session.Timeouts.fromJSON({script: "foobar"});
+  } catch (e) {
+    equal(e.name, InvalidArgumentError.name);
+    equal(e.message, "Expected [object String] \"foobar\" to be an integer");
+  }
+
+  run_next_test();
+});
+
+add_test(function test_Timeouts_fromJSON_bounds() {
+  try {
+    session.Timeouts.fromJSON({script: -42});
+  } catch (e) {
+    equal(e.name, InvalidArgumentError.name);
+    equal(e.message, "Expected [object Number] -42 to be >= 0");
+  }
+
+  run_next_test();
+});
+
 add_test(function test_PageLoadStrategy() {
   equal(session.PageLoadStrategy.None, "none");
   equal(session.PageLoadStrategy.Eager, "eager");
   equal(session.PageLoadStrategy.Normal, "normal");
 
   run_next_test();
 });