Exemple #1
0
        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);
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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));
                }
            }
        }
Exemple #4
0
        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));
            }
        }