PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/test/data/extensions/api_test/vpn_provider/basic.js

https://github.com/chromium/chromium
JavaScript | 289 lines | 254 code | 13 blank | 22 comment | 8 complexity | 32042b1a26a4a78bd2c782c80befaf68 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. 'use strict';
  5. var selectedTest = location.hash.slice(1);
  6. // The below *Failures() function are called with no configuration created.
  7. function createConfigFailures() {
  8. chrome.vpnProvider.createConfig('some config name', function() {
  9. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  10. chrome.vpnProvider.createConfig('some config name', function() {
  11. chrome.test.assertEq('Name not unique.',
  12. chrome.runtime.lastError.message);
  13. chrome.vpnProvider.createConfig('', function() {
  14. chrome.test.assertEq('Empty name not supported.',
  15. chrome.runtime.lastError.message);
  16. chrome.test.succeed();
  17. });
  18. });
  19. });
  20. }
  21. function destroyConfigFailures() {
  22. chrome.vpnProvider.destroyConfig('nonexistent', function() {
  23. chrome.test.assertEq('Unauthorized access.',
  24. chrome.runtime.lastError.message);
  25. chrome.test.succeed();
  26. });
  27. }
  28. function setParameterFailures() {
  29. var errors = [
  30. "Address CIDR sanity check failed.",
  31. "DNS server IP sanity check failed.",
  32. // If none of the above errors are thrown, the API will throw the below
  33. // error because of the missing 'connected' message from the platform.
  34. "Unauthorized access."
  35. ];
  36. // First entry in each element is an index into the |errors| array.
  37. // Second entry is the input to the address entry in parameters passed to
  38. // chrome.vpnProvider.setParameters API.
  39. // Third entry is the input to the dnsServers entry in parameters passed to
  40. // chrome.vpnProvider.setParameters API.
  41. var argsList = [
  42. [0, "1+++", ""], // + not allowed
  43. [0, "1", ""], // 3 dots and separator missing
  44. [0, "1..", ""], // A dot and separator missing
  45. [0, "1...", ""], // Separator missing
  46. [0, "1.../", ""], // No digit after separator in address
  47. [1, "1.../0", ""], // Address passes sanity check, DNS incorrect
  48. [1, "1.../0", "1.../"], // DNS is not CIDR
  49. [2, "1.../0", "1..."], // Passes sanity checks for IPv4
  50. [0, ".../", "..."], // Address has no digits
  51. [0, "0.../", "..."], // Address has no digits post separator
  52. [1, "0.../0", "..."], // Address passes sanity check, DNS incorrect
  53. [2, "0.../0", "...0"], // Passes sanity checks for IPv4
  54. [0, "1...:::/1279abe", ""], // : not allowed for ipv4
  55. [0, "1.../1279abcde", ""], // Hex not allowed after separator
  56. [0, "1...abcde/1279", ""], // Hex not allowed in ipv4
  57. [1, "1.../1279", ""], // Address passes sanity check, DNS incorrect
  58. [2, "1.../1279", "1..."], // Passes sanity checks for IPv4
  59. [0, "1--++", ""], // + and - not supported
  60. [0, "1.1.1.1", ""], // Missing separator
  61. [0, "1.1.1.1/", ""], // No digits after separator in address
  62. [1, "1.1.1.1/1", ""], // Address passes sanity check, DNS incorrect
  63. [2, "1.1.1.1/1", "1.1.1.1"], // Passes sanity checks for IPv4
  64. [0, "1.1.1./e", "1.1.1."], // Hex not okay in ipv4
  65. [2, "1.1.1./0", "1.1.1."], // Passes sanity checks for IPv4
  66. [1, "1.../1279", "..."], // No digits in DNS
  67. [1, "1.../1279", "e..."], // Hex not allowed in ipv4
  68. [2, "1.../1279", "4..."], // Passes sanity checks for IPv4
  69. ];
  70. function recurse(index) {
  71. if (index >= argsList.length) {
  72. chrome.test.succeed();
  73. return;
  74. }
  75. var args = argsList[index];
  76. var error = errors[args[0]];
  77. var params = {
  78. address: args[1],
  79. exclusionList: [],
  80. inclusionList: [],
  81. dnsServers: [args[2]]
  82. };
  83. chrome.vpnProvider.setParameters(params, function() {
  84. chrome.test.assertEq(error, chrome.runtime.lastError.message,
  85. 'Test ' + index + ' failed');
  86. recurse(index + 1);
  87. });
  88. }
  89. recurse(0);
  90. }
  91. function sendPacketFailures() {
  92. var data1 = new ArrayBuffer(1);
  93. chrome.vpnProvider.sendPacket(data1, function() {
  94. chrome.test.assertEq('Unauthorized access.',
  95. chrome.runtime.lastError.message);
  96. chrome.test.succeed();
  97. });
  98. }
  99. function notifyConnectionStateChangedFailures() {
  100. chrome.vpnProvider.notifyConnectionStateChanged('connected', function() {
  101. chrome.test.assertEq('Unauthorized access.',
  102. chrome.runtime.lastError.message);
  103. chrome.vpnProvider.notifyConnectionStateChanged('failure', function() {
  104. chrome.test.assertEq('Unauthorized access.',
  105. chrome.runtime.lastError.message);
  106. chrome.test.succeed();
  107. });
  108. });
  109. }
  110. function createDestroyRace() {
  111. chrome.vpnProvider.createConfig('test-config', function() {});
  112. chrome.vpnProvider.destroyConfig('test-config', function() {
  113. // Depending upon who wins the race either destroyConfig succeeds or a
  114. // 'Pending create.' error is returned.
  115. if (chrome.runtime.lastError) {
  116. chrome.test.assertEq('Pending create.', chrome.runtime.lastError.message);
  117. }
  118. chrome.test.succeed();
  119. });
  120. }
  121. function destroyCreateRace() {
  122. chrome.vpnProvider.createConfig('test-config1', function() {
  123. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  124. chrome.vpnProvider.destroyConfig('test-config1', function() {});
  125. chrome.vpnProvider.createConfig('test-config1', function() {
  126. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  127. chrome.test.succeed();
  128. });
  129. });
  130. }
  131. var testRoutines = {
  132. comboSuite: function() {
  133. var tests = [
  134. createConfigFailures,
  135. destroyConfigFailures,
  136. setParameterFailures,
  137. sendPacketFailures,
  138. notifyConnectionStateChangedFailures,
  139. createDestroyRace,
  140. destroyCreateRace
  141. ];
  142. chrome.test.runTests(tests);
  143. },
  144. createConfigSuccess: function() {
  145. chrome.vpnProvider.createConfig('testconfig', function() {
  146. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  147. chrome.test.succeed();
  148. });
  149. },
  150. createConfigConnectAndDisconnect: function() {
  151. // The test sets up a set of listeners and creates a config.
  152. // The created config is connected to by the C++ side, which initiates the
  153. // VPN connection routine. When the routine is complete, a few data packets
  154. // are exchanged between the C++ side and the JS side. After this the C++
  155. // side sends a disconnect message which ends the test.
  156. var expectDisconnect = false;
  157. chrome.vpnProvider.onPacketReceived.addListener(function(data) {
  158. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  159. // The variable packet contains the string 'deadbeef'.
  160. var packet = new Uint8Array([100, 101, 97, 100, 98, 101, 101, 102]);
  161. chrome.test.assertEq(packet, new Uint8Array(data));
  162. chrome.test.succeed();
  163. });
  164. var onNotifyComplete = function() {
  165. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  166. chrome.vpnProvider.sendPacket(new ArrayBuffer(0), function() {
  167. chrome.test.assertEq(chrome.runtime.lastError.message,
  168. "Can't send an empty packet.");
  169. // The variable packet contains the string 'feebdaed'.
  170. var packet = new Uint8Array([102, 101, 101, 98, 100, 97, 101, 100]);
  171. chrome.vpnProvider.sendPacket(packet.buffer, function() {
  172. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  173. expectDisconnect = true;
  174. chrome.test.succeed();
  175. });
  176. });
  177. };
  178. var onSetParameterComplete = function() {
  179. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  180. chrome.vpnProvider.notifyConnectionStateChanged('connected',
  181. onNotifyComplete);
  182. };
  183. chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
  184. message, error) {
  185. chrome.test.assertEq(config_name, 'testconfig');
  186. if (expectDisconnect) {
  187. chrome.test.assertEq(message, 'disconnected');
  188. // After disconnect authorization failures should happen.
  189. chrome.test.runTests([
  190. setParameterFailures,
  191. sendPacketFailures,
  192. notifyConnectionStateChangedFailures,
  193. ]);
  194. } else {
  195. chrome.test.assertEq(message, 'connected');
  196. var params = {
  197. address: "10.10.10.10/24",
  198. exclusionList: ["63.145.213.129/32", "63.145.212.0/24"],
  199. inclusionList: ["0.0.0.0/0", "63.145.212.128/25"],
  200. mtu: "1600",
  201. broadcastAddress: "10.10.10.255",
  202. domainSearch: ["foo", "bar"],
  203. dnsServers: ["8.8.8.8"],
  204. reconnect: "false"
  205. };
  206. chrome.vpnProvider.setParameters(params, onSetParameterComplete);
  207. }
  208. });
  209. chrome.vpnProvider.createConfig('testconfig', function() {
  210. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  211. chrome.test.succeed();
  212. });
  213. },
  214. configInternalRemove: function() {
  215. chrome.vpnProvider.createConfig('testconfig', function() {
  216. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  217. chrome.vpnProvider.onConfigRemoved.addListener(function(name) {
  218. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  219. chrome.test.assertEq('testconfig', name);
  220. chrome.test.succeed();
  221. });
  222. chrome.test.succeed();
  223. });
  224. },
  225. destroyConnectedConfigSetup: function() {
  226. chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
  227. message, error) {
  228. chrome.test.assertEq(message, 'disconnected');
  229. chrome.test.succeed();
  230. });
  231. chrome.test.succeed();
  232. },
  233. createConfigWithoutNetworkProfile: function() {
  234. chrome.vpnProvider.createConfig('exists', function() {
  235. chrome.test.assertEq(
  236. 'No user profile for unshared network configuration.',
  237. chrome.runtime.lastError.message);
  238. chrome.test.succeed();
  239. });
  240. },
  241. expectEvents: function() {
  242. // The variable |i| is used to verify the order in which events are fired.
  243. var i = 0;
  244. chrome.vpnProvider.createConfig('testconfig', function() {
  245. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  246. chrome.test.succeed();
  247. });
  248. chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
  249. message, error) {
  250. chrome.test.assertEq(i, 0);
  251. chrome.test.assertEq(config_name, 'testconfig');
  252. chrome.test.assertEq(message, 'error');
  253. chrome.test.assertEq(error, 'error_message');
  254. i++;
  255. });
  256. chrome.vpnProvider.onUIEvent.addListener(function(event, id) {
  257. if (event == 'showAddDialog') {
  258. chrome.test.assertEq(i, 1);
  259. chrome.test.assertEq(id, '');
  260. i++;
  261. } else {
  262. chrome.test.assertEq(i, 2);
  263. chrome.test.assertEq(event, 'showConfigureDialog');
  264. chrome.test.assertEq(id, 'testconfig');
  265. chrome.test.succeed();
  266. }
  267. });
  268. },
  269. destroyConfigSuccess: function() {
  270. chrome.vpnProvider.destroyConfig('testconfig', function() {
  271. chrome.test.assertEq(chrome.runtime.lastError, undefined);
  272. chrome.test.succeed();
  273. });
  274. },
  275. };
  276. testRoutines[selectedTest]();