test-telemetry.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #!/usr/bin/env node
  2. /* Copyright 2016 Mozilla Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* eslint-disable no-var */
  17. "use strict";
  18. var assert = require("assert");
  19. var fs = require("fs");
  20. var vm = require("vm");
  21. var SRC_DIR = __dirname + "/../../";
  22. var telemetryJsPath = "extensions/chromium/telemetry.js";
  23. var telemetryJsSource = fs.readFileSync(SRC_DIR + telemetryJsPath);
  24. var telemetryScript = new vm.Script(telemetryJsSource, {
  25. filename: telemetryJsPath,
  26. displayErrors: true,
  27. });
  28. var LOG_URL = /LOG_URL = '([^']+)'/.exec(telemetryJsSource)[1];
  29. // Create a minimal extension global that mocks the extension environment that
  30. // is used by telemetry.js
  31. function createExtensionGlobal() {
  32. var window = {};
  33. // Whenever a "request" was made, the extra headers are appended to this list.
  34. var test_requests = (window.test_requests = []);
  35. // Extension API mocks.
  36. window.window = window;
  37. window.chrome = {};
  38. window.chrome.extension = {};
  39. window.chrome.extension.inIncognitoContext = false;
  40. window.chrome.runtime = {};
  41. window.chrome.runtime.id = "oemmndcbldboiebfnladdacbdfmadadm";
  42. window.chrome.runtime.getManifest = function () {
  43. return { version: "1.0.0" };
  44. };
  45. function createStorageAPI() {
  46. var storageArea = {};
  47. storageArea.get = function (key, callback) {
  48. assert.equal(key, "disableTelemetry");
  49. // chrome.storage.*. is async, but we make it synchronous to ease testing.
  50. callback(storageArea.mock_data);
  51. };
  52. return storageArea;
  53. }
  54. window.chrome.storage = {};
  55. window.chrome.storage.managed = createStorageAPI();
  56. window.chrome.storage.local = createStorageAPI();
  57. window.chrome.storage.sync = createStorageAPI();
  58. // Other DOM.
  59. window.navigator = {};
  60. window.navigator.onLine = true;
  61. window.navigator.userAgent =
  62. "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " +
  63. "Chrome/50.0.2661.94 Safari/537.36";
  64. window.localStorage = {};
  65. var getRandomValues_state = 0;
  66. window.crypto = {};
  67. window.crypto.getRandomValues = function (buf) {
  68. var state = getRandomValues_state++;
  69. for (var i = 0; i < buf.length; ++i) {
  70. // Totally random byte ;)
  71. buf[i] = 0x42 + state;
  72. }
  73. return buf;
  74. };
  75. // Network-related mocks.
  76. window.Request = {};
  77. window.Request.prototype = {
  78. get mode() {
  79. throw new TypeError("Illegal invocation");
  80. },
  81. };
  82. window.fetch = function (url, options) {
  83. assert.equal(url, LOG_URL);
  84. assert.equal(options.method, "POST");
  85. assert.equal(options.mode, "cors");
  86. assert.ok(!options.body);
  87. test_requests.push(options.headers);
  88. };
  89. window.Headers = function (headers) {
  90. headers = JSON.parse(JSON.stringify(headers)); // Clone.
  91. Object.keys(headers).forEach(function (k) {
  92. headers[k] = String(headers[k]);
  93. });
  94. return headers;
  95. };
  96. window.XMLHttpRequest = function () {
  97. var invoked = {
  98. open: false,
  99. send: false,
  100. };
  101. var headers = {};
  102. return {
  103. open(method, url) {
  104. assert.equal(invoked.open, false);
  105. invoked.open = true;
  106. assert.equal(method, "POST");
  107. assert.equal(url, LOG_URL);
  108. },
  109. setRequestHeader(k, v) {
  110. assert.equal(invoked.open, true);
  111. headers[k] = String(v);
  112. },
  113. send(body) {
  114. assert.equal(invoked.open, true);
  115. assert.equal(invoked.send, false);
  116. invoked.send = true;
  117. assert.ok(!body);
  118. test_requests.push(headers);
  119. },
  120. };
  121. };
  122. // Time-related logic.
  123. var timers = [];
  124. window.setInterval = function (callback, ms) {
  125. assert.equal(typeof callback, "function");
  126. timers.push(callback);
  127. };
  128. window.Date = {
  129. test_now_value: Date.now(),
  130. now() {
  131. return window.Date.test_now_value;
  132. },
  133. };
  134. window.test_fireTimers = function () {
  135. assert.ok(timers.length);
  136. timers.forEach(function (timer) {
  137. timer();
  138. });
  139. };
  140. return window;
  141. }
  142. // Simulate an update of the browser.
  143. function updateBrowser(window) {
  144. window.navigator.userAgent = window.navigator.userAgent.replace(
  145. /Chrome\/(\d+)/,
  146. function (_, v) {
  147. return "Chrome/" + (parseInt(v) + 1);
  148. }
  149. );
  150. }
  151. var tests = [
  152. function test_first_run() {
  153. // Default settings, run extension for the first time.
  154. var window = createExtensionGlobal();
  155. telemetryScript.runInNewContext(window);
  156. assert.deepEqual(window.test_requests, [
  157. {
  158. "Deduplication-Id": "4242424242",
  159. "Extension-Version": "1.0.0",
  160. },
  161. ]);
  162. },
  163. function test_first_run_incognito() {
  164. // The extension should not send any requests when in incognito mode.
  165. var window = createExtensionGlobal();
  166. window.chrome.extension.inIncognitoContext = true;
  167. telemetryScript.runInNewContext(window);
  168. assert.deepEqual(window.test_requests, []);
  169. },
  170. function test_storage_managed_unavailable() {
  171. var window = createExtensionGlobal();
  172. delete window.chrome.storage.managed;
  173. telemetryScript.runInNewContext(window);
  174. assert.deepEqual(window.test_requests, [
  175. {
  176. "Deduplication-Id": "4242424242",
  177. "Extension-Version": "1.0.0",
  178. },
  179. ]);
  180. },
  181. function test_managed_pref() {
  182. var window = createExtensionGlobal();
  183. window.chrome.storage.managed.mock_data = {
  184. disableTelemetry: true,
  185. };
  186. telemetryScript.runInNewContext(window);
  187. assert.deepEqual(window.test_requests, []);
  188. },
  189. function test_local_pref() {
  190. var window = createExtensionGlobal();
  191. window.chrome.storage.local.mock_data = {
  192. disableTelemetry: true,
  193. };
  194. telemetryScript.runInNewContext(window);
  195. assert.deepEqual(window.test_requests, []);
  196. },
  197. function test_managed_pref_is_overridden() {
  198. var window = createExtensionGlobal();
  199. window.chrome.storage.managed.mock_data = {
  200. disableTelemetry: true,
  201. };
  202. window.chrome.storage.sync.mock_data = {
  203. disableTelemetry: false,
  204. };
  205. telemetryScript.runInNewContext(window);
  206. assert.deepEqual(window.test_requests, [
  207. {
  208. "Deduplication-Id": "4242424242",
  209. "Extension-Version": "1.0.0",
  210. },
  211. ]);
  212. },
  213. function test_run_extension_again() {
  214. var window = createExtensionGlobal();
  215. telemetryScript.runInNewContext(window);
  216. telemetryScript.runInNewContext(window);
  217. // Only one request should be sent because of rate-limiting.
  218. assert.deepEqual(window.test_requests, [
  219. {
  220. "Deduplication-Id": "4242424242",
  221. "Extension-Version": "1.0.0",
  222. },
  223. ]);
  224. // Simulate that quite some hours passed, but it's still rate-limited.
  225. window.Date.test_now_value += 11 * 36e5;
  226. telemetryScript.runInNewContext(window);
  227. // Only one request should be sent because of rate-limiting.
  228. assert.deepEqual(window.test_requests, [
  229. {
  230. "Deduplication-Id": "4242424242",
  231. "Extension-Version": "1.0.0",
  232. },
  233. ]);
  234. // Another hour passes and the request should not be rate-limited any more.
  235. window.Date.test_now_value += 1 * 36e5;
  236. telemetryScript.runInNewContext(window);
  237. assert.deepEqual(window.test_requests, [
  238. {
  239. "Deduplication-Id": "4242424242",
  240. "Extension-Version": "1.0.0",
  241. },
  242. {
  243. "Deduplication-Id": "4242424242",
  244. "Extension-Version": "1.0.0",
  245. },
  246. ]);
  247. },
  248. function test_running_for_a_while() {
  249. var window = createExtensionGlobal();
  250. telemetryScript.runInNewContext(window);
  251. assert.deepEqual(window.test_requests, [
  252. {
  253. "Deduplication-Id": "4242424242",
  254. "Extension-Version": "1.0.0",
  255. },
  256. ]);
  257. // Simulate that the timer fired 11 hours since the last ping. The request
  258. // should still be rate-limited.
  259. window.Date.test_now_value += 11 * 36e5;
  260. window.test_fireTimers();
  261. assert.deepEqual(window.test_requests, [
  262. {
  263. "Deduplication-Id": "4242424242",
  264. "Extension-Version": "1.0.0",
  265. },
  266. ]);
  267. // Another hour passes and the request should not be rate-limited any more.
  268. window.Date.test_now_value += 1 * 36e5;
  269. window.test_fireTimers();
  270. assert.deepEqual(window.test_requests, [
  271. {
  272. "Deduplication-Id": "4242424242",
  273. "Extension-Version": "1.0.0",
  274. },
  275. {
  276. "Deduplication-Id": "4242424242",
  277. "Extension-Version": "1.0.0",
  278. },
  279. ]);
  280. },
  281. function test_browser_update() {
  282. var window = createExtensionGlobal();
  283. telemetryScript.runInNewContext(window);
  284. updateBrowser(window);
  285. telemetryScript.runInNewContext(window);
  286. assert.deepEqual(window.test_requests, [
  287. {
  288. "Deduplication-Id": "4242424242",
  289. "Extension-Version": "1.0.0",
  290. },
  291. {
  292. // Generate a new ID for better privacy.
  293. "Deduplication-Id": "4343434343",
  294. "Extension-Version": "1.0.0",
  295. },
  296. ]);
  297. },
  298. function test_browser_update_between_pref_toggle() {
  299. var window = createExtensionGlobal();
  300. telemetryScript.runInNewContext(window);
  301. window.chrome.storage.local.mock_data = {
  302. disableTelemetry: true,
  303. };
  304. updateBrowser(window);
  305. telemetryScript.runInNewContext(window);
  306. window.chrome.storage.local.mock_data = {
  307. disableTelemetry: false,
  308. };
  309. telemetryScript.runInNewContext(window);
  310. assert.deepEqual(window.test_requests, [
  311. {
  312. "Deduplication-Id": "4242424242",
  313. "Extension-Version": "1.0.0",
  314. },
  315. {
  316. // Generate a new ID for better privacy, even if the update happened
  317. // while telemetry was disabled.
  318. "Deduplication-Id": "4343434343",
  319. "Extension-Version": "1.0.0",
  320. },
  321. ]);
  322. },
  323. function test_extension_update() {
  324. var window = createExtensionGlobal();
  325. telemetryScript.runInNewContext(window);
  326. window.chrome.runtime.getManifest = function () {
  327. return { version: "1.0.1" };
  328. };
  329. window.Date.test_now_value += 12 * 36e5;
  330. telemetryScript.runInNewContext(window);
  331. assert.deepEqual(window.test_requests, [
  332. {
  333. "Deduplication-Id": "4242424242",
  334. "Extension-Version": "1.0.0",
  335. },
  336. {
  337. // The ID did not change because the browser version did not change.
  338. "Deduplication-Id": "4242424242",
  339. "Extension-Version": "1.0.1",
  340. },
  341. ]);
  342. },
  343. function test_unofficial_build() {
  344. var window = createExtensionGlobal();
  345. var didWarn = false;
  346. window.console = {};
  347. window.console.warn = function () {
  348. didWarn = true;
  349. };
  350. window.chrome.runtime.id = "abcdefghijklmnopabcdefghijklmnop";
  351. telemetryScript.runInNewContext(window);
  352. assert.deepEqual(window.test_requests, []);
  353. assert.ok(didWarn);
  354. },
  355. function test_fetch_is_supported() {
  356. var window = createExtensionGlobal();
  357. // XMLHttpRequest should not be called when fetch is available. So removing
  358. // the XMLHttpRequest API should not change behavior.
  359. delete window.XMLHttpRequest;
  360. telemetryScript.runInNewContext(window);
  361. assert.deepEqual(window.test_requests, [
  362. {
  363. "Deduplication-Id": "4242424242",
  364. "Extension-Version": "1.0.0",
  365. },
  366. ]);
  367. },
  368. function test_fetch_not_supported() {
  369. var window = createExtensionGlobal();
  370. delete window.fetch;
  371. delete window.Request;
  372. delete window.Headers;
  373. telemetryScript.runInNewContext(window);
  374. assert.deepEqual(window.test_requests, [
  375. {
  376. "Deduplication-Id": "4242424242",
  377. "Extension-Version": "1.0.0",
  378. },
  379. ]);
  380. },
  381. function test_fetch_mode_not_supported() {
  382. var window = createExtensionGlobal();
  383. delete window.Request.prototype.mode;
  384. window.fetch = function () {
  385. throw new Error("Unexpected call to fetch!");
  386. };
  387. telemetryScript.runInNewContext(window);
  388. assert.deepEqual(window.test_requests, [
  389. {
  390. "Deduplication-Id": "4242424242",
  391. "Extension-Version": "1.0.0",
  392. },
  393. ]);
  394. },
  395. function test_network_offline() {
  396. var window = createExtensionGlobal();
  397. // Simulate that the network is down for sure.
  398. window.navigator.onLine = false;
  399. telemetryScript.runInNewContext(window);
  400. assert.deepEqual(window.test_requests, []);
  401. // Simulate that the network might be up.
  402. window.navigator.onLine = true;
  403. telemetryScript.runInNewContext(window);
  404. assert.deepEqual(window.test_requests, [
  405. {
  406. "Deduplication-Id": "4242424242",
  407. "Extension-Version": "1.0.0",
  408. },
  409. ]);
  410. },
  411. ];
  412. var test_i = 0;
  413. (function next() {
  414. var test = tests[test_i++];
  415. if (!test) {
  416. console.log("All tests completed.");
  417. return;
  418. }
  419. console.log("Running test " + test_i + "/" + tests.length + ": " + test.name);
  420. test();
  421. process.nextTick(next);
  422. })();