Example #1
0
        public void Can_Encode_And_Decode_MulipleValue_Fields_SingleValue_Alt_Format()
        {
            var wireFrame = new byte[]
            {
                0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x10, 0xd0, 0x00, 0x00, 0x00, 0x3c, 0x00,
                0x00, 0x00, 0x09, 0xb1, 0x00, 0x00, 0x00, 0x00,
                0x40, 0x70, 0xff, 0xff, 0xff, 0xff, 0x60, 0xff,
                0xff, 0x40, 0x40, 0x40, 0x40,
                0xa3, 0x24, 0x30, 0x64, 0x36, 0x36, 0x32, 0x65,
                0x64, 0x35, 0x2d, 0x33, 0x30, 0x62, 0x38, 0x2d,
                0x34, 0x32, 0x62, 0x39, 0x2d, 0x61, 0x61, 0x37,
                0x30, 0x2d, 0x66, 0x33, 0x30, 0x66, 0x30, 0x37,
                0x66, 0x38, 0x61, 0x34, 0x62, 0x34, 0x40, 0x38,
                0x61, 0x34, 0x62, 0x34,
            };

            var buffer = new ByteBuffer(wireFrame);

            var decodedValue = AmqpCodec.DecodeObject <Open>(buffer);

            Assert.NotNull(decodedValue.DesiredCapabilities);
            Assert.AreEqual(1, decodedValue.DesiredCapabilities.Length);
            Assert.IsNotNullOrEmpty(decodedValue.DesiredCapabilities[0]);
        }
Example #2
0
 private static TArrayElement[] DecodeArrayWrapper <TArrayElement>(ByteBuffer buffer, byte formatCode)
 {
     if (FormatCodeIsArrayType(formatCode))
     {
         return(Encoder.ReadStronglyTypedArray <TArrayElement>(buffer, formatCode));
     }
     else
     {
         return new TArrayElement[] { AmqpCodec.DecodeObject <TArrayElement>(buffer, formatCode) }
     };
 }
Example #3
0
        public void Can_Encode_And_Decode_MulipleValue_Fields_SingleValue()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Open();

            value.DesiredCapabilities = new Symbol[] { Guid.NewGuid().ToString() };

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Open>(buffer);

            CollectionAssert.AreEqual(value.DesiredCapabilities, decodedValue.DesiredCapabilities);
        }
Example #4
0
        public void Can_Encode_And_Decode_Binary_Fields()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Transfer();

            value.DeliveryTag = new byte[] { (byte)randNum.Next(0, 10), (byte)randNum.Next(0, 10), (byte)randNum.Next(0, 10), (byte)randNum.Next(0, 10) };

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Transfer>(buffer);

            CollectionAssert.AreEqual(value.DeliveryTag, decodedValue.DeliveryTag);
        }
Example #5
0
        public void Can_Encode_And_Decode_Nested_DescribedList()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Attach();

            value.Source         = new Source();
            value.Source.Durable = (uint)randNum.Next(0, 1000);

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Attach>(buffer);

            Assert.NotNull(decodedValue.Source);
            Assert.AreEqual(value.Source.Durable, decodedValue.Source.Durable);
        }
Example #6
0
        public void Can_Encode_And_Decode_Simple_DescribedList()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Open();

            value.ContainerID = Guid.NewGuid().ToString();
            value.IdleTimeOut = (uint)randNum.Next(0, 1000);
            value.ChannelMax  = (ushort)randNum.Next(0, 1000);

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Open>(buffer);

            Assert.AreEqual(value.ContainerID, decodedValue.ContainerID);
            Assert.AreEqual(value.IdleTimeOut, decodedValue.IdleTimeOut);
            Assert.AreEqual(value.ChannelMax, decodedValue.ChannelMax);
        }
Example #7
0
        public void Can_Encode_And_Decode_Nested_Polymorphic_DescribedList()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Transfer();

            value.State = new Received()
            {
                SectionNumber = (uint)randNum.Next(0, 1000),
                SectionOffset = (uint)randNum.Next(0, 1000),
            };

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Transfer>(buffer);
            var decodedState = decodedValue.State as Received;

            Assert.NotNull(decodedState);
            Assert.AreEqual(((Received)value.State).SectionNumber, decodedState.SectionNumber);
            Assert.AreEqual(((Received)value.State).SectionOffset, decodedState.SectionOffset);
        }
Example #8
0
        public void Can_Encode_and_Decode_Skipping_Null_Values()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Disposition();

            value.First   = (uint)randNum.Next();
            value.Settled = randNum.Next(1, 101) % 2 == 0 ? true : false;
            value.Role    = randNum.Next(1, 101) % 2 == 0 ? true : false;
            value.State   = randNum.Next(1, 101) % 2 == 0 ? (DeliveryState) new Accepted() : (DeliveryState) new Rejected();

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Disposition>(buffer);

            Assert.AreEqual(value.First, decodedValue.First);
            Assert.AreEqual(value.Last, decodedValue.Last);
            Assert.AreEqual(value.Settled, decodedValue.Settled);
            Assert.AreEqual(value.Role, decodedValue.Role);
            Assert.AreEqual(value.State.Descriptor, decodedValue.State.Descriptor);
        }
Example #9
0
        public void AmqpCodecSingleValueTest()
        {
            byte[] workBuffer = new byte[2048];

            RunSingleValueTest(workBuffer, boolTrue, boolTrueEncoded, "Boolean value is not true.");
            RunSingleValueTest(workBuffer, boolFalse, boolFalseEncoded, "Boolean value is not false.");
            RunSingleValueTest(workBuffer, ubyteValue, ubyteValueEncoded, "UByte value is not equal.");
            RunSingleValueTest(workBuffer, ushortValue, ushortValueEncoded, "UShort value is not equal.");
            RunSingleValueTest(workBuffer, uint0Value, uint0ValueEncoded, "UInt0 value is not equal.");
            RunSingleValueTest(workBuffer, uintSmallValue, uintSmallValueEncoded, "UIntSmall value is not equal.");
            RunSingleValueTest(workBuffer, uintValue, uintValueEncoded, "UInt value is not equal.");
            RunSingleValueTest(workBuffer, ulong0Value, ulong0ValueEncoded, "ULong0 value is not equal.");
            RunSingleValueTest(workBuffer, ulongSmallValue, ulongSmallValueEncoded, "ULongSmall value is not equal.");
            RunSingleValueTest(workBuffer, ulongValue, ulongValueEncoded, "ULong value is not equal.");
            RunSingleValueTest(workBuffer, byteValue, byteValueEncoded, "Byte value is not equal.");
            RunSingleValueTest(workBuffer, shortValue, shortValueEncoded, "Short value is not equal.");
            RunSingleValueTest(workBuffer, intSmallValue, intSmallValueEncoded, "Int small value is not equal.");
            RunSingleValueTest(workBuffer, intValue, intValueEncoded, "Int value is not equal.");
            RunSingleValueTest(workBuffer, longSmallValue, longSmallValueEncoded, "Long small value is not equal.");
            RunSingleValueTest(workBuffer, longValue, longValueEncoded, "Long value is not equal.");
            RunSingleValueTest(workBuffer, floatValue, floatValueEncoded, "Float value is not equal.");
            RunSingleValueTest(workBuffer, doubleValue, doubleValueEncoded, "Double value is not equal.");
            RunSingleValueTest(workBuffer, charValue, charValueEncoded, "Char value is not equal.");
            RunSingleValueTest(workBuffer, dtValue, dtValueEncoded, "Timestamp value is not equal.");
            RunSingleValueTest(workBuffer, uuidValue, uuidValueEncoded, "Uuid value is not equal.");
            RunSingleValueTest(workBuffer, bin8Value, bin8ValueEncoded, "Binary8 value is not equal.");
            RunSingleValueTest(workBuffer, bin32Value, bin32ValueEncoded, "Binary32 value is not equal.");
            RunSingleValueTest(workBuffer, (Symbol)strValue, sym8ValueEncoded, "Symbol8 string value is not equal.");
            RunSingleValueTest(workBuffer, strValue, str8Utf8ValueEncoded, "UTF8 string8 string value is not equal.");

            // symbol 32
            Symbol symbol32v = AmqpCodec.DecodeObject <Symbol>(new ByteBuffer(sym32ValueEncoded, 0, sym32ValueEncoded.Length, sym32ValueEncoded.Length));

            Assert.AreEqual((string)symbol32v, strValue, "Symbol32 string value is not equal.");

            // string 32 UTF8
            string str32Utf8 = AmqpCodec.DecodeObject <string>(new ByteBuffer(str32Utf8ValueEncoded, 0, str32Utf8ValueEncoded.Length, str32Utf8ValueEncoded.Length));

            Assert.AreEqual(str32Utf8, strValue, "UTF8 string32 string value is not equal.");
        }
Example #10
0
        static void RunDescribedValueTest <T>(object descriptor, T value, byte[] workBuffer)
        {
            var        dv = new DescribedValue <T>(new Descriptor(descriptor), value);
            ByteBuffer buffer;

            AmqpCodec.EncodeObject(buffer = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length), dv);
            var dv2 = (DescribedValue <T>)AmqpCodec.DecodeObject <DescribedValue <T> >(buffer);

            Assert.AreEqual(dv.Descriptor, dv2.Descriptor);
            if (dv.Value == null)
            {
                Assert.IsNull(dv2.Value);
            }
            else if (dv.Value.GetType() == typeof(List))
            {
                EnsureEqual((IList)dv.Value, (IList)dv2.Value);
            }
            else
            {
                Assert.AreEqual(dv.Value, dv2.Value);
            }
        }
Example #11
0
        public void Can_Encode_And_Decode_Multi_Nested_DescribedList()
        {
            var buffer = new ByteBuffer(1024, false);

            var value = new Attach();

            value.Source = new Source();
            value.Source.DefaultOutcome = new Rejected();
            ((Rejected)value.Source.DefaultOutcome).Error             = new Error();
            ((Rejected)value.Source.DefaultOutcome).Error.Condition   = Guid.NewGuid().ToString();
            ((Rejected)value.Source.DefaultOutcome).Error.Description = Guid.NewGuid().ToString();

            AmqpCodec.EncodeObject(buffer, value);

            var decodedValue = AmqpCodec.DecodeObject <Attach>(buffer);

            Assert.NotNull(decodedValue.Source);
            Assert.NotNull(decodedValue.Source.DefaultOutcome as Rejected);
            Assert.NotNull(((Rejected)decodedValue.Source.DefaultOutcome).Error);
            Assert.AreEqual(((Rejected)value.Source.DefaultOutcome).Error.Condition, ((Rejected)decodedValue.Source.DefaultOutcome).Error.Condition);
            Assert.AreEqual(((Rejected)value.Source.DefaultOutcome).Error.Description, ((Rejected)decodedValue.Source.DefaultOutcome).Error.Description);
        }
Example #12
0
        static void RunSingleValueTest <T>(byte[] workBuffer, T value, byte[] result, string message)
        {
            var buffer = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);

            AmqpCodec.EncodeObject(buffer, value);

            EnsureEqual(result, 0, result.Length, buffer.Buffer, buffer.ReadOffset, buffer.LengthAvailableToRead);

            T decodeValue = AmqpCodec.DecodeObject <T>(new ByteBuffer(result, 0, result.Length, result.Length));

            if (typeof(T) == typeof(byte[]))
            {
                byte[] b1 = (byte[])(object)value;
                byte[] b2 = (byte[])(object)decodeValue;
                EnsureEqual(b1, 0, b1.Length, b2, 0, b2.Length);
            }
            else
            {
                Assert.AreEqual(value, decodeValue, message);
            }

            System.Diagnostics.Trace.WriteLine($"Test Passed For Type={typeof(T).FullName}");
        }
Example #13
0
        public void AmqpCodecListTest()
        {
            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);
            string     strBig     = new string('A', 512);

            var list = new AmqpList();

            list.Add(boolTrue);
            list.Add(boolFalse);
            list.Add(ubyteValue);
            list.Add(ushortValue);
            list.Add(uintValue);
            list.Add(ulongValue);
            list.Add(byteValue);
            list.Add(shortValue);
            list.Add(intValue);
            list.Add(longValue);
            list.Add(null);
            list.Add(floatValue);
            list.Add(doubleValue);
            list.Add(charValue);
            list.Add(dtValue);
            list.Add(uuidValue);
            list.Add(bin8ValueEncoded);
            list.Add(bin32ValueEncoded);
            list.Add((Symbol)null);
            list.Add(new Symbol(strValue));
            list.Add(new Symbol(strBig));
            list.Add(strValue);
            list.Add(strBig);
            list.Add(described1);
            list.Add(described2);
            list.Add(described3);
            list.Add(described4);

            AmqpCodec.EncodeObject(buffer, list);

            // make sure the size written is correct (it has to be List32)
            // the first byte is FormatCode.List32
            int listSize = (workBuffer[1] << 24) | (workBuffer[2] << 16) | (workBuffer[3] << 8) | workBuffer[4];

            Assert.AreEqual(buffer.LengthAvailableToRead - 5, listSize);

            IList decList = AmqpCodec.DecodeObject <AmqpList>(buffer);
            int   index   = 0;

            Assert.IsTrue(decList[index++].Equals(true), "Boolean true expected.");
            Assert.IsTrue(decList[index++].Equals(false), "Boolean false expected.");
            Assert.IsTrue(decList[index++].Equals(ubyteValue), "UByte value not equal.");
            Assert.IsTrue(decList[index++].Equals(ushortValue), "UShort value not equal.");
            Assert.IsTrue(decList[index++].Equals(uintValue), "UInt value not equal.");
            Assert.IsTrue(decList[index++].Equals(ulongValue), "ULong value not equal.");
            Assert.IsTrue(decList[index++].Equals(byteValue), "Byte value not equal.");
            Assert.IsTrue(decList[index++].Equals(shortValue), "Short value not equal.");
            Assert.IsTrue(decList[index++].Equals(intValue), "Int value not equal.");
            Assert.IsTrue(decList[index++].Equals(longValue), "Long value not equal.");
            Assert.IsTrue(decList[index++] == null, "Null object expected.");
            Assert.IsTrue(decList[index++].Equals(floatValue), "Float value not equal.");
            Assert.IsTrue(decList[index++].Equals(doubleValue), "Double value not equal.");
            Assert.IsTrue(decList[index++].Equals(charValue), "Char value not equal.");
            Assert.IsTrue(decList[index++].Equals(dtValue), "TimeStamp value not equal.");
            Assert.IsTrue(decList[index++].Equals(uuidValue), "Uuid value not equal.");

            byte[] bin8 = (byte[])decList[index++];
            EnsureEqual(bin8, 0, bin8.Length, bin8ValueEncoded, 0, bin8ValueEncoded.Length);
            byte[] bin32 = (byte[])decList[index++];
            EnsureEqual(bin32, 0, bin32.Length, bin32ValueEncoded, 0, bin32ValueEncoded.Length);

            Assert.IsTrue(decList[index++] == null, "Null symbol expected.");
            Symbol symDecode = (Symbol)decList[index++];

            Assert.IsTrue(symDecode.Equals((Symbol)strValue), "AmqpSymbol value not equal.");
            symDecode = (Symbol)decList[index++];
            Assert.IsTrue(symDecode.Equals((Symbol)strBig), "AmqpSymbol value (big) not equal.");

            string strDecode = (string)decList[index++];

            Assert.IsTrue(strDecode.Equals(strValue), "string value not equal.");
            strDecode = (string)decList[index++];
            Assert.IsTrue(strDecode.Equals(strBig), "string value (big) not equal.");

            //Assert.AreEqual(described1, (DescribedType)decList[index++]);
            //Assert.AreEqual(described2, (DescribedType)decList[index++]);
            //Assert.AreEqual(described3, (DescribedType)decList[index++]);
            //Assert.AreEqual(described4, (DescribedType)decList[index++]);

            DescribedType described;

            described = (DescribedType)decList[index++];
            Assert.IsTrue(described.Descriptor.Equals(described1.Descriptor), "Described value 1 descriptor is different");
            Assert.IsTrue(described.Equals(described1), "Described value 1 value is different");

            described = (DescribedType)decList[index++];
            Assert.IsTrue(described.Descriptor.Equals(described2.Descriptor), "Described value 2 descriptor is different");
            Assert.IsTrue(described.Equals(described2), "Described value 2 value is different");

            described = (DescribedType)decList[index++];
            Assert.IsTrue(described.Descriptor.Equals(described3.Descriptor), "Described value 3 descriptor is different");
            Assert.IsTrue(described.Equals(described3), "Described value 3 value is different");

            described = (DescribedType)decList[index++];
            Assert.IsTrue(described.Descriptor.Equals(described4.Descriptor), "Described value 4 descriptor is different");
            EnsureEqual(((DescribedValue <AmqpList>)described).Value, ((DescribedValue <AmqpList>)described4).Value);
        }