Exemplo 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());
        }
Exemplo n.º 2
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.WriteStartTextStringIndefiniteLength): writer.WriteStartTextStringIndefiniteLength(); break;

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

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

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

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

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

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

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

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

            writer.WriteStartArray();
            writer.WriteTag(CborTag.Uri);
            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
Exemplo n.º 4
0
 public static void WriteArray(CborWriter writer, params object[] values)
 {
     writer.WriteStartArray(values.Length);
     foreach (object value in values)
     {
         WriteValue(writer, value);
     }
     writer.WriteEndArray();
 }
Exemplo n.º 5
0
        public static void WriteEndArray_ImbalancedCall_ShouldThrowInvalidOperationException(int depth)
        {
            using var writer = new CborWriter();
            for (int i = 0; i < depth; i++)
            {
                writer.WriteStartMap(1);
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
Exemplo n.º 6
0
        public static void GetEncoding_MultipleRootLevelValuesAllowed_PartialRootValue_ShouldThrowInvalidOperationException()
        {
            using 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.GetEncoding());
        }
Exemplo n.º 7
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());
        }
Exemplo n.º 8
0
            public static void WriteArray(CborWriter writer, object[] values, bool useDefiniteLengthCollections = true)
            {
                if (useDefiniteLengthCollections)
                {
                    writer.WriteStartArray(values.Length);
                }
                else
                {
                    writer.WriteStartArrayIndefiniteLength();
                }

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

                writer.WriteEndArray();
            }
Exemplo n.º 9
0
        public static void WriteEndArray_AfterStartMap_ShouldThrowInvalidOperationException(int depth)
        {
            using var writer = new CborWriter();

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

            writer.WriteStartMap(definiteLength: 0);
            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
Exemplo n.º 10
0
        public static void WriteArray(CborWriter writer, params object[] values)
        {
            writer.WriteStartArray(values.Length);
            foreach (object value in values)
            {
                switch (value)
                {
                case int i: writer.WriteInt64(i); break;

                case string s: writer.WriteTextString(s); break;

                case byte[] b: writer.WriteByteString(b); break;

                case object[] nested: ArrayWriterHelper.WriteArray(writer, nested); break;

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
            writer.WriteEndArray();
        }
Exemplo n.º 11
0
 public static void EndWriteArray_ImbalancedCall_ShouldThrowInvalidOperationException()
 {
     using var writer = new CborWriter();
     Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
 }