public void PacketStreamReadWhenWriteOnlyTest()
 {
     using (var packetStream = new NetPacketStream())
     {
         Assert.Throws <InvalidOperationException>(() => packetStream.Read <byte>());
     }
 }
 public void PacketStreamReadNonPrimitiveTest()
 {
     using (var packetStream = new NetPacketStream(this._randomizer.Bytes(this._randomizer.Byte())))
     {
         Assert.Throws <NotImplementedException>(() => packetStream.Read <object>());
     }
 }
 public void PacketStreamReadArrayWhenWriteOnlyTest()
 {
     using (var packetStream = new NetPacketStream())
     {
         Assert.Throws <InvalidOperationException>(() => packetStream.Read <byte>(this._randomizer.Byte(min: 1)));
     }
 }
 public void PacketStreamReadArrayWithInvalidAmountTest(int amount)
 {
     using (var packetStream = new NetPacketStream(this._randomizer.Bytes(this._randomizer.Byte())))
     {
         Assert.Throws <ArgumentException>(() => packetStream.Read <byte>(amount));
     }
 }
        private void TestRead <T>(byte[] input, T expected)
        {
            T value = default(T);

            using (INetPacketStream packetStream = new NetPacketStream(input))
                value = packetStream.Read <T>();

            Assert.Equal(expected, value);
        }
        public void PacketStreamReadByteArrayTest()
        {
            var buffer = this._randomizer.Bytes(this._randomizer.Byte());
            int amount = this._randomizer.Int(min: 1, max: buffer.Length);

            byte[] expectedBuffer = buffer.Take(amount).ToArray();

            using (var packetStream = new NetPacketStream(buffer))
            {
                byte[] readBuffer = packetStream.Read <byte>(amount);

                Assert.Equal(amount, readBuffer.Length);
                Assert.Equal(expectedBuffer, readBuffer);
            }
        }
        private void PacketStreamReadTest <T>(T expectedValue, byte[] valueAsBytes, bool adjustBuffer = true)
        {
            byte[] adjustedBuffer = adjustBuffer ? valueAsBytes.Take(Marshal.SizeOf <T>()).ToArray() : valueAsBytes;

            using (INetPacketStream packetStream = new NetPacketStream(adjustedBuffer))
            {
                Assert.Equal(NetPacketStateType.Read, packetStream.State);
                Assert.False(packetStream.IsEndOfStream);

                var readValue = packetStream.Read <T>();

                Assert.Equal(expectedValue, readValue);
                Assert.True(packetStream.IsEndOfStream);
            }
        }
        public void WriteByteArray()
        {
            byte[] readByteArray      = null;
            byte[] packetStreamBuffer = null;

            using (INetPacketStream packetStream = new NetPacketStream())
            {
                packetStream.Write(ByteArrayValue);
                packetStreamBuffer = packetStream.Buffer;
            }

            using (INetPacketStream readPacketStream = new NetPacketStream(packetStreamBuffer))
            {
                readByteArray = readPacketStream.Read <byte[]>();
            }

            Assert.Equal(ByteArrayValue, readByteArray);
        }
        public void WriteString()
        {
            string readString = null;

            byte[] packetStreamBuffer = null;

            using (INetPacketStream packetStream = new NetPacketStream())
            {
                packetStream.Write(StringValue);
                packetStreamBuffer = packetStream.Buffer;
            }

            using (INetPacketStream readPacketStream = new NetPacketStream(packetStreamBuffer))
            {
                readString = readPacketStream.Read <string>();
            }

            Assert.Equal(StringValue, readString);
        }
        public void ReadByteArray()
        {
            byte[] value = null;

            byte[] testValue = null;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
                {
                    binaryWriter.Write(ByteArrayValue.Length);
                    binaryWriter.Write(ByteArrayValue);
                }

                testValue = memoryStream.ToArray();
            }

            using (INetPacketStream packetStream = new NetPacketStream(testValue))
                value = packetStream.Read <byte[]>();

            Assert.Equal(ByteArrayValue, value);
        }