Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Write each MicroVM frame in one write and test the agent on this machine

MicroVMProtocol.WriteAsync wrote a frame's header and payload in two writes,
and the agent pumps a command's output and errors from two tasks: a command
that wrote to both at once could interleave two headers before their
payloads and break the connection. A frame is now one write, and the agent
writes its frames one at a time.

The agent serves a connection on any Stream (ServeAsync is internal and takes
the power-off as an action), so AgentTests run it at one end of a socket
pair: Hello and Ready, PowerOff, a connection that opens with anything else,
a broken frame, a command with output, errors and its status, 3000 lines on
each stream at once arriving as whole frames, every refusal (empty, outside
the shares, a read-only share, a missing directory or program, a file that
cannot start), Stop and a host that leaves, Find, a VsockStream over a Unix
socket pair, and the vsock address's equality.
Commit
d9cfbf0737f24f170be1dd489b0e7e91ec2e3e28
Author
Leitwolf11 ·
Parents
f265b6d8bd1d

Files · Download .tar.gz

Changes

 .github/workflows/ci.yml                   |   8 +
 src/Progmasoft.Tazel.Guest/Agent.cs        |  31 ++-
 src/Shared/MicroVMProtocol.cs              |  15 +-
 tests/Progmasoft.Tazel.Tests/AgentTests.cs | 325 +++++++++++++++++++++++++++++
 4 files changed, 360 insertions(+), 19 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 3abd8f1..de4cb80 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -108,6 +108,14 @@ jobs:
           --filter-class Progmasoft.Tazel.Tests.ActionCacheTests
           --coverlet --coverlet-output-format cobertura --results-directory artifacts/coverage
 
+      - name: Test AgentTests (macOS)
+        if: runner.os == 'macOS'
+        timeout-minutes: 10
+        run: >-
+          dotnet test --project tests/Progmasoft.Tazel.Tests -c Release --no-build --output Detailed
+          --filter-class Progmasoft.Tazel.Tests.AgentTests
+          --coverlet --coverlet-output-format cobertura --results-directory artifacts/coverage
+
       - name: Test ArchiveTests (macOS)
         if: runner.os == 'macOS'
         timeout-minutes: 10
diff --git a/src/Progmasoft.Tazel.Guest/Agent.cs b/src/Progmasoft.Tazel.Guest/Agent.cs
index 9e7da91..1f6e1e0 100644
--- a/src/Progmasoft.Tazel.Guest/Agent.cs
+++ b/src/Progmasoft.Tazel.Guest/Agent.cs
@@ -115,7 +115,7 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
                 var connection = Libc.Accept(listener, IntPtr.Zero, IntPtr.Zero);
                 if (connection >= 0)
                 {
-                    _ = Task.Run(() => ServeAsync(new VsockStream(connection), off));
+                    _ = Task.Run(() => ServeAsync(new VsockStream(connection), off.Set));
                 }
             }
         }) { IsBackground = true, Name = "vsock" };
@@ -193,8 +193,9 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
         return "/" + string.Join('/', parts);
     }
 
+    /// <summary>Serves one connection: Hello, a command, or the order to power off, which calls <paramref name="off"/>.</summary>
     [System.Runtime.Versioning.SupportedOSPlatform("linux")]
-    private async Task ServeAsync(VsockStream stream, ManualResetEventSlim off)
+    internal async Task ServeAsync(Stream stream, Action off)
     {
         using (stream)
         {
@@ -211,7 +212,7 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
                         await RunAsync(stream, MicroVMProtocol.DecodeRun(message.Value.Payload)).ConfigureAwait(false);
                         break;
                     case MessageKind.PowerOff:
-                        off.Set();
+                        off();
                         break;
                     default:
                         // A connection opens with Hello, Run or PowerOff; anything else is closed unanswered.
@@ -226,7 +227,7 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
     }
 
     [System.Runtime.Versioning.SupportedOSPlatform("linux")]
-    private async Task RunAsync(VsockStream stream, RunRequest request)
+    private async Task RunAsync(Stream stream, RunRequest request)
     {
         if (request.Arguments.Count == 0)
         {
@@ -296,8 +297,10 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
                 process.Kill(entireProcessTree: true);
             }
         });
-        var output = Pump(process.StandardOutput.BaseStream, 1, stream);
-        var errors = Pump(process.StandardError.BaseStream, 2, stream);
+        // Output and errors are written from two tasks; the frames go out one at a time.
+        using var writing = new SemaphoreSlim(1, 1);
+        var output = Pump(process.StandardOutput.BaseStream, 1, stream, writing);
+        var errors = Pump(process.StandardError.BaseStream, 2, stream, writing);
         await Task.WhenAll(output, errors).ConfigureAwait(false);
         await process.WaitForExitAsync().ConfigureAwait(false);
         await MicroVMProtocol.WriteAsync(stream, MessageKind.Exit, MicroVMProtocol.EncodeStatus(process.ExitCode), CancellationToken.None)
@@ -306,7 +309,7 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
     }
 
     [System.Runtime.Versioning.SupportedOSPlatform("linux")]
-    private static async Task Pump(Stream from, byte which, VsockStream to)
+    private static async Task Pump(Stream from, byte which, Stream to, SemaphoreSlim writing)
     {
         var buffer = new byte[65536];
         while (true)
@@ -318,13 +321,21 @@ internal sealed class Agent(IReadOnlyList<Share> shares)
             }
 
             buffer[0] = which;
-            await MicroVMProtocol.WriteAsync(to, MessageKind.Output, buffer.AsMemory(0, read + 1), CancellationToken.None)
-                .ConfigureAwait(false);
+            await writing.WaitAsync().ConfigureAwait(false);
+            try
+            {
+                await MicroVMProtocol.WriteAsync(to, MessageKind.Output, buffer.AsMemory(0, read + 1), CancellationToken.None)
+                    .ConfigureAwait(false);
+            }
+            finally
+            {
+                writing.Release();
+            }
         }
     }
 
     [System.Runtime.Versioning.SupportedOSPlatform("linux")]
-    private static Task Refuse(VsockStream stream, string why) =>
+    private static Task Refuse(Stream stream, string why) =>
         MicroVMProtocol.WriteAsync(stream, MessageKind.Error, Encoding.UTF8.GetBytes(why), CancellationToken.None);
 
     [System.Runtime.Versioning.SupportedOSPlatform("linux")]
diff --git a/src/Shared/MicroVMProtocol.cs b/src/Shared/MicroVMProtocol.cs
index 689353d..1876640 100644
--- a/src/Shared/MicroVMProtocol.cs
+++ b/src/Shared/MicroVMProtocol.cs
@@ -76,15 +76,12 @@ internal static class MicroVMProtocol
     public static async Task WriteAsync(Stream stream, MessageKind kind, ReadOnlyMemory<byte> payload, CancellationToken cancel)
     {
         ArgumentNullException.ThrowIfNull(stream);
-        var header = new byte[5];
-        header[0] = (byte)kind;
-        BinaryPrimitives.WriteInt32LittleEndian(header.AsSpan(1), payload.Length);
-        await stream.WriteAsync(header, cancel).ConfigureAwait(false);
-        if (payload.Length > 0)
-        {
-            await stream.WriteAsync(payload, cancel).ConfigureAwait(false);
-        }
-
+        // One write for the whole frame, so that frames written from two tasks never interleave.
+        var frame = new byte[5 + payload.Length];
+        frame[0] = (byte)kind;
+        BinaryPrimitives.WriteInt32LittleEndian(frame.AsSpan(1), payload.Length);
+        payload.CopyTo(frame.AsMemory(5));
+        await stream.WriteAsync(frame, cancel).ConfigureAwait(false);
         await stream.FlushAsync(cancel).ConfigureAwait(false);
     }
 
diff --git a/tests/Progmasoft.Tazel.Tests/AgentTests.cs b/tests/Progmasoft.Tazel.Tests/AgentTests.cs
new file mode 100644
index 0000000..e34ac54
--- /dev/null
+++ b/tests/Progmasoft.Tazel.Tests/AgentTests.cs
@@ -0,0 +1,325 @@
+// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
+// SPDX-License-Identifier: MIT
+
+extern alias guest;
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Runtime.InteropServices;
+using System.Runtime.Versioning;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Progmasoft.Tazel.MicroVM;
+using Xunit;
+using Agent = guest::Progmasoft.Tazel.Guest.Agent;
+using Libc = guest::Progmasoft.Tazel.Guest.Libc;
+using Share = guest::Progmasoft.Tazel.Guest.Share;
+using VsockStream = guest::Progmasoft.Tazel.Guest.VsockStream;
+
+namespace Progmasoft.Tazel.Tests;
+
+/// <summary>
+/// The agent's serving of one connection, on this machine: the agent at one end of a pair of sockets, the test at the
+/// other, speaking the protocol Tazel speaks to the guest.
+/// </summary>
+[SupportedOSPlatform("linux")]
+public sealed class AgentTests
+{
+    private static readonly Lazy<bool> Resolved = new(() =>
+    {
+        Libc.Resolve();
+        return true;
+    });
+
+    private static void SkipOffLinux() => Assert.SkipUnless(OperatingSystem.IsLinux(), "the agent runs on Linux");
+
+    private static CancellationToken Cancel => TestContext.Current.CancellationToken;
+
+    /// <summary>Two connected streams: the agent's end and the test's.</summary>
+    private static async Task<(NetworkStream Agent, NetworkStream Host, IDisposable Sockets)> PairAsync()
+    {
+        using var listener = new TcpListener(IPAddress.Loopback, 0);
+        listener.Start();
+        var client = new TcpClient();
+        await client.ConnectAsync((IPEndPoint)listener.LocalEndpoint, Cancel);
+        var server = await listener.AcceptTcpClientAsync(Cancel);
+        return (server.GetStream(), client.GetStream(), new Both(server, client));
+    }
+
+    private sealed class Both(TcpClient one, TcpClient two) : IDisposable
+    {
+        public void Dispose()
+        {
+            one.Dispose();
+            two.Dispose();
+        }
+    }
+
+    /// <summary>Runs <paramref name="arguments"/> through the agent in <paramref name="directory"/> and reads every answer.</summary>
+    private static async Task<(int? Status, string Output, string Errors, string? Refused)> RunAsync(
+        Agent agent, string directory, params string[] arguments)
+    {
+        var (agentEnd, host, sockets) = await PairAsync();
+        using (sockets)
+        {
+            var serving = agent.ServeAsync(agentEnd, () => { });
+            var request = new RunRequest(arguments,
+                new Dictionary<string, string>(StringComparer.Ordinal) { ["PATH"] = "/usr/bin:/bin" }, directory);
+            await MicroVMProtocol.WriteAsync(host, MessageKind.Run, MicroVMProtocol.Encode(request), Cancel);
+            var output = new StringBuilder();
+            var errors = new StringBuilder();
+            while (await MicroVMProtocol.ReadAsync(host, Cancel) is { } message)
+            {
+                switch (message.Kind)
+                {
+                    case MessageKind.Output:
+                        (message.Payload[0] == 1 ? output : errors).Append(Encoding.UTF8.GetString(message.Payload, 1, message.Payload.Length - 1));
+                        break;
+                    case MessageKind.Exit:
+                        host.Close();
+                        await serving;
+                        return (MicroVMProtocol.DecodeStatus(message.Payload), output.ToString(), errors.ToString(), null);
+                    case MessageKind.Error:
+                        await serving;
+                        return (null, output.ToString(), errors.ToString(), Encoding.UTF8.GetString(message.Payload));
+                    default:
+                        throw new InvalidDataException($"the agent answered {message.Kind}");
+                }
+            }
+
+            throw new EndOfStreamException("the agent closed the connection without an answer");
+        }
+    }
+
+    [Fact]
+    public async Task HelloIsAnsweredWithTheVersionAndPowerOffIsPassedOn()
+    {
+        SkipOffLinux();
+        var agent = new Agent([]);
+        var (agentEnd, host, sockets) = await PairAsync();
+        using (sockets)
+        {
+            var serving = agent.ServeAsync(agentEnd, () => { });
+            await MicroVMProtocol.WriteAsync(host, MessageKind.Hello, ReadOnlyMemory<byte>.Empty, Cancel);
+            var answer = await MicroVMProtocol.ReadAsync(host, Cancel);
+            Assert.Equal(MessageKind.Ready, answer!.Value.Kind);
+            Assert.Equal(Agent.Version, Encoding.UTF8.GetString(answer.Value.Payload));
+            await serving;
+        }
+
+        (agentEnd, host, sockets) = await PairAsync();
+        using (sockets)
+        {
+            var off = false;
+            var serving = agent.ServeAsync(agentEnd, () => off = true);
+            await MicroVMProtocol.WriteAsync(host, MessageKind.PowerOff, ReadOnlyMemory<byte>.Empty, Cancel);
+            await serving;
+            Assert.True(off);
+        }
+    }
+
+    [Theory]
+    [InlineData((byte)MessageKind.Stop)]
+    [InlineData((byte)MessageKind.Ready)]
+    public async Task AConnectionThatOpensWithAnythingElseIsClosedUnanswered(byte kind)
+    {
+        SkipOffLinux();
+        var (agentEnd, host, sockets) = await PairAsync();
+        using (sockets)
+        {
+            var serving = new Agent([]).ServeAsync(agentEnd, () => { });
+            await MicroVMProtocol.WriteAsync(host, (MessageKind)kind, ReadOnlyMemory<byte>.Empty, Cancel);
+            await serving;
+            Assert.Null(await MicroVMProtocol.ReadAsync(host, Cancel));
+        }
+    }
+
+    [Fact]
+    public async Task ABrokenMessageEndsTheConnectionAndNotTheAgent()
+    {
+        SkipOffLinux();
+        var (agentEnd, host, sockets) = await PairAsync();
+        using (sockets)
+        {
+            var serving = new Agent([]).ServeAsync(agentEnd, () => { });
+            // A Run frame that says 100 bytes follow, and then the host goes away.
+            await host.WriteAsync(new byte[] { (byte)MessageKind.Run, 100, 0, 0, 0, 1, 2 }, Cancel);
+            host.Close();
+            await serving;
+        }
+    }
+
+    [Fact]
+    public async Task ACommandRunsInASharedDirectoryWithItsOutputErrorsAndStatus()
+    {
+        SkipOffLinux();
+        using var module = new TemporaryModule();
+        var agent = new Agent([new Share("work", module.Root, ReadOnly: false)]);
+
+        var (status, output, errors, refused) = await RunAsync(agent, module.Root, "sh", "-c", "echo out; echo err >&2; exit 4");
+
+        Assert.Null(refused);
+        Assert.Equal(4, status);
+        Assert.Equal("out\n", output);
+        Assert.Equal("err\n", errors);
+    }
+
+    [Fact]
+    public async Task OutputAndErrorsWrittenTogetherArriveAsWholeFrames()
+    {
+        SkipOffLinux();
+        using var module = new TemporaryModule();
+        var agent = new Agent([new Share("work", module.Root, ReadOnly: false)]);
+
+        var (status, output, errors, _) = await RunAsync(agent, module.Root, "sh", "-c",
+            "i=0; while [ $i -lt 3000 ]; do echo o$i; echo e$i >&2; i=$((i+1)); done");
+
+        Assert.Equal(0, status);
+        Assert.Equal(Enumerable.Range(0, 3000).Select(i => $"o{i}\n"), output.Split('\n', StringSplitOptions.RemoveEmptyEntries).Select(l => l + "\n"));
+        Assert.Equal(3000, errors.Split('\n', StringSplitOptions.RemoveEmptyEntries).Length);
+    }
+
+    [Fact]
+    public async Task WhatCannotRunIsRefusedWithItsReason()
+    {
+        SkipOffLinux();
+        using var module = new TemporaryModule().Write("plain.txt", "not a program\n");
+        var outside = Path.GetTempPath();
+        var agent = new Agent([new Share("work", module.Root, ReadOnly: false), new Share("tools", "/usr", ReadOnly: true)]);
+
+        Assert.Equal("the command is empty", (await RunAsync(agent, module.Root)).Refused);
+        Assert.Contains("is not inside a directory Tazel shared", (await RunAsync(agent, outside, "true")).Refused, StringComparison.Ordinal);
+        Assert.Contains("is not inside a directory Tazel shared", (await RunAsync(agent, "/usr", "true")).Refused, StringComparison.Ordinal);
+        Assert.Contains("is not inside a directory Tazel shared",
+            (await RunAsync(agent, Path.Combine(module.Root, "missing"), "true")).Refused, StringComparison.Ordinal);
+        Assert.Contains("no-such-program is not found in the MicroVM",
+            (await RunAsync(agent, module.Root, "no-such-program")).Refused, StringComparison.Ordinal);
+        Assert.Contains("could not be started",
+            (await RunAsync(agent, module.Root, Path.Combine(module.Root, "plain.txt"))).Refused, StringComparison.Ordinal);
+        Assert.Contains("is not found in the MicroVM",
+            (await RunAsync(agent, module.Root, Path.Combine(module.Root, "missing.sh"))).Refused, StringComparison.Ordinal);
+    }
+
+    [Fact]
+    public async Task StopOrAHostThatLeavesEndsTheCommand()
+    {
+        SkipOffLinux();
+        using var module = new TemporaryModule();
+        var agent = new Agent([new Share("work", module.Root, ReadOnly: false)]);
+        foreach (var leave in new[] { false, true })
+        {
+            var (agentEnd, host, sockets) = await PairAsync();
+            using (sockets)
+            {
+                var serving = agent.ServeAsync(agentEnd, () => { });
+                var request = new RunRequest(["sleep", "60"], new Dictionary<string, string>(StringComparer.Ordinal), module.Root);
+                await MicroVMProtocol.WriteAsync(host, MessageKind.Run, MicroVMProtocol.Encode(request), Cancel);
+                await Task.Delay(300, Cancel);
+                var started = DateTime.UtcNow;
+                if (leave)
+                {
+                    host.Socket.Shutdown(SocketShutdown.Send);
+                }
+                else
+                {
+                    await MicroVMProtocol.WriteAsync(host, MessageKind.Stop, ReadOnlyMemory<byte>.Empty, Cancel);
+                }
+
+                var answer = await MicroVMProtocol.ReadAsync(host, Cancel);
+                Assert.Equal(MessageKind.Exit, answer!.Value.Kind);
+                Assert.NotEqual(0, MicroVMProtocol.DecodeStatus(answer.Value.Payload));
+                Assert.True(DateTime.UtcNow - started < TimeSpan.FromSeconds(30));
+                host.Close();
+                await serving;
+            }
+        }
+    }
+
+    [Fact]
+    public void FindTakesAPathWithASlashAsItIsAndSearchesThePathForAName()
+    {
+        SkipOffLinux();
+        Assert.Equal("/bin/sh", Agent.Find("/bin/sh", ""));
+        Assert.Null(Agent.Find("/no/such/program", ""));
+        Assert.EndsWith("/sh", Agent.Find("sh", "/nowhere:/usr/bin:/bin"), StringComparison.Ordinal);
+        Assert.Null(Agent.Find("sh", "/nowhere"));
+        Assert.NotNull(Agent.Find("sh", null!));
+    }
+
+    [Fact]
+    public async Task AVsockStreamReadsAndWritesItsDescriptorAndRefusesWhatItCannotDo()
+    {
+        SkipOffLinux();
+        _ = Resolved.Value;
+        var (one, two) = SocketPair();
+        using var left = new VsockStream(one);
+        using var right = new VsockStream(two);
+        Assert.True(left.CanRead && left.CanWrite && !left.CanSeek);
+        Assert.Throws<NotSupportedException>(() => left.Length);
+        Assert.Throws<NotSupportedException>(() => left.Position);
+        Assert.Throws<NotSupportedException>(() => left.Position = 0);
+        Assert.Throws<NotSupportedException>(() => left.Seek(0, SeekOrigin.Begin));
+        Assert.Throws<NotSupportedException>(() => left.SetLength(0));
+
+        left.Write("hello"u8.ToArray(), 0, 5);
+        left.Flush();
+        var buffer = new byte[16];
+        Assert.Equal(5, right.Read(buffer, 0, buffer.Length));
+        Assert.Equal("hello", Encoding.ASCII.GetString(buffer, 0, 5));
+
+        await left.WriteAsync("again"u8.ToArray(), 0, 5, Cancel);
+        await left.WriteAsync("!"u8.ToArray().AsMemory(), Cancel);
+        var memory = new byte[16];
+        var read = 0;
+        while (read < 6)
+        {
+            read += await right.ReadAsync(memory.AsMemory(read), Cancel);
+        }
+
+        Assert.Equal("again!", Encoding.ASCII.GetString(memory, 0, 6));
+        Assert.Equal(0, await right.ReadAsync(buffer, 0, 0, Cancel));
+
+        right.Dispose();
+        Assert.Throws<IOException>(() =>
+        {
+            for (var i = 0; i < 1000; i++)
+            {
+                left.Write(new byte[65536], 0, 65536);
+            }
+        });
+        Assert.Throws<IOException>(() => right.Read(buffer, 0, buffer.Length));
+    }
+
+    [Fact]
+    public void AVsockAddressIsEqualByEachOfItsFields()
+    {
+        var address = new Libc.SockaddrVM { Family = Libc.AfVsock, Port = MicroVMProtocol.Port, Cid = Libc.VmaddrCidAny };
+        var same = address;
+        var other = address with { Port = 1025 };
+
+        Assert.True(address == same);
+        Assert.False(address != same);
+        Assert.True(address != other);
+        Assert.True(address.Equals((object)same));
+        Assert.False(address.Equals((object)"not an address"));
+        Assert.Equal(address.GetHashCode(), same.GetHashCode());
+    }
+
+    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+    private delegate int SocketPairFunction(int domain, int type, int protocol, [Out] int[] descriptors);
+
+    /// <summary>A pair of connected Unix sockets, from the C library the test runs with (AF_UNIX, SOCK_STREAM).</summary>
+    private static (int, int) SocketPair()
+    {
+        var socketpair = Marshal.GetDelegateForFunctionPointer<SocketPairFunction>(
+            NativeLibrary.GetExport(NativeLibrary.GetMainProgramHandle(), "socketpair"));
+        var descriptors = new int[2];
+        Assert.Equal(0, socketpair(1, 1, 0, descriptors));
+        return (descriptors[0], descriptors[1]);
+    }
+}