Example #1
0
        public void ReadSingleRequestInParts()
        {
            // Split the message bytes into 3 parts
            Assert.IsTrue(requestBytes.Length > 15);
            var part1 = new byte[4];
            var part2 = new byte[6];
            var part3 = new byte[requestBytes.Length - 10];

            Array.Copy(requestBytes, 0, part1, 0, part1.Length);
            Array.Copy(requestBytes, part1.Length, part2, 0, part2.Length);
            Array.Copy(requestBytes, part1.Length + part2.Length, part3, 0, part3.Length);

            // Write part 1
            var stream = new MemoryStream();

            stream.Write(part1, 0, part1.Length);
            stream.Seek(0, SeekOrigin.Begin);

            // Read part 1
            var byteStream = new TestStream(stream);
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            Assert.IsFalse(rpcStream.DataAvailable);
            Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(part1.Length, rpcStream.BytesRead);

            // Write part 2
            Assert.AreEqual(part1.Length, stream.Position);
            stream.Write(part2, 0, part2.Length);
            stream.Seek(part1.Length, SeekOrigin.Begin);

            // Read part 2
            Assert.IsFalse(rpcStream.DataAvailable);
            Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(part1.Length + part2.Length, rpcStream.BytesRead);

            // Write part 3
            Assert.AreEqual(part1.Length + part2.Length, stream.Position);
            stream.Write(part3, 0, part3.Length);
            stream.Seek(-part3.Length, SeekOrigin.Current);

            // Read part 3
            Assert.IsTrue(rpcStream.DataAvailable);
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(part1.Length + part2.Length + part3.Length, rpcStream.BytesRead);
            Request request = rpcStream.Read();

            Assert.IsFalse(rpcStream.DataAvailable);
            Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(part1.Length + part2.Length + part3.Length, rpcStream.BytesRead);
            Assert.AreEqual(expectedRequest.Request.Calls.Count, request.Calls.Count);
            Assert.AreEqual(expectedRequest.Request.Calls [0].Service, request.Calls [0].Service);
            Assert.AreEqual(expectedRequest.Request.Calls [0].Procedure, request.Calls [0].Procedure);
            Assert.IsFalse(byteStream.Closed);
        }
Example #2
0
        public void Empty()
        {
            var byteStream = new TestStream(new MemoryStream());
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            Assert.IsFalse(rpcStream.DataAvailable);
            Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(0, rpcStream.BytesRead);
            Assert.IsFalse(byteStream.Closed);
        }
Example #3
0
        public void WriteSingleResponse()
        {
            var stream     = new MemoryStream();
            var byteStream = new TestStream(null, stream);
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            rpcStream.Write(expectedResponseMessage);
            Assert.AreEqual(responseBytes.ToHexString(), stream.ToArray().ToHexString());
            Assert.AreEqual(responseBytes.Length, rpcStream.BytesWritten);
            Assert.AreEqual(0, rpcStream.BytesRead);
            Assert.IsFalse(byteStream.Closed);
        }
Example #4
0
        public void ReadGarbage()
        {
            var data = new byte [4000];
            var rand = new Random(42);

            rand.NextBytes(data);
            var byteStream = new TestStream(data);
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            Assert.Throws <KRPC.Server.Message.MalformedRequestException> (() => rpcStream.Read());
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(data.Length, rpcStream.BytesRead);
            Assert.IsTrue(byteStream.Closed);
        }
Example #5
0
        public void ReadSingleRequest()
        {
            var byteStream = new TestStream(requestBytes);
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(0, rpcStream.BytesRead);
            Assert.IsTrue(rpcStream.DataAvailable);
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(requestBytes.Length, rpcStream.BytesRead);
            Request request = rpcStream.Read();

            Assert.IsFalse(rpcStream.DataAvailable);
            Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
            Assert.AreEqual(expectedRequest.Request.Calls.Count, request.Calls.Count);
            Assert.AreEqual(expectedRequest.Request.Calls [0].Service, request.Calls [0].Service);
            Assert.AreEqual(expectedRequest.Request.Calls [0].Procedure, request.Calls [0].Procedure);
            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(requestBytes.Length, rpcStream.BytesRead);
            Assert.IsFalse(byteStream.Closed);
        }
Example #6
0
        public void ReadMultipleRequests()
        {
            var repeats = 5;
            var multipleRequestBytes = new byte [requestBytes.Length * repeats];

            for (int i = 0; i < repeats; i++)
            {
                Array.Copy(requestBytes, 0, multipleRequestBytes, i * requestBytes.Length, requestBytes.Length);
            }
            var byteStream = new TestStream(multipleRequestBytes);
            var rpcStream  = new KRPC.Server.SerialIO.RPCStream(byteStream, null);

            Assert.AreEqual(0, rpcStream.BytesWritten);
            Assert.AreEqual(0, rpcStream.BytesRead);
            for (int i = 0; i < repeats; i++)
            {
                Assert.IsTrue(rpcStream.DataAvailable);
                Assert.AreEqual(0, rpcStream.BytesWritten);
                Assert.AreEqual(multipleRequestBytes.Length, rpcStream.BytesRead);
                Request request = rpcStream.Read();
                if (i < repeats - 1)
                {
                    Assert.IsTrue(rpcStream.DataAvailable);
                }
                else
                {
                    Assert.IsFalse(rpcStream.DataAvailable);
                    Assert.Throws <KRPC.Server.Message.NoRequestException> (() => rpcStream.Read());
                }
                Assert.AreEqual(expectedRequest.Request.Calls.Count, request.Calls.Count);
                Assert.AreEqual(expectedRequest.Request.Calls [0].Service, request.Calls [0].Service);
                Assert.AreEqual(expectedRequest.Request.Calls [0].Procedure, request.Calls [0].Procedure);
                Assert.AreEqual(0, rpcStream.BytesWritten);
                Assert.AreEqual(multipleRequestBytes.Length, rpcStream.BytesRead);
            }
            Assert.IsFalse(byteStream.Closed);
        }