Skip to content

Commit

Permalink
update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tqk2811 committed Apr 1, 2024
1 parent 8fd629a commit 3a46607
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 100 deletions.
9 changes: 2 additions & 7 deletions src/TestProxy/BaseBindTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;

Expand All @@ -17,7 +12,7 @@ public BaseBindTest() : base()
{
_sockProxySource = GetSocksProxySource(_proxyServer);
}
protected abstract IProxySource GetSocksProxySource(BaseProxyServer baseProxyServer);
protected abstract IProxySource GetSocksProxySource(ProxyServer baseProxyServer);

// [local source] <-> [socks server] <-> [socks source] <-> [IPEndPoint -> TcpClient]
[TestMethod]
Expand Down
12 changes: 3 additions & 9 deletions src/TestProxy/BaseClassTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;

namespace TestProxy
{
public abstract class BaseClassTest : IDisposable
{
protected readonly BaseProxyServer _proxyServer;
protected readonly ProxyServer _proxyServer;
protected BaseClassTest()
{
_proxyServer = CreateServer(GetProxySource());
Expand All @@ -31,6 +25,6 @@ protected virtual void Dispose(bool isDisposing)
_proxyServer.Dispose();
}
protected abstract IProxySource GetProxySource();
protected abstract BaseProxyServer CreateServer(IProxySource proxySource);
protected abstract ProxyServer CreateServer(IProxySource proxySource);
}
}
8 changes: 1 addition & 7 deletions src/TestProxy/BaseConnectTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.ProxyServers;

namespace TestProxy
Expand All @@ -21,7 +15,7 @@ protected override void Dispose(bool isDisposing)
_httpClient.Dispose();
base.Dispose(isDisposing);
}
protected abstract HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer);
protected abstract HttpMessageHandler CreateHttpMessageHandler(ProxyServer baseProxyServer);


[TestMethod]
Expand Down
24 changes: 24 additions & 0 deletions src/TestProxy/CustomHttpProxyServerHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using TqkLibrary.Proxy.Authentications;
using TqkLibrary.Proxy.Handlers;
using TqkLibrary.Proxy.Interfaces;

namespace TestProxy
{
class CustomHttpProxyServerHandler : BaseProxyServerHandler
{
readonly HttpProxyAuthentication? _httpProxyAuthentication;
public CustomHttpProxyServerHandler(IProxySource proxySource, HttpProxyAuthentication? httpProxyAuthentication = null) : base(proxySource)
{
_httpProxyAuthentication = httpProxyAuthentication;
}

public override async Task<bool> IsAcceptUserAsync(IUserInfo userInfo, CancellationToken cancellationToken = default)
{
if (userInfo.Authentication is HttpProxyAuthentication httpProxyAuthentication)
{
return httpProxyAuthentication.Equals(_httpProxyAuthentication);
}
return false;
}
}
}
22 changes: 10 additions & 12 deletions src/TestProxy/HttpProxySourceTest/HttpProxySourceIpV6Test.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TestProxy.ServerTest;
using TqkLibrary.Proxy.Handlers;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;
using TqkLibrary.Proxy.ProxySources;

namespace TestProxy.HttpProxySourceTest
{
[TestClass]
public class HttpProxySourceIpV6Test: HttpProxyServerIpV6Test
public class HttpProxySourceIpV6Test : HttpProxyServerIpV6Test
{
IProxySource _proxySource;
BaseProxyServer _proxyServer2;
IProxySource? _proxySource;
ProxyServer? _proxyServer2;
protected override IProxySource GetProxySource()
{
_proxySource = base.GetProxySource();
_proxyServer2 = new HttpProxyServer(IPEndPoint.Parse("[::1]:0"), _proxySource);
_proxyServer2 = new ProxyServer(IPEndPoint.Parse("[::1]:0"))
{
ProxyServerHandler = new BaseProxyServerHandler(_proxySource)
};
_proxyServer2.StartListen();

return new HttpProxySource(new Uri($"http://{_proxyServer2.IPEndPoint}"), _networkCredential);
Expand All @@ -29,7 +27,7 @@ protected override IProxySource GetProxySource()
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
_proxyServer2.Dispose();
_proxyServer2?.Dispose();
}
}
}
16 changes: 5 additions & 11 deletions src/TestProxy/HttpProxySourceTest/HttpProxySourceTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TestProxy.ServerTest;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;
Expand All @@ -15,12 +9,12 @@ namespace TestProxy.HttpProxySourceTest
[TestClass]
public class HttpProxySourceTest : HttpProxyServerTest
{
IProxySource _proxySource;
BaseProxyServer _proxyServer2;
IProxySource? _proxySource;
ProxyServer? _proxyServer2;
protected override IProxySource GetProxySource()
{
_proxySource = base.GetProxySource();
_proxyServer2 = new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:0"), _proxySource);
_proxyServer2 = new ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), _proxySource);
_proxyServer2.StartListen();

return new HttpProxySource(new Uri($"http://{_proxyServer2.IPEndPoint}"), _networkCredential);
Expand All @@ -29,7 +23,7 @@ protected override IProxySource GetProxySource()
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
_proxyServer2.Dispose();
_proxyServer2?.Dispose();
}
}
}
24 changes: 9 additions & 15 deletions src/TestProxy/ServerTest/HttpProxyServerIpV6Test.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.Handlers;
using TqkLibrary.Proxy.Authentications;
using TqkLibrary.Proxy.ProxySources;

namespace TestProxy.ServerTest
{
Expand All @@ -21,13 +13,15 @@ protected override IProxySource GetProxySource()
{
return new LocalProxySource();
}
protected override BaseProxyServer CreateServer(IProxySource proxySource)
protected override ProxyServer CreateServer(IProxySource proxySource)
{
HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(proxySource);
handler.WithAuthentications(_networkCredential);
return new HttpProxyServer(IPEndPoint.Parse("[::1]:0"), handler);
return new ProxyServer(IPEndPoint.Parse("[::1]:0"))
{
ProxyServerHandler = new CustomHttpProxyServerHandler(proxySource, _networkCredential)
};
}
protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer)

protected override HttpMessageHandler CreateHttpMessageHandler(ProxyServer baseProxyServer)
{
return new HttpClientHandler()
{
Expand Down
24 changes: 9 additions & 15 deletions src/TestProxy/ServerTest/HttpProxyServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.Handlers;
using TqkLibrary.Proxy.Authentications;
using TqkLibrary.Proxy.ProxySources;

namespace TestProxy.ServerTest
{
//[TestClass]
public class HttpProxyServerTest : BaseConnectTest
{

protected readonly NetworkCredential _networkCredential = new NetworkCredential("user", "password");
protected override IProxySource GetProxySource()
{
return new LocalProxySource();
}
protected override BaseProxyServer CreateServer(IProxySource proxySource)
protected override ProxyServer CreateServer(IProxySource proxySource)
{
HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(proxySource);
handler.WithAuthentications(_networkCredential);
return new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:0"), handler);
return new ProxyServer(IPEndPoint.Parse("127.0.0.1:0"))
{
ProxyServerHandler = new CustomHttpProxyServerHandler(proxySource, _networkCredential)
};
}
protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer)
protected override HttpMessageHandler CreateHttpMessageHandler(ProxyServer baseProxyServer)
{
return new HttpClientHandler()
{
Expand Down
18 changes: 6 additions & 12 deletions src/TestProxy/ServerTest/Socks4ProxyServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.ProxySources;

namespace TestProxy.ServerTest
{
Expand All @@ -18,11 +12,11 @@ protected override IProxySource GetProxySource()
{
return new LocalProxySource();
}
protected override BaseProxyServer CreateServer(IProxySource proxySource)
protected override ProxyServer CreateServer(IProxySource proxySource)
{
return new Socks4ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource);
return new ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource);
}
protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer)
protected override HttpMessageHandler CreateHttpMessageHandler(ProxyServer baseProxyServer)
{
return new SocketsHttpHandler()
{
Expand All @@ -34,7 +28,7 @@ protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer b
UseProxy = true,
};
}
protected override IProxySource GetSocksProxySource(BaseProxyServer baseProxyServer)
protected override IProxySource GetSocksProxySource(ProxyServer baseProxyServer)
{
return new Socks4ProxySource(baseProxyServer.IPEndPoint);
}
Expand Down
18 changes: 6 additions & 12 deletions src/TestProxy/ServerTest/Socks5ProxyServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.ProxySources;

namespace TestProxy.ServerTest
{
Expand All @@ -18,11 +12,11 @@ protected override IProxySource GetProxySource()
{
return new LocalProxySource();
}
protected override BaseProxyServer CreateServer(IProxySource proxySource)
protected override ProxyServer CreateServer(IProxySource proxySource)
{
return new Socks5ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource);
return new ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource);
}
protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer)
protected override HttpMessageHandler CreateHttpMessageHandler(ProxyServer baseProxyServer)
{
return new SocketsHttpHandler()
{
Expand All @@ -34,7 +28,7 @@ protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer b
UseProxy = true,
};
}
protected override IProxySource GetSocksProxySource(BaseProxyServer baseProxyServer)
protected override IProxySource GetSocksProxySource(ProxyServer baseProxyServer)
{
return new Socks5ProxySource(baseProxyServer.IPEndPoint);
}
Expand Down
1 change: 1 addition & 0 deletions src/TestProxy/TestProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 3a46607

Please sign in to comment.