Ejemplo n.º 1
0
        public static void WriteEndArray_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartArray(definiteLength);
            for (int i = 1; i < definiteLength; i++)
            {
                writer.WriteStartArray(definiteLength: 1);
                writer.WriteInt64(i);
                writer.WriteEndArray();
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
        public static void GetEncoding_MultipleRootLevelValuesAllowed_PartialRootValue_ShouldThrowInvalidOperationException()
        {
            var writer = new CborWriter(allowMultipleRootLevelValues: true);

            writer.WriteStartArray(1);
            writer.WriteDouble(3.14);
            writer.WriteEndArray();
            writer.WriteStartArray(1);
            writer.WriteDouble(3.14);
            // misses writer.WriteEndArray();

            Assert.Throws <InvalidOperationException>(() => writer.Encode());
        }
Ejemplo n.º 3
0
        public void SetEncodedValue_KnownHeaders_ThrowIf_IncorrectValue(SetValueMethod method)
        {
            if (method == SetValueMethod.AddShortcut)
            {
                return;
            }

            var writer = new CborWriter();

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

            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.Algorithm, encodedNullValue, method));
            // [ +label ] (non-empty array)
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, encodedNullValue, method));
            writer.Reset();
            writer.WriteStartArray(0);
            writer.WriteEndArray();
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, writer.Encode(), method));
            // tstr / uint
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.ContentType, encodedNullValue, method));
            // bstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.KeyIdentifier, encodedNullValue, method));
        }
Ejemplo n.º 4
0
            public static void ExecOperation(CborWriter writer, string op)
            {
                switch (op)
                {
                case nameof(writer.WriteInt64): writer.WriteInt64(42); break;

                case nameof(writer.WriteByteString): writer.WriteByteString(Array.Empty <byte>()); break;

                case nameof(writer.WriteTextString): writer.WriteTextString(""); break;

                case nameof(writer.WriteStartIndefiniteLengthTextString): writer.WriteStartIndefiniteLengthTextString(); break;

                case nameof(writer.WriteStartIndefiniteLengthByteString): writer.WriteStartIndefiniteLengthByteString(); break;

                case nameof(writer.WriteStartArray): writer.WriteStartArray(null); break;

                case nameof(writer.WriteStartMap): writer.WriteStartMap(null); break;

                case nameof(writer.WriteEndIndefiniteLengthByteString): writer.WriteEndIndefiniteLengthByteString(); break;

                case nameof(writer.WriteEndIndefiniteLengthTextString): writer.WriteEndIndefiniteLengthTextString(); break;

                case nameof(writer.WriteEndArray): writer.WriteEndArray(); break;

                case nameof(writer.WriteEndMap): writer.WriteEndMap(); break;

                default: throw new Exception($"Unrecognized CborWriter operation name {op}");
                }
            }
Ejemplo n.º 5
0
        public static void WriteTag_NoValueInNestedContext_ShouldThrowInvalidOperationException()
        {
            var writer = new CborWriter();

            writer.WriteStartArray(null);
            writer.WriteTag(CborTag.Uri);
            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
Ejemplo n.º 6
0
            public static void WriteArray(CborWriter writer, object[] values, bool useDefiniteLengthCollections = true)
            {
                if (useDefiniteLengthCollections)
                {
                    writer.WriteStartArray(values.Length);
                }
                else
                {
                    writer.WriteStartArray(null);
                }

                foreach (object value in values)
                {
                    WriteValue(writer, value, useDefiniteLengthCollections);
                }

                writer.WriteEndArray();
            }
Ejemplo n.º 7
0
        public static void WriteEndMap_AfterStartArray_ShouldThrowInvalidOperationException(int depth)
        {
            var writer = new CborWriter();

            for (int i = 0; i < depth; i++)
            {
                if (i % 2 == 0)
                {
                    writer.WriteStartArray(1);
                }
                else
                {
                    writer.WriteStartMap(1);
                }
            }

            writer.WriteStartArray(definiteLength: 0);
            Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap());
        }
Ejemplo n.º 8
0
        public static void WriteEndMap_ImbalancedCall_ShouldThrowInvalidOperationException(int depth)
        {
            using var writer = new CborWriter();
            for (int i = 0; i < depth; i++)
            {
                writer.WriteStartArray(1);
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap());
        }
Ejemplo n.º 9
0
        public static void WriteArray(this CborWriter writer, CborArray array)
        {
            writer.WriteStartArray(array.Length);

            foreach (var item in array.Values)
            {
                WriteObject(writer, item);
            }

            writer.WriteEndArray();
        }
Ejemplo n.º 10
0
        public static void WriteArray_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartArray(definiteLength);
            for (int i = 0; i < definiteLength; i++)
            {
                writer.WriteInt64(i);
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteInt64(0));
        }
Ejemplo n.º 11
0
        public void DecodeSign1_IncorrectStructure()
        {
            var writer = new CborWriter();

            writer.WriteStartArray(4);
            writer.WriteNull();
            writer.WriteNull();
            writer.WriteNull();
            writer.WriteNull();
            writer.WriteEndArray();
            Assert.Throws <CryptographicException>(() => CoseMessage.DecodeSign1(writer.Encode()));
        }
Ejemplo n.º 12
0
        public static void WriteEncodedValue_NestedValue_HappyPath(string hexEncodedValue)
        {
            byte[] encodedValue = hexEncodedValue.HexToByteArray();

            using var writer = new CborWriter();
            writer.WriteStartArray(3);
            writer.WriteInt64(1);
            writer.WriteEncodedValue(encodedValue);
            writer.WriteTextString("");
            writer.WriteEndArray();

            string hexResult = writer.GetEncoding().ByteArrayToHex();

            Assert.Equal("8301" + hexEncodedValue + "60", hexResult.ToLower());
        }
        public static void Reset_NonTrivialWriter_HappyPath()
        {
            // Set up: build a nontrivial writer state.
            // Favor maps and Ctap2 canonicalization since
            // since that utilizes most of the moving parts.
            var writer = new CborWriter(conformanceMode: CborConformanceMode.Ctap2Canonical);

            for (int i = 0; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    writer.WriteStartMap(100);
                }
                else
                {
                    writer.WriteStartArray(100);
                }
            }

            writer.WriteStartMap(3);

            writer.WriteInt32(1);  // key
            writer.WriteInt32(2);  // value

            writer.WriteInt32(-1); // key
            writer.WriteInt32(1);  // value

            // End set up

            Assert.Equal(11, writer.CurrentDepth);
            Assert.True(writer.BytesWritten > 11, "must have written a nontrivial number of bytes to the buffer");

            writer.Reset();

            Assert.Equal(0, writer.CurrentDepth);
            Assert.Equal(0, writer.BytesWritten);

            // Write an object from scratch and validate that it is correct

            writer.WriteInt32(42);
            Assert.Equal(new byte[] { 0x18, 0x2a }, writer.Encode());
        }
Ejemplo n.º 14
0
        public static void WriteStartArray_IndefiniteLength_NoPatching_UnsupportedConformance_ShouldThrowInvalidOperationException(CborConformanceLevel conformanceLevel)
        {
            var writer = new CborWriter(conformanceLevel, convertIndefiniteLengthEncodings: false);

            Assert.Throws <InvalidOperationException>(() => writer.WriteStartArray());
        }