public void GetValueAsBytesSucceeds(ContentTestCase @case) { byte[] content = GetDummyContent(@case); var writer = new CborWriter(); writer.WriteByteString(content); CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); AssertExtensions.SequenceEqual(content, headerValue.GetValueAsBytes()); Span <byte> buffer = new byte[content.Length]; int length = headerValue.GetValueAsBytes(buffer); Assert.Equal(content.Length, length); AssertExtensions.SequenceEqual(content, buffer); }
public void FromBytesThrowsBufferTooSmall(ContentTestCase @case) { byte[] content = GetDummyContent(@case); CoseHeaderValue headerValue = CoseHeaderValue.FromBytes(content.AsSpan()); Memory <byte> buffer = new byte[content.Length - 1]; Assert.Throws <ArgumentException>("destination", () => headerValue.GetValueAsBytes(buffer.Span)); }
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)); } } }
public void GetValueAsThrows(byte[] encodedValue, GetValueAs method) { CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(encodedValue); if (method == GetValueAs.Int32) { Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsInt32()); } else if (method == GetValueAs.String) { Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsString()); } else if (method == GetValueAs.Bytes) { Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsBytes()); } else { Assert.Equal(GetValueAs.BytesSpan, method); Memory <byte> buffer = new byte[1024]; // big enough to not throw ArgumentException. Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsBytes(buffer.Span)); } }