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.
7 var selectedTest = location.hash.slice(1);
9 // The below *Failures() function are called with no configuration created.
10 function createConfigFailures() {
11 chrome.vpnProvider.createConfig('some config name', function() {
12 chrome.test.assertEq(chrome.runtime.lastError, undefined);
13 chrome.vpnProvider.createConfig('some config name', function() {
14 chrome.test.assertEq('Name not unique.',
15 chrome.runtime.lastError.message);
16 chrome.vpnProvider.createConfig('', function() {
17 chrome.test.assertEq('Empty name not supported.',
18 chrome.runtime.lastError.message);
19 chrome.test.succeed();
25 function destroyConfigFailures() {
26 chrome.vpnProvider.destroyConfig('nonexistent', function() {
27 chrome.test.assertEq('Unauthorized access.',
28 chrome.runtime.lastError.message);
29 chrome.test.succeed();
33 function setParameterFailures() {
35 "Address CIDR sanity check failed.",
36 "DNS server IP sanity check failed.",
37 // If none of the above errors are thrown, the API will throw the below
38 // error because of the missing 'connected' message from the platform.
39 "Unauthorized access."
41 // First entry in each element is an index into the |errors| array.
42 // Second entry is the input to the address entry in parameters passed to
43 // chrome.vpnProvider.setParameters API.
44 // Third entry is the input to the dnsServers entry in parameters passed to
45 // chrome.vpnProvider.setParameters API.
47 [0, "1+++", ""], // + not allowed
48 [0, "1", ""], // 3 dots and separator missing
49 [0, "1..", ""], // A dot and separator missing
50 [0, "1...", ""], // Separator missing
51 [0, "1.../", ""], // No digit after separator in address
52 [1, "1.../0", ""], // Address passes sanity check, DNS incorrect
53 [1, "1.../0", "1.../"], // DNS is not CIDR
54 [2, "1.../0", "1..."], // Passes sanity checks for IPv4
55 [0, ".../", "..."], // Address has no digits
56 [0, "0.../", "..."], // Address has no digits post separator
57 [1, "0.../0", "..."], // Address passes sanity check, DNS incorrect
58 [2, "0.../0", "...0"], // Passes sanity checks for IPv4
59 [0, "1...:::/1279abe", ""], // : not allowed for ipv4
60 [0, "1.../1279abcde", ""], // Hex not allowed after separator
61 [0, "1...abcde/1279", ""], // Hex not allowed in ipv4
62 [1, "1.../1279", ""], // Address passes sanity check, DNS incorrect
63 [2, "1.../1279", "1..."], // Passes sanity checks for IPv4
64 [0, "1--++", ""], // + and - not supported
65 [0, "1.1.1.1", ""], // Missing separator
66 [0, "1.1.1.1/", ""], // No digits after separator in address
67 [1, "1.1.1.1/1", ""], // Address passes sanity check, DNS incorrect
68 [2, "1.1.1.1/1", "1.1.1.1"], // Passes sanity checks for IPv4
69 [0, "1.1.1./e", "1.1.1."], // Hex not okay in ipv4
70 [2, "1.1.1./0", "1.1.1."], // Passes sanity checks for IPv4
71 [1, "1.../1279", "..."], // No digits in DNS
72 [1, "1.../1279", "e..."], // Hex not allowed in ipv4
73 [2, "1.../1279", "4..."], // Passes sanity checks for IPv4
76 function recurse(index) {
77 if (index >= argsList.length) {
78 chrome.test.succeed();
81 var args = argsList[index];
82 var error = errors[args[0]];
89 chrome.vpnProvider.setParameters(params, function() {
90 chrome.test.assertEq(error, chrome.runtime.lastError.message,
91 'Test ' + index + ' failed');
99 function sendPacketFailures() {
100 var data1 = new ArrayBuffer(1);
101 chrome.vpnProvider.sendPacket(data1, function() {
102 chrome.test.assertEq('Unauthorized access.',
103 chrome.runtime.lastError.message);
104 chrome.test.succeed();
108 function notifyConnectionStateChangedFailures() {
109 chrome.vpnProvider.notifyConnectionStateChanged('connected', function() {
110 chrome.test.assertEq('Unauthorized access.',
111 chrome.runtime.lastError.message);
112 chrome.vpnProvider.notifyConnectionStateChanged('failure', function() {
113 chrome.test.assertEq('Unauthorized access.',
114 chrome.runtime.lastError.message);
115 chrome.test.succeed();
120 function createDestroyRace() {
121 chrome.vpnProvider.createConfig('test-config', function() {});
122 chrome.vpnProvider.destroyConfig('test-config', function() {
123 // Depending upon who wins the race either destroyConfig succeeds or a
124 // 'Pending create.' error is returned.
125 if (chrome.runtime.lastError) {
126 chrome.test.assertEq('Pending create.', chrome.runtime.lastError.message);
128 chrome.test.succeed();
132 function destroyCreateRace() {
133 chrome.vpnProvider.createConfig('test-config1', function() {
134 chrome.test.assertEq(chrome.runtime.lastError, undefined);
135 chrome.vpnProvider.destroyConfig('test-config1', function() {});
136 chrome.vpnProvider.createConfig('test-config1', function() {
137 chrome.test.assertEq(chrome.runtime.lastError, undefined);
138 chrome.test.succeed();
144 comboSuite: function() {
146 createConfigFailures,
147 destroyConfigFailures,
148 setParameterFailures,
150 notifyConnectionStateChangedFailures,
154 chrome.test.runTests(tests);
156 createConfigSuccess: function() {
157 chrome.vpnProvider.createConfig('testconfig', function() {
158 chrome.test.assertEq(chrome.runtime.lastError, undefined);
159 chrome.test.succeed();
162 createConfigConnectAndDisconnect: function() {
163 // The test sets up a set of listeners and creates a config.
164 // The created config is connected to by the C++ side, which initiates the
165 // VPN connection routine. When the routine is complete, a few data packets
166 // are exchanged between the C++ side and the JS side. After this the C++
167 // side sends a disconnect message which ends the test.
168 var expectDisconnect = false;
169 chrome.vpnProvider.onPacketReceived.addListener(function(data) {
170 chrome.test.assertEq(chrome.runtime.lastError, undefined);
171 // The variable packet contains the string 'deadbeef'.
172 var packet = new Uint8Array([100, 101, 97, 100, 98, 101, 101, 102]);
173 chrome.test.assertEq(packet, new Uint8Array(data));
174 chrome.test.succeed();
176 var onNotifyComplete = function() {
177 chrome.test.assertEq(chrome.runtime.lastError, undefined);
178 chrome.vpnProvider.sendPacket(new ArrayBuffer(0), function() {
179 chrome.test.assertEq(chrome.runtime.lastError.message,
180 "Can't send an empty packet.");
181 // The variable packet contains the string 'feebdaed'.
182 var packet = new Uint8Array([102, 101, 101, 98, 100, 97, 101, 100]);
183 chrome.vpnProvider.sendPacket(packet.buffer, function() {
184 chrome.test.assertEq(chrome.runtime.lastError, undefined);
185 expectDisconnect = true;
186 chrome.test.succeed();
190 var onSetParameterComplete = function() {
191 chrome.test.assertEq(chrome.runtime.lastError, undefined);
192 chrome.vpnProvider.notifyConnectionStateChanged('connected',
195 chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
197 chrome.test.assertEq(config_name, 'testconfig');
198 if (expectDisconnect) {
199 chrome.test.assertEq(message, 'disconnected');
200 // After disconnect authorization failures should happen.
201 chrome.test.runTests([
202 setParameterFailures,
204 notifyConnectionStateChangedFailures,
207 chrome.test.assertEq(message, 'connected');
209 address: "10.10.10.10/24",
210 exclusionList: ["63.145.213.129/32", "63.145.212.0/24"],
211 inclusionList: ["0.0.0.0/0", "63.145.212.128/25"],
213 broadcastAddress: "10.10.10.255",
214 domainSearch: ["foo", "bar"],
215 dnsServers: ["8.8.8.8"]
217 chrome.vpnProvider.setParameters(params, onSetParameterComplete);
220 chrome.vpnProvider.createConfig('testconfig', function() {
221 chrome.test.assertEq(chrome.runtime.lastError, undefined);
222 chrome.test.succeed();
225 configInternalRemove: function() {
226 chrome.vpnProvider.createConfig('testconfig', function() {
227 chrome.test.assertEq(chrome.runtime.lastError, undefined);
228 chrome.vpnProvider.onConfigRemoved.addListener(function(name) {
229 chrome.test.assertEq(chrome.runtime.lastError, undefined);
230 chrome.test.assertEq('testconfig', name);
231 chrome.test.succeed();
233 chrome.test.succeed();
236 destroyConnectedConfigSetup: function() {
237 chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
239 chrome.test.assertEq(message, 'disconnected');
240 chrome.test.succeed();
242 chrome.test.succeed();
244 createConfigWithoutNetworkProfile: function() {
245 chrome.vpnProvider.createConfig('exists', function() {
246 chrome.test.assertEq(
247 'No user profile for unshared network configuration.',
248 chrome.runtime.lastError.message);
249 chrome.test.succeed();
252 expectEvents: function() {
253 // The variable |i| is used to verify the order in which events are fired.
255 chrome.vpnProvider.createConfig('testconfig', function() {
256 chrome.test.assertEq(chrome.runtime.lastError, undefined);
257 chrome.test.succeed();
259 chrome.vpnProvider.onPlatformMessage.addListener(function(config_name,
261 chrome.test.assertEq(i, 0);
262 chrome.test.assertEq(config_name, 'testconfig');
263 chrome.test.assertEq(message, 'error');
264 chrome.test.assertEq(error, 'error_message');
267 chrome.vpnProvider.onUIEvent.addListener(function(event, id) {
268 if (event == 'showAddDialog') {
269 chrome.test.assertEq(i, 1);
270 chrome.test.assertEq(id, '');
273 chrome.test.assertEq(i, 2);
274 chrome.test.assertEq(event, 'showConfigureDialog');
275 chrome.test.assertEq(id, 'testconfig');
276 chrome.test.succeed();
280 destroyConfigSuccess: function() {
281 chrome.vpnProvider.destroyConfig('testconfig', function() {
282 chrome.test.assertEq(chrome.runtime.lastError, undefined);
283 chrome.test.succeed();
288 testRoutines[selectedTest]();