public static void SkipValue_NonConformingValues_ShouldSucceed(CborConformanceLevel level, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);

            reader.SkipValue();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
 public static void WriteMap_DuplicateKeys_StrictConformance_ShouldFail(CborConformanceLevel level, object dupeKey)
 {
     using var writer = new CborWriter(level);
     writer.WriteStartMap(2);
     Helpers.WriteValue(writer, dupeKey);
     writer.WriteInt32(0);
     Assert.Throws <InvalidOperationException>(() => Helpers.WriteValue(writer, dupeKey));
 }
 public static void Validate(CborConformanceLevel conformanceLevel)
 {
     if (conformanceLevel < CborConformanceLevel.Lax ||
         conformanceLevel > CborConformanceLevel.Ctap2Canonical)
     {
         throw new ArgumentOutOfRangeException(nameof(conformanceLevel));
     }
 }
        public static bool RequireCanonicalSimpleValueEncodings(CborConformanceLevel conformanceLevel)
        {
            switch (conformanceLevel)
            {
            case CborConformanceLevel.Lax:
                return(false);

            case CborConformanceLevel.Strict:
            case CborConformanceLevel.Canonical:
            case CborConformanceLevel.Ctap2Canonical:
                return(true);

            default:
                throw new ArgumentOutOfRangeException(nameof(conformanceLevel));
            }
        }
        public static bool RequiresCanonicalIntegerRepresentation(CborConformanceLevel conformanceLevel)
        {
            switch (conformanceLevel)
            {
            case CborConformanceLevel.Lax:
            case CborConformanceLevel.Strict:
                return(false);

            case CborConformanceLevel.Canonical:
            case CborConformanceLevel.Ctap2Canonical:
                return(true);

            default:
                throw new ArgumentOutOfRangeException(nameof(conformanceLevel));
            }
            ;
        }
        public static bool RequiresSortedKeys(CborConformanceLevel conformanceLevel)
        {
            switch (conformanceLevel)
            {
            case CborConformanceLevel.Strict:
            case CborConformanceLevel.Lax:
                return(false);

            case CborConformanceLevel.Rfc7049Canonical:
            case CborConformanceLevel.Ctap2Canonical:
                return(true);

            default:
                throw new ArgumentOutOfRangeException(nameof(conformanceLevel));
            }
            ;
        }
        public static bool AllowsTags(CborConformanceLevel conformanceLevel)
        {
            switch (conformanceLevel)
            {
            case CborConformanceLevel.Lax:
            case CborConformanceLevel.Strict:
            case CborConformanceLevel.Canonical:
                return(true);

            case CborConformanceLevel.Ctap2Canonical:
                return(false);

            default:
                throw new ArgumentOutOfRangeException(nameof(conformanceLevel));
            }
            ;
        }
        public static void SkipValue_ValidationEnabled_NonConformingValues_ShouldThrowFormatException(CborConformanceLevel level, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);

            Assert.Throws <FormatException>(() => reader.SkipValue(validateConformance: true));
        }
        public static void ReadArray_IndefiniteLength_SupportedConformanceLevel_ShouldSucceed(CborConformanceLevel level, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);
            int?   length   = reader.ReadStartArray();

            Assert.Null(length);
        }
        public static void SkipValue_ValidationEnabled_InvalidUtf8_LaxConformance_ShouldSucceed(CborConformanceLevel conformanceLevel)
        {
            byte[] encoding = "62f090".HexToByteArray();
            var    reader   = new CborReader(encoding, conformanceLevel);

            reader.SkipValue();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
        public static void ReadInt64_NonCanonicalEncodings_UnSupportedConformanceLevel_ShouldThrowFormatException(CborConformanceLevel level, string hexEncoding)
        {
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data, level);

            Assert.Throws <FormatException>(() => reader.ReadInt64());
            Assert.Equal(0, reader.BytesRead);
        }
        public static void ReadMap_UnsortedKeys_ConformanceRequiringSortedKeys_ShouldThrowFormatException(CborConformanceLevel level, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.ReadStartMap();
            foreach (object key in keySequence.SkipLast(1))
            {
                Helpers.VerifyValue(reader, key); // verify key
                reader.ReadInt32();               // value is always an integer
            }

            int             bytesRead = reader.BytesRead;
            CborReaderState state     = reader.PeekState();

            // the final element violates sorting invariant
            Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, keySequence.Last()));

            // ensure reader state is preserved
            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(state, reader.PeekState());
        }
        public static void SkipValue_ValidationEnabled_InvalidUtf8_StrictConformance_ShouldThrowFormatException(CborConformanceLevel conformanceLevel)
        {
            byte[] encoding = "62f090".HexToByteArray();
            var    reader   = new CborReader(encoding, conformanceLevel);

            FormatException exn = Assert.Throws <FormatException>(() => reader.SkipValue());

            Assert.NotNull(exn.InnerException);
            Assert.IsType <DecoderFallbackException>(exn.InnerException);

            Assert.Equal(0, reader.BytesRead);
        }
Example #14
0
 internal static void InvalidConformanceLevel_ShouldThrowArgumentOutOfRangeException(CborConformanceLevel level)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => new CborWriter(conformanceLevel: level));
 }
        public static void PeekTag_InvalidType_UnsupportedConformanceLevel_ShouldThrowInvalidOperationException(CborConformanceLevel level, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            Assert.Throws <InvalidOperationException>(() => reader.PeekTag());
        }
Example #16
0
 internal static void WriteTaggedValue_UnsupportedConformance_ShouldThrowInvalidOperationException(CborConformanceLevel level, object value)
 {
     using var writer = new CborWriter(level);
     Assert.Throws <InvalidOperationException>(() => Helpers.WriteValue(writer, value));
     Assert.Equal(0, writer.BytesWritten);
 }
Example #17
0
        public static void WriteStartArray_IndefiniteLength_NoPatching_UnsupportedConformance_ShouldThrowInvalidOperationException(CborConformanceLevel conformanceLevel)
        {
            var writer = new CborWriter(conformanceLevel, convertIndefiniteLengthEncodings: false);

            Assert.Throws <InvalidOperationException>(() => writer.WriteStartArray());
        }
        public static void ReadTaggedValue_UnsupportedConformance_ShouldThrowFormatException(CborConformanceLevel level, object expectedValue, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, expectedValue));
            Assert.Equal(0, reader.BytesRead);
        }
        public static int CompareKeyEncodings(ReadOnlySpan <byte> left, ReadOnlySpan <byte> right, CborConformanceLevel level)
        {
            Debug.Assert(!left.IsEmpty && !right.IsEmpty);

            switch (level)
            {
            case CborConformanceLevel.Rfc7049Canonical:
                // Implements key sorting according to
                // https://tools.ietf.org/html/rfc7049#section-3.9

                if (left.Length != right.Length)
                {
                    return(left.Length - right.Length);
                }

                return(left.SequenceCompareTo(right));

            case CborConformanceLevel.Ctap2Canonical:
                // Implements key sorting according to
                // https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#message-encoding

                int leftMt  = (int)new CborInitialByte(left[0]).MajorType;
                int rightMt = (int)new CborInitialByte(right[0]).MajorType;

                if (leftMt != rightMt)
                {
                    return(leftMt - rightMt);
                }

                if (left.Length != right.Length)
                {
                    return(left.Length - right.Length);
                }

                return(left.SequenceCompareTo(right));

            default:
                Debug.Fail("Invalid conformance level used in encoding sort.");
                throw new Exception("Invalid conformance level used in encoding sort.");
            }
        }
Example #20
0
        public static void WriteSimpleValue_InvalidValue_UnsupportedConformance_ShouldThrowArgumentOutOfRangeException(CborConformanceLevel conformanceLevel, CborSimpleValue input)
        {
            var writer = new CborWriter(conformanceLevel);

            Assert.Throws <ArgumentOutOfRangeException>(() => writer.WriteSimpleValue(input));
        }
        public static void ReadArray_NonCanonicalLengths_SupportedConformanceLevel_ShouldSucceed(CborConformanceLevel level, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);
            int?   length   = reader.ReadStartArray();

            Assert.NotNull(length);
            Assert.Equal(0, length !.Value);
        }
        public static void ReadTaggedValue_SupportedConformance_ShouldSucceed(CborConformanceLevel level, object expectedValue, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            Helpers.VerifyValue(reader, expectedValue);
        }
        public static void ReadArray_NonCanonicalLengths_UnSupportedConformanceLevel_ShouldThrowFormatException(CborConformanceLevel level, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);

            Assert.Throws <FormatException>(() => reader.ReadStartArray());
            Assert.Equal(0, reader.BytesRead);
        }
        public static void PeekTag_SupportedConformanceLevel_ShouldSucceed(CborConformanceLevel level, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.PeekTag();
        }
Example #25
0
 internal static void WriteTaggedValue_SupportedConformance_ShouldSucceed(CborConformanceLevel level, object value)
 {
     using var writer = new CborWriter(level);
     Helpers.WriteValue(writer, value);
 }
        public static void ReadMap_NestedValues_ShouldAcceptKeysSortedAccordingToConformanceLevel(CborConformanceLevel level, object expectedValue, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, level);

            Helpers.VerifyValue(reader, expectedValue);
        }
Example #27
0
 public static void InvalidConformanceLevel_ShouldThrowArgumentOutOfRangeException(CborConformanceLevel level)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => new CborReader(Array.Empty <byte>(), conformanceLevel: level));
 }
        public static void ReadMap_DuplicateKeys_StrictConformance_ShouldThrowFormatException(CborConformanceLevel level, object dupeKey, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.ReadStartMap();
            Helpers.VerifyValue(reader, dupeKey);
            reader.ReadInt32();

            int             bytesRead = reader.BytesRead;
            CborReaderState state     = reader.PeekState();

            Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, dupeKey));

            // ensure reader state is preserved
            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(state, reader.PeekState());
        }
Example #29
0
 internal static void EncodeIndefiniteLengths_UnsupportedConformanceLevel_ShouldThrowArgumentException(CborConformanceLevel level)
 {
     Assert.Throws <ArgumentException>(() => new CborWriter(level, encodeIndefiniteLengths: true));
 }
        public static void ReadMap_UnsortedKeys_ConformanceNotRequiringSortedKeys_ShouldSucceed(CborConformanceLevel level, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.ReadStartMap();
            foreach (object key in keySequence)
            {
                Helpers.VerifyValue(reader, key); // verify key
                reader.ReadInt32();               // value is always an integer
            }

            reader.ReadEndMap();
        }