public static void ExecOperation(CborWriter writer, string op) { switch (op) { case nameof(writer.WriteInt64): writer.WriteInt64(42); break; case nameof(writer.WriteByteString): writer.WriteByteString(Array.Empty <byte>()); break; case nameof(writer.WriteTextString): writer.WriteTextString(""); break; case nameof(writer.WriteStartIndefiniteLengthTextString): writer.WriteStartIndefiniteLengthTextString(); break; case nameof(writer.WriteStartIndefiniteLengthByteString): writer.WriteStartIndefiniteLengthByteString(); break; case nameof(writer.WriteStartArray): writer.WriteStartArray(null); break; case nameof(writer.WriteStartMap): writer.WriteStartMap(null); break; case nameof(writer.WriteEndIndefiniteLengthByteString): writer.WriteEndIndefiniteLengthByteString(); break; case nameof(writer.WriteEndIndefiniteLengthTextString): writer.WriteEndIndefiniteLengthTextString(); break; case nameof(writer.WriteEndArray): writer.WriteEndArray(); break; case nameof(writer.WriteEndMap): writer.WriteEndMap(); break; default: throw new Exception($"Unrecognized CborWriter operation name {op}"); } }
public static void WriteChunkedByteString(CborWriter writer, byte[][] chunks) { writer.WriteStartIndefiniteLengthByteString(); foreach (byte[] chunk in chunks) { writer.WriteByteString(chunk); } writer.WriteEndIndefiniteLengthByteString(); }
public static void WriteEncodedValue_IndefiniteLengthByteString_HappyPath() { var writer = new CborWriter(convertIndefiniteLengthEncodings: false); writer.WriteStartIndefiniteLengthByteString(); writer.WriteByteString(new byte[] { 1, 1, 1 }); writer.WriteEncodedValue("43020202".HexToByteArray()); writer.WriteEndIndefiniteLengthByteString(); byte[] encoding = writer.Encode(); Assert.Equal("5f4301010143020202ff", encoding.ByteArrayToHex().ToLower()); }
public static void Roundtrip_IndefiniteByteString(CborConformanceMode mode, byte[][] chunks) { bool convertIndefiniteLengthEncodings = mode is CborConformanceMode.Canonical or CborConformanceMode.Ctap2Canonical; var writer = new CborWriter(convertIndefiniteLengthEncodings: convertIndefiniteLengthEncodings); writer.WriteStartIndefiniteLengthByteString(); foreach (byte[] chunk in chunks) { writer.WriteByteString(chunk); } writer.WriteEndIndefiniteLengthByteString(); byte[] encoding = writer.Encode(); var reader = new CborReader(encoding); byte[] expected = chunks.SelectMany(ch => ch).ToArray(); byte[] result = reader.ReadByteString(); AssertHelper.HexEqual(expected, result); }
public void GetValueAsBytesBeingIndefiniteLengthSucceeds(ContentTestCase @case) { Verify(0); Verify(1); Verify(10); void Verify(int repetitions) { byte[] content = GetDummyContent(@case); var writer = new CborWriter(); writer.WriteStartIndefiniteLengthByteString(); for (int i = 0; i < repetitions; i++) { writer.WriteByteString(content); } writer.WriteEndIndefiniteLengthByteString(); int expectedLength = content.Length * repetitions; CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); ReadOnlySpan <byte> result = headerValue.GetValueAsBytes(); Assert.Equal(expectedLength, result.Length); for (int i = 0; i < expectedLength; i += content.Length) { AssertExtensions.SequenceEqual(content, result.Slice(i, content.Length)); } Span <byte> buffer = new byte[expectedLength]; int length = headerValue.GetValueAsBytes(buffer); Assert.Equal(expectedLength, length); for (int i = 0; i < expectedLength; i += content.Length) { AssertExtensions.SequenceEqual(content, buffer.Slice(i, content.Length)); } } }