public async Task TestConnectSocks4ByDomainAsync() { using (var proxy = new Socks4ProxyListener()) { proxy.Start(IPAddress.Loopback, 0); var socks = new Socks4aClient(proxy.IPAddress.ToString(), proxy.Port); Socket socket = null; try { socket = await socks.ConnectAsync("www.google.com", 80); socket.Disconnect(false); Assert.Fail("Sending a domain to a SOCKS4-only proxy server should have failed."); } catch (ProxyProtocolException) { // This is expected since this proxy does not support Socks4a Assert.Pass($"{socks.ProxyHost} does not support Socks4a."); } catch (TimeoutException) { Assert.Inconclusive("Timed out."); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (socket != null) { socket.Dispose(); } } } }
public async Task TestConnectByIPv4Async() { using (var proxy = new Socks4aProxyListener()) { proxy.Start(IPAddress.Loopback, 0); var socks = new Socks4aClient(proxy.IPAddress.ToString(), proxy.Port); var host = "74.125.197.99"; // ResolveIPv4 ("www.google.com"); Socket socket = null; if (host == null) { return; } try { socket = await socks.ConnectAsync(host, 80, ConnectTimeout); socket.Disconnect(false); } catch (TimeoutException) { Assert.Inconclusive("Timed out."); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (socket != null) { socket.Dispose(); } } } }
public async void TestConnectByIPv4Async() { var socks = new Socks4aClient("100.39.36.100", 58288); var host = "74.125.197.99"; // ResolveIPv4 ("www.google.com"); Socket socket = null; if (host == null) { return; } try { socket = await socks.ConnectAsync(host, 80, 10 * 1000); socket.Disconnect(false); } catch (TimeoutException) { Assert.Inconclusive("Timed out."); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (socket != null) { socket.Dispose(); } } }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("user", "password"); var socks = new Socks4aClient("socks4.proxy.com", 0, credentials); Assert.Throws <ArgumentNullException> (() => new Socks4aClient(null, 1080)); Assert.Throws <ArgumentException> (() => new Socks4aClient(string.Empty, 1080)); Assert.Throws <ArgumentOutOfRangeException> (() => new Socks4aClient(socks.ProxyHost, -1)); Assert.Throws <ArgumentNullException> (() => new Socks4aClient(socks.ProxyHost, 1080, null)); Assert.AreEqual(4, socks.SocksVersion); Assert.AreEqual(1080, socks.ProxyPort); Assert.AreEqual("socks4.proxy.com", socks.ProxyHost); Assert.AreEqual(credentials, socks.ProxyCredentials); Assert.Throws <ArgumentNullException> (() => socks.Connect(null, 80)); Assert.Throws <ArgumentNullException> (() => socks.Connect(null, 80, ConnectTimeout)); Assert.ThrowsAsync <ArgumentNullException> (async() => await socks.ConnectAsync(null, 80)); Assert.ThrowsAsync <ArgumentNullException> (async() => await socks.ConnectAsync(null, 80, ConnectTimeout)); Assert.Throws <ArgumentException> (() => socks.Connect(string.Empty, 80)); Assert.Throws <ArgumentException> (() => socks.Connect(string.Empty, 80, ConnectTimeout)); Assert.ThrowsAsync <ArgumentException> (async() => await socks.ConnectAsync(string.Empty, 80)); Assert.ThrowsAsync <ArgumentException> (async() => await socks.ConnectAsync(string.Empty, 80, ConnectTimeout)); Assert.Throws <ArgumentOutOfRangeException> (() => socks.Connect("www.google.com", 0)); Assert.Throws <ArgumentOutOfRangeException> (() => socks.Connect("www.google.com", 0, ConnectTimeout)); Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await socks.ConnectAsync("www.google.com", 0)); Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await socks.ConnectAsync("www.google.com", 0, ConnectTimeout)); Assert.Throws <ArgumentOutOfRangeException> (() => socks.Connect("www.google.com", 80, -ConnectTimeout)); Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await socks.ConnectAsync("www.google.com", 80, -ConnectTimeout)); }
public async Task TestTimeoutException() { using (var proxy = new Socks4aProxyListener()) { proxy.Start(IPAddress.Loopback, 0); var socks = new Socks4aClient(proxy.IPAddress.ToString(), proxy.Port); Stream stream = null; try { stream = await socks.ConnectAsync("example.com", 25, 1000); } catch (TimeoutException) { Assert.Pass(); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { stream?.Dispose(); } } }
public async Task TestConnectByDomainAsync() { using (var proxy = new Socks4aProxyListener()) { proxy.Start(IPAddress.Loopback, 0); var socks = new Socks4aClient(proxy.IPAddress.ToString(), proxy.Port); Stream socket = null; try { socket = await socks.ConnectAsync("www.google.com", 80, ConnectTimeout); } catch (TimeoutException) { Assert.Inconclusive("Timed out."); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { socket?.Dispose(); } } }
public async void TestConnectByDomainAsync() { // Note: this Socks4 proxy does not support the Socks4a protocol var socks = new Socks4aClient("100.39.36.100", 58288); Socket socket = null; try { socket = await socks.ConnectAsync("www.google.com", 80, 10 * 1000); socket.Disconnect(false); } catch (ProxyProtocolException) { // This is expected since this proxy does not support Socks4a Assert.Pass($"{socks.ProxyHost} does not support Socks4a, just as predicted."); } catch (TimeoutException) { Assert.Inconclusive("Timed out."); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (socket != null) { socket.Dispose(); } } }