Exemplo n.º 1
0
 private static bool EncodeObjectWrapper <T>(ByteBuffer buffer, T value, bool arrayEncoding)
 {
     AmqpCodec.EncodeObject(buffer, value, arrayEncoding);
     if (EqualityComparer <T> .Default.Equals(value, default(T)) &&
         typeof(T).IsClass)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        static void RunArrayTest <T>(byte[] workBuffer, Func <int, T> generator, int count)
        {
            ByteBuffer buffer = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);

            T[] array = new T[count];
            for (int i = 0; i < count; i++)
            {
                array[i] = generator(i);
            }
            AmqpCodec.EncodeObject(buffer, array);

            var array2 = (T[])AmqpCodec.DecodeBoxedObject(buffer);

            Assert.AreEqual(array.Length, array2.Length);
        }
Exemplo n.º 5
0
        public void AmqpCodecList0Test()
        {
            byte[]     list0Bin   = new byte[] { 0x45 };
            byte[]     workBuffer = new byte[128];
            ByteBuffer buffer     = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);

            var list0 = new AmqpList();

            AmqpCodec.EncodeObject(buffer, list0);
            EnsureEqual(list0Bin, 0, list0Bin.Length, buffer.Buffer, buffer.ReadOffset, buffer.LengthAvailableToRead);

            IList list0v = (IList)AmqpCodec.DecodeBoxedObject(buffer);

            Assert.AreEqual(0, list0v.Count, "The list should contain 0 items.");
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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}");
        }
Exemplo n.º 13
0
        public void AmqpCodecMapTest()
        {
            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);
            string     strBig     = new string('A', 512);

            Map map = new Map();

            map.Add(new Symbol("boolTrue"), boolTrue);
            map.Add(new Symbol("boolFalse"), boolFalse);
            map.Add(new Symbol("ubyte"), ubyteValue);
            map.Add(new Symbol("ushort"), ushortValue);
            map.Add(new Symbol("uint"), uintValue);
            map.Add(new Symbol("ulong"), ulongValue);
            map.Add(new Symbol("byte"), byteValue);
            map.Add(new Symbol("short"), shortValue);
            map.Add(new Symbol("int"), intValue);
            map.Add(new Symbol("long"), longValue);
            map.Add(new Symbol("null"), null);
            map.Add(new Symbol("float"), floatValue);
            map.Add(new Symbol("double"), doubleValue);
            map.Add(new Symbol("char"), charValue);
            map.Add(new Symbol("datetime"), dtValue);
            map.Add(new Symbol("uuid"), uuidValue);
            map.Add(new Symbol("binaryNull"), null);
            map.Add(new Symbol("binary8"), bin8ValueEncoded);
            map.Add(new Symbol("binary32"), bin32ValueEncoded);
            map.Add(new Symbol("symbolNull"), (Symbol)null);
            map.Add(new Symbol("symbol8"), new Symbol(strValue));
            map.Add(new Symbol("symbol32"), new Symbol(strBig));
            map.Add(new Symbol("string8"), strValue);
            map.Add(new Symbol("string32"), strBig);
            map.Add(new Symbol("described1"), described1);

            AmqpCodec.EncodeObject(buffer, map);

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

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

            Map decMap = (Map)AmqpCodec.DecodeBoxedObject(buffer);

            Assert.IsTrue(decMap[new Symbol("boolTrue")].Equals(true), "Boolean true expected.");
            Assert.IsTrue(decMap[new Symbol("boolFalse")].Equals(false), "Boolean false expected.");
            Assert.IsTrue(decMap[new Symbol("ubyte")].Equals(ubyteValue), "UByte value not equal.");
            Assert.IsTrue(decMap[new Symbol("ushort")].Equals(ushortValue), "UShort value not equal.");
            Assert.IsTrue(decMap[new Symbol("uint")].Equals(uintValue), "UInt value not equal.");
            Assert.IsTrue(decMap[new Symbol("ulong")].Equals(ulongValue), "ULong value not equal.");
            Assert.IsTrue(decMap[new Symbol("byte")].Equals(byteValue), "Byte value not equal.");
            Assert.IsTrue(decMap[new Symbol("short")].Equals(shortValue), "Short value not equal.");
            Assert.IsTrue(decMap[new Symbol("int")].Equals(intValue), "Int value not equal.");
            Assert.IsTrue(decMap[new Symbol("long")].Equals(longValue), "Long value not equal.");
            Assert.IsTrue(decMap[new Symbol("null")] == null, "Null object expected.");
            Assert.IsTrue(decMap[new Symbol("float")].Equals(floatValue), "Float value not equal.");
            Assert.IsTrue(decMap[new Symbol("double")].Equals(doubleValue), "Double value not equal.");
            Assert.IsTrue(decMap[new Symbol("char")].Equals(charValue), "Char value not equal.");
            Assert.IsTrue(decMap[new Symbol("datetime")].Equals(dtValue), "TimeStamp value not equal.");
            Assert.IsTrue(decMap[new Symbol("uuid")].Equals(uuidValue), "Uuid value not equal.");
            Assert.IsTrue(decMap[new Symbol("binaryNull")] == null, "Null binary expected.");
            byte[] bin8 = (byte[])decMap[new Symbol("binary8")];
            EnsureEqual(bin8, 0, bin8.Length, bin8ValueEncoded, 0, bin8ValueEncoded.Length);
            byte[] bin32 = (byte[])decMap[new Symbol("binary32")];
            EnsureEqual(bin32, 0, bin32.Length, bin32ValueEncoded, 0, bin32ValueEncoded.Length);

            Assert.Null(decMap[new Symbol("symbolNull")], "Null symbol expected.");
            Symbol symDecode = (Symbol)decMap[new Symbol("symbol8")];

            Assert.AreEqual(symDecode, (Symbol)strValue, "AmqpSymbol value not equal.");
            symDecode = (Symbol)decMap[new Symbol("symbol32")];
            Assert.AreEqual(symDecode, (Symbol)strBig, "AmqpSymbol value (big) not equal.");

            string strDecode = (string)decMap[new Symbol("string8")];

            Assert.AreEqual(strDecode, strValue, "string value not equal.");
            strDecode = (string)decMap[new Symbol("string32")];
            Assert.AreEqual(strDecode, strBig, "string value (big) not equal.");

            DescribedType described = (DescribedType)decMap[new Symbol("described1")];

            Assert.AreEqual(described1.Descriptor, described.Descriptor, "Described value 1 descriptor is different");
            Assert.AreEqual(described1, described, "Described value 1 value is different");
        }
Exemplo n.º 14
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);
        }