예제 #1
0
        public void Enumerate()
        {
            var map = new CoseHeaderMap();

            map.SetValue(CoseHeaderLabel.Algorithm, (int)ECDsaAlgorithm.ES256);
            map.SetEncodedValue(CoseHeaderLabel.Critical, GetDummyCritHeaderValue());
            map.SetValue(CoseHeaderLabel.ContentType, ContentTypeDummyValue);
            map.SetValue(CoseHeaderLabel.KeyIdentifier, s_sampleContent);
            map.SetValue(CoseHeaderLabel.IV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.PartialIV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.CounterSignature, ReadOnlySpan <byte> .Empty);

            var writer        = new CborWriter();
            int currentHeader = KnownHeaderAlg;

            foreach ((CoseHeaderLabel label, ReadOnlyMemory <byte> encodedValue) in map)
            {
                Assert.Equal(new CoseHeaderLabel(currentHeader), label);
                ReadOnlyMemory <byte> expectedValue = currentHeader switch
                {
                    KnownHeaderAlg => EncodeInt32((int)ECDsaAlgorithm.ES256, writer),
                    KnownHeaderCrit => GetDummyCritHeaderValue(),
                    KnownHeaderContentType => EncodeString(ContentTypeDummyValue, writer),
                    KnownHeaderKid => EncodeBytes(s_sampleContent, writer),
                    KnownHeaderIV or KnownHeaderPartialIV or KnownHeaderCounterSignature => EncodeBytes(ReadOnlySpan <byte> .Empty, writer),
                    _ => throw new InvalidOperationException()
                };
                AssertExtensions.SequenceEqual(expectedValue.Span, encodedValue.Span);
                currentHeader++;
            }
            Assert.Equal(KnownHeaderCounterSignature + 1, currentHeader);
예제 #2
0
        public void SetEncodedValue_KnownHeaders_ThrowIf_IncorrectValue()
        {
            var writer = new CborWriter();

            writer.WriteNull();
            byte[] encodedNullValue = writer.Encode();

            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <InvalidOperationException>(() => map.SetEncodedValue(CoseHeaderLabel.Algorithm, encodedNullValue));
            // [ +label ] (non-empty array) Not yet properly supported.
            //Assert.Throws<NotSupportedException>(() => map.SetEncodedValue(CoseHeaderLabel.Critical, encodedNullValue));
            // tstr / uint
            Assert.Throws <InvalidOperationException>(() => map.SetEncodedValue(CoseHeaderLabel.ContentType, encodedNullValue));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetEncodedValue(CoseHeaderLabel.KeyIdentifier, encodedNullValue));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetEncodedValue(CoseHeaderLabel.IV, encodedNullValue));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetEncodedValue(CoseHeaderLabel.PartialIV, encodedNullValue));
        }
예제 #3
0
        public void SetEncodedValue_GetEncodedValue_KnownCoseHeaderLabel(int knownHeader, byte[] encodedValue)
        {
            var map   = new CoseHeaderMap();
            var label = new CoseHeaderLabel(knownHeader);

            map.SetEncodedValue(label, encodedValue);

            ReadOnlyMemory <byte> returnedEncocedValue = map.GetEncodedValue(label);

            AssertExtensions.SequenceEqual(encodedValue, returnedEncocedValue.Span);

            map.TryGetEncodedValue(label, out returnedEncocedValue);
            AssertExtensions.SequenceEqual(encodedValue, returnedEncocedValue.Span);
        }