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

Test the protocol buffer decoder and the remote cache's refusals

- RemoteCacheTests: unknown 64- and 32-bit fields are passed over; a
  result without a digest, with a short hash, a body past its end, field
  0, a varint over 64 bits, a length or fixed field past the end, or a
  group is refused. TrySkip keeps only the fixed sizes its caller gives
  it.
- RemoteActionsTests: no key without an input, a cache that cannot be
  reached while storing is said once, storing stops without an output or
  a found input, a list of found inputs that is missing, malformed or
  different here refuses the result, as does content of another size, and
  signature records Tazel cannot read are not trusted.
Commit
6c07a67cda54556ae2ec271689f60dcfd4ca7099
Author
Leitwolf11 ·
Parents
29928f674fd7

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Cache/ActionResultMessage.cs  |  21 +--
 tests/Progmasoft.Tazel.Tests/RemoteActionsTests.cs | 165 +++++++++++++++++++++
 tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs   |  31 ++++
 3 files changed, 202 insertions(+), 15 deletions(-)

diff --git a/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs b/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
index 4ddcde5..8c3c88a 100644
--- a/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
+++ b/src/Progmasoft.Tazel/Cache/ActionResultMessage.cs
@@ -242,22 +242,13 @@ internal static class ActionResultMessage
             return true;
         }
 
-        public bool TrySkip(int wire)
+        /// <summary>Passes over a fixed-size field: 64-bit (wire 1) or 32-bit (wire 5); groups are refused.</summary>
+        public bool TrySkip(int wire) => wire switch
         {
-            switch (wire)
-            {
-                case WireVarint:
-                    return TryVarint(out _);
-                case WireLength:
-                    return TryLength(out _);
-                case 1:
-                    return Advance(8);
-                case 5:
-                    return Advance(4);
-                default:
-                    return false;
-            }
-        }
+            1 => Advance(8),
+            5 => Advance(4),
+            _ => false,
+        };
 
         private bool Advance(int count)
         {
diff --git a/tests/Progmasoft.Tazel.Tests/RemoteActionsTests.cs b/tests/Progmasoft.Tazel.Tests/RemoteActionsTests.cs
new file mode 100644
index 0000000..da6306c
--- /dev/null
+++ b/tests/Progmasoft.Tazel.Tests/RemoteActionsTests.cs
@@ -0,0 +1,165 @@
+// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
+// SPDX-License-Identifier: MIT
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Progmasoft.Tazel.Cache;
+using Progmasoft.Tazel.Driver;
+using Xunit;
+
+namespace Progmasoft.Tazel.Tests;
+
+/// <summary>What the remote cache takes and refuses, action by action, with no compiler.</summary>
+public sealed class RemoteActionsTests
+{
+    private static CancellationToken Cancel => TestContext.Current.CancellationToken;
+
+    private static BuildAction Copy(TemporaryModule module, string input = "in.txt", string? depfile = null) => new()
+    {
+        Description = "Copying",
+        Command = ["/bin/cp", input, "out.txt"],
+        Directory = module.Root,
+        Inputs = [input],
+        Outputs = ["out.txt"],
+        Depfile = depfile,
+    };
+
+    private static string Put(FakeRemoteCache server, string text)
+    {
+        var bytes = Encoding.UTF8.GetBytes(text);
+        var hash = Digests.Sha256(bytes);
+        server.Content[hash] = bytes;
+        return hash;
+    }
+
+    [Fact]
+    public async Task AnActionWithAMissingInputHasNoKeyAndIsNeitherFetchedNorStored()
+    {
+        using var server = new FakeRemoteCache();
+        using var module = new TemporaryModule();
+        using var cache = new RemoteCache(new Uri(server.Address), token: null);
+        var remote = new RemoteActions(cache, read: true, write: true, new Digests(), _ => { });
+        var action = Copy(module, "missing.txt");
+        Assert.Null(remote.KeyOf(action));
+        Assert.False(await remote.FetchAsync(action, Cancel));
+        await remote.StoreAsync(action, Cancel);
+        Assert.Empty(server.Actions);
+    }
+
+    [Fact]
+    public async Task ACacheThatCannotBeReachedWhileStoringIsSaidOnceAndNotTriedAgain()
+    {
+        using var module = new TemporaryModule().Write("in.txt", "in\n").Write("out.txt", "in\n");
+        var warnings = new List<string>();
+        // A port nothing listens on: the server is made and closed at once.
+        string address;
+        using (var gone = new FakeRemoteCache())
+        {
+            address = gone.Address;
+        }
+
+        using var cache = new RemoteCache(new Uri(address), token: null);
+        var remote = new RemoteActions(cache, read: true, write: true, new Digests(), warnings.Add);
+        await remote.StoreAsync(Copy(module), Cancel);
+        await remote.StoreAsync(Copy(module), Cancel);
+        Assert.False(remote.Writes);
+        Assert.Single(warnings);
+    }
+
+    [Fact]
+    public async Task StoringStopsWhenAnOutputOrAFoundInputIsMissing()
+    {
+        using var server = new FakeRemoteCache();
+        using var module = new TemporaryModule().Write("in.txt", "in\n");
+        using var cache = new RemoteCache(new Uri(server.Address), token: null);
+        var remote = new RemoteActions(cache, read: false, write: true, new Digests(), _ => { });
+        // No out.txt: nothing is stored.
+        await remote.StoreAsync(Copy(module), Cancel);
+        Assert.Empty(server.Actions);
+        // The depfile names a header that is not there.
+        module.Write("out.txt", "in\n").Write("out.d", "out.txt: in.txt gone.h\n");
+        await remote.StoreAsync(Copy(module, depfile: "out.d"), Cancel);
+        Assert.Empty(server.Actions);
+        // With every file there, the result is stored with the list of found inputs.
+        module.Write("gone.h", "\n");
+        await remote.StoreAsync(Copy(module, depfile: "out.d"), Cancel);
+        Assert.Single(server.Actions);
+    }
+
+    [Fact]
+    public async Task AListOfFoundInputsThatIsMissingWrongOrDifferentHereRefusesTheResult()
+    {
+        using var server = new FakeRemoteCache();
+        using var module = new TemporaryModule().Write("in.txt", "in\n");
+        using var cache = new RemoteCache(new Uri(server.Address), token: null);
+        var digests = new Digests();
+        var remote = new RemoteActions(cache, read: true, write: false, digests, _ => { });
+        var action = Copy(module);
+        var key = remote.KeyOf(action)!;
+        var output = Put(server, "in\n");
+        var inHere = digests.Of(Path.Combine(module.Root, "in.txt"))!;
+        string[] lists =
+        [
+            "",                                             // not in the cache
+            "short line\n",                                 // not a digest and a path
+            new string('0', 64) + " in.txt\n",              // a digest the input does not have here
+        ];
+        foreach (var text in lists)
+        {
+            var bytes = Encoding.UTF8.GetBytes(text);
+            var hash = Digests.Sha256(bytes);
+            if (text.Length > 0)
+            {
+                server.Content[hash] = bytes;
+            }
+
+            server.Actions[key] = ActionResultMessage.Encode([
+                new OutputFile("out.txt", output, 3, false),
+                new OutputFile(RemoteActions.DiscoveredInputs, text.Length > 0 ? hash : new string('9', 64), bytes.Length, false),
+            ]);
+            Assert.False(await remote.FetchAsync(action, Cancel), text);
+        }
+
+        // A list that holds, and content of another size than the result says: refused too.
+        var good = Put(server, inHere + " in.txt\n");
+        server.Actions[key] = ActionResultMessage.Encode([
+            new OutputFile("out.txt", output, 99, false),
+            new OutputFile(RemoteActions.DiscoveredInputs, good, (inHere + " in.txt\n").Length, false),
+        ]);
+        Assert.False(await remote.FetchAsync(action, Cancel));
+        Assert.False(File.Exists(Path.Combine(module.Root, "out.txt")));
+    }
+
+    [Theory]
+    [InlineData("tazel signature 1\nalgorithm ed25519\nsignature !!!not base64!!!\n")]
+    [InlineData("tazel signature 1\nalgorithm unknown\nsignature AAAA\n")]
+    [InlineData("tazel signature 2\nalgorithm ed25519\nsignature AAAA\n")]
+    [InlineData("")]
+    public async Task ASignatureRecordTazelCannotReadIsNotTrusted(string record)
+    {
+        using var server = new FakeRemoteCache();
+        using var module = new TemporaryModule().Write("in.txt", "in\n");
+        using var cache = new RemoteCache(new Uri(server.Address), token: null);
+        var digests = new Digests();
+        var trusted = CacheKey.ParsePublic(CacheKey.Generate(SigningAlgorithm.Ed25519).PublicKey, "PublicKey");
+        var warnings = new List<string>();
+        var remote = new RemoteActions(cache, read: true, write: false, digests, warnings.Add, trusted);
+        var action = Copy(module);
+        var output = Put(server, "in\n");
+        var inHere = digests.Of(Path.Combine(module.Root, "in.txt"))!;
+        var list = Put(server, inHere + " in.txt\n");
+        // An empty record is a record the cache does not have.
+        var signature = record.Length > 0 ? Put(server, record) : new string('8', 64);
+        server.Actions[remote.KeyOf(action)!] = ActionResultMessage.Encode([
+            new OutputFile("out.txt", output, 3, false),
+            new OutputFile(RemoteActions.DiscoveredInputs, list, (inHere + " in.txt\n").Length, false),
+            new OutputFile(RemoteActions.Signature, signature, Encoding.UTF8.GetByteCount(record), false),
+        ]);
+        Assert.False(await remote.FetchAsync(action, Cancel));
+        Assert.Single(warnings);
+    }
+}
diff --git a/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs b/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
index 118c71b..a3b0c52 100644
--- a/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/RemoteCacheTests.cs
@@ -83,6 +83,37 @@ public sealed class RemoteCacheTests
         Assert.Contains("CS0103", error.Message, StringComparison.Ordinal);
     }
 
+    private static byte[] Length(int field, params byte[] body) => [(byte)((field << 3) | 2), (byte)body.Length, .. body];
+
+    private static byte[] Text(int field, string text) => Length(field, System.Text.Encoding.ASCII.GetBytes(text));
+
+    private static byte[] Digest(string hash, byte size) => Length(2, [.. Text(1, hash), 0x10, size]);
+
+    [Fact]
+    public void FieldsTazelDoesNotReadArePassedOverAndBrokenOnesRefuseTheResult()
+    {
+        var hash = new string('c', 64);
+        // An OutputFile with an unknown 64-bit field (3) and an unknown 32-bit field (5) between its own.
+        byte[] file = [.. Text(1, "out/x"), 0x19, 1, 2, 3, 4, 5, 6, 7, 8, .. Digest(hash, 9), 0x2D, 1, 2, 3, 4, 0x20, 0x01];
+        var decoded = ActionResultMessage.Decode(Length(2, file));
+        Assert.Equal([new OutputFile("out/x", hash, 9, true)], decoded);
+
+        foreach (byte[] broken in new byte[][]
+        {
+            Length(2, Text(1, "out/x")),                              // no digest
+            Length(2, [.. Text(1, "out/x"), .. Digest("short", 1)]),   // a hash that is not 64 hex digits
+            Length(2, [0x0A, 0x09, 0x41]),                            // a path longer than its message
+            [0x00, 0x01],                                             // field 0
+            [0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01], // a varint of more than 64 bits
+            [0x12, 0x05, 0x01],                                       // a length past the end
+            [0x19, 0x01, 0x02],                                       // a 64-bit field past the end
+            [0x1B],                                                   // a group, which is not read
+        })
+        {
+            Assert.Null(ActionResultMessage.Decode(broken));
+        }
+    }
+
     [Fact]
     public void AResultIsTheProtocolBufferOfTheRemoteExecutionApi()
     {