aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorArtur <5359825+sethidden@users.noreply.github.com>2024-05-30 09:27:41 +0000
committerGitHub <noreply@github.com>2024-05-30 09:27:41 +0000
commita783e181982dd8c435bb51345a8528a65a4304ac (patch)
tree236c9b7bc40ca09cf3f40878a9beb1dea292a20d /tests
parent02bf77158d3bf3dcb6919a9bd0637bf040644c26 (diff)
parentb33449b0282d7f466be22570c26e9e3c982b888a (diff)
downloadslskd-a783e181982dd8c435bb51345a8528a65a4304ac.tar.gz
Merge branch 'slskd:master' into master
Diffstat (limited to 'tests')
-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/Files/FileExtensionsTests.cs71
-rw-r--r--tests/slskd.Tests.Unit/Files/FileServiceTests.cs16
-rw-r--r--tests/slskd.Tests.Unit/slskd.Tests.Unit.csproj18
9 files changed, 264 insertions, 8 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/Files/FileExtensionsTests.cs b/tests/slskd.Tests.Unit/Files/FileExtensionsTests.cs
new file mode 100644
index 00000000..c270bb31
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Files/FileExtensionsTests.cs
@@ -0,0 +1,71 @@
+using System;
+using System.IO;
+using slskd.Files;
+using Xunit;
+
+namespace slskd.Tests.Unit.Files;
+
+public class FileExtensionsTests
+{
+ [Theory]
+ [InlineData("0000", UnixFileMode.None)]
+ [InlineData("0100", UnixFileMode.UserExecute)]
+ [InlineData("0200", UnixFileMode.UserWrite)]
+ [InlineData("0300", UnixFileMode.UserWrite | UnixFileMode.UserExecute)]
+ [InlineData("0400", UnixFileMode.UserRead)]
+ [InlineData("0500", UnixFileMode.UserRead | UnixFileMode.UserExecute)]
+ [InlineData("0600", UnixFileMode.UserRead | UnixFileMode.UserWrite)]
+ [InlineData("0700", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute)]
+ [InlineData("0010", UnixFileMode.GroupExecute)]
+ [InlineData("0020", UnixFileMode.GroupWrite)]
+ [InlineData("0030", UnixFileMode.GroupWrite | UnixFileMode.GroupExecute)]
+ [InlineData("0040", UnixFileMode.GroupRead)]
+ [InlineData("0050", UnixFileMode.GroupRead | UnixFileMode.GroupExecute)]
+ [InlineData("0060", UnixFileMode.GroupRead | UnixFileMode.GroupWrite)]
+ [InlineData("0070", UnixFileMode.GroupRead | UnixFileMode.GroupWrite | UnixFileMode.GroupExecute)]
+ [InlineData("0001", UnixFileMode.OtherExecute)]
+ [InlineData("0002", UnixFileMode.OtherWrite)]
+ [InlineData("0003", UnixFileMode.OtherWrite | UnixFileMode.OtherExecute)]
+ [InlineData("0004", UnixFileMode.OtherRead)]
+ [InlineData("0005", UnixFileMode.OtherRead | UnixFileMode.OtherExecute)]
+ [InlineData("0006", UnixFileMode.OtherRead | UnixFileMode.OtherWrite)]
+ [InlineData("0007", UnixFileMode.OtherRead | UnixFileMode.OtherWrite | UnixFileMode.OtherExecute)]
+ [InlineData("0644", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead)]
+ [InlineData("0777", UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead | UnixFileMode.GroupExecute | UnixFileMode.GroupWrite | UnixFileMode.GroupRead | UnixFileMode.OtherExecute | UnixFileMode.OtherWrite | UnixFileMode.OtherRead)]
+ [InlineData("1000", UnixFileMode.StickyBit)]
+ [InlineData("2000", UnixFileMode.SetGroup)]
+ [InlineData("3000", UnixFileMode.SetGroup | UnixFileMode.StickyBit)]
+ [InlineData("4000", UnixFileMode.SetUser)]
+ [InlineData("5000", UnixFileMode.SetUser | UnixFileMode.StickyBit)]
+ [InlineData("6000", UnixFileMode.SetUser | UnixFileMode.SetGroup)]
+ [InlineData("7000", UnixFileMode.SetUser | UnixFileMode.SetGroup | UnixFileMode.StickyBit)]
+ [InlineData("7777", UnixFileMode.SetUser | UnixFileMode.SetGroup | UnixFileMode.StickyBit | UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead | UnixFileMode.GroupExecute | UnixFileMode.GroupWrite | UnixFileMode.GroupRead | UnixFileMode.OtherExecute | UnixFileMode.OtherWrite | UnixFileMode.OtherRead)]
+ public void ToUnixFileMode_Works_As_Expected(string permissions, UnixFileMode expected)
+ {
+ var result = permissions.ToUnixFileMode();
+
+ Assert.Equal(expected, result);
+ }
+
+ [Fact]
+ public void ToUinixFileMode_Throws_ArgumentException_Given_Empty()
+ {
+ var ex = Record.Exception(() => "".ToUnixFileMode());
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("0")]
+ [InlineData("00")]
+ [InlineData("00000")]
+ [InlineData("foo bar baz")]
+ public void ToUinixFileMode_Throws_ArgumentOutOfRangeException_Given_Bad_String(string str)
+ {
+ var ex = Record.Exception(() => str.ToUnixFileMode());
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentOutOfRangeException>(ex);
+ }
+} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs
index 7c9931ab..6c3d1d73 100644
--- a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs
+++ b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs
@@ -13,13 +13,13 @@ namespace slskd.Tests.Unit.Files
{
public FileServiceTests()
{
- OptionsSnapshotMock = new Mock<IOptionsSnapshot<Options>>();
+ OptionsMonitorMock = new Mock<IOptionsMonitor<Options>>();
Temp = Path.Combine(Path.GetTempPath(), $"slskd.test.{Guid.NewGuid()}");
Directory.CreateDirectory(Temp);
FileService = new FileService(
- optionsSnapshot: OptionsSnapshotMock.Object);
+ optionsMonitor: OptionsMonitorMock.Object);
}
public void Dispose()
@@ -27,9 +27,9 @@ namespace slskd.Tests.Unit.Files
Directory.Delete(Temp, recursive: true);
}
- private Mock<IOptionsSnapshot<Options>> OptionsSnapshotMock { get; init; }
+ private Mock<IOptionsMonitor<Options>> OptionsMonitorMock { get; init; }
private string Temp { get; init; }
- private IFileService FileService { get; init; }
+ private FileService FileService { get; init; }
[Fact]
public async Task ListContentsAsync_Throws_ArgumentException_Given_Relative_Path()
@@ -44,7 +44,7 @@ namespace slskd.Tests.Unit.Files
[Fact]
public async Task ListContentsAsync_Throws_UnauthorizedException_Given_Disallowed_Directory()
{
- OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options
+ OptionsMonitorMock.Setup(o => o.CurrentValue).Returns(new Options
{
Directories = new Options.DirectoriesOptions
{
@@ -62,7 +62,7 @@ namespace slskd.Tests.Unit.Files
[Fact]
public async Task ListContentsAsync_Throws_NotFoundException_Given_NonExistent_Directory()
{
- OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options
+ OptionsMonitorMock.Setup(o => o.CurrentValue).Returns(new Options
{
Directories = new Options.DirectoriesOptions
{
@@ -90,7 +90,7 @@ namespace slskd.Tests.Unit.Files
[Fact]
public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Disallowed_Path()
{
- OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options
+ OptionsMonitorMock.Setup(o => o.CurrentValue).Returns(new Options
{
Directories = new Options.DirectoriesOptions
{
@@ -118,7 +118,7 @@ namespace slskd.Tests.Unit.Files
[Fact]
public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Disallowed_Path()
{
- OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options
+ OptionsMonitorMock.Setup(o => o.CurrentValue).Returns(new Options
{
Directories = new Options.DirectoriesOptions
{
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>