Repository navigation
Expand file tree
/
Copy pathLocalTests.cs
More file actions
453 lines (398 loc) · 19.4 KB
/
Copy pathLocalTests.cs
File metadata and controls
453 lines (398 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using System;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using NUnit.Framework;
using BrowserStack;
using System.Collections.Generic;
using Moq;
using System.IO;
namespace BrowserStack_Unit_Tests
{
[TestClass]
public class LocalTests
{
static readonly string logAbsolute = Path.Combine(Directory.GetCurrentDirectory(), "local.log");
private string user = "";
private string accessKey = "";
private LocalClass local;
private List<KeyValuePair<String, String>> options;
public class LocalClass : Local
{
public void setTunnel(BrowserStackTunnel tunnel)
{
this.tunnel = tunnel;
}
}
[TestMethod]
public void TestThrowsWithNoAccessKey()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", ""));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
Assert.Throws(typeof(Exception),
new TestDelegate(startWithOptions),
"BROWSERSTACK_ACCESS_KEY cannot be empty. Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
local.stop();
}
[TestMethod]
public void TestWorksWithAccessKeyInOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
Assert.DoesNotThrow(new TestDelegate(startWithOptions),
"BROWSERSTACK_ACCESS_KEY cannot be empty. Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute, "--source") && StartsWithAny(a, "c-sharp:"))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksWithAccessKeyNotInOptions()
{
Environment.SetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY", "envDummyKey");
options = new List<KeyValuePair<string, string>>();
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("envDummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
Assert.DoesNotThrow(new TestDelegate(startWithOptions),
"BROWSERSTACK_ACCESS_KEY cannot be empty. Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute))), Times.Once());
tunnelMock.Verify(mock => mock.Run("envDummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksForFolderTesting()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("f", "dummyFolderPath"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "dummyFolderPath", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "dummyFolderPath", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksForBinaryPath()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("binarypath", "dummyPath"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath("dummyPath", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksWithBooleanOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("v", "true"));
options.Add(new KeyValuePair<string, string>("force", "true"));
options.Add(new KeyValuePair<string, string>("forcelocal", "true"));
options.Add(new KeyValuePair<string, string>("forceproxy", "true"));
options.Add(new KeyValuePair<string, string>("onlyAutomate", "true"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath("", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-vvv", "-force", "-forcelocal", "-forceproxy", "-onlyAutomate"))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksWithValueOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("localIdentifier", "dummyIdentifier"));
options.Add(new KeyValuePair<string, string>("hosts", "dummyHost"));
options.Add(new KeyValuePair<string, string>("proxyHost", "dummyHost"));
options.Add(new KeyValuePair<string, string>("proxyPort", "dummyPort"));
options.Add(new KeyValuePair<string, string>("proxyUser", "dummyUser"));
options.Add(new KeyValuePair<string, string>("proxyPass", "dummyPass"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock =>mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath("", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-localIdentifier", "dummyIdentifier", "dummyHost", "-proxyHost", "dummyHost",
"-proxyPort", "dummyPort", "-proxyUser", "dummyUser", "-proxyPass", "dummyPass"))
), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestWorksWithCustomOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("customBoolKey1", "true"));
options.Add(new KeyValuePair<string, string>("customBoolKey2", "false"));
options.Add(new KeyValuePair<string, string>("customKey1", "customValue1"));
options.Add(new KeyValuePair<string, string>("customKey2", "customValue2"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath("", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-customBoolKey1", "-customBoolKey2", "-customKey1", "customValue1",
"-customKey2", "customValue2"))
), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestCallsFallbackOnFailure()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
local = new LocalClass();
int count = 0;
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start")).Callback(() =>
{
count++;
if (count == 1)
throw new ApplicationException();
});
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath("", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Exactly(2));
tunnelMock.Verify(mock => mock.fallbackPaths(), Times.Once());
local.stop();
}
[TestMethod]
public void TestKillsTunnel()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
local.stop();
tunnelMock.Verify(mock => mock.addBinaryPath("", "", It.IsAny<bool>(), It.IsAny<Exception>()), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", logAbsolute))), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
}
[TestMethod]
public void TestSetProxyCalledWithProxyOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("proxyHost", "proxy.example.com"));
options.Add(new KeyValuePair<string, string>("proxyPort", "8080"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.SetProxy("proxy.example.com", 8080), Times.Once);
local.stop();
}
[TestMethod]
public void TestSetProxyCalledWithDefaultsWhenAbsent()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.SetProxy(null, 0), Times.Once);
local.stop();
}
[TestMethod]
public void TestSetProxyIgnoresInvalidPort()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("proxyHost", "proxy.example.com"));
options.Add(new KeyValuePair<string, string>("proxyPort", "not-a-number"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.SetProxy("proxy.example.com", 0), Times.Once);
local.stop();
}
// ---- argv helpers -------------------------------------------------------
// Arguments are now discrete argv elements rather than one concatenated string,
// so assertions match elements in order instead of matching a regex.
private static bool InOrder(List<string> actual, params string[] expected)
{
int idx = 0;
foreach (string e in expected)
{
idx = actual.IndexOf(e, idx);
if (idx < 0) return false;
idx++;
}
return true;
}
private static bool StartsWithAny(List<string> actual, string prefix)
{
return actual.Exists(a => a != null && a.StartsWith(prefix));
}
// ---- regression tests: CWE-88 argument injection ------------------------
// Each of these fails on the pre-fix code, where every value was concatenated
// into one string that Process.Start then re-tokenised on whitespace.
[TestMethod]
public void TestOptionValueWithSpacesStaysOneArgument()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("proxyPass", "p@ss --proxy evil.example.com"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-proxyPass", "p@ss --proxy evil.example.com")
&& !a.Contains("--proxy"))), Times.Once());
local.stop();
}
[TestMethod]
public void TestUnknownOptionValueWithSpacesStaysOneArgument()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("customKey", "legit --config /tmp/attacker.cfg"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-customKey", "legit --config /tmp/attacker.cfg")
&& !a.Contains("--config"))), Times.Once());
local.stop();
}
[TestMethod]
public void TestLogFilePathWithQuoteStaysOneArgument()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("logfile", "/tmp/x\" --proxy evil.example.com \""));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-logFile", "/tmp/x\" --proxy evil.example.com \"")
&& !a.Contains("--proxy"))), Times.Once());
local.stop();
}
[TestMethod]
public void TestAccessKeyWhitespaceIsStrippedFromOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", " dummy Key --proxy evil.example.com "));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
// Whitespace removed, so no "--proxy" token can split out of the key.
tunnelMock.Verify(mock => mock.Run("dummyKey--proxyevil.example.com", "", logAbsolute, "start"),
Times.Once());
local.stop();
}
[TestMethod]
public void TestAccessKeyWhitespaceIsStrippedFromEnvironmentVariable()
{
Environment.SetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY", "env Dummy\tKey");
options = new List<KeyValuePair<string, string>>();
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.Run("envDummyKey", "", logAbsolute, "start"), Times.Once());
local.stop();
}
[TestMethod]
public void TestFolderPathWithSpacesIsPreserved()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("f", "/my/awesome folder"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.Run("dummyKey", "/my/awesome folder", logAbsolute, "start"),
Times.Once());
local.stop();
}
[TestMethod]
public void TestDocumentedPassThroughOptionsStillWork()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("localProxyHost", "127.0.0.1"));
options.Add(new KeyValuePair<string, string>("localProxyPort", "8000"));
options.Add(new KeyValuePair<string, string>("-pac-file", "/tmp/my proxy.pac"));
local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>("test-user-agent");
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.Is<List<string>>(a =>
InOrder(a, "-localProxyHost", "127.0.0.1", "-localProxyPort", "8000",
"--pac-file", "/tmp/my proxy.pac"))), Times.Once());
local.stop();
}
public void startWithOptions()
{
local.start(options);
}
[TestInitialize]
public void beforeEveryTest()
{
user = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
Environment.SetEnvironmentVariable("BROWSERSTACK_USERNAME", "");
Environment.SetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY", "");
}
[TestCleanup]
public void afterEveryTest()
{
Environment.SetEnvironmentVariable("BROWSERSTACK_USERNAME", user);
Environment.SetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY", accessKey);
}
}
}