using System.Buffers; using System.Text.Json; using VoiceCat.Crypto; using VoiceCat.Protocol; using Voicecat.V1; namespace VoiceCat.Tests; public class GoldenTests { [Fact] public void EnvelopeMatchesCppFixture() { using var fixture = Load(); var expected = Convert.FromHexString(fixture.RootElement.GetProperty("envelope").GetString()!); var envelope = new Envelope { RequestId = 42, ClientHello = new() { ProtoVersion = 1, ClientName = "test-client", ClientVersion = "0.0.1" } }; envelope.ClientHello.Features.Add("text"); var output = new ArrayBufferWriter(); ControlFraming.WriteEnvelope(output, envelope); Assert.Equal(expected, output.WrittenSpan.ToArray()); } [Theory] [InlineData(false)] [InlineData(true)] public void MediaPacketsMatchCppFixtures(bool managed) { using var fixture = Load(); foreach (var vector in fixture.RootElement.GetProperty("media").EnumerateArray()) { byte[] key = Convert.FromHexString(vector.GetProperty("key").GetString()!); byte[] plaintext = Convert.FromHexString(vector.GetProperty("plaintext").GetString()!); byte[] expected = Convert.FromHexString(vector.GetProperty("packet").GetString()!); ulong sequence = vector.GetProperty("sequence").GetUInt64(); using var sender = new MediaEncryptor(key, managed, sequence); using var receiver = new MediaDecryptor(key, managed); var header = new VoiceFrameHeader(MediaFrameType.Voice, VoiceFrameFlags.Marker, 0, 0xcafebabe, 0, 960); byte[] actual = new byte[expected.Length]; sender.Encrypt(header, plaintext, actual); Assert.Equal(expected, actual); byte[] decoded = new byte[plaintext.Length]; Assert.True(receiver.TryDecrypt(expected, decoded, out var parsed, out int written)); Assert.Equal(sequence, parsed.Sequence); Assert.Equal(plaintext.Length, written); Assert.Equal(plaintext, decoded); } } private static JsonDocument Load() => JsonDocument.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "cpp-wire.json"))); }