diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index e33e753..f80a5d4 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -18,7 +18,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore ./ShareClipbrd/ShareClipbrd.sln - name: Build @@ -28,7 +28,7 @@ jobs: - name: Publish run: | - dotnet publish "./ShareClipbrd\ShareClipbrdApp/ShareClipbrdApp.csproj" -c PublishRelease -r win-x64 -f net7.0-windows -p:PublishSingleFile=true --self-contained false -o "./out/win64" -p:DebugType=None + dotnet publish "./ShareClipbrd\ShareClipbrdApp/ShareClipbrdApp.csproj" -c PublishRelease -r win-x64 -f net8.0-windows -p:PublishSingleFile=true --self-contained true -o "./out/win64" -p:DebugType=None - name: Deploy Win64 uses: actions/upload-artifact@v3 @@ -45,7 +45,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore ./ShareClipbrd/ShareClipbrd.sln - name: Build @@ -55,7 +55,7 @@ jobs: - name: Publish run: | - dotnet publish "./ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj" -c PublishRelease -r linux-x64 -f net7.0 -p:PublishSingleFile=true --self-contained false -o "./out/_linux64" -p:DebugType=None + dotnet publish "./ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj" -c PublishRelease -r linux-x64 -f net8.0 -p:PublishSingleFile=true --self-contained true -o "./out/_linux64" -p:DebugType=None - name: Tar Linux64 files run: mkdir -p ./out/linux64 && tar -cjvf ./out/linux64/ShareClipbrd.tar -C ./out/_linux64/ . && rm -rf ./out/_linux64 diff --git a/.vscode/launch.json b/.vscode/launch.json index a739da4..b7f3896 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/ShareClipbrd/ShareClipbrdApp/bin/Debug/net7.0-windows/ShareClipbrdApp.dll", + "program": "${workspaceFolder}/ShareClipbrd/ShareClipbrdApp/bin/Debug/net8.0-windows/ShareClipbrdApp.dll", "args": [], "cwd": "${workspaceFolder}/ShareClipbrd/ShareClipbrdApp", "console": "internalConsole", diff --git a/ShareClipbrd/Clipboard.Core.Tests/Clipboard.Core.Tests.csproj b/ShareClipbrd/Clipboard.Core.Tests/Clipboard.Core.Tests.csproj index 70f4869..1c45f77 100644 --- a/ShareClipbrd/Clipboard.Core.Tests/Clipboard.Core.Tests.csproj +++ b/ShareClipbrd/Clipboard.Core.Tests/Clipboard.Core.Tests.csproj @@ -1,24 +1,30 @@ - - - - net7.0 - enable - enable - - false - true - - - - - - - - - - - - - - - + + + + net8.0 + enable + enable + + false + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/ShareClipbrd/Clipboard.Core/Clipboard.Core.csproj b/ShareClipbrd/Clipboard.Core/Clipboard.Core.csproj index 59aa431..c4b39f8 100644 --- a/ShareClipbrd/Clipboard.Core/Clipboard.Core.csproj +++ b/ShareClipbrd/Clipboard.Core/Clipboard.Core.csproj @@ -1,16 +1,16 @@ - - - - net7.0 - enable - enable - - - - - - - - - - + + + + net8.0 + enable + enable + + + + + + + + + + diff --git a/ShareClipbrd/Clipboard.Tests/Clipboard.Tests.csproj b/ShareClipbrd/Clipboard.Tests/Clipboard.Tests.csproj index aee16f4..df2a145 100644 --- a/ShareClipbrd/Clipboard.Tests/Clipboard.Tests.csproj +++ b/ShareClipbrd/Clipboard.Tests/Clipboard.Tests.csproj @@ -1,25 +1,31 @@ - - - - net7.0-windows - net7.0 - enable - enable - - false - true - - - - - - - - - - - - - - - + + + + net8.0-windows + net8.0 + enable + enable + + false + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/ShareClipbrd/Clipboard.Win/Clipboard.Win.csproj b/ShareClipbrd/Clipboard.Win/Clipboard.Win.csproj index 80460e2..7317ac4 100644 --- a/ShareClipbrd/Clipboard.Win/Clipboard.Win.csproj +++ b/ShareClipbrd/Clipboard.Win/Clipboard.Win.csproj @@ -1,19 +1,19 @@ - - - - net7.0-windows - enable - enable - true - true - - - - - - - - - - - + + + + net8.0-windows + enable + enable + true + true + + + + + + + + + + + diff --git a/ShareClipbrd/Clipboard.X11/Clipboard.X11.csproj b/ShareClipbrd/Clipboard.X11/Clipboard.X11.csproj index c37173b..1b76866 100644 --- a/ShareClipbrd/Clipboard.X11/Clipboard.X11.csproj +++ b/ShareClipbrd/Clipboard.X11/Clipboard.X11.csproj @@ -1,17 +1,17 @@ - - - - net7.0 - true - enable - enable - - - - - - - - - - + + + + net8.0 + true + enable + enable + + + + + + + + + + diff --git a/ShareClipbrd/Clipboard/Clipboard.csproj b/ShareClipbrd/Clipboard/Clipboard.csproj index e63a5c5..8890bfd 100644 --- a/ShareClipbrd/Clipboard/Clipboard.csproj +++ b/ShareClipbrd/Clipboard/Clipboard.csproj @@ -1,26 +1,26 @@ - - - - net7.0-windows - net7.0 - enable - enable - - - - - - - - - - - - - - - - - - - + + + + net8.0-windows + net8.0 + enable + enable + + + + + + + + + + + + + + + + + + + diff --git a/ShareClipbrd/ShareClipbrd.Core.Tests/Helpers/PathHelperTests.cs b/ShareClipbrd/ShareClipbrd.Core.Tests/Helpers/PathHelperTests.cs index 5741cbe..75caf56 100644 --- a/ShareClipbrd/ShareClipbrd.Core.Tests/Helpers/PathHelperTests.cs +++ b/ShareClipbrd/ShareClipbrd.Core.Tests/Helpers/PathHelperTests.cs @@ -1,37 +1,37 @@ -using ShareClipbrd.Core.Helpers; - -namespace ShareClipbrd.Core.Tests.Helpers { - public class PathHelperTests { - [Test] - public void IsAbsolute_Test() { - if(OperatingSystem.IsWindows()) { - Assert.True(PathHelper.IsAbsolute("C:\\ShareClipbrd\\ShareClipbrd.Core\\Clipboard\\ClipboardFile.cs")); - Assert.True(PathHelper.IsAbsolute("C:\\ShareClipbrd//ClipboardFile.cs")); - Assert.True(PathHelper.IsAbsolute("\\tsclient\\Documents.zip")); - Assert.False(PathHelper.IsAbsolute("//")); - Assert.False(PathHelper.IsAbsolute("//tsclient/Documents.zip")); - Assert.False(PathHelper.IsAbsolute("//file/1")); - Assert.False(PathHelper.IsAbsolute("//file")); - } - - if(OperatingSystem.IsLinux()) { - Assert.True(PathHelper.IsAbsolute("/ShareClipbrd/ShareClipbrd.Core/Clipboard/ClipboardFile.cs")); - Assert.True(PathHelper.IsAbsolute("/ShareClipbrd//ClipboardFile.cs")); - Assert.True(PathHelper.IsAbsolute("/tsclient/Documents.zip")); - Assert.True(PathHelper.IsAbsolute("//")); - Assert.True(PathHelper.IsAbsolute("//tsclient/Documents.zip")); - Assert.True(PathHelper.IsAbsolute("//file/1")); - Assert.True(PathHelper.IsAbsolute("//file")); - } - Assert.True(PathHelper.IsAbsolute("/home/Documents.zip")); - - Assert.False(PathHelper.IsAbsolute("ShareClipbrd\\ShareClipbrd.Core\\Clipboard\\ClipboardFile.cs")); - Assert.False(PathHelper.IsAbsolute("ShareClipbrd/ShareClipbrd.Core/Clipboard/ClipboardFile.cs")); - Assert.False(PathHelper.IsAbsolute("Documents")); - Assert.False(PathHelper.IsAbsolute(" ")); - Assert.False(PathHelper.IsAbsolute("/")); - Assert.False(PathHelper.IsAbsolute("\\")); - Assert.False(PathHelper.IsAbsolute("\\\\")); - } - } -} +using ShareClipbrd.Core.Helpers; + +namespace ShareClipbrd.Core.Tests.Helpers { + public class PathHelperTests { + [Test] + public void IsAbsolute_Test() { + if(OperatingSystem.IsWindows()) { + Assert.That(PathHelper.IsAbsolute("C:\\ShareClipbrd\\ShareClipbrd.Core\\Clipboard\\ClipboardFile.cs"), Is.True); + Assert.That(PathHelper.IsAbsolute("C:\\ShareClipbrd//ClipboardFile.cs"), Is.True); + Assert.That(PathHelper.IsAbsolute("\\tsclient\\Documents.zip"), Is.True); + Assert.That(PathHelper.IsAbsolute("//"), Is.False); + Assert.That(PathHelper.IsAbsolute("//tsclient/Documents.zip"), Is.False); + Assert.That(PathHelper.IsAbsolute("//file/1"), Is.False); + Assert.That(PathHelper.IsAbsolute("//file"), Is.False); + } + + if(OperatingSystem.IsLinux()) { + Assert.That(PathHelper.IsAbsolute("/ShareClipbrd/ShareClipbrd.Core/Clipboard/ClipboardFile.cs"), Is.True); + Assert.That(PathHelper.IsAbsolute("/ShareClipbrd//ClipboardFile.cs"), Is.True); + Assert.That(PathHelper.IsAbsolute("/tsclient/Documents.zip"), Is.True); + Assert.That(PathHelper.IsAbsolute("//"), Is.True); + Assert.That(PathHelper.IsAbsolute("//tsclient/Documents.zip"), Is.True); + Assert.That(PathHelper.IsAbsolute("//file/1"), Is.True); + Assert.That(PathHelper.IsAbsolute("//file"), Is.True); + } + Assert.That(PathHelper.IsAbsolute("/home/Documents.zip"), Is.True); + + Assert.That(PathHelper.IsAbsolute("ShareClipbrd\\ShareClipbrd.Core\\Clipboard\\ClipboardFile.cs"), Is.False); + Assert.That(PathHelper.IsAbsolute("ShareClipbrd/ShareClipbrd.Core/Clipboard/ClipboardFile.cs"), Is.False); + Assert.That(PathHelper.IsAbsolute("Documents"), Is.False); + Assert.That(PathHelper.IsAbsolute(" "), Is.False); + Assert.That(PathHelper.IsAbsolute("/"), Is.False); + Assert.That(PathHelper.IsAbsolute("\\"), Is.False); + Assert.That(PathHelper.IsAbsolute("\\\\"), Is.False); + } + } +} diff --git a/ShareClipbrd/ShareClipbrd.Core.Tests/Services/AddressResolverTests.cs b/ShareClipbrd/ShareClipbrd.Core.Tests/Services/AddressResolverTests.cs index 574db74..7567802 100644 --- a/ShareClipbrd/ShareClipbrd.Core.Tests/Services/AddressResolverTests.cs +++ b/ShareClipbrd/ShareClipbrd.Core.Tests/Services/AddressResolverTests.cs @@ -6,27 +6,27 @@ public class AddressResolverTests { public void UseAddressDiscoveryService_When_Invalid_Address_Return_False() { string id; int? mandatoryPort; - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService(":55542", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService(":55542", out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService(string.Empty, out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService(string.Empty, out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService("mdns 1234", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns 1234", out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService("mdns", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns", out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService("mdns :", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns :", out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsFalse(AddressResolver.UseAddressDiscoveryService("mdns : ", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns : ", out id, out mandatoryPort), Is.False); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); } @@ -35,15 +35,15 @@ public void UseAddressDiscoveryService_When_Invalid_Address_Return_False() { public void UseAddressDiscoveryService_Valid_Address_Return_True_And_Id() { string id; int? mandatoryPort; - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:a", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:a", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("a")); Assert.That(mandatoryPort, Is.Null); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:55542", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:55542", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("55542")); Assert.That(mandatoryPort, Is.Null); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")); Assert.That(mandatoryPort, Is.Null); } @@ -58,7 +58,7 @@ public void UseAddressDiscoveryService_When_Invalid_Ports_Throws_ArgumentExcepti Assert.Throws(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde:-1", out id, out mandatoryPort)); Assert.Throws(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde:65536", out id, out mandatoryPort)); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns::", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns::", out id, out mandatoryPort)); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); } @@ -68,15 +68,15 @@ public void UseAddressDiscoveryService_Returns_True_And_Empty_Id_If_Address_Is_N string id; int? mandatoryPort; - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde:", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:abcde:", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("abcde")); Assert.That(mandatoryPort, Is.Null); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:", out id, out mandatoryPort), Is.True); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.Null); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns::1", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns::1", out id, out mandatoryPort), Is.True); Assert.That(id, Is.Empty); Assert.That(mandatoryPort, Is.EqualTo(1)); } @@ -85,13 +85,13 @@ public void UseAddressDiscoveryService_Returns_True_And_Empty_Id_If_Address_Is_N public void UseAddressDiscoveryService_Valid_Ports_And_Entire_Id_Extracting_Tests() { string id; int? mandatoryPort; - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde:0", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:abcde:0", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("abcde")); Assert.That(mandatoryPort, Is.EqualTo(0)); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde 12345:1", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:abcde 12345:1", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("abcde 12345")); Assert.That(mandatoryPort, Is.EqualTo(1)); - Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde:65535", out id, out mandatoryPort)); + Assert.That(AddressResolver.UseAddressDiscoveryService("mdns:abcde:65535", out id, out mandatoryPort), Is.True); Assert.That(id, Is.EqualTo("abcde")); Assert.That(mandatoryPort, Is.EqualTo(65535)); } diff --git a/ShareClipbrd/ShareClipbrd.Core.Tests/Services/ClientServerTests.cs b/ShareClipbrd/ShareClipbrd.Core.Tests/Services/ClientServerTests.cs index c9bc4ca..87fb70b 100644 --- a/ShareClipbrd/ShareClipbrd.Core.Tests/Services/ClientServerTests.cs +++ b/ShareClipbrd/ShareClipbrd.Core.Tests/Services/ClientServerTests.cs @@ -1,441 +1,441 @@ -using System.Collections.Specialized; -using System.Diagnostics; -using Clipboard.Core; -using Moq; -using ShareClipbrd.Core.Configuration; -using ShareClipbrd.Core.Helpers; -using ShareClipbrd.Core.Services; - -namespace ShareClipbrd.Core.Tests.Services { - public class Tests { - Mock systemConfigurationMock; - Mock dialogServiceMock; - Mock dispatchServiceMock; - Mock progressServiceMock; - Mock connectStatusServiceMock; - Mock timeServiceMock; - Mock addressDiscoveryServiceMock; - DataServer server; - DataClient client; - - - [SetUp] - public void Setup() { - systemConfigurationMock = new(); - dialogServiceMock = new(); - dispatchServiceMock = new(); - progressServiceMock = new(); - connectStatusServiceMock = new(); - timeServiceMock = new(); - addressDiscoveryServiceMock = new(); - timeServiceMock.SetupGet(x => x.DataClientPingPeriod).Returns(TimeSpan.FromMilliseconds(10000)); - - systemConfigurationMock.SetupGet(x => x.HostAddress).Returns("127.0.0.1:0"); - - server = new DataServer(systemConfigurationMock.Object, dialogServiceMock.Object, dispatchServiceMock.Object, - progressServiceMock.Object, connectStatusServiceMock.Object, addressDiscoveryServiceMock.Object); - client = new DataClient(systemConfigurationMock.Object, progressServiceMock.Object, - connectStatusServiceMock.Object, dialogServiceMock.Object, timeServiceMock.Object, addressDiscoveryServiceMock.Object); - - } - - [TearDown] - public void Teardown() { - } - - [Test] - public async Task Send_CommonData_Test() { - var receivedClipboard = new List(); - - dispatchServiceMock - .Setup(x => x.ReceiveData(It.IsAny())) - .Callback(x => receivedClipboard.Add(x)); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - - var clipboardData = new ClipboardData(); - clipboardData.Add("UnicodeText", new MemoryStream(System.Text.Encoding.Unicode.GetBytes("UnicodeText юникод Œ"))); - await client.SendData(clipboardData); - - clipboardData = new ClipboardData(); - clipboardData.Add("Text", new MemoryStream(System.Text.Encoding.Unicode.GetBytes("Text 0123456789"))); - - await client.SendData(clipboardData); - await Task.Delay(500); - await server.Stop(); - client.Stop(); - - dispatchServiceMock.Verify(x => x.ReceiveData(It.IsAny()), Times.Exactly(2)); - Assert.IsNotNull(receivedClipboard); - Assert.That(receivedClipboard.SelectMany(x => x!.Formats).Select(x => x.Format), Is.EquivalentTo(new[] { "UnicodeText", "Text" })); - Assert.That(receivedClipboard.SelectMany(x => x!.Formats).Select(x => x.Stream), Is.EquivalentTo(new[] { - new MemoryStream(System.Text.Encoding.Unicode.GetBytes("UnicodeText юникод Œ")), - new MemoryStream(System.Text.Encoding.Unicode.GetBytes("Text 0123456789")) - })); - } - - [Test] - public async Task Send_Common_Big_Data_Test() { - ClipboardData? receivedClipboard = null; - - dispatchServiceMock - .Setup(x => x.ReceiveData(It.IsAny())) - .Callback(x => receivedClipboard = x); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - - var clipboardData = new ClipboardData(); - - var rnd = new Random(); - var bytes = new byte[1_000_000_003]; - rnd.NextBytes(bytes); - - clipboardData.Add("Text", new MemoryStream(bytes)); - - await client.SendData(clipboardData); - await Task.Delay(3000); - await server.Stop(); - client.Stop(); - - dispatchServiceMock.VerifyAll(); - Assert.IsNotNull(receivedClipboard); - Assert.That(receivedClipboard.Formats.Select(x => x.Format), Is.EquivalentTo(new[] { "Text" })); - Assert.That(receivedClipboard.Formats.First(x => x.Format == "Text").Stream, Has.Length.EqualTo(1_000_000_003)); - Assert.That(receivedClipboard.Formats.First(x => x.Format == "Text").Stream.ToArray().Take(1_000_000), Is.EquivalentTo(bytes.Take(1_000_000))); - - } - - [Test] - public async Task Send_Files_Test() { - IList? fileDropList = null; - - dispatchServiceMock - .Setup(x => x.ReceiveFiles(It.IsAny>())) - .Callback>(x => fileDropList = x); - - var clipboardData = new ClipboardData(); - - var testsPath = Path.Combine(Path.GetTempPath(), "tests"); - Directory.CreateDirectory(testsPath); - - var rnd = new Random(); - var bytes = new byte[3_333_333]; - rnd.NextBytes(bytes); - var testdata = new MemoryStream(bytes); - - var files = new StringCollection(); - - var buffer = new byte[3_333_333 / 100]; - testdata.Position = 0; - for(int i = 0; i < 100; i++) { - var filename = Path.Combine(testsPath, $"Unicode юникод ® _{i}"); - testdata.Read(buffer, 0, buffer.Length); - - File.WriteAllBytes(filename, buffer); - files.Add(filename); - } - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - try { - await client.SendFileDropList(files); - await client.SendFileDropList(files); - await client.SendFileDropList(files); - - } finally { - Directory.Delete(testsPath, true); - await Task.Delay(1000); - await server.Stop(); - client.Stop(); - } - - dispatchServiceMock.Verify(x => x.ReceiveFiles(It.IsAny>()), Times.Exactly(3)); - Assert.IsNotNull(fileDropList); - Assert.That(fileDropList.Count, Is.EqualTo(100)); - - - testdata.Position = 0; - for(int i = 0; i < 100; i++) { - var otherFilename = fileDropList.First(x => x.EndsWith($"Unicode юникод ® _{i}")); - - Assert.That(otherFilename, Does.Exist); - - var otherBytes = File.ReadAllBytes(otherFilename); - File.Delete(otherFilename); - - Assert.That(otherBytes, Has.Length.EqualTo(3_333_333 / 100)); - testdata.Read(buffer, 0, buffer.Length); - Assert.That(otherBytes, Is.EquivalentTo(buffer)); - } - } - - [Test] - public async Task Send_Big_File_Test() { - IList? fileDropList = null; - - dispatchServiceMock - .Setup(x => x.ReceiveFiles(It.IsAny>())) - .Callback>(x => fileDropList = x); - - var rnd = new Random(); - var bytes = new byte[1_000_003]; - rnd.NextBytes(bytes); - - var testsPath = Path.Combine(Path.GetTempPath(), "tests"); - Directory.CreateDirectory(testsPath); - - var files = new StringCollection(); - var filename = Path.Combine(testsPath, Path.GetFileName(Path.GetTempFileName())); - - using(var fs = new FileStream(filename, FileMode.CreateNew)) { - fs.Write(bytes); - fs.Seek(4096L * 1024 * 1024, SeekOrigin.Begin); - fs.WriteByte(0); - } - - files.Add(filename); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - - try { - await client.SendFileDropList(files); - } finally { - Directory.Delete(testsPath, true); - await Task.Delay(3000); - await server.Stop(); - client.Stop(); - } - - dispatchServiceMock.VerifyAll(); - Assert.IsNotNull(fileDropList); - var otherFilename = fileDropList[0]; - - Assert.That(otherFilename, Does.Exist); - - - using(var fs = new FileStream(otherFilename, FileMode.Open, FileAccess.Read)) { - Assert.That(fs.Length, Is.EqualTo(4096L * 1024 * 1024 + 1)); - - var otherBytes = new byte[1_000_003]; - fs.Read(otherBytes); - Assert.That(otherBytes, Is.EquivalentTo(bytes)); - } - File.Delete(otherFilename); - - } - - [Test] - public async Task Send_Files_And_Folders_Test() { - IList? fileDropList = null; - - dispatchServiceMock - .Setup(x => x.ReceiveFiles(It.IsAny>())) - .Callback>(x => fileDropList = x); - - - var testsPath = Path.Combine(Path.GetTempPath(), "tests"); - Directory.CreateDirectory(testsPath); - - var rnd = new Random(); - var bytes0 = new byte[555_000]; - rnd.NextBytes(bytes0); - var bytes1 = new byte[777_000]; - rnd.NextBytes(bytes1); - - var filename0 = Path.Combine(testsPath, "filename0.bin"); - File.WriteAllBytes(filename0, bytes0); - - var directory0 = Path.Combine(testsPath, "directory0"); - Directory.CreateDirectory(directory0); - - var directory0_filename1 = Path.Combine(directory0, "filename1.bin"); - File.WriteAllBytes(directory0_filename1, bytes1); - - Directory.CreateDirectory(Path.Combine(directory0, "Child0")); - - var directory0_child1 = Path.Combine(directory0, "Дочерний1™"); - Directory.CreateDirectory(directory0_child1); - - var directory0_child1_empty0 = Path.Combine(directory0_child1, "Empty0"); - Directory.CreateDirectory(directory0_child1_empty0); - - var filename2 = Path.Combine(directory0_child1, "Файл2.dat"); - File.WriteAllBytes(filename2, bytes1); - - var files = new StringCollection(); - files.Add(filename0); - files.Add(directory0); - files.Add(directory0_filename1); - files.Add(directory0_child1); - files.Add(directory0_child1_empty0); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - try { - await client.SendFileDropList(files); - } finally { - Directory.Delete(testsPath, true); - await Task.Delay(1000); - await server.Stop(); - client.Stop(); - } - - dispatchServiceMock.VerifyAll(); - Assert.IsNotNull(fileDropList); - - Assert.That(fileDropList.Count, Is.EqualTo(5)); - - Assert.That(fileDropList.First(x => Path.GetFileName(x) == "filename0.bin"), Does.Exist); - Assert.That(fileDropList.First(x => x.EndsWith("directory0")), Does.Exist); - Assert.That(fileDropList.First(x => Path.GetFileName(x) == "filename1.bin"), Does.Exist); - Assert.That(fileDropList.First(x => x.EndsWith("Дочерний1™")), Does.Exist); - Assert.That(fileDropList.First(x => x.EndsWith("Empty0")), Does.Exist); - - const string pathShareClipbrd = "ShareClipbrd_60D54950"; - var tempDir = Path.Combine(Path.GetTempPath(), pathShareClipbrd); - var storedFiles = DirectoryHelper.RecursiveGetFiles(tempDir) - .Concat(DirectoryHelper.RecursiveGetEmptyFolders(tempDir)) - .Select(x => x.Replace('\\', Path.AltDirectorySeparatorChar)); - Assert.That(storedFiles.Count, Is.EqualTo(9)); - - var otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/filename0.bin")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes0)); - - otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/filename1.bin")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); - - var otherDirectory = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Empty0")); - Assert.That(otherDirectory, Does.Exist); - - otherDirectory = storedFiles.First(x => x.EndsWith("directory0/Child0")); - Assert.That(otherDirectory, Does.Exist); - - otherDirectory = storedFiles.First(x => x.EndsWith("directory0/Дочерний1™/Empty0")); - Assert.That(otherDirectory, Does.Exist); - - otherFilename = storedFiles.First(x => x.EndsWith("directory0/Дочерний1™/Файл2.dat")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); - - otherFilename = storedFiles.First(x => x.EndsWith("directory0/filename1.bin")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); - - otherDirectory = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Дочерний1™/Empty0")); - Assert.That(otherDirectory, Does.Exist); - - otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Дочерний1™/Файл2.dat")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); - - progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Send)), Times.Once); - progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Receive)), Times.Once); - progressServiceMock.Verify(x => x.SetMaxTick(It.IsAny()), Times.Exactly(2)); - progressServiceMock.Verify(x => x.Tick(It.IsAny()), Times.Exactly(9 * 2)); - } - - [Test] - public async Task Send_identical_Files__Test() { - IList? fileDropList = null; - - dispatchServiceMock - .Setup(x => x.ReceiveFiles(It.IsAny>())) - .Callback>(x => fileDropList = x); - - - var testsPath = Path.Combine(Path.GetTempPath(), "tests"); - Directory.CreateDirectory(testsPath); - - var rnd = new Random(); - var bytes0 = new byte[1000]; - rnd.NextBytes(bytes0); - - var files = new StringCollection(); - - var filename0 = Path.Combine(testsPath, "filename0.bin"); - File.WriteAllBytes(filename0, bytes0); - files.Add(filename0); - files.Add(filename0); - files.Add(filename0); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - try { - await client.SendFileDropList(files); - } finally { - Directory.Delete(testsPath, true); - await Task.Delay(500); - await server.Stop(); - client.Stop(); - } - - dispatchServiceMock.VerifyAll(); - Assert.IsNotNull(fileDropList); - Assert.That(fileDropList.Count, Is.EqualTo(1)); - - var otherFilename = fileDropList[0]; - Assert.That(otherFilename, Does.Exist); - Assert.That(Path.GetFileName(otherFilename), Is.EqualTo("filename0.bin")); - Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes0)); - - progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Send)), Times.Once); - progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Receive)), Times.Once); - progressServiceMock.Verify(x => x.SetMaxTick(It.IsAny()), Times.Exactly(2)); - progressServiceMock.Verify(x => x.Tick(It.IsAny()), Times.Exactly(2)); - } - - [Test] - public async Task Sequential_Calls_Stop_Server_Test() { - await server.Stop(); - connectStatusServiceMock.Verify(x => x.Offline(), Times.Once()); - await server.Stop(); - connectStatusServiceMock.Verify(x => x.Offline(), Times.Exactly(2)); - } - - - bool clientConnected = false; - async Task AwaitClientConnectStatus(bool isConnected) { - var cts = new CancellationTokenSource(5000); - while(clientConnected != isConnected && !cts.IsCancellationRequested) { - await Task.Delay(10); - } - Assert.That(clientConnected, Is.EqualTo(isConnected)); - } - - [Test] - public async Task DataClient_Ping_Periodic_Test() { - timeServiceMock.SetupGet(x => x.DataClientPingPeriod).Returns(TimeSpan.FromMilliseconds(100)); - clientConnected = false; - connectStatusServiceMock - .Setup(x => x.ClientOnline()) - .Callback(() => { - clientConnected = true; - Debug.WriteLine("clientConnected = true"); - }); - - connectStatusServiceMock - .Setup(x => x.ClientOffline()) - .Callback(() => { - clientConnected = false; - Debug.WriteLine("clientConnected = false"); - }); - - var port = await server.Start(); - systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); - client.Start(); - - await AwaitClientConnectStatus(true); - - connectStatusServiceMock.Verify(x => x.ClientOffline(), Times.Once()); - connectStatusServiceMock.Verify(x => x.ClientOnline(), Times.Once()); - - await Task.Delay(1000); - - connectStatusServiceMock.Verify(x => x.ClientOffline(), Times.Once()); - connectStatusServiceMock.Verify(x => x.ClientOnline(), Times.AtLeast(3)); - - await server.Stop(); - client.Stop(); - } - } -} +using System.Collections.Specialized; +using System.Diagnostics; +using Clipboard.Core; +using Moq; +using ShareClipbrd.Core.Configuration; +using ShareClipbrd.Core.Helpers; +using ShareClipbrd.Core.Services; + +namespace ShareClipbrd.Core.Tests.Services { + public class Tests { + Mock systemConfigurationMock; + Mock dialogServiceMock; + Mock dispatchServiceMock; + Mock progressServiceMock; + Mock connectStatusServiceMock; + Mock timeServiceMock; + Mock addressDiscoveryServiceMock; + DataServer server; + DataClient client; + + + [SetUp] + public void Setup() { + systemConfigurationMock = new(); + dialogServiceMock = new(); + dispatchServiceMock = new(); + progressServiceMock = new(); + connectStatusServiceMock = new(); + timeServiceMock = new(); + addressDiscoveryServiceMock = new(); + timeServiceMock.SetupGet(x => x.DataClientPingPeriod).Returns(TimeSpan.FromMilliseconds(10000)); + + systemConfigurationMock.SetupGet(x => x.HostAddress).Returns("127.0.0.1:0"); + + server = new DataServer(systemConfigurationMock.Object, dialogServiceMock.Object, dispatchServiceMock.Object, + progressServiceMock.Object, connectStatusServiceMock.Object, addressDiscoveryServiceMock.Object); + client = new DataClient(systemConfigurationMock.Object, progressServiceMock.Object, + connectStatusServiceMock.Object, dialogServiceMock.Object, timeServiceMock.Object, addressDiscoveryServiceMock.Object); + + } + + [TearDown] + public void Teardown() { + } + + [Test] + public async Task Send_CommonData_Test() { + var receivedClipboard = new List(); + + dispatchServiceMock + .Setup(x => x.ReceiveData(It.IsAny())) + .Callback(x => receivedClipboard.Add(x)); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + + var clipboardData = new ClipboardData(); + clipboardData.Add("UnicodeText", new MemoryStream(System.Text.Encoding.Unicode.GetBytes("UnicodeText юникод Œ"))); + await client.SendData(clipboardData); + + clipboardData = new ClipboardData(); + clipboardData.Add("Text", new MemoryStream(System.Text.Encoding.Unicode.GetBytes("Text 0123456789"))); + + await client.SendData(clipboardData); + await Task.Delay(500); + await server.Stop(); + client.Stop(); + + dispatchServiceMock.Verify(x => x.ReceiveData(It.IsAny()), Times.Exactly(2)); + Assert.That(receivedClipboard, Is.Not.Null); + Assert.That(receivedClipboard.SelectMany(x => x!.Formats).Select(x => x.Format), Is.EquivalentTo(new[] { "UnicodeText", "Text" })); + Assert.That(receivedClipboard.SelectMany(x => x!.Formats).Select(x => x.Stream), Is.EquivalentTo(new[] { + new MemoryStream(System.Text.Encoding.Unicode.GetBytes("UnicodeText юникод Œ")), + new MemoryStream(System.Text.Encoding.Unicode.GetBytes("Text 0123456789")) + })); + } + + [Test] + public async Task Send_Common_Big_Data_Test() { + ClipboardData? receivedClipboard = null; + + dispatchServiceMock + .Setup(x => x.ReceiveData(It.IsAny())) + .Callback(x => receivedClipboard = x); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + + var clipboardData = new ClipboardData(); + + var rnd = new Random(); + var bytes = new byte[1_000_000_003]; + rnd.NextBytes(bytes); + + clipboardData.Add("Text", new MemoryStream(bytes)); + + await client.SendData(clipboardData); + await Task.Delay(3000); + await server.Stop(); + client.Stop(); + + dispatchServiceMock.VerifyAll(); + Assert.That(receivedClipboard, Is.Not.Null); + Assert.That(receivedClipboard.Formats.Select(x => x.Format), Is.EquivalentTo(new[] { "Text" })); + Assert.That(receivedClipboard.Formats.First(x => x.Format == "Text").Stream, Has.Length.EqualTo(1_000_000_003)); + Assert.That(receivedClipboard.Formats.First(x => x.Format == "Text").Stream.ToArray().Take(1_000_000), Is.EquivalentTo(bytes.Take(1_000_000))); + + } + + [Test] + public async Task Send_Files_Test() { + IList? fileDropList = null; + + dispatchServiceMock + .Setup(x => x.ReceiveFiles(It.IsAny>())) + .Callback>(x => fileDropList = x); + + var clipboardData = new ClipboardData(); + + var testsPath = Path.Combine(Path.GetTempPath(), "tests"); + Directory.CreateDirectory(testsPath); + + var rnd = new Random(); + var bytes = new byte[3_333_333]; + rnd.NextBytes(bytes); + var testdata = new MemoryStream(bytes); + + var files = new StringCollection(); + + var buffer = new byte[3_333_333 / 100]; + testdata.Position = 0; + for(int i = 0; i < 100; i++) { + var filename = Path.Combine(testsPath, $"Unicode юникод ® _{i}"); + testdata.Read(buffer, 0, buffer.Length); + + File.WriteAllBytes(filename, buffer); + files.Add(filename); + } + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + try { + await client.SendFileDropList(files); + await client.SendFileDropList(files); + await client.SendFileDropList(files); + + } finally { + Directory.Delete(testsPath, true); + await Task.Delay(1000); + await server.Stop(); + client.Stop(); + } + + dispatchServiceMock.Verify(x => x.ReceiveFiles(It.IsAny>()), Times.Exactly(3)); + Assert.That(fileDropList, Is.Not.Null); + Assert.That(fileDropList.Count, Is.EqualTo(100)); + + + testdata.Position = 0; + for(int i = 0; i < 100; i++) { + var otherFilename = fileDropList.First(x => x.EndsWith($"Unicode юникод ® _{i}")); + + Assert.That(otherFilename, Does.Exist); + + var otherBytes = File.ReadAllBytes(otherFilename); + File.Delete(otherFilename); + + Assert.That(otherBytes, Has.Length.EqualTo(3_333_333 / 100)); + testdata.Read(buffer, 0, buffer.Length); + Assert.That(otherBytes, Is.EquivalentTo(buffer)); + } + } + + [Test] + public async Task Send_Big_File_Test() { + IList? fileDropList = null; + + dispatchServiceMock + .Setup(x => x.ReceiveFiles(It.IsAny>())) + .Callback>(x => fileDropList = x); + + var rnd = new Random(); + var bytes = new byte[1_000_003]; + rnd.NextBytes(bytes); + + var testsPath = Path.Combine(Path.GetTempPath(), "tests"); + Directory.CreateDirectory(testsPath); + + var files = new StringCollection(); + var filename = Path.Combine(testsPath, Path.GetFileName(Path.GetTempFileName())); + + using(var fs = new FileStream(filename, FileMode.CreateNew)) { + fs.Write(bytes); + fs.Seek(4096L * 1024 * 1024, SeekOrigin.Begin); + fs.WriteByte(0); + } + + files.Add(filename); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + + try { + await client.SendFileDropList(files); + } finally { + Directory.Delete(testsPath, true); + await Task.Delay(3000); + await server.Stop(); + client.Stop(); + } + + dispatchServiceMock.VerifyAll(); + Assert.That(fileDropList, Is.Not.Null); + var otherFilename = fileDropList[0]; + + Assert.That(otherFilename, Does.Exist); + + + using(var fs = new FileStream(otherFilename, FileMode.Open, FileAccess.Read)) { + Assert.That(fs.Length, Is.EqualTo(4096L * 1024 * 1024 + 1)); + + var otherBytes = new byte[1_000_003]; + fs.Read(otherBytes); + Assert.That(otherBytes, Is.EquivalentTo(bytes)); + } + File.Delete(otherFilename); + + } + + [Test] + public async Task Send_Files_And_Folders_Test() { + IList? fileDropList = null; + + dispatchServiceMock + .Setup(x => x.ReceiveFiles(It.IsAny>())) + .Callback>(x => fileDropList = x); + + + var testsPath = Path.Combine(Path.GetTempPath(), "tests"); + Directory.CreateDirectory(testsPath); + + var rnd = new Random(); + var bytes0 = new byte[555_000]; + rnd.NextBytes(bytes0); + var bytes1 = new byte[777_000]; + rnd.NextBytes(bytes1); + + var filename0 = Path.Combine(testsPath, "filename0.bin"); + File.WriteAllBytes(filename0, bytes0); + + var directory0 = Path.Combine(testsPath, "directory0"); + Directory.CreateDirectory(directory0); + + var directory0_filename1 = Path.Combine(directory0, "filename1.bin"); + File.WriteAllBytes(directory0_filename1, bytes1); + + Directory.CreateDirectory(Path.Combine(directory0, "Child0")); + + var directory0_child1 = Path.Combine(directory0, "Дочерний1™"); + Directory.CreateDirectory(directory0_child1); + + var directory0_child1_empty0 = Path.Combine(directory0_child1, "Empty0"); + Directory.CreateDirectory(directory0_child1_empty0); + + var filename2 = Path.Combine(directory0_child1, "Файл2.dat"); + File.WriteAllBytes(filename2, bytes1); + + var files = new StringCollection(); + files.Add(filename0); + files.Add(directory0); + files.Add(directory0_filename1); + files.Add(directory0_child1); + files.Add(directory0_child1_empty0); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + try { + await client.SendFileDropList(files); + } finally { + Directory.Delete(testsPath, true); + await Task.Delay(1000); + await server.Stop(); + client.Stop(); + } + + dispatchServiceMock.VerifyAll(); + Assert.That(fileDropList, Is.Not.Null); + + Assert.That(fileDropList.Count, Is.EqualTo(5)); + + Assert.That(fileDropList.First(x => Path.GetFileName(x) == "filename0.bin"), Does.Exist); + Assert.That(fileDropList.First(x => x.EndsWith("directory0")), Does.Exist); + Assert.That(fileDropList.First(x => Path.GetFileName(x) == "filename1.bin"), Does.Exist); + Assert.That(fileDropList.First(x => x.EndsWith("Дочерний1™")), Does.Exist); + Assert.That(fileDropList.First(x => x.EndsWith("Empty0")), Does.Exist); + + const string pathShareClipbrd = "ShareClipbrd_60D54950"; + var tempDir = Path.Combine(Path.GetTempPath(), pathShareClipbrd); + var storedFiles = DirectoryHelper.RecursiveGetFiles(tempDir) + .Concat(DirectoryHelper.RecursiveGetEmptyFolders(tempDir)) + .Select(x => x.Replace('\\', Path.AltDirectorySeparatorChar)); + Assert.That(storedFiles.Count, Is.EqualTo(9)); + + var otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/filename0.bin")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes0)); + + otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/filename1.bin")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); + + var otherDirectory = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Empty0")); + Assert.That(otherDirectory, Does.Exist); + + otherDirectory = storedFiles.First(x => x.EndsWith("directory0/Child0")); + Assert.That(otherDirectory, Does.Exist); + + otherDirectory = storedFiles.First(x => x.EndsWith("directory0/Дочерний1™/Empty0")); + Assert.That(otherDirectory, Does.Exist); + + otherFilename = storedFiles.First(x => x.EndsWith("directory0/Дочерний1™/Файл2.dat")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); + + otherFilename = storedFiles.First(x => x.EndsWith("directory0/filename1.bin")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); + + otherDirectory = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Дочерний1™/Empty0")); + Assert.That(otherDirectory, Does.Exist); + + otherFilename = storedFiles.First(x => x.EndsWith(pathShareClipbrd + "/Дочерний1™/Файл2.dat")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes1)); + + progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Send)), Times.Once); + progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Receive)), Times.Once); + progressServiceMock.Verify(x => x.SetMaxTick(It.IsAny()), Times.Exactly(2)); + progressServiceMock.Verify(x => x.Tick(It.IsAny()), Times.Exactly(9 * 2)); + } + + [Test] + public async Task Send_identical_Files__Test() { + IList? fileDropList = null; + + dispatchServiceMock + .Setup(x => x.ReceiveFiles(It.IsAny>())) + .Callback>(x => fileDropList = x); + + + var testsPath = Path.Combine(Path.GetTempPath(), "tests"); + Directory.CreateDirectory(testsPath); + + var rnd = new Random(); + var bytes0 = new byte[1000]; + rnd.NextBytes(bytes0); + + var files = new StringCollection(); + + var filename0 = Path.Combine(testsPath, "filename0.bin"); + File.WriteAllBytes(filename0, bytes0); + files.Add(filename0); + files.Add(filename0); + files.Add(filename0); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + try { + await client.SendFileDropList(files); + } finally { + Directory.Delete(testsPath, true); + await Task.Delay(500); + await server.Stop(); + client.Stop(); + } + + dispatchServiceMock.VerifyAll(); + Assert.That(fileDropList, Is.Not.Null); + Assert.That(fileDropList.Count, Is.EqualTo(1)); + + var otherFilename = fileDropList[0]; + Assert.That(otherFilename, Does.Exist); + Assert.That(Path.GetFileName(otherFilename), Is.EqualTo("filename0.bin")); + Assert.That(File.ReadAllBytes(otherFilename), Is.EquivalentTo(bytes0)); + + progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Send)), Times.Once); + progressServiceMock.Verify(x => x.Begin(It.Is(p => p == ProgressMode.Receive)), Times.Once); + progressServiceMock.Verify(x => x.SetMaxTick(It.IsAny()), Times.Exactly(2)); + progressServiceMock.Verify(x => x.Tick(It.IsAny()), Times.Exactly(2)); + } + + [Test] + public async Task Sequential_Calls_Stop_Server_Test() { + await server.Stop(); + connectStatusServiceMock.Verify(x => x.Offline(), Times.Once()); + await server.Stop(); + connectStatusServiceMock.Verify(x => x.Offline(), Times.Exactly(2)); + } + + + bool clientConnected = false; + async Task AwaitClientConnectStatus(bool isConnected) { + var cts = new CancellationTokenSource(5000); + while(clientConnected != isConnected && !cts.IsCancellationRequested) { + await Task.Delay(10); + } + Assert.That(clientConnected, Is.EqualTo(isConnected)); + } + + [Test] + public async Task DataClient_Ping_Periodic_Test() { + timeServiceMock.SetupGet(x => x.DataClientPingPeriod).Returns(TimeSpan.FromMilliseconds(100)); + clientConnected = false; + connectStatusServiceMock + .Setup(x => x.ClientOnline()) + .Callback(() => { + clientConnected = true; + Debug.WriteLine("clientConnected = true"); + }); + + connectStatusServiceMock + .Setup(x => x.ClientOffline()) + .Callback(() => { + clientConnected = false; + Debug.WriteLine("clientConnected = false"); + }); + + var port = await server.Start(); + systemConfigurationMock.SetupGet(x => x.PartnerAddress).Returns($"127.0.0.1:{port}"); + client.Start(); + + await AwaitClientConnectStatus(true); + + connectStatusServiceMock.Verify(x => x.ClientOffline(), Times.Once()); + connectStatusServiceMock.Verify(x => x.ClientOnline(), Times.Once()); + + await Task.Delay(1000); + + connectStatusServiceMock.Verify(x => x.ClientOffline(), Times.Once()); + connectStatusServiceMock.Verify(x => x.ClientOnline(), Times.AtLeast(3)); + + await server.Stop(); + client.Stop(); + } + } +} diff --git a/ShareClipbrd/ShareClipbrd.Core.Tests/ShareClipbrd.Core.Tests.csproj b/ShareClipbrd/ShareClipbrd.Core.Tests/ShareClipbrd.Core.Tests.csproj index 684e009..58091e4 100644 --- a/ShareClipbrd/ShareClipbrd.Core.Tests/ShareClipbrd.Core.Tests.csproj +++ b/ShareClipbrd/ShareClipbrd.Core.Tests/ShareClipbrd.Core.Tests.csproj @@ -1,29 +1,32 @@ - - - - net7.0-windows - net7.0 - enable - enable - - false - true - - - - - - - - - - - - - - - - - - - + + + + net8.0-windows + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/ShareClipbrd/ShareClipbrd.Core/ShareClipbrd.Core.csproj b/ShareClipbrd/ShareClipbrd.Core/ShareClipbrd.Core.csproj index 64bc96d..d3b1725 100644 --- a/ShareClipbrd/ShareClipbrd.Core/ShareClipbrd.Core.csproj +++ b/ShareClipbrd/ShareClipbrd.Core/ShareClipbrd.Core.csproj @@ -1,30 +1,30 @@ - - - - net7.0-windows - net7.0 - enable - enable - - - - - - - - - - - - - - - - - - - - - - - + + + + net8.0-windows + net8.0 + enable + enable + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ShareClipbrd/ShareClipbrdApp.Tests/ShareClipbrdApp.Tests.csproj b/ShareClipbrd/ShareClipbrdApp.Tests/ShareClipbrdApp.Tests.csproj index f154e3c..afef40b 100644 --- a/ShareClipbrd/ShareClipbrdApp.Tests/ShareClipbrdApp.Tests.csproj +++ b/ShareClipbrd/ShareClipbrdApp.Tests/ShareClipbrdApp.Tests.csproj @@ -1,24 +1,27 @@ - - - - net7.0-windows - enable - enable - - false - true - - - - - - - - - - - - - - - + + + + net8.0-windows + enable + enable + + false + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj b/ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj index 1e0f9aa..95bfe00 100644 --- a/ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj +++ b/ShareClipbrd/ShareClipbrdApp/ShareClipbrdApp.csproj @@ -1,8 +1,8 @@ - + WinExe - net7.0-windows - net7.0 + net8.0-windows + net8.0 enable true app.manifest @@ -37,8 +37,8 @@ - - + +