예제 #1
0
        public void GetValueAsStringSucceeds(string value)
        {
            var writer = new CborWriter();

            writer.WriteTextString(value);

            CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode());

            Assert.Equal(value, headerValue.GetValueAsString());
        }
예제 #2
0
        public void FromStringSucceeds(string value)
        {
            var writer = new CborWriter();

            writer.WriteTextString(value);

            CoseHeaderValue headerValue = CoseHeaderValue.FromString(value);

            AssertExtensions.SequenceEqual(writer.Encode(), headerValue.EncodedValue.Span);
            Assert.Equal(value, headerValue.GetValueAsString());
        }
예제 #3
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));
            }
        }
예제 #4
0
        public void GetValueAsStringBeingIndefiniteLengthSucceeds(string value)
        {
            Verify(0);
            Verify(1);
            Verify(10);

            void Verify(int repetitions)
            {
                var writer = new CborWriter();

                writer.WriteStartIndefiniteLengthTextString();
                for (int i = 0; i < repetitions; i++)
                {
                    writer.WriteTextString(value);
                }
                writer.WriteEndIndefiniteLengthTextString();

                CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode());

                string expectedValue = string.Join("", Enumerable.Repeat(value, repetitions));

                Assert.Equal(expectedValue, headerValue.GetValueAsString());
            }
        }