using System.Security.Cryptography; using Agent.Configuration; using Agent.Files; using Backend.DeviceFiles; using Microsoft.Extensions.Logging.Abstractions; using NATS.Client.Core; using Shared.Contracts.Messages; using Shared.Infrastructure.Configuration; using Xunit; namespace Integration.Tests; /// /// The file browser over a real NATS server: the agent's listener against the /// server's client, each on its own connection, on a real temporary folder. /// Requires NATS at NATS_URL (default nats://127.0.0.1:4222); skipped when unreachable. /// public sealed class DeviceFileIntegrationTests : IAsyncLifetime { private static string NatsUrl => Environment.GetEnvironmentVariable("NATS_URL") ?? "nats://127.0.0.1:4222"; private readonly string clientId = "itest" + Guid.NewGuid().ToString("N")[..16]; private readonly string root = Directory.CreateTempSubdirectory("device-files-nats-").FullName; private readonly CancellationTokenSource stop = new(); private NatsConnection? agent; private NatsConnection? server; private Task? listening; private NatsDeviceFileClient? client; private static CancellationToken Ct => TestContext.Current.CancellationToken; private static AgentConfiguration Config(bool allowFiles) => new() { ServerUrlText = new(null, ConfigurationSource.Default), BootstrapToken = new(null, ConfigurationSource.Default), LogLevel = new("Information", ConfigurationSource.Default), InventoryInterval = new(TimeSpan.FromMinutes(15), ConfigurationSource.Default), ReconnectMinDelay = new(TimeSpan.FromSeconds(1), ConfigurationSource.Default), ReconnectMaxDelay = new(TimeSpan.FromSeconds(60), ConfigurationSource.Default), AllowRemoteShell = new(false, ConfigurationSource.Default), AllowScriptExecution = new(true, ConfigurationSource.Default), AllowFileAccess = new(allowFiles, ConfigurationSource.Default), TlsSkipVerify = new(false, ConfigurationSource.Default), ProxyUrl = new(null, ConfigurationSource.Default), }; public async ValueTask InitializeAsync() { var opts = NatsOpts.Default with { Url = NatsUrl, ConnectTimeout = TimeSpan.FromSeconds(2), MaxReconnectRetry = 0 }; var first = new NatsConnection(opts); try { await first.ConnectAsync(); } catch (Exception) { await first.DisposeAsync(); return; } server = first; agent = new NatsConnection(opts); await agent.ConnectAsync(); client = new NatsDeviceFileClient(() => server); await StartListeningAsync(allowFiles: true); } private async Task StartListeningAsync(bool allowFiles) { var listener = new DeviceFileListener(agent!, clientId, Config(allowFiles), NullLogger.Instance); listening = listener.RunAsync(stop.Token); // The subscription is live once a request is answered. for (var i = 0; i < 100; i++) { try { await client!.RequestAsync(clientId, new DeviceFileRequest(DeviceFileOperations.Roots), TimeSpan.FromSeconds(1), Ct); return; } catch (DeviceFilesUnavailableException) { await Task.Delay(50, Ct); } } } public async ValueTask DisposeAsync() { await stop.CancelAsync(); if (listening is not null) { try { await listening; } catch (OperationCanceledException) { // Stopped. } } if (agent is not null) { try { await agent.DisposeAsync(); } catch (NatsTimeoutException) { // Now and then NATS.Net times out writing an unsubscribe after a 256 KB frame's // publish was cancelled part way (a cancelled download); the connection closes anyway. } } if (server is not null) { await server.DisposeAsync(); } stop.Dispose(); Directory.Delete(root, recursive: true); } private void RequireNats() { if (server is null) { Assert.Skip($"No NATS server reachable at {NatsUrl}; set NATS_URL to run this test."); } } private string At(string name) => Path.Combine(root, name); private Task Ask(DeviceFileRequest request) => client!.RequestAsync(clientId, request, TimeSpan.FromSeconds(10), Ct); [Fact] public async Task ADeviceThatIsNotThereSaysSoAtOnce() { RequireNats(); var started = DateTime.UtcNow; await Assert.ThrowsAsync( () => client!.RequestAsync("nobody" + Guid.NewGuid().ToString("N")[..12], new DeviceFileRequest(DeviceFileOperations.Roots), TimeSpan.FromSeconds(10), Ct)); Assert.True(DateTime.UtcNow - started < TimeSpan.FromSeconds(5), "No responders should answer without waiting out the timeout."); } [Fact] public async Task AFolderIsListedAndChanged() { RequireNats(); Assert.Equal(DeviceFileStatuses.Ok, (await Ask(new DeviceFileRequest(DeviceFileOperations.CreateFolder, Path: At("made")))).Status); Assert.Equal(DeviceFileStatuses.Ok, (await Ask(new DeviceFileRequest(DeviceFileOperations.Rename, Path: At("made"), Name: "named"))).Status); var listed = await Ask(new DeviceFileRequest(DeviceFileOperations.List, Path: root)); Assert.Equal(["named"], listed.Entries!.Select(e => e.Name)); var deleted = await Ask(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("named")])); Assert.Equal(DeviceFileStatuses.Ok, deleted.Deleted![0].Status); Assert.Empty(Directory.EnumerateFileSystemEntries(root)); } [Fact] public async Task ALargeFileGoesThereAndBackIntact() { RequireNats(); var data = RandomNumberGenerator.GetBytes(64 * 1024 * 1024 + 17); using (var content = new MemoryStream(data)) { var written = await client!.WriteAsync( clientId, new DeviceFileRequest(DeviceFileOperations.Write, Path: At("big.bin"), Length: data.Length), content, Ct); Assert.Equal(DeviceFileStatuses.Ok, written.Status); Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(data)), written.Sha256); } Assert.Equal(data.Length, new FileInfo(At("big.bin")).Length); await using var read = await client!.OpenReadAsync(clientId, new DeviceFileRequest(DeviceFileOperations.Read, Paths: [At("big.bin")]), Ct); Assert.Equal(data.Length, read.Reply.Length); using var back = new MemoryStream(); await read.CopyToAsync(back, Ct); Assert.True(SHA256.HashData(back.ToArray()).SequenceEqual(SHA256.HashData(data))); } [Fact] public async Task AFolderComesBackAsAZip() { RequireNats(); Directory.CreateDirectory(At("logs")); File.WriteAllText(Path.Combine(At("logs"), "a.log"), "aaa"); await using var read = await client!.OpenReadAsync(clientId, new DeviceFileRequest(DeviceFileOperations.Read, Paths: [At("logs")]), Ct); Assert.Null(read.Reply.Length); using var bytes = new MemoryStream(); await read.CopyToAsync(bytes, Ct); bytes.Position = 0; using var zip = new System.IO.Compression.ZipArchive(bytes); Assert.NotNull(zip.GetEntry("logs/a.log")); } [Fact] public async Task ACancelledUploadLeavesNothing() { RequireNats(); using var cancel = CancellationTokenSource.CreateLinkedTokenSource(Ct); var body = new BreakingStream(afterBytes: 1024 * 1024, cancel); await Assert.ThrowsAnyAsync(() => client!.WriteAsync( clientId, new DeviceFileRequest(DeviceFileOperations.Write, Path: At("cancelled.bin")), body, cancel.Token)); // The agent hears of it and removes its temporary file. for (var i = 0; i < 100 && Directory.EnumerateFileSystemEntries(root).Any(); i++) { await Task.Delay(50, Ct); } Assert.Empty(Directory.EnumerateFileSystemEntries(root)); } [Fact] public async Task ACancelledDownloadReleasesTheDevice() { RequireNats(); File.WriteAllBytes(At("big.bin"), new byte[32 * 1024 * 1024]); // More downloads started and dropped than the agent moves at once: slots come back. for (var i = 0; i < DeviceFileDefaults.MaxConcurrentTransfers + 2; i++) { await using var read = await client!.OpenReadAsync(clientId, new DeviceFileRequest(DeviceFileOperations.Read, Paths: [At("big.bin")]), Ct); Assert.Equal(DeviceFileStatuses.Ok, read.Reply.Status); } } [Fact] public async Task TextOpensAndSavesThroughTheStream() { RequireNats(); File.WriteAllBytes(At("a.reg"), [0xFF, 0xFE, 0x61, 0x00, 0x0D, 0x00, 0x0A, 0x00]); DeviceFileReply opened; await using (var read = await client!.OpenReadAsync(clientId, new DeviceFileRequest(DeviceFileOperations.ReadText, Path: At("a.reg")), Ct)) { opened = read.Reply; Assert.Equal(DeviceTextEncodings.Utf16Le, opened.Encoding); using var text = new MemoryStream(); await read.CopyToAsync(text, Ct); Assert.Equal("a\r\n", System.Text.Encoding.UTF8.GetString(text.ToArray())); } using var content = new MemoryStream("b\r\n"u8.ToArray()); var saved = await client.WriteAsync( clientId, new DeviceFileRequest( DeviceFileOperations.WriteText, Path: At("a.reg"), Encoding: opened.Encoding, LineEndings: opened.LineEndings, ExpectedSha256: opened.Version!.Sha256), content, Ct); Assert.Equal(DeviceFileStatuses.Ok, saved.Status); Assert.Equal(new byte[] { 0xFF, 0xFE, 0x62, 0x00, 0x0D, 0x00, 0x0A, 0x00 }, File.ReadAllBytes(At("a.reg"))); } [Fact] public async Task APolicyOffRefusesWhateverTheServerAsks() { RequireNats(); var other = "itest" + Guid.NewGuid().ToString("N")[..16]; var listener = new DeviceFileListener(agent!, other, Config(allowFiles: false), NullLogger.Instance); using var end = new CancellationTokenSource(); var running = listener.RunAsync(end.Token); DeviceFileReply? reply = null; for (var i = 0; i < 100 && reply is null; i++) { try { reply = await client!.RequestAsync(other, new DeviceFileRequest(DeviceFileOperations.List, Path: root), TimeSpan.FromSeconds(1), Ct); } catch (DeviceFilesUnavailableException) { await Task.Delay(50, Ct); } } Assert.Equal(DeviceFileStatuses.Disabled, reply!.Status); await end.CancelAsync(); await running; } /// An upload whose browser goes away part way. private sealed class BreakingStream(int afterBytes, CancellationTokenSource cancel) : Stream { private int sent; public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length => throw new NotSupportedException(); public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { if (sent >= afterBytes) { await cancel.CancelAsync(); await Task.Delay(Timeout.Infinite, cancellationToken); } var count = Math.Min(buffer.Length, 64 * 1024); sent += count; return count; } public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); public override void Flush() => throw new NotSupportedException(); public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); public override void SetLength(long value) => throw new NotSupportedException(); public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); } }