Exemplo n.º 1
0
        public void Test32BitIntZero()
        {
            Int32 intIn = (Int32)0x00000000;

            DatagramWriter writer = new DatagramWriter();
            writer.Write(intIn, 32);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32 intOut = reader.Read(32);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 2
0
        public void Test16BitInt()
        {
            Int32 intIn = (Int32)0x00004321;

            DatagramWriter writer = new DatagramWriter();
            writer.Write(intIn, 16);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32 intOut = reader.Read(16);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 3
0
        public void Test8BitInt()
        {
            Int32 intIn = 0x00000021;

            DatagramWriter writer = new DatagramWriter();
            writer.Write(intIn, 8);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32 intOut = reader.Read(8);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 4
0
        public void Test4BitInt()
        {
            Int32 intIn = 0x0000005;

            DatagramWriter writer = new DatagramWriter();

            writer.Write(intIn, 4);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32          intOut = reader.Read(4);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 5
0
        public void Test16BitInt()
        {
            Int32 intIn = (Int32)0x00004321;

            DatagramWriter writer = new DatagramWriter();

            writer.Write(intIn, 16);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32          intOut = reader.Read(16);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 6
0
        public void Test32BitIntZero()
        {
            Int32 intIn = (Int32)0x00000000;

            DatagramWriter writer = new DatagramWriter();

            writer.Write(intIn, 32);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32          intOut = reader.Read(32);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 7
0
        public void TestAlignedBytes()
        {
            Byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes("Some aligned bytes");

            DatagramWriter writer = new DatagramWriter();

            writer.WriteBytes(bytesIn);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());

            Byte[] bytesOut = reader.ReadBytesLeft();

            Assert.IsTrue(bytesIn.SequenceEqual(bytesOut));
        }
Exemplo n.º 8
0
        public void Test32BitInt()
        {
            unchecked
            {
                Int32 intIn = (Int32)0x87654321;

                DatagramWriter writer = new DatagramWriter();
                writer.Write(intIn, 32);

                DatagramReader reader = new DatagramReader(writer.ToByteArray());
                Int32 intOut = reader.Read(32);

                Assert.AreEqual(intIn, intOut);
            }
        }
Exemplo n.º 9
0
        public void Test32BitInt()
        {
            unchecked
            {
                Int32 intIn = (Int32)0x87654321;

                DatagramWriter writer = new DatagramWriter();
                writer.Write(intIn, 32);

                DatagramReader reader = new DatagramReader(writer.ToByteArray());
                Int32          intOut = reader.Read(32);

                Assert.AreEqual(intIn, intOut);
            }
        }
Exemplo n.º 10
0
        public void TestByteOrder()
        {
            Int32 intIn = 1234567890;

            DatagramWriter writer = new DatagramWriter();

            writer.Write(intIn, 32);

            Byte[] data     = writer.ToByteArray();
            Int32  intTrans = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data, 0));

            Assert.AreEqual(intIn, intTrans);

            DatagramReader reader = new DatagramReader(data);
            Int32          intOut = reader.Read(32);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 11
0
        public void TestBytesLeftUnaligned()
        {
            Int32 bitCount = 7;
            Int32 bitsIn   = 0x55;

            Byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes("Some aligned bytes");

            DatagramWriter writer = new DatagramWriter();

            writer.Write(bitsIn, bitCount);
            writer.WriteBytes(bytesIn);

            DatagramReader reader  = new DatagramReader(writer.ToByteArray());
            Int32          bitsOut = reader.Read(bitCount);

            Byte[] bytesOut = reader.ReadBytesLeft();

            Assert.AreEqual(bitsIn, bitsOut);
            Assert.IsTrue(bytesIn.SequenceEqual(bytesOut));
        }
Exemplo n.º 12
0
        public void TestGETRequestHeader()
        {
            Int32 versionIn   = 1;
            Int32 versionSz   = 2;
            Int32 typeIn      = 0; // Confirmable
            Int32 typeSz      = 2;
            Int32 optionCntIn = 1;
            Int32 optionCntSz = 4;
            Int32 codeIn      = 1; // GET Request
            Int32 codeSz      = 8;
            Int32 msgIdIn     = 0x1234;
            Int32 msgIdSz     = 16;

            DatagramWriter writer = new DatagramWriter();

            writer.Write(versionIn, versionSz);
            writer.Write(typeIn, typeSz);
            writer.Write(optionCntIn, optionCntSz);
            writer.Write(codeIn, codeSz);
            writer.Write(msgIdIn, msgIdSz);

            Byte[] data    = writer.ToByteArray();
            Byte[] dataRef = { 0x41, 0x01, 0x12, 0x34 };

            Assert.IsTrue(dataRef.SequenceEqual(data));

            DatagramReader reader       = new DatagramReader(data);
            Int32          versionOut   = reader.Read(versionSz);
            Int32          typeOut      = reader.Read(typeSz);
            Int32          optionCntOut = reader.Read(optionCntSz);
            Int32          codeOut      = reader.Read(codeSz);
            Int32          msgIdOut     = reader.Read(msgIdSz);

            Assert.AreEqual(versionIn, versionOut);
            Assert.AreEqual(typeIn, typeOut);
            Assert.AreEqual(optionCntIn, optionCntOut);
            Assert.AreEqual(codeIn, codeOut);
            Assert.AreEqual(msgIdIn, msgIdOut);
        }
Exemplo n.º 13
0
        public void TestGETRequestHeader()
        {
            Int32 versionIn = 1;
            Int32 versionSz = 2;
            Int32 typeIn = 0; // Confirmable
            Int32 typeSz = 2;
            Int32 optionCntIn = 1;
            Int32 optionCntSz = 4;
            Int32 codeIn = 1; // GET Request
            Int32 codeSz = 8;
            Int32 msgIdIn = 0x1234;
            Int32 msgIdSz = 16;

            DatagramWriter writer = new DatagramWriter();
            writer.Write(versionIn, versionSz);
            writer.Write(typeIn, typeSz);
            writer.Write(optionCntIn, optionCntSz);
            writer.Write(codeIn, codeSz);
            writer.Write(msgIdIn, msgIdSz);

            Byte[] data = writer.ToByteArray();
            Byte[] dataRef = { 0x41, 0x01, 0x12, 0x34 };

            Assert.IsTrue(dataRef.SequenceEqual(data));

            DatagramReader reader = new DatagramReader(data);
            Int32 versionOut = reader.Read(versionSz);
            Int32 typeOut = reader.Read(typeSz);
            Int32 optionCntOut = reader.Read(optionCntSz);
            Int32 codeOut = reader.Read(codeSz);
            Int32 msgIdOut = reader.Read(msgIdSz);

            Assert.AreEqual(versionIn, versionOut);
            Assert.AreEqual(typeIn, typeOut);
            Assert.AreEqual(optionCntIn, optionCntOut);
            Assert.AreEqual(codeIn, codeOut);
            Assert.AreEqual(msgIdIn, msgIdOut);
        }
Exemplo n.º 14
0
        public void TestBytesLeftUnaligned()
        {
            Int32 bitCount = 7;
            Int32 bitsIn = 0x55;
            Byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes("Some aligned bytes");

            DatagramWriter writer = new DatagramWriter();
            writer.Write(bitsIn, bitCount);
            writer.WriteBytes(bytesIn);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Int32 bitsOut = reader.Read(bitCount);
            Byte[] bytesOut = reader.ReadBytesLeft();

            Assert.AreEqual(bitsIn, bitsOut);
            Assert.IsTrue(bytesIn.SequenceEqual(bytesOut));
        }
Exemplo n.º 15
0
        public void TestAlignedBytes()
        {
            Byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes("Some aligned bytes");

            DatagramWriter writer = new DatagramWriter();
            writer.WriteBytes(bytesIn);

            DatagramReader reader = new DatagramReader(writer.ToByteArray());
            Byte[] bytesOut = reader.ReadBytesLeft();

            Assert.IsTrue(bytesIn.SequenceEqual(bytesOut));
        }
Exemplo n.º 16
0
 /// <summary>
 /// Calculates the value used in the extended option fields as specified
 /// in draft-ietf-core-coap-13, section 3.1.
 /// </summary>
 /// <param name="nibble">the 4-bit option header value</param>
 /// <param name="datagram">the datagram</param>
 /// <returns>the value calculated from the nibble and the extended option value</returns>
 private static Int32 GetValueFromOptionNibble(Int32 nibble, DatagramReader datagram)
 {
     if (nibble < 13)
     {
         return nibble;
     }
     else if (nibble == 13)
     {
         return datagram.Read(8) + 13;
     }
     else if (nibble == 14)
     {
         return datagram.Read(16) + 269;
     }
     else
     {
         // TODO error
         if (log.IsWarnEnabled)
             log.Warn("15 is reserved for payload marker, message format error");
         return 0;
     }
 }
Exemplo n.º 17
0
 /// <summary>
 /// Instantiates.
 /// </summary>
 /// <param name="data">the bytes array to decode</param>
 public MessageDecoder(Byte[] data)
 {
     m_reader = new DatagramReader(data);
 }
Exemplo n.º 18
0
        public void TestByteOrder()
        {
            Int32 intIn = 1234567890;

            DatagramWriter writer = new DatagramWriter();
            writer.Write(intIn, 32);

            Byte[] data = writer.ToByteArray();
            Int32 intTrans = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data, 0));

            Assert.AreEqual(intIn, intTrans);

            DatagramReader reader = new DatagramReader(data);
            Int32 intOut = reader.Read(32);

            Assert.AreEqual(intIn, intOut);
        }
Exemplo n.º 19
0
 /// <summary>
 /// Calculates the value used in the extended option fields as specified
 /// in draft-ietf-core-coap-14, section 3.1.
 /// </summary>
 /// <param name="nibble">the 4-bit option header value</param>
 /// <param name="datagram">the datagram</param>
 /// <returns>the value calculated from the nibble and the extended option value</returns>
 private static Int32 GetValueFromOptionNibble(Int32 nibble, DatagramReader datagram)
 {
     if (nibble < 13)
         return nibble;
     else if (nibble == 13)
         return datagram.Read(8) + 13;
     else if (nibble == 14)
         return datagram.Read(16) + 269;
     else
         throw ThrowHelper.Argument("nibble", "Unsupported option delta " + nibble);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Instantiates.
 /// </summary>
 /// <param name="data">the bytes array to decode</param>
 public MessageDecoder(Byte[] data)
 {
     _reader = new DatagramReader(data);
 }