コード例 #1
0
ファイル: CborReaderTests.Map.cs プロジェクト: z77ma/runtime
        public static void ReadMap_NestedValues_ShouldAcceptKeysSortedAccordingToConformanceMode(CborConformanceMode mode, object expectedValue, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            Helpers.VerifyValue(reader, expectedValue);
        }
コード例 #2
0
        public static int CompareKeyEncodings(ReadOnlySpan <byte> left, ReadOnlySpan <byte> right, CborConformanceMode mode)
        {
            Debug.Assert(!left.IsEmpty && !right.IsEmpty);

            switch (mode)
            {
            case CborConformanceMode.Canonical:
                // 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 CborConformanceMode.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 mode used in encoding sort.");
                throw new Exception();
            }
        }
コード例 #3
0
        public static void ReadEncodedValue_InvalidConformance_ConformanceCheckEnabled_ShouldThrowCborContentException(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            Assert.Throws <CborContentException>(() => reader.ReadEncodedValue(disableConformanceModeChecks: false));
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
コード例 #4
0
 public static void InvalidConformanceMode_ShouldThrowArgumentOutOfRangeException(CborConformanceMode mode)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => new CborWriter(conformanceMode: mode));
 }
コード例 #5
0
        public static void WriteTaggedValue_SupportedConformance_ShouldSucceed(CborConformanceMode mode, object value)
        {
            var writer = new CborWriter(mode);

            Helpers.WriteValue(writer, value);
        }
コード例 #6
0
        public static void ReadArray_NonCanonicalLengths_UnSupportedConformanceMode_ShouldThrowCborContentException(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            Assert.Throws <CborContentException>(() => reader.ReadStartArray());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
コード例 #7
0
        public static void ReadInt64_NonCanonicalEncodings_UnSupportedConformanceMode_ShouldThrowCborContentException(CborConformanceMode mode, string hexEncoding)
        {
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data, mode);

            Assert.Throws <CborContentException>(() => reader.ReadInt64());
            Assert.Equal(data.Length, reader.BytesRemaining);
        }
コード例 #8
0
        public static void WriteTextString_InvalidUnicodeString_StrictConformance_ShouldThrowArgumentException(CborConformanceMode conformanceMode)
        {
            // NB Xunit's InlineDataAttribute will corrupt string literals containing invalid unicode
            string            invalidUnicodeString = "\ud800";
            var               writer = new CborWriter(conformanceMode);
            ArgumentException exn    = Assert.Throws <ArgumentException>(() => writer.WriteTextString(invalidUnicodeString));

            Assert.NotNull(exn.InnerException);
            Assert.IsType <System.Text.EncoderFallbackException>(exn.InnerException);
        }
コード例 #9
0
        public static void SkipValue_ValidationEnabled_InvalidUtf8_StrictConformance_ShouldThrowCborContentException(CborConformanceMode conformanceMode)
        {
            byte[] encoding = "62f090".HexToByteArray();
            var    reader   = new CborReader(encoding, conformanceMode);

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

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

            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
コード例 #10
0
 public CborEncoding(string name, string hexPayload, CborConformanceMode conformanceMode = CborConformanceMode.Strict)
 {
     Name            = name;
     Payload         = hexPayload.HexToByteArray();
     ConformanceMode = conformanceMode;
 }
コード例 #11
0
        public static void WriteTextString_InvalidUnicodeString_LaxConformance_ShouldSucceed(CborConformanceMode conformanceMode)
        {
            string invalidUnicodeString = "\ud800";

            byte[] expectedEncoding = { 0x63, 0xef, 0xbf, 0xbd };

            var writer = new CborWriter(conformanceMode);

            writer.WriteTextString(invalidUnicodeString);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
コード例 #12
0
ファイル: CborReaderTests.Map.cs プロジェクト: z77ma/runtime
        public static void ReadMap_UnsortedKeys_ConformanceRequiringSortedKeys_ShouldThrowCborContentException(CborConformanceMode mode, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), mode);

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

            int             bytesRemaining = reader.BytesRemaining;
            CborReaderState state          = reader.PeekState();

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

            // ensure reader state is preserved
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(state, reader.PeekState());
        }
コード例 #13
0
ファイル: CborReaderTests.Map.cs プロジェクト: z77ma/runtime
        public static void ReadMap_UnsortedKeys_ConformanceNotRequiringSortedKeys_ShouldSucceed(CborConformanceMode mode, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), mode);

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

            reader.ReadEndMap();
        }
コード例 #14
0
ファイル: CborReaderTests.Map.cs プロジェクト: z77ma/runtime
        public static void ReadMap_DuplicateKeys_StrictConformance_ShouldThrowCborContentException(CborConformanceMode mode, object dupeKey, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), mode);

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

            int             bytesRemaining = reader.BytesRemaining;
            CborReaderState state          = reader.PeekState();

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

            // ensure reader state is preserved
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(state, reader.PeekState());
        }
コード例 #15
0
        public static void ReadArray_IndefiniteLength_SupportedConformanceMode_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);
            int?   length   = reader.ReadStartArray();

            Assert.Null(length);
        }
コード例 #16
0
        public static void SkipValue_ValidationDisabled_NonConformingValues_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            reader.SkipValue(disableConformanceModeChecks: true);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
コード例 #17
0
        public static void ReadArray_NonCanonicalLengths_SupportedConformanceMode_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);
            int?   length   = reader.ReadStartArray();

            Assert.NotNull(length);
            Assert.Equal(0, length !.Value);
        }
コード例 #18
0
        public static void SkipValue_ValidationEnabled_NonConformingValues_ShouldThrowCborContentException(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            Assert.Throws <CborContentException>(() => reader.SkipValue());
        }
コード例 #19
0
        public static void ReadInt64_NonCanonicalEncodings_SupportedConformanceMode_ShouldSucceed(CborConformanceMode mode, string hexEncoding, long expectedValue)
        {
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data, mode);
            long   result = reader.ReadInt64();

            Assert.Equal(expectedValue, result);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
コード例 #20
0
        public static void SkipValue_ValidationEnabled_InvalidUtf8_LaxConformance_ShouldSucceed(CborConformanceMode conformanceMode)
        {
            byte[] encoding = "62f090".HexToByteArray();
            var    reader   = new CborReader(encoding, conformanceMode);

            reader.SkipValue();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
コード例 #21
0
        [InlineData(CborConformanceMode.Ctap2Canonical, "a280800101")] // unsorted key encodings
        public static void WriteEncodedValue_InvalidConformance_ShouldThrowArgumentException(CborConformanceMode conformanceMode, string hexEncodedInput)
        {
            byte[] encodedInput = hexEncodedInput.HexToByteArray();
            var    writer       = new CborWriter(conformanceMode);

            Assert.Throws <ArgumentException>(() => writer.WriteEncodedValue(encodedInput));
        }
コード例 #22
0
        public static void WriteMap_SimpleValues_ShouldSortKeysAccordingToConformanceMode(CborConformanceMode mode, object value, string expectedHexEncoding)
        {
            byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
            var    writer           = new CborWriter(mode);

            Helpers.WriteValue(writer, value);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
コード例 #23
0
        public static void WriteTaggedValue_UnsupportedConformance_ShouldThrowInvalidOperationException(CborConformanceMode mode, object value)
        {
            var writer = new CborWriter(mode);

            Assert.Throws <InvalidOperationException>(() => Helpers.WriteValue(writer, value));
            Assert.Equal(0, writer.BytesWritten);
        }
コード例 #24
0
        public static void WriteMap_NestedValues_ShouldSortKeysAccordingToConformanceMode(string expectedHexEncoding, CborConformanceMode mode)
        {
            object[] value            = new object[] { Map, -1, 0, new object[] { Map, 3, 3, 2, 2, 1, 1 }, 0, "a", 0, 256, 0, new object[] { Map, 2, 2, 1, 1 }, 0 };
            byte[]   expectedEncoding = expectedHexEncoding.HexToByteArray();
            var      writer           = new CborWriter(mode);

            Helpers.WriteValue(writer, value);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
コード例 #25
0
 public static Encoding GetUtf8Encoding(CborConformanceMode conformanceMode)
 {
     return(conformanceMode == CborConformanceMode.Lax ? s_utf8EncodingLax : s_utf8EncodingStrict);
 }
コード例 #26
0
        public static void WriteMap_DuplicateKeys_StrictConformance_ShouldBeRecoverableError(CborConformanceMode mode, object dupeKey)
        {
            byte[] expected = PerformWrite(attemptDuplicateWrite: false);
            byte[] actual   = PerformWrite(attemptDuplicateWrite: true);
            Assert.Equal(expected.ByteArrayToHex(), actual.ByteArrayToHex());

            byte[] PerformWrite(bool attemptDuplicateWrite)
            {
                var writer = new CborWriter(mode);

                writer.WriteStartMap(2);
                Helpers.WriteValue(writer, dupeKey);
                writer.WriteInt32(0);

                if (attemptDuplicateWrite)
                {
                    Assert.Throws <InvalidOperationException>(() => Helpers.WriteValue(writer, dupeKey));
                }

                // wrap dupe key in an array to satisfy key sorting & uniqueness constraints
                Helpers.WriteValue(writer, new object[] { dupeKey });
                writer.WriteInt32(0);
                writer.WriteEndMap();

                return(writer.Encode());
            }
        }
コード例 #27
0
 public static void InvalidConformanceMode_ShouldThrowArgumentOutOfRangeException(CborConformanceMode mode)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => new CborReader(Array.Empty <byte>(), conformanceMode: mode));
 }
コード例 #28
0
        public static void WriteStartMap_IndefiniteLength_NoPatching_UnsupportedConformance_ShouldThrowInvalidOperationException(CborConformanceMode conformanceMode)
        {
            var writer = new CborWriter(conformanceMode, convertIndefiniteLengthEncodings: false);

            Assert.Throws <InvalidOperationException>(() => writer.WriteStartMap(null));
        }
コード例 #29
0
        public static void ReadEncodedValue_InvalidConformance_ConformanceCheckDisabled_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);
            ReadOnlyMemory <byte> encodedValue = reader.ReadEncodedValue(disableConformanceModeChecks: true);

            Assert.Equal(encoding, encodedValue);
            Assert.Equal(0, reader.BytesRemaining);
        }
コード例 #30
0
        public static void PeekTag_InvalidType_UnsupportedConformanceMode_ShouldThrowInvalidOperationException(CborConformanceMode mode, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), mode);

            Assert.Throws <InvalidOperationException>(() => reader.PeekTag());
        }