public static void ReadStartMap_InvalidData_ShouldThrowFormatException(string hexEncoding) { byte[] data = hexEncoding.HexToByteArray(); var reader = new CborReader(data); Assert.Throws <FormatException>(() => reader.ReadStartMap()); }
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.PeekState()); int?length = reader.ReadStartMap(); 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)) { VerifyValue(reader, value, expectDefiniteLengthCollections); } Assert.Equal(CborReaderState.EndMap, reader.PeekState()); reader.ReadEndMap(); }
public static void ReadStartMap_EmptyBuffer_ShouldThrowFormatException() { byte[] encoding = Array.Empty <byte>(); var reader = new CborReader(encoding); Assert.Throws <FormatException>(() => reader.ReadStartMap()); }
public static void SkipValue_NestedFormatException_ShouldPreserveOriginalReaderState() { string hexEncoding = "820181bf01ff"; // [1, [ {_ 1 : <missing value> } ]] var reader = new CborReader(hexEncoding.HexToByteArray()); reader.ReadStartArray(); reader.ReadInt64(); // capture current state int currentBytesRead = reader.BytesRead; int currentBytesRemaining = reader.BytesRemaining; // make failing call int bytesRemaining = reader.BytesRemaining; Assert.Throws <FormatException>(() => reader.SkipValue()); Assert.Equal(bytesRemaining, reader.BytesRemaining); // ensure reader state has reverted to original Assert.Equal(reader.BytesRead, currentBytesRead); Assert.Equal(reader.BytesRemaining, currentBytesRemaining); // ensure we can read every value up to the format error Assert.Equal(CborReaderState.StartArray, reader.PeekState()); reader.ReadStartArray(); Assert.Equal(CborReaderState.StartMap, reader.PeekState()); reader.ReadStartMap(); Assert.Equal(CborReaderState.UnsignedInteger, reader.PeekState()); reader.ReadUInt64(); Assert.Equal(CborReaderState.FormatError, reader.PeekState()); }
internal static void ReadMap_IndefiniteLength_UnSupportedConformanceLevel_ShouldThrowFormatException(CborConformanceLevel level, string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding, level); Assert.Throws <FormatException>(() => reader.ReadStartMap()); Assert.Equal(0, reader.BytesRead); }
internal static void ReadMap_IndefiniteLength_SupportedConformanceLevel_ShouldSucceed(CborConformanceLevel level, string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding, level); int? length = reader.ReadStartMap(); Assert.Null(length); }
public static void ReadStartMap_InvalidType_ShouldThrowInvalidOperationException(string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); Assert.Throws <InvalidOperationException>(() => reader.ReadStartMap()); Assert.Equal(encoding.Length, reader.BytesRemaining); }
public static void ReadStartMap_BufferTooSmall_ShouldThrowFormatException(string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); Assert.Throws <FormatException>(() => reader.ReadStartMap()); Assert.Equal(encoding.Length, reader.BytesRemaining); }
public static void ReadStartMap_LargeFieldCount_ShouldThrowOverflowException(string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); Assert.Throws <OverflowException>(() => reader.ReadStartMap()); Assert.Equal(encoding.Length, reader.BytesRemaining); }
internal static void ReadMap_NonCanonicalLengths_SupportedConformanceLevel_ShouldSucceed(CborConformanceLevel level, string hexEncoding) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding, level); int? length = reader.ReadStartMap(); Assert.NotNull(length); Assert.Equal(0, length !.Value); reader.ReadEndMap(); }
public static void ReadTag_CallingEndReadMapPrematurely_ShouldThrowInvalidOperationException(string hexEncoding) { // encoding is valid CBOR, so should not throw FormatException byte[] data = hexEncoding.HexToByteArray(); var reader = new CborReader(data); reader.ReadStartMap(); reader.ReadInt64(); reader.ReadTag(); Assert.Equal(CborReaderState.UnsignedInteger, reader.Peek()); Assert.Throws <InvalidOperationException>(() => reader.ReadEndArray()); }
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()); }
public static void ReadEndMap_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(string hexEncoding, int expectedLength) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); ulong?length = reader.ReadStartMap(); Assert.Equal(expectedLength, (int)length !.Value); for (int i = 1; i < expectedLength; i++) { reader.ReadInt64(); // key ulong?nestedLength = reader.ReadStartMap(); Assert.Equal(1, (int)nestedLength !.Value); reader.ReadInt64(); reader.ReadInt64(); reader.ReadEndMap(); } Assert.Throws <InvalidOperationException>(() => reader.ReadEndMap()); }
internal static void ReadMap_UnsortedKeys_ConformanceNotRequiringSortedKeys_ShouldSucceed(CborConformanceLevel level, object[] keySequence, string hexEncoding) { var reader = new CborReader(hexEncoding.HexToByteArray(), level); reader.ReadStartMap(); foreach (object key in keySequence) { Helpers.VerifyValue(reader, key); // verify key reader.ReadInt32(); // value is always an integer } reader.ReadEndMap(); }
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()); }
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()); }
public static void ReadMap_IncorrectDefiniteLength_ShouldThrowFormatException(string hexEncoding, int expectedLength, int actualLength) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); ulong?length = reader.ReadStartMap(); Assert.Equal(expectedLength, (int)length !.Value); for (int i = 0; i < actualLength; i++) { reader.ReadInt64(); // key reader.ReadInt64(); // value } Assert.Throws <FormatException>(() => reader.ReadInt64()); }
internal static void ReadMap_DuplicateKeys_StrictConformance_ShouldThrowFormatException(CborConformanceLevel level, object dupeKey, string hexEncoding) { var reader = new CborReader(hexEncoding.HexToByteArray(), level); reader.ReadStartMap(); Helpers.VerifyValue(reader, dupeKey); reader.ReadInt32(); int bytesRead = reader.BytesRead; int bytesRemaining = reader.BytesRemaining; CborReaderState state = reader.PeekState(); Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, dupeKey)); // ensure reader state is preserved Assert.Equal(bytesRead, reader.BytesRead); Assert.Equal(bytesRemaining, reader.BytesRemaining); Assert.Equal(state, reader.PeekState()); }
public static void ReadMap_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(string hexEncoding, int expectedLength) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); ulong?length = reader.ReadStartMap(); Assert.Equal(expectedLength, (int)length !.Value); for (int i = 0; i < expectedLength; i++) { reader.ReadInt64(); // key reader.ReadInt64(); // value } int bytesRemaining = reader.BytesRemaining; Assert.Throws <InvalidOperationException>(() => reader.ReadInt64()); Assert.Equal(bytesRemaining, reader.BytesRemaining); }
internal static void ReadMap_UnsortedKeys_ConformanceRequiringSortedKeys_ShouldThrowFormatException(CborConformanceLevel level, object[] keySequence, string hexEncoding) { var reader = new CborReader(hexEncoding.HexToByteArray(), level); reader.ReadStartMap(); foreach (object key in keySequence.SkipLast(1)) { Helpers.VerifyValue(reader, key); // verify key reader.ReadInt32(); // value is always an integer } int bytesRead = reader.BytesRead; int bytesRemaining = reader.BytesRemaining; CborReaderState state = reader.PeekState(); // the final element violates sorting invariant Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, keySequence.Last())); // ensure reader state is preserved Assert.Equal(bytesRead, reader.BytesRead); Assert.Equal(bytesRemaining, reader.BytesRemaining); Assert.Equal(state, reader.PeekState()); }
public static void ReadMap_IncorrectDefiniteLength_NestedValues_ShouldThrowFormatException(string hexEncoding, int expectedLength, int actualLength) { byte[] encoding = hexEncoding.HexToByteArray(); var reader = new CborReader(encoding); int?length = reader.ReadStartMap(); Assert.Equal(expectedLength, (int)length !.Value); for (int i = 0; i < actualLength; i++) { reader.ReadInt64(); // key int?innerLength = reader.ReadStartArray(); Assert.Equal(1, innerLength !.Value); reader.ReadInt64(); reader.ReadEndArray(); } int bytesRemaining = reader.BytesRemaining; Assert.Throws <FormatException>(() => reader.ReadInt64()); Assert.Equal(bytesRemaining, reader.BytesRemaining); }