Exemplo n.º 1
0
        public void TestPacketReadingWithTooLittleData()
        {
            MemoryStream memStream = new MemoryStream();
            BinaryWriter bWriter   = new BinaryWriter(memStream);

            byte[] firstTwentyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05,
                                                   0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 };
            byte[] lastThreeBytes = new byte[] { 0x09, 0x13, 0x41 };

            // we'll write 23 random bytes.
            bWriter.Write(firstTwentyBytes);
            bWriter.Write(lastThreeBytes);

            byte[] memStreamArr = memStream.ToArray();

            ArraySegment <byte> buf = new ArraySegment <byte>(memStreamArr, 0, memStreamArr.Length);

            TestPacketIn packet = new TestPacketIn(memStreamArr);

            Assert.IsTrue(ByteArrayEquals(firstTwentyBytes, packet.ReadBytes(20)));
            Assert.AreEqual(float.NaN, packet.ReadFloat());
            Assert.AreEqual(0, packet.ReadInt32());
            Assert.AreEqual <uint>(0, packet.ReadUInt32());
            Assert.AreEqual(double.NaN, packet.ReadDouble());
            Assert.AreEqual(0, packet.ReadInt64());
            Assert.AreEqual <ulong>(0, packet.ReadUInt64());

            Assert.AreEqual(22, packet.Seek(2, SeekOrigin.Current));
            Assert.AreEqual(0, packet.ReadInt16());
            Assert.AreEqual(0, packet.ReadUInt16());

            Assert.AreEqual(23, packet.Seek(0, SeekOrigin.End));
            Assert.AreEqual(0, packet.ReadByte());
            Assert.AreEqual(0, packet.ReadSByte());
            Assert.IsFalse(packet.ReadBoolean());
            Assert.AreEqual('\0', packet.ReadChar());
            Assert.AreEqual(0, packet.ReadBigInteger(1).LongValue());
            Assert.AreEqual(0, packet.ReadChars(1).Length);

            MemoryStream memStreamTwo = new MemoryStream();
            BinaryWriter bWriterTwo   = new BinaryWriter(memStreamTwo);

            byte[] truncatedStrBytes = UTF8Encoding.UTF8.GetBytes("thisisatruncatedstr");

            // we'll write 23 random bytes.
            bWriterTwo.Write((byte)truncatedStrBytes.Length);
            bWriterTwo.Write(truncatedStrBytes);

            byte[] truncatedBytes = new byte[] { 0x03, 0x04, 0x05, 0x09 };
            bWriterTwo.Write(truncatedBytes);

            byte[] arr2 = memStreamTwo.ToArray();

            TestPacketIn packetTwo = new TestPacketIn(arr2);

            Assert.AreEqual(packetTwo.ReadPascalString(), "thisisatruncatedstr");
            Assert.IsTrue(ByteArrayEquals(packetTwo.ReadBytes(10), truncatedBytes));
        }
Exemplo n.º 2
0
        public void TestPacketReading()
        {
            MemoryStream memStream = new MemoryStream();
            BinaryWriter bWriter   = new BinaryWriter(memStream);

            bWriter.Write((byte)122);
            bWriter.Write((sbyte)-122);
            bWriter.Write((short)-14323);
            bWriter.Write((ushort)14323);
            bWriter.Write((int)-4124444);
            bWriter.Write((uint)4124444);
            bWriter.Write((ulong)412124553);
            bWriter.Write((long)-412124553);
            bWriter.Write((float)450.2500f);
            bWriter.Write((double)90350.45350d);
            bWriter.Write("test");

            bWriter.Write(UTF8Encoding.UTF8.GetBytes(new char[] { 't', 'e', 's', 't' }));
            bWriter.Write((byte)0);

            byte[] testBytes = new byte[] { 0x01, 0x02, 0x03, 0x02, 0x01 };
            bWriter.Write(testBytes);

            BigInteger bi = new BigInteger(1000000);

            byte[] biBytes = bi.GetBytes();
            bWriter.Write(biBytes);

            bWriter.Write((byte)biBytes.Length);
            bWriter.Write(biBytes);

            bWriter.Write(false);

            byte[] addrBytes = IPAddress.Parse("192.168.1.100").GetAddressBytes();
            bWriter.Write(addrBytes);

            bWriter.Write(UTF8Encoding.UTF8.GetBytes("tset"));
            bWriter.Write((byte)0);

            bWriter.Write(UTF8Encoding.UTF8.GetBytes(new char[] { 'z' }));

            bWriter.Write(UTF8Encoding.UTF8.GetBytes("rabooftset"));

            byte[] bytes = memStream.ToArray();

            TestPacketIn packet = new TestPacketIn(bytes);

            Assert.AreEqual((byte)122, packet.ReadByte());
            Assert.AreEqual((sbyte)-122, packet.ReadSByte());
            Assert.AreEqual((short)-14323, packet.ReadInt16());
            Assert.AreEqual((ushort)14323, packet.ReadUInt16());
            Assert.AreEqual((int)-4124444, packet.ReadInt32());
            Assert.AreEqual((uint)4124444, packet.ReadUInt32());
            Assert.AreEqual((ulong)412124553, packet.ReadUInt64());
            Assert.AreEqual((long)-412124553, packet.ReadInt64());
            Assert.AreEqual((float)450.2500f, packet.ReadFloat());
            Assert.AreEqual((double)90350.45350d, packet.ReadDouble());
            Assert.AreEqual("test", packet.ReadPascalString());
            Assert.AreEqual("test", packet.ReadCString());
            Assert.IsTrue(ByteArrayEquals(testBytes, packet.ReadBytes(5)));
            Assert.AreEqual(bi, packet.ReadBigInteger(biBytes.Length));
            Assert.AreEqual(bi, packet.ReadBigIntegerLengthValue());
            Assert.AreEqual(false, packet.ReadBoolean());
            Assert.AreEqual("192.168.1.100", packet.ReadIPAddress().Address);
            Assert.AreEqual("test", packet.ReadReversedString());
            Assert.AreEqual('z', packet.ReadChar());
            Assert.AreEqual("testfoobar", packet.ReadReversedPascalString(10));

            Assert.AreEqual(memStream.Length, packet.Length);
            Assert.AreEqual(memStream.Position, packet.Position);
        }
Exemplo n.º 3
0
        public void TestPacketReading()
        {
            MemoryStream memStream = new MemoryStream();
            BinaryWriter bWriter = new BinaryWriter(memStream);

            bWriter.Write((byte)122);
            bWriter.Write((sbyte)-122);
            bWriter.Write((short)-14323);
            bWriter.Write((ushort)14323);
            bWriter.Write((int)-4124444);
            bWriter.Write((uint)4124444);
            bWriter.Write((ulong)412124553);
            bWriter.Write((long)-412124553);
            bWriter.Write((float)450.2500f);
            bWriter.Write((double)90350.45350d);
            bWriter.Write("test");

            bWriter.Write(UTF8Encoding.UTF8.GetBytes(new char[] { 't', 'e', 's', 't' }));
            bWriter.Write((byte)0);

            byte[] testBytes = new byte[] { 0x01, 0x02, 0x03, 0x02, 0x01 };
            bWriter.Write(testBytes);

            BigInteger bi = new BigInteger(1000000);
            byte[] biBytes = bi.GetBytes();
            bWriter.Write(biBytes);

            bWriter.Write((byte)biBytes.Length);
            bWriter.Write(biBytes);

            bWriter.Write(false);

            byte[] addrBytes = IPAddress.Parse("192.168.1.100").GetAddressBytes();
            bWriter.Write(addrBytes);

            bWriter.Write(UTF8Encoding.UTF8.GetBytes("tset"));
            bWriter.Write((byte)0);

            bWriter.Write(UTF8Encoding.UTF8.GetBytes(new char[] { 'z' }));

            bWriter.Write(UTF8Encoding.UTF8.GetBytes("rabooftset"));

			byte[] bytes = memStream.ToArray();

			TestPacketIn packet = new TestPacketIn(bytes);

            Assert.AreEqual((byte)122, packet.ReadByte());
            Assert.AreEqual((sbyte)-122, packet.ReadSByte());
            Assert.AreEqual((short)-14323, packet.ReadInt16());
            Assert.AreEqual((ushort)14323, packet.ReadUInt16());
            Assert.AreEqual((int)-4124444, packet.ReadInt32());
            Assert.AreEqual((uint)4124444, packet.ReadUInt32());
            Assert.AreEqual((ulong)412124553, packet.ReadUInt64());
            Assert.AreEqual((long)-412124553, packet.ReadInt64());
            Assert.AreEqual((float)450.2500f, packet.ReadFloat());
            Assert.AreEqual((double)90350.45350d, packet.ReadDouble());
            Assert.AreEqual("test", packet.ReadPascalString());
            Assert.AreEqual("test", packet.ReadCString());
            Assert.IsTrue(ByteArrayEquals(testBytes, packet.ReadBytes(5)));
            Assert.AreEqual(bi, packet.ReadBigInteger(biBytes.Length));
            Assert.AreEqual(bi, packet.ReadBigIntegerLengthValue());
            Assert.AreEqual(false, packet.ReadBoolean());
            Assert.AreEqual("192.168.1.100", packet.ReadIPAddress().Address);
            Assert.AreEqual("test", packet.ReadReversedString());
            Assert.AreEqual('z', packet.ReadChar());
            Assert.AreEqual("testfoobar", packet.ReadReversedPascalString(10));

            Assert.AreEqual(memStream.Length, packet.Length);
            Assert.AreEqual(memStream.Position, packet.Position);
        }
Exemplo n.º 4
0
        public void TestPacketReadingWithTooLittleData()
        {
            MemoryStream memStream = new MemoryStream();
            BinaryWriter bWriter = new BinaryWriter(memStream);

            byte[] firstTwentyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 
                                                   0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 };
            byte[] lastThreeBytes = new byte[] { 0x09, 0x13, 0x41 };

            // we'll write 23 random bytes.
            bWriter.Write(firstTwentyBytes);
            bWriter.Write(lastThreeBytes);

			byte[] memStreamArr = memStream.ToArray();

			ArraySegment<byte> buf = new ArraySegment<byte>(memStreamArr, 0, memStreamArr.Length);

			TestPacketIn packet = new TestPacketIn(memStreamArr);

            Assert.IsTrue(ByteArrayEquals(firstTwentyBytes, packet.ReadBytes(20)));
            Assert.AreEqual(float.NaN, packet.ReadFloat());
            Assert.AreEqual(0, packet.ReadInt32());
            Assert.AreEqual<uint>(0, packet.ReadUInt32());
            Assert.AreEqual(double.NaN, packet.ReadDouble());
            Assert.AreEqual(0, packet.ReadInt64());
            Assert.AreEqual<ulong>(0, packet.ReadUInt64());

            Assert.AreEqual(22, packet.Seek(2, SeekOrigin.Current));
            Assert.AreEqual(0, packet.ReadInt16());
            Assert.AreEqual(0, packet.ReadUInt16());

            Assert.AreEqual(23, packet.Seek(0, SeekOrigin.End));
            Assert.AreEqual(0, packet.ReadByte());
            Assert.AreEqual(0, packet.ReadSByte());
            Assert.IsFalse(packet.ReadBoolean());
            Assert.AreEqual('\0', packet.ReadChar());
            Assert.AreEqual(0, packet.ReadBigInteger(1).LongValue());
            Assert.AreEqual(0, packet.ReadChars(1).Length);

            MemoryStream memStreamTwo = new MemoryStream();
            BinaryWriter bWriterTwo = new BinaryWriter(memStreamTwo);

            byte[] truncatedStrBytes = UTF8Encoding.UTF8.GetBytes("thisisatruncatedstr");

            // we'll write 23 random bytes.
            bWriterTwo.Write((byte)truncatedStrBytes.Length);
            bWriterTwo.Write(truncatedStrBytes);

            byte[] truncatedBytes = new byte[] { 0x03, 0x04, 0x05, 0x09 };
            bWriterTwo.Write(truncatedBytes);

			byte[] arr2 = memStreamTwo.ToArray();

			TestPacketIn packetTwo = new TestPacketIn(arr2);

            Assert.AreEqual(packetTwo.ReadPascalString(), "thisisatruncatedstr");
            Assert.IsTrue(ByteArrayEquals(packetTwo.ReadBytes(10), truncatedBytes));
        }