aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-05-05 13:47:13 -0500
committerGitHub <noreply@github.com>2024-05-05 13:47:13 -0500
commitbbbd09d8208f005fd2a976c025e888e84a3448ec (patch)
tree3804a25c0af9bf4bb21017c1f9abfec74e342abd /tests/slskd.Tests.Unit
parent37dea1f91371900dcfb4fae8f46c6f3a6dc98126 (diff)
parent2a5c671499ac7bb542d7373464ac4a57465d971d (diff)
downloadslskd-bbbd09d8208f005fd2a976c025e888e84a3448ec.tar.gz
Merge pull request #1100 from slskd/blacklist
[WIP] Add support for blacklist files
Diffstat (limited to 'tests/slskd.Tests.Unit')
-rw-r--r--tests/slskd.Tests.Unit/Common/BlacklistTests.cs130
-rw-r--r--tests/slskd.Tests.Unit/Data/Blacklist/cidr.txt7
-rw-r--r--tests/slskd.Tests.Unit/Data/Blacklist/dat.txt7
-rw-r--r--tests/slskd.Tests.Unit/Data/Blacklist/empty.txt13
-rw-r--r--tests/slskd.Tests.Unit/Data/Blacklist/junk.txt3
-rw-r--r--tests/slskd.Tests.Unit/Data/Blacklist/p2p.txt7
-rw-r--r--tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj18
7 files changed, 185 insertions, 0 deletions
diff --git a/tests/slskd.Tests.Unit/Common/BlacklistTests.cs b/tests/slskd.Tests.Unit/Common/BlacklistTests.cs
new file mode 100644
index 00000000..9f21dde8
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/BlacklistTests.cs
@@ -0,0 +1,130 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common;
+
+public class BlacklistTests
+{
+ private List<(string, bool)> IPs = new List<(string, bool)>()
+ {
+ ("1.2.4.0", true),
+ ("1.2.4.128", true),
+ ("1.2.4.255", true),
+ ("1.2.8.0", true),
+ ("1.2.8.128", true),
+ ("1.2.8.255", true),
+ ("1.9.96.104", false),
+ ("1.9.96.105", true),
+ ("1.9.102.250", false),
+ ("1.9.102.251", true),
+ ("1.9.106.186", true),
+ ("1.9.106.187", false),
+ ("192.168.1.1", false),
+ ("4.4.4.4", false),
+ ("123.234.111.123", false),
+ };
+
+ [Theory]
+ [InlineData("Data/Blacklist/cidr.txt", BlacklistFormat.CIDR)]
+ [InlineData("Data/Blacklist/dat.txt", BlacklistFormat.DAT)]
+ [InlineData("Data/Blacklist/p2p.txt", BlacklistFormat.P2P)]
+ public async Task DetectFormat_Detects(string filename, BlacklistFormat expected)
+ {
+ var result = await Blacklist.DetectFormat(filename);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/junk.txt", typeof(FormatException))]
+ [InlineData("Data/Blacklist/empty.txt", typeof(FormatException))]
+ [InlineData("Data/does.not.exist.txt", typeof(IOException))]
+ public async Task DetectFormat_Throws(string filename, Type exType)
+ {
+ var ex = await Record.ExceptionAsync(() => Blacklist.DetectFormat(filename));
+
+ Assert.NotNull(ex);
+ Assert.IsAssignableFrom(exType, ex);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/junk.txt", BlacklistFormat.CIDR, typeof(FormatException))]
+ [InlineData("Data/does.not.exist.txt", BlacklistFormat.DAT, typeof(IOException))]
+ public async Task Load_throws(string filename, BlacklistFormat format, Type exType)
+ {
+ var bl = new Blacklist();
+ var ex = await Record.ExceptionAsync(() => bl.Load(filename, format));
+
+ Assert.NotNull(ex);
+ Assert.IsAssignableFrom(exType, ex);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/cidr.txt", BlacklistFormat.CIDR)]
+ [InlineData("Data/Blacklist/dat.txt", BlacklistFormat.DAT)]
+ [InlineData("Data/Blacklist/p2p.txt", BlacklistFormat.P2P)]
+ public async Task Load_Loads(string filename, BlacklistFormat format)
+ {
+ var bl = new Blacklist();
+ var ex = await Record.ExceptionAsync(() => bl.Load(filename, format));
+
+ Assert.Null(ex);
+
+ // the test files are assumed to all contain 5 entries
+ Assert.Equal(5, bl.Count);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/cidr.txt", BlacklistFormat.CIDR)]
+ public async Task Load_Then_Clear(string filename, BlacklistFormat format)
+ {
+ var bl = new Blacklist();
+ var ex = await Record.ExceptionAsync(() => bl.Load(filename, format));
+
+ Assert.Null(ex);
+
+ // the test files are assumed to all contain 5 entries
+ Assert.Equal(5, bl.Count);
+
+ bl.Clear();
+
+ Assert.Equal(0, bl.Count);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/cidr.txt")]
+ [InlineData("Data/Blacklist/dat.txt")]
+ [InlineData("Data/Blacklist/p2p.txt")]
+ public async Task Load_AutoDetects_And_Loads(string filename)
+ {
+ var bl = new Blacklist();
+ var ex = await Record.ExceptionAsync(() => bl.Load(filename));
+
+ Assert.Null(ex);
+
+ // the test files are assumed to all contain 5 entries
+ Assert.Equal(5, bl.Count);
+ }
+
+ [Theory]
+ [InlineData("Data/Blacklist/cidr.txt")]
+ [InlineData("Data/Blacklist/dat.txt")]
+ [InlineData("Data/Blacklist/p2p.txt")]
+ public async Task CIDR_Contains(string filename)
+ {
+ var bl = new Blacklist();
+ await bl.Load(filename);
+
+ foreach (var ip in IPs)
+ {
+ if (bl.Contains(IPAddress.Parse(ip.Item1)) != ip.Item2)
+ {
+ throw new Exception($"Expected contains for {ip.Item1} to be {ip.Item2} but it wasn't");
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Data/Blacklist/cidr.txt b/tests/slskd.Tests.Unit/Data/Blacklist/cidr.txt
new file mode 100644
index 00000000..850a2074
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Data/Blacklist/cidr.txt
@@ -0,0 +1,7 @@
+# here's a comment
+
+1.2.4.0/24
+1.2.8.0/24
+1.9.96.105/32
+1.9.102.251/32
+1.9.106.186/32 \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Data/Blacklist/dat.txt b/tests/slskd.Tests.Unit/Data/Blacklist/dat.txt
new file mode 100644
index 00000000..9386a98f
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Data/Blacklist/dat.txt
@@ -0,0 +1,7 @@
+# here's a comment
+
+001.002.004.000 - 001.002.004.255 , 000 , China Internet Information Center (CNNIC)
+001.002.008.000 - 001.002.008.255 , 000 , China Internet Information Center (CNNIC)
+001.009.096.105 - 001.009.096.105 , 000 , Botnet on Telekom Malaysia
+001.009.102.251 - 001.009.102.251 , 000 , Botnet on Telekom Malaysia
+001.009.106.186 - 001.009.106.186 , 000 , Botnet on Telekom Malaysia \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Data/Blacklist/empty.txt b/tests/slskd.Tests.Unit/Data/Blacklist/empty.txt
new file mode 100644
index 00000000..a50080e3
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Data/Blacklist/empty.txt
@@ -0,0 +1,13 @@
+# only a comment and some blank lines
+
+
+
+
+
+
+
+
+
+
+
+# end \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Data/Blacklist/junk.txt b/tests/slskd.Tests.Unit/Data/Blacklist/junk.txt
new file mode 100644
index 00000000..b42f309e
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Data/Blacklist/junk.txt
@@ -0,0 +1,3 @@
+{
+ "foo": "bar"
+} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Data/Blacklist/p2p.txt b/tests/slskd.Tests.Unit/Data/Blacklist/p2p.txt
new file mode 100644
index 00000000..fb18aa83
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Data/Blacklist/p2p.txt
@@ -0,0 +1,7 @@
+# here's a comment
+
+China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255
+China Internet Information Center (CNNIC):1.2.8.0-1.2.8.255
+Botnet on Telekom Malaysia:1.9.96.105-1.9.96.105
+Botnet on Telekom Malaysia:1.9.102.251-1.9.102.251
+Botnet on Telekom Malaysia:1.9.106.186-1.9.106.186 \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj b/tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj
index 127e0c1d..04b18bb7 100644
--- a/tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj
+++ b/tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj
@@ -25,4 +25,22 @@
<ProjectReference Include="..\..\src\slskd\slskd.csproj" />
</ItemGroup>
+ <ItemGroup>
+ <None Update="Data\Blacklist\cidr.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Update="Data\Blacklist\dat.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Update="Data\Blacklist\p2p.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Update="Data\Blacklist\empty.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Update="Data\Blacklist\junk.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+
</Project>