Exemple #1
0
        public static void EndWriteMap_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartMap(definiteLength);
            for (int i = 1; i < definiteLength; i++)
            {
                writer.WriteTextString($"key_{i}");
                writer.WriteStartMap(definiteLength: 1);
                writer.WriteInt64(i);
                writer.WriteInt64(i);
                writer.WriteEndMap();
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap());
        }
            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.WriteStartTextString): writer.WriteStartTextString(); break;

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

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

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

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

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

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

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

                default: throw new Exception($"Unrecognized CborWriter operation name {op}");
                }
            }
Exemple #3
0
        internal static void WriteMap_DuplicateKeys_StrictConformance_ShouldBeRecoverableError(CborConformanceLevel level, object dupeKey)
        {
            byte[] expected = PerformWrite(attemptDuplicateWrite: false);
            byte[] actual   = PerformWrite(attemptDuplicateWrite: true);
            Assert.Equal(expected.ByteArrayToHex(), actual.ByteArrayToHex());

            byte[] PerformWrite(bool attemptDuplicateWrite)
            {
                using var writer = new CborWriter(level);
                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.GetEncoding());
            }
        }
Exemple #4
0
 internal 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 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());
        }
        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());
        }
Exemple #7
0
        public static void WriteMap_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartMap(definiteLength);
            for (int i = 0; i < definiteLength; i++)
            {
                writer.WriteTextString($"key_{i}");
                writer.WriteInt64(i);
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteInt64(0));
        }
            public static void WriteMap(CborWriter writer, object[] keyValuePairs, bool useDefiniteLengthCollections = true)
            {
                if (!IsCborMapRepresentation(keyValuePairs))
                {
                    throw new ArgumentException($"CBOR map representation must contain odd number of elements prepended with a '{MapPrefixIdentifier}' constant.");
                }

                if (useDefiniteLengthCollections)
                {
                    writer.WriteStartMap(keyValuePairs.Length / 2);
                }
                else
                {
                    writer.WriteStartMap();
                }

                foreach (object value in keyValuePairs.Skip(1))
                {
                    WriteValue(writer, value, useDefiniteLengthCollections);
                }

                writer.WriteEndMap();
            }
Exemple #9
0
        public static void EndWriteMap_IndefiniteLength_OddItems_ShouldThrowInvalidOperationException(int length)
        {
            using var writer = new CborWriter();
            writer.WriteStartMap();

            for (int i = 1; i < length; i++)
            {
                writer.WriteTextString($"key_{i}");
                writer.WriteInt64(i);
            }

            writer.WriteInt64(0);

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