From 1f001445ed15f3e7913cea7b2bfaa0c38c5f284d Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Mon, 1 Apr 2024 09:57:52 -0700 Subject: [PATCH] Remove scan server tests --- .../Remoting/FallbackScanServerTests.cs | 48 ----- NAPS2.Sdk.Tests/Remoting/ScanServerTests.cs | 191 ------------------ .../Remoting/ScanServerTestsBase.cs | 66 ------ .../Remoting/SelfSignedCertScanServerTests.cs | 51 ----- .../Remoting/TlsScanServerTests.cs | 52 ----- 5 files changed, 408 deletions(-) delete mode 100644 NAPS2.Sdk.Tests/Remoting/FallbackScanServerTests.cs delete mode 100644 NAPS2.Sdk.Tests/Remoting/ScanServerTests.cs delete mode 100644 NAPS2.Sdk.Tests/Remoting/ScanServerTestsBase.cs delete mode 100644 NAPS2.Sdk.Tests/Remoting/SelfSignedCertScanServerTests.cs delete mode 100644 NAPS2.Sdk.Tests/Remoting/TlsScanServerTests.cs diff --git a/NAPS2.Sdk.Tests/Remoting/FallbackScanServerTests.cs b/NAPS2.Sdk.Tests/Remoting/FallbackScanServerTests.cs deleted file mode 100644 index 2c3f03f4cb..0000000000 --- a/NAPS2.Sdk.Tests/Remoting/FallbackScanServerTests.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Net.Http; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using NAPS2.Escl; -using NAPS2.Scan; -using NAPS2.Sdk.Tests.Asserts; -using Xunit; -using Xunit.Abstractions; - -namespace NAPS2.Sdk.Tests.Remoting; - -public class FallbackScanServerTests(ITestOutputHelper testOutputHelper) : ScanServerTestsBase(testOutputHelper, - EsclSecurityPolicy.None, new X509Certificate2(BinaryResources.testcert)) -{ - [Fact(Timeout = TIMEOUT)] - public async Task ScanFallbackFromHttpsToHttp() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - // This policy makes sure HTTPS will fail due to an untrusted certificate, which simulates the case - // where we're failing due to the server only supporting obsolete TLS versions. - SecurityPolicy = EsclSecurityPolicy.ClientRequireTrustedCertificate - } - }).ToListAsync(); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog, images[0]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanPreventedByTrustedCertificateSecurityPolicy() - { - var scanResult = _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireTrustedCertificate - } - }); - var exception = await Assert.ThrowsAsync(async () => await scanResult.ToListAsync()); - Assert.True(exception.InnerException is AuthenticationException || - exception.InnerException?.InnerException is AuthenticationException); - } -} \ No newline at end of file diff --git a/NAPS2.Sdk.Tests/Remoting/ScanServerTests.cs b/NAPS2.Sdk.Tests/Remoting/ScanServerTests.cs deleted file mode 100644 index af58b13060..0000000000 --- a/NAPS2.Sdk.Tests/Remoting/ScanServerTests.cs +++ /dev/null @@ -1,191 +0,0 @@ -using NAPS2.Escl; -using NAPS2.Scan; -using NAPS2.Scan.Exceptions; -using NAPS2.Sdk.Tests.Asserts; -using NSubstitute; -using Xunit; -using Xunit.Abstractions; - -namespace NAPS2.Sdk.Tests.Remoting; - -public class ScanServerTests(ITestOutputHelper testOutputHelper) - : ScanServerTestsBase(testOutputHelper, EsclSecurityPolicy.ServerDisableHttps) -{ - [Fact(Timeout = TIMEOUT)] - public async Task FindDevice() - { - Assert.True(await TryFindClientDevice()); - } - - [Fact(Timeout = TIMEOUT)] - public async Task Scan() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice - }).ToListAsync(); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog, images[0]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanMultiplePages() - { - _bridge.MockOutput = - CreateScannedImages(ImageResources.dog, ImageResources.dog_h_n300, ImageResources.dog_h_p300); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - PaperSource = PaperSource.Feeder - }).ToListAsync(); - Assert.Equal(3, images.Count); - ImageAsserts.Similar(ImageResources.dog, images[0]); - ImageAsserts.Similar(ImageResources.dog_h_n300, images[1]); - ImageAsserts.Similar(ImageResources.dog_h_p300, images[2]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanWithCorrectOptions() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - BitDepth = BitDepth.Color, - Dpi = 100, - PaperSource = PaperSource.Flatbed, - PageSize = PageSize.Letter, - PageAlign = HorizontalAlign.Right - }).ToListAsync(); - - var opts = _bridge.LastOptions; - Assert.Equal(BitDepth.Color, opts.BitDepth); - Assert.Equal(100, opts.Dpi); - Assert.Equal(PaperSource.Flatbed, opts.PaperSource); - Assert.Equal(PageSize.Letter, opts.PageSize); - Assert.Equal(HorizontalAlign.Right, opts.PageAlign); - Assert.Equal(75, opts.Quality); - Assert.False(opts.MaxQuality); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog, images[0]); - - _bridge.MockOutput = CreateScannedImages(ImageResources.dog_gray); - images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - BitDepth = BitDepth.Grayscale, - Dpi = 300, - PaperSource = PaperSource.Feeder, - PageSize = PageSize.Legal, - PageAlign = HorizontalAlign.Center, - Quality = 0, - MaxQuality = true - }).ToListAsync(); - - opts = _bridge.LastOptions; - Assert.Equal(BitDepth.Grayscale, opts.BitDepth); - Assert.Equal(300, opts.Dpi); - Assert.Equal(PaperSource.Feeder, opts.PaperSource); - Assert.Equal(PageSize.Legal, opts.PageSize); - Assert.Equal(HorizontalAlign.Center, opts.PageAlign); - Assert.Equal(0, opts.Quality); - Assert.True(opts.MaxQuality); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog_gray, images[0]); - - _bridge.MockOutput = CreateScannedImages(ImageResources.dog_bw); - images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - BitDepth = BitDepth.BlackAndWhite, - Dpi = 4800, - PaperSource = PaperSource.Duplex, - PageSize = PageSize.A3, - PageAlign = HorizontalAlign.Left, - Quality = 100 - }).ToListAsync(); - - opts = _bridge.LastOptions; - Assert.Equal(BitDepth.BlackAndWhite, opts.BitDepth); - Assert.Equal(4800, opts.Dpi); - Assert.Equal(PaperSource.Duplex, opts.PaperSource); - Assert.Equal(PageSize.A3.WidthInMm, opts.PageSize!.WidthInMm, 1); - Assert.Equal(PageSize.A3.HeightInMm, opts.PageSize!.HeightInMm, 1); - Assert.Equal(HorizontalAlign.Left, opts.PageAlign); - Assert.Equal(100, opts.Quality); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog_bw, images[0]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanWithError() - { - _bridge.Error = new DeviceFeederEmptyException(); - - await Assert.ThrowsAsync(async () => await _client.Scan(new ScanOptions - { - Device = _clientDevice, - PaperSource = PaperSource.Feeder - }).ToListAsync()); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanWithErrorAfterPage() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - _bridge.Error = new DeviceException(SdkResources.DevicePaperJam); - - await using var enumerator = _client.Scan(new ScanOptions - { - Device = _clientDevice, - PaperSource = PaperSource.Feeder - }).GetAsyncEnumerator(); - - Assert.True(await enumerator.MoveNextAsync()); - ImageAsserts.Similar(ImageResources.dog, enumerator.Current); - - var exception = await Assert.ThrowsAsync(async () => await enumerator.MoveNextAsync()); - Assert.Equal(SdkResources.DevicePaperJam, exception.Message); - } - - [Fact(Timeout = TIMEOUT, Skip = "Flaky")] - public async Task ScanProgress() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog, ImageResources.dog); - _bridge.ProgressReports = [0.5]; - - var pageStartMock = Substitute.For>(); - var pageProgressMock = Substitute.For>(); - _client.PageStart += pageStartMock; - _client.PageProgress += pageProgressMock; - - await _client.Scan(new ScanOptions - { - Device = _clientDevice, - PaperSource = PaperSource.Feeder - }).ToListAsync(); - - pageStartMock.Received()(Arg.Any(), Arg.Is(args => args.PageNumber == 1)); - // TODO: This flaked and we only got the second one - why? Can we fix it? - pageProgressMock.Received()(Arg.Any(), - Arg.Is(args => args.PageNumber == 1 && args.Progress == 0.5)); - pageStartMock.Received()(Arg.Any(), Arg.Is(args => args.PageNumber == 2)); - pageProgressMock.Received()(Arg.Any(), - Arg.Is(args => args.PageNumber == 2 && args.Progress == 0.5)); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanPreventedByHttpsSecurityPolicy() - { - var scanResult = _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireHttps - } - }); - await Assert.ThrowsAsync(async () => await scanResult.ToListAsync()); - } -} \ No newline at end of file diff --git a/NAPS2.Sdk.Tests/Remoting/ScanServerTestsBase.cs b/NAPS2.Sdk.Tests/Remoting/ScanServerTestsBase.cs deleted file mode 100644 index bdeb7c370e..0000000000 --- a/NAPS2.Sdk.Tests/Remoting/ScanServerTestsBase.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using Microsoft.Extensions.Logging; -using NAPS2.Escl; -using NAPS2.Escl.Server; -using NAPS2.Remoting.Server; -using NAPS2.Scan; -using NAPS2.Scan.Internal; -using NAPS2.Sdk.Tests.Mocks; -using NSubstitute; -using Xunit.Abstractions; - -namespace NAPS2.Sdk.Tests.Remoting; - -public class ScanServerTestsBase : ContextualTests -{ - protected const int TIMEOUT = 60_000; - - protected readonly ScanServer _server; - private protected readonly MockScanBridge _bridge; - protected readonly ScanController _client; - protected readonly ScanDevice _clientDevice; - - public ScanServerTestsBase(ITestOutputHelper testOutputHelper, - EsclSecurityPolicy securityPolicy = EsclSecurityPolicy.None, - X509Certificate2 certificate = null) : base(testOutputHelper) - { - _server = new ScanServer(ScanningContext, new EsclServer()); - - // Set up a server connecting to a mock scan backend - _bridge = new MockScanBridge(); - var scanBridgeFactory = Substitute.For(); - scanBridgeFactory.Create(Arg.Any()).Returns(_bridge); - _server.ScanController = new ScanController(ScanningContext, scanBridgeFactory); - _server.SecurityPolicy = securityPolicy; - _server.Certificate = certificate; - - // Initialize the server with a single device with a unique ID for the test - var displayName = $"testName-{Guid.NewGuid()}"; - ScanningContext.Logger.LogDebug("Display name: {Name}", displayName); - var serverDevice = new ScanDevice(ScanOptionsValidator.SystemDefaultDriver, "testID", "testName"); - _server.RegisterDevice(serverDevice, displayName); - _server.Start().Wait(); - - // Set up a client ScanController for scanning through EsclScanDriver -> network -> ScanServer - _client = new ScanController(ScanningContext); - var uuid = new ScanServerDevice { Device = serverDevice, Name = displayName }.GetUuid(_server.InstanceId); - _clientDevice = new ScanDevice(Driver.Escl, uuid, displayName); - } - - public override void Dispose() - { - _server.Stop().Wait(); - base.Dispose(); - } - - protected async Task TryFindClientDevice() - { - var cts = new CancellationTokenSource(); - // The device name is suffixed with the IP so we just check the prefix matches - bool found = await _client.GetDevices(Driver.Escl, cts.Token) - .AnyAsync(device => device.Name.StartsWith(_clientDevice.Name) && device.ID == _clientDevice.ID); - cts.Cancel(); - return found; - } -} \ No newline at end of file diff --git a/NAPS2.Sdk.Tests/Remoting/SelfSignedCertScanServerTests.cs b/NAPS2.Sdk.Tests/Remoting/SelfSignedCertScanServerTests.cs deleted file mode 100644 index 76c25517bd..0000000000 --- a/NAPS2.Sdk.Tests/Remoting/SelfSignedCertScanServerTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Net.Http; -using System.Security.Authentication; -using NAPS2.Escl; -using NAPS2.Scan; -using NAPS2.Sdk.Tests.Asserts; -using Xunit; -using Xunit.Abstractions; - -namespace NAPS2.Sdk.Tests.Remoting; - -public class SelfSignedCertScanServerTests(ITestOutputHelper testOutputHelper) - : ScanServerTestsBase(testOutputHelper, EsclSecurityPolicy.RequireHttps) -{ - [Fact(Timeout = TIMEOUT)] - public async Task FindDevice() - { - Assert.True(await TryFindClientDevice()); - } - - [Fact(Timeout = TIMEOUT)] - public async Task Scan() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireHttps - } - }).ToListAsync(); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog, images[0]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanPreventedByTrustedCertificateSecurityPolicy() - { - var scanResult = _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireTrustedCertificate - } - }); - var exception = await Assert.ThrowsAsync(async () => await scanResult.ToListAsync()); - Assert.True(exception.InnerException is AuthenticationException || - exception.InnerException?.InnerException is AuthenticationException); - } -} \ No newline at end of file diff --git a/NAPS2.Sdk.Tests/Remoting/TlsScanServerTests.cs b/NAPS2.Sdk.Tests/Remoting/TlsScanServerTests.cs deleted file mode 100644 index e5a2b37651..0000000000 --- a/NAPS2.Sdk.Tests/Remoting/TlsScanServerTests.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Net.Http; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using NAPS2.Escl; -using NAPS2.Scan; -using NAPS2.Sdk.Tests.Asserts; -using Xunit; -using Xunit.Abstractions; - -namespace NAPS2.Sdk.Tests.Remoting; - -public class TlsScanServerTests(ITestOutputHelper testOutputHelper) : ScanServerTestsBase(testOutputHelper, - EsclSecurityPolicy.RequireHttps, new X509Certificate2(BinaryResources.testcert)) -{ - [Fact(Timeout = TIMEOUT)] - public async Task FindDevice() - { - Assert.True(await TryFindClientDevice()); - } - - [Fact(Timeout = TIMEOUT)] - public async Task Scan() - { - _bridge.MockOutput = CreateScannedImages(ImageResources.dog); - var images = await _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireHttps - } - }).ToListAsync(); - Assert.Single(images); - ImageAsserts.Similar(ImageResources.dog, images[0]); - } - - [Fact(Timeout = TIMEOUT)] - public async Task ScanPreventedByTrustedCertificateSecurityPolicy() - { - var scanResult = _client.Scan(new ScanOptions - { - Device = _clientDevice, - EsclOptions = - { - SecurityPolicy = EsclSecurityPolicy.RequireTrustedCertificate - } - }); - var exception = await Assert.ThrowsAsync(async () => await scanResult.ToListAsync()); - Assert.True(exception.InnerException is AuthenticationException || - exception.InnerException?.InnerException is AuthenticationException); - } -} \ No newline at end of file