Bug 645325 - Part 5: Add stricter type and range checks for JavaScript Coordinates properties. r=garvank r=jdm
authorChris Peterson <cpeterson@mozilla.com>
Sat, 10 Mar 2018 01:50:20 -0800
changeset 422064 d0cd7366b1a23ba8fc9ead34f4fd4281e7383318
parent 422063 7482eb372d941c9e171fade2c32eb5bc44ef97e1
child 422065 5759d2ab032b348f16f7ced9a8e266d98bc6c3a1
push id34114
push userbtara@mozilla.com
push dateSat, 09 Jun 2018 15:31:58 +0000
treeherdermozilla-central@e02a5155d815 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgarvank, jdm
bugs645325
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 645325 - Part 5: Add stricter type and range checks for JavaScript Coordinates properties. r=garvank r=jdm MozReview-Commit-ID: 32XkDhYjIyu
dom/tests/mochitest/geolocation/geolocation_common.js
--- a/dom/tests/mochitest/geolocation/geolocation_common.js
+++ b/dom/tests/mochitest/geolocation/geolocation_common.js
@@ -1,8 +1,10 @@
+"use strict";
+
 var harness = SimpleTest.harnessParameters.testRoot == "chrome" ? "chrome" : "tests";
 var BASE_URL = "http://mochi.test:8888/" + harness + "/dom/tests/mochitest/geolocation/network_geolocation.sjs";
 
 function sleep(delay)
 {
     var start = Date.now();
     while (Date.now() < start + delay);
 }
@@ -59,34 +61,59 @@ function delay_geolocationProvider(delay
 }
 
 function send404_geolocationProvider(callback)
 {
   set_network_request_cache_enabled(false, function() {
     SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=send404"]]}, callback);});
 }
 
-function check_geolocation(location) {
-
+function check_geolocation(location)
+{
   ok(location, "Check to see if this location is non-null");
 
-  ok("timestamp" in location, "Check to see if there is a timestamp");
+  const timestamp = location.timestamp;
+  dump(`timestamp=$timestamp}\n`);
+  ok(IsNumber(timestamp), "check timestamp type");
+  ok(timestamp > 0, "check timestamp range");
 
   // eventually, coords may be optional (eg, when civic addresses are supported)
   ok("coords" in location, "Check to see if this location has a coords");
 
-  var coords = location.coords;
+  const {
+    latitude, longitude, accuracy, altitude, altitudeAccuracy, speed, heading
+  } = location.coords;
+
+  dump(`latitude=${latitude}\n`);
+  dump(`longitude=${longitude}\n`);
+  dump(`accuracy=${accuracy}\n`);
+  dump(`altitude=${altitude}\n`);
+  dump(`altitudeAccuracy=${altitudeAccuracy}\n`);
+  dump(`speed=${speed}\n`);
+  dump(`heading=${heading}\n`);
 
-  ok("latitude" in coords, "Check to see if there is a latitude");
-  ok("longitude" in coords, "Check to see if there is a longitude");
-  ok("accuracy" in coords, "Check to see if there is a accuracy");
-  
-  // optional ok("altitude" in coords, "Check to see if there is a altitude");
-  // optional ok("altitudeAccuracy" in coords, "Check to see if there is a alt accuracy");
-  // optional ok("heading" in coords, "Check to see if there is a heading");
-  // optional ok("speed" in coords, "Check to see if there is a speed");
+  ok(IsNumber(latitude), "check latitude type");
+  ok(IsNumber(longitude), "check longitude type");
+
+  ok(Math.abs(latitude - 37.41857) < 0.001, "latitude matches hard-coded value");
+  ok(Math.abs(longitude + 122.08769) < 0.001, "longitude matches hard-coded value");
 
-  ok (Math.abs(location.coords.latitude - 37.41857) < 0.001, "lat matches known value");
-  ok (Math.abs(location.coords.longitude + 122.08769) < 0.001, "lon matches known value");
-  // optional  ok(location.coords.altitude == 42, "alt matches known value");
-  // optional  ok(location.coords.altitudeAccuracy == 42, "alt acc matches known value");
+  ok(IsNonNegativeNumber(accuracy), "check accuracy type and range");
+  ok(IsNumber(altitude) || altitude === null, "check accuracy type");
+
+  ok((IsNonNegativeNumber(altitudeAccuracy) && IsNumber(altitude)) ||
+     (altitudeAccuracy === null), "check altitudeAccuracy type and range");
+
+  ok(IsNonNegativeNumber(speed) || speed === null, "check speed type and range");
+
+  ok((IsNonNegativeNumber(heading) && heading < 360 && speed > 0) ||
+     heading === null, "check heading type and range");
 }
 
+function IsNumber(x)
+{
+  return typeof(x) === "number" && !Number.isNaN(x);
+}
+
+function IsNonNegativeNumber(x)
+{
+  return IsNumber(x) && x >= 0;
+}