Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CBOR] Implement indefinite length writer and reader support #33831

Merged
merged 12 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public static void ReadArray_NestedValues_HappyPath(object[] expectedValues, str
Assert.Equal(CborReaderState.Finished, reader.Peek());
}

[Theory]
[InlineData(new object[] { }, "9fff")]
[InlineData(new object[] { 42 }, "9f182aff")]
[InlineData(new object[] { 1, 2, 3 }, "9f010203ff")]
[InlineData(new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }, "9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff")]
[InlineData(new object[] { 1, -1, "", new byte[] { 7 } }, "9f0120604107ff")]
[InlineData(new object[] { "lorem", "ipsum", "dolor" }, "9f656c6f72656d65697073756d65646f6c6f72ff")]
public static void ReadArray_IndefiniteLength_HappyPath(object[] expectedValues, string hexEncoding)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
Helpers.VerifyArray(reader, expectedValues, expectDefiniteLengthCollections: false);
Assert.Equal(CborReaderState.Finished, reader.Peek());
}

[Theory]
[InlineData("80", 0)]
[InlineData("8101", 1)]
Expand Down Expand Up @@ -83,6 +98,40 @@ public static void ReadArray_DefiniteLengthExceeded_WithNestedData_ShouldThrowIn
Assert.Throws<InvalidOperationException>(() => reader.ReadInt64());
}

[Theory]
[InlineData("9f")]
[InlineData("9f01")]
[InlineData("9f0102")]
public static void ReadArray_IndefiniteLength_MissingBreakByte_ShouldReportEndOfData(string hexEncoding)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
reader.ReadStartArray();
while (reader.Peek() == CborReaderState.UnsignedInteger)
{
reader.ReadInt64();
}

Assert.Equal(CborReaderState.EndOfData, reader.Peek());
}

[Theory]
[InlineData("9f01ff", 1)]
[InlineData("9f0102ff", 2)]
[InlineData("9f010203ff", 3)]
public static void ReadArray_IndefiniteLength_PrematureEndArrayCall_ShouldThrowInvalidOperationException(string hexEncoding, int length)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
reader.ReadStartArray();
for (int i = 1; i < length; i++)
{
reader.ReadInt64();
}

Assert.Throws<InvalidOperationException>(() => reader.ReadEndArray());
}

[Theory]
[InlineData("8101", 1)]
[InlineData("83010203", 3)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable enable
using System.Linq;
using Test.Cryptography;
using Xunit;

namespace System.Security.Cryptography.Encoding.Tests.Cbor
Expand All @@ -12,7 +13,7 @@ public partial class CborReaderTests
{
internal static class Helpers
{
public static void VerifyValue(CborReader reader, object expectedValue)
public static void VerifyValue(CborReader reader, object expectedValue, bool expectDefiniteLengthCollections = true)
{
switch (expectedValue)
{
Expand All @@ -39,13 +40,38 @@ public static void VerifyValue(CborReader reader, object expectedValue)
case byte[] expected:
Assert.Equal(CborReaderState.ByteString, reader.Peek());
byte[] b = reader.ReadByteString();
Assert.Equal(expected, b);
Assert.Equal(expected.ByteArrayToHex(), b.ByteArrayToHex());
break;
case string[] expectedChunks:
Assert.Equal(CborReaderState.StartTextString, reader.Peek());
reader.ReadStartTextStringIndefiniteLength();
foreach(string expectedChunk in expectedChunks)
{
Assert.Equal(CborReaderState.TextString, reader.Peek());
string chunk = reader.ReadTextString();
Assert.Equal(expectedChunk, chunk);
}
Assert.Equal(CborReaderState.EndTextString, reader.Peek());
reader.ReadEndTextStringIndefiniteLength();
break;
case byte[][] expectedChunks:
Assert.Equal(CborReaderState.StartByteString, reader.Peek());
reader.ReadStartByteStringIndefiniteLength();
foreach (byte[] expectedChunk in expectedChunks)
{
Assert.Equal(CborReaderState.ByteString, reader.Peek());
byte[] chunk = reader.ReadByteString();
Assert.Equal(expectedChunk.ByteArrayToHex(), chunk.ByteArrayToHex());
}
Assert.Equal(CborReaderState.EndByteString, reader.Peek());
reader.ReadEndByteStringIndefiniteLength();
break;

case object[] nested when CborWriterTests.Helpers.IsCborMapRepresentation(nested):
VerifyMap(reader, nested);
VerifyMap(reader, nested, expectDefiniteLengthCollections);
break;
case object[] nested:
VerifyArray(reader, nested);
VerifyArray(reader, nested, expectDefiniteLengthCollections);
break;
default:
throw new ArgumentException($"Unrecognized argument type {expectedValue.GetType()}");
Expand All @@ -58,14 +84,21 @@ static void VerifyPeekInteger(CborReader reader, bool isUnsignedInteger)
}
}

public static void VerifyArray(CborReader reader, params object[] expectedValues)
public static void VerifyArray(CborReader reader, object[] expectedValues, bool expectDefiniteLengthCollections = true)
{
Assert.Equal(CborReaderState.StartArray, reader.Peek());

ulong? length = reader.ReadStartArray();

Assert.NotNull(length);
Assert.Equal(expectedValues.Length, (int)length!.Value);
if (expectDefiniteLengthCollections)
{
Assert.NotNull(length);
Assert.Equal(expectedValues.Length, (int)length!.Value);
}
else
{
Assert.Null(length);
}

foreach (object value in expectedValues)
{
Expand All @@ -76,18 +109,26 @@ public static void VerifyArray(CborReader reader, params object[] expectedValues
reader.ReadEndArray();
}

public static void VerifyMap(CborReader reader, params object[] expectedValues)
public static void VerifyMap(CborReader reader, object[] expectedValues, bool expectDefiniteLengthCollections = true)
{
if (!CborWriterTests.Helpers.IsCborMapRepresentation(expectedValues))
{
throw new ArgumentException($"cbor map expected values missing '{CborWriterTests.Helpers.MapPrefixIdentifier}' prefix.");
}

Assert.Equal(CborReaderState.StartMap, reader.Peek());

ulong? length = reader.ReadStartMap();

Assert.NotNull(length);
Assert.Equal((expectedValues.Length - 1) / 2, (int)length!.Value);
if (expectDefiniteLengthCollections)
{
Assert.NotNull(length);
Assert.Equal((expectedValues.Length - 1) / 2, (int)length!.Value);
}
else
{
Assert.Null(length);
}

foreach (object value in expectedValues.Skip(1))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ public static void ReadCborNegativeIntegerEncoding_InvalidData_ShouldThrowFormat
[Theory]
[InlineData("1f")]
[InlineData("3f")]
public static void ReadInt64_IndefiniteLengthIntegers_ShouldThrowNotImplementedException(string hexEncoding)
public static void ReadInt64_IndefiniteLengthIntegers_ShouldThrowFormatException(string hexEncoding)
{
byte[] data = hexEncoding.HexToByteArray();
var reader = new CborReader(data);

Assert.Throws<NotImplementedException>(() => reader.ReadInt64());
Assert.Throws<FormatException>(() => reader.ReadInt64());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public static void ReadMap_NestedListValues_HappyPath(object expectedValue, stri
Assert.Equal(CborReaderState.Finished, reader.Peek());
}

[Theory]
[InlineData(new object[] { Map }, "bfff")]
[InlineData(new object[] { Map, 1, 2, 3, 4 }, "bf01020304ff")]
[InlineData(new object[] { Map, "a", "A", "b", "B", "c", "C", "d", "D", "e", "E" }, "bf6161614161626142616361436164614461656145ff")]
[InlineData(new object[] { Map, "a", "A", -1, 2, new byte[] { }, new byte[] { 1 } }, "bf616161412002404101ff")]
public static void ReadMap_IndefiniteLength_SimpleValues_HappyPath(object[] exoectedValues, string hexEncoding)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
Helpers.VerifyMap(reader, exoectedValues, expectDefiniteLengthCollections: false);
Assert.Equal(CborReaderState.Finished, reader.Peek());
}


[Theory]
[InlineData(new object[] { Map, "a", 1, "a", 2 }, "a2616101616102")]
Expand Down Expand Up @@ -193,6 +206,59 @@ public static void ReadMap_IncorrectDefiniteLength_ShouldThrowFormatException(st
Assert.Throws<FormatException>(() => reader.ReadInt64());
}

[Theory]
[InlineData("bf")]
[InlineData("bf0102")]
[InlineData("bf01020304")]
public static void ReadMap_IndefiniteLength_MissingBreakByte_ShouldReportEndOfData(string hexEncoding)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
reader.ReadStartMap();
while (reader.Peek() == CborReaderState.UnsignedInteger)
{
reader.ReadInt64();
}

Assert.Equal(CborReaderState.EndOfData, reader.Peek());
}

[Theory]
[InlineData("bf0102ff", 1)]
[InlineData("bf01020304ff", 2)]
[InlineData("bf010203040506ff", 3)]
public static void ReadMap_IndefiniteLength_PrematureEndArrayCall_ShouldThrowInvalidOperationException(string hexEncoding, int length)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
reader.ReadStartMap();
for (int i = 1; i < length; i++)
{
reader.ReadInt64();
}

Assert.Equal(CborReaderState.UnsignedInteger, reader.Peek());
Assert.Throws<InvalidOperationException>(() => reader.ReadEndMap());
}

[Theory]
[InlineData("bf01ff", 1)]
[InlineData("bf010203ff", 3)]
[InlineData("bf0102030405ff", 5)]
public static void ReadMap_IndefiniteLength_OddKeyValuePairs_ShouldThrowFormatException(string hexEncoding, int length)
{
byte[] encoding = hexEncoding.HexToByteArray();
var reader = new CborReader(encoding);
reader.ReadStartMap();
for (int i = 0; i < length; i++)
{
reader.ReadInt64();
}

Assert.Equal(CborReaderState.FormatError, reader.Peek()); // don't want this to fail
Assert.Throws<FormatException>(() => reader.ReadEndMap());
}

[Theory]
[InlineData("a201811907e4", 2, 1)]
[InlineData("a61907e4811907e402811907e4", 6, 2)]
Expand Down
Loading