Skip to content

Commit cf6a445

Browse files
committed
Tests: introduce HS compat test with TOR client
Upgrade to .NET 6 was necessary to be able to use SocksV5 (which is what tor gives us) as proxy for HttpClient.
1 parent 737ac26 commit cf6a445

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

.github/workflows/CI.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ jobs:
1010
- uses: actions/checkout@v2
1111
with:
1212
submodules: recursive
13-
- name: Setup .NET Core SDK 3.1.x
13+
- name: Setup .NET SDK 6.0.x
1414
uses: actions/[email protected]
1515
with:
16-
dotnet-version: '3.1.x'
16+
dotnet-version: '6.0.x'
1717
- name: Install dependencies
1818
run: dotnet restore
1919
- name: Build
2020
run: dotnet build --configuration Release --no-restore
21+
# Please keep in mind that NOnion DOES NOT require tor client to function
22+
# tor client is only installed here for testing purposes.
23+
- name: Install Tor and wait for startup
24+
run: |
25+
sudo apt install tor
26+
echo -e "SOCKSPort 127.0.0.1:9050" | sudo tee -a /etc/tor/torrc
27+
sudo systemctl restart tor
28+
sleep 2m
2129
- name: Test
2230
run: dotnet test --no-restore --verbosity normal
2331

@@ -31,10 +39,10 @@ jobs:
3139
submodules: recursive
3240
# needed because of commit-lint, see https://github.com/conventional-changelog/commitlint/issues/3376
3341
fetch-depth: 0
34-
- name: Setup .NET SDK 5.0.x
42+
- name: Setup .NET SDK 6.0.x
3543
uses: actions/[email protected]
3644
with:
37-
dotnet-version: '5.0.x'
45+
dotnet-version: '6.0.x'
3846
- name: Install dependencies
3947
run: dotnet restore
4048
- name: Build
@@ -84,10 +92,10 @@ jobs:
8492
- uses: actions/checkout@v2
8593
with:
8694
submodules: recursive
87-
- name: Setup .NET Core SDK 3.1.x
95+
- name: Setup .NET SDK 6.0.x
8896
uses: actions/[email protected]
8997
with:
90-
dotnet-version: '3.1.x'
98+
dotnet-version: '6.0.x'
9199
- name: Install dependencies
92100
run: dotnet restore
93101
- name: Build

NOnion.Tests/HiddenServicesTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
using System;
33
using System.IO;
44
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
57
using System.Security.Cryptography;
8+
using System.Text;
69
using System.Threading.Tasks;
710

811
using NUnit.Framework;
@@ -162,6 +165,78 @@ public void CanEstablishAndCommunicateOverHSConnectionOnionStyle()
162165
{
163166
Assert.DoesNotThrowAsync(EstablishAndCommunicateOverHSConnectionOnionStyle);
164167
}
168+
169+
[Test]
170+
[Retry(TestsRetryCount)]
171+
public void CanConnectToHiddenServiceUsingTorClient()
172+
{
173+
Assert.DoesNotThrowAsync(ConnectToHiddenServiceUsingTorClient);
174+
}
175+
176+
private async Task ConnectToHiddenServiceUsingTorClient()
177+
{
178+
int descriptorUploadRetryLimit = 2;
179+
180+
TorDirectory directory = await TorDirectory.BootstrapAsync(FallbackDirectorySelector.GetRandomFallbackDirectory(), new DirectoryInfo(Path.GetTempPath()));
181+
182+
TorLogger.Log("Finished bootstraping");
183+
184+
SecureRandom random = new SecureRandom();
185+
Ed25519KeyPairGenerator kpGen = new Ed25519KeyPairGenerator();
186+
kpGen.Init(new Ed25519KeyGenerationParameters(random));
187+
Ed25519PrivateKeyParameters masterPrivateKey = (Ed25519PrivateKeyParameters)kpGen.GenerateKeyPair().Private;
188+
189+
TorServiceHost host = new TorServiceHost(directory, descriptorUploadRetryLimit, TestsRetryCount, FSharpOption<Ed25519PrivateKeyParameters>.Some(masterPrivateKey));
190+
await host.StartAsync();
191+
192+
TorLogger.Log("Finished starting HS host");
193+
194+
var stringToSendAndReceive =
195+
"We are using tor!";
196+
197+
var serverSide =
198+
Task.Run(async () => {
199+
var stream = await host.AcceptClientAsync();
200+
201+
var httpResponse =
202+
"HTTP/1.1 200 OK\r\n" +
203+
"Server: NOnion\r\n" +
204+
$"Content-Length: {stringToSendAndReceive.Length}\r\n" +
205+
"Connection: close\r\n" +
206+
"Content-Type: text/plain" +
207+
"\r\n" +
208+
"\r\n" +
209+
stringToSendAndReceive +
210+
"\r\n";
211+
212+
await stream.SendDataAsync(Encoding.ASCII.GetBytes(httpResponse));
213+
await stream.EndAsync();
214+
});
215+
216+
var clientSide =
217+
Task.Run(async () => {
218+
var handler = new HttpClientHandler
219+
{
220+
Proxy = new WebProxy(new Uri("socks5://localhost:9050"))
221+
};
222+
223+
TestContext.Progress.WriteLine("Trying to connect to hidden service...");
224+
using (handler)
225+
using (var httpClient = new HttpClient(handler))
226+
{
227+
// Sometimes tor client takes a while to bootstrap and stalls
228+
// the request.
229+
httpClient.Timeout = TimeSpan.FromMinutes(20);
230+
var result = await httpClient.GetStringAsync("http://" + host.ExportUrl());
231+
Assert.AreEqual(result, stringToSendAndReceive);
232+
}
233+
}
234+
);
235+
236+
await TaskUtils.WhenAllFailFast(serverSide, clientSide);
237+
238+
((IDisposable)host).Dispose();
239+
}
165240
}
166241
}
167242

NOnion.Tests/NOnion.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

0 commit comments

Comments
 (0)