public static void WriteArray_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(int definiteLength) { using var writer = new CborWriter(); writer.WriteStartArray(definiteLength); for (int i = 0; i < definiteLength; i++) { writer.WriteInt64(i); } Assert.Throws <InvalidOperationException>(() => writer.WriteInt64(0)); }
public static void WriteMap_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(int definiteLength) { var writer = new CborWriter(); writer.WriteStartMap(definiteLength); for (int i = 0; i < definiteLength; i++) { writer.WriteTextString($"key_{i}"); writer.WriteInt64(i); } Assert.Throws <InvalidOperationException>(() => writer.WriteInt64(0)); }
public static void EndWriteMap_IndefiniteLength_OddItems_ShouldThrowInvalidOperationException(int length) { using var writer = new CborWriter(); writer.WriteStartMap(); for (int i = 1; i < length; i++) { writer.WriteTextString($"key_{i}"); writer.WriteInt64(i); } writer.WriteInt64(0); Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap()); }
public static void EndWriteMap_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(int definiteLength) { using var writer = new CborWriter(); writer.WriteStartMap(definiteLength); for (int i = 1; i < definiteLength; i++) { writer.WriteTextString($"key_{i}"); writer.WriteStartMap(definiteLength: 1); writer.WriteInt64(i); writer.WriteInt64(i); writer.WriteEndMap(); } Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap()); }
public static void WriteObject(this CborWriter writer, CborObject @object) { if (@object is CborTextString text) { writer.WriteTextString(text.Value); } else if (@object is CborByteString data) { writer.WriteByteString(data.Value); } else if (@object is CborBoolean boolean) { writer.WriteBoolean(boolean.Value); } else if (@object is CborInteger number) { writer.WriteInt64(number.Value); } else if (@object is CborMap map) { writer.WriteMap(map); } else if (@object is CborArray array) { writer.WriteArray(array); } else if (@object.Type == CborType.Null) { writer.WriteNull(); } else { throw new Exception($"Unknown type. Was {@object.Type}"); } }
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 WriteInt64_SingleValue_HappyPath(long input, string hexExpectedEncoding) { byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray(); using var writer = new CborWriter(); writer.WriteInt64(input); AssertHelper.HexEqual(expectedEncoding, writer.GetEncoding()); }
public static void WriteEncodedValue_AtEndOfDefiniteLengthCollection_ShouldThrowInvalidOperationException() { var writer = new CborWriter(); writer.WriteInt64(0); Assert.Throws <InvalidOperationException>(() => writer.WriteEncodedValue(new byte[] { 0x01 })); }
public static void IsWriteCompleted_OnWrittenPrimitive_ShouldBeTrue() { using var writer = new CborWriter(); Assert.False(writer.IsWriteCompleted); writer.WriteInt64(42); Assert.True(writer.IsWriteCompleted); }
public static void WriteValue(CborWriter writer, object value, bool useDefiniteLengthCollections = true) { switch (value) { case null: writer.WriteNull(); break; case bool b: writer.WriteBoolean(b); break; case int i: writer.WriteInt32(i); break; case long i: writer.WriteInt64(i); break; case ulong i: writer.WriteUInt64(i); break; case float f: writer.WriteSingle(f); break; case double d: writer.WriteDouble(d); break; case decimal d: writer.WriteDecimal(d); break; case string s: writer.WriteTextString(s); break; case BigInteger i: writer.WriteBigInteger(i); break; case DateTimeOffset d: writer.WriteDateTimeOffset(d); break; case byte[] b: writer.WriteByteString(b); break; case byte[][] chunks: WriteChunkedByteString(writer, chunks); break; case string[] chunks when IsIndefiniteLengthByteString(chunks): byte[][] byteChunks = chunks.Skip(1).Select(ch => ch.HexToByteArray()).ToArray(); WriteChunkedByteString(writer, byteChunks); break; case string[] chunks: WriteChunkedTextString(writer, chunks); break; case object[] nested when IsCborMapRepresentation(nested): WriteMap(writer, nested, useDefiniteLengthCollections); break; case object[] nested when IsEncodedValueRepresentation(nested): byte[] encodedValue = ((string)nested[1]).HexToByteArray(); writer.WriteEncodedValue(encodedValue); break; case object[] nested when IsTaggedValueRepresentation(nested): writer.WriteTag((CborTag)nested[0]); WriteValue(writer, nested[1]); break; case object[] nested: WriteArray(writer, nested, useDefiniteLengthCollections); break; default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}"); } ; }
public static void CborWriter_WritingTwoPrimitiveValues_ShouldThrowInvalidOperationException() { using var writer = new CborWriter(); writer.WriteInt64(42); int bytesWritten = writer.BytesWritten; Assert.Throws <InvalidOperationException>(() => writer.WriteTextString("lorem ipsum")); Assert.Equal(bytesWritten, writer.BytesWritten); }
public void GetValueAsInt32Overflows(long value) { var writer = new CborWriter(); writer.WriteInt64(value); CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); Exception ex = Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsInt32()); Assert.IsType <OverflowException>(ex.InnerException); }
public static void Roundtrip_Int64(long input) { using var writer = new CborWriter(); writer.WriteInt64(input); byte[] encoding = writer.GetEncoding(); var reader = new CborReader(encoding); long result = reader.ReadInt64(); Assert.Equal(input, result); }
public static void WriteEndArray_DefiniteLengthNotMet_ShouldThrowInvalidOperationException(int definiteLength) { var writer = new CborWriter(); writer.WriteStartArray(definiteLength); for (int i = 1; i < definiteLength; i++) { writer.WriteInt64(i); } Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray()); }
public static void Roundtrip_Int64(CborConformanceMode mode, long input) { var writer = new CborWriter(mode); writer.WriteInt64(input); byte[] encoding = writer.Encode(); var reader = new CborReader(encoding, mode); long result = reader.ReadInt64(); Assert.Equal(input, result); }
public void SignWithInt64AlgorithmHeaderValue(long value) { var writer = new CborWriter(); writer.WriteInt64(value); ReadOnlySpan <byte> encodedValue = writer.Encode(); CoseHeaderMap protectedHeaders = new CoseHeaderMap(); protectedHeaders[CoseHeaderLabel.Algorithm] = CoseHeaderValue.FromEncodedValue(encodedValue); Assert.Throws <CryptographicException>(() => Sign(s_sampleContent, DefaultKey, DefaultHash, protectedHeaders)); }
public override void Write(ref CborWriter writer, DateTime value) { switch (_options.DateTimeFormat) { case DateTimeFormat.ISO8601: writer.WriteString(value.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFK")); break; case DateTimeFormat.Unix: writer.WriteInt64(new DateTimeOffset(value).ToUnixTimeSeconds()); break; } }
public override void Write(ref CborWriter writer, CborValue value, LengthMode lengthMode) { switch (value.Type) { case CborValueType.Object: ((ICborConverter <CborObject>) this).Write(ref writer, (CborObject)value, lengthMode); break; case CborValueType.Array: ((ICborConverter <CborArray>) this).Write(ref writer, (CborArray)value, lengthMode); break; case CborValueType.Positive: writer.WriteUInt64(value.Value <ulong>()); break; case CborValueType.Negative: writer.WriteInt64(value.Value <long>()); break; case CborValueType.Single: writer.WriteSingle(value.Value <float>()); break; case CborValueType.Double: writer.WriteDouble(value.Value <double>()); break; case CborValueType.Decimal: writer.WriteDecimal(value.Value <decimal>()); break; case CborValueType.String: writer.WriteString(value.Value <string>()); break; case CborValueType.Boolean: writer.WriteBoolean(value.Value <bool>()); break; case CborValueType.Null: writer.WriteNull(); break; case CborValueType.ByteString: writer.WriteByteString(value.Value <ReadOnlyMemory <byte> >().Span); break; } }
public static void WriteEncodedValue_NestedValue_HappyPath(string hexEncodedValue) { byte[] encodedValue = hexEncodedValue.HexToByteArray(); using var writer = new CborWriter(); writer.WriteStartArray(3); writer.WriteInt64(1); writer.WriteEncodedValue(encodedValue); writer.WriteTextString(""); writer.WriteEndArray(); string hexResult = writer.GetEncoding().ByteArrayToHex(); Assert.Equal("8301" + hexEncodedValue + "60", hexResult.ToLower()); }
public override void Write(ref CborWriter writer, long value) { writer.WriteInt64(value); }
public void WriteInt32(ref CborWriter writer, T value) { writer.WriteInt64(Unsafe.As <T, int>(ref value)); }