Esempio n. 1
0
        public void testRecvNoAnnotations()
        {
            var msg = new Message(Message.MSG_CONNECT, new byte[] { 1, 2, 3, 4, 5 }, 42, 0, 0, null);
            var c   = new ConnectionMock();

            c.send(msg.to_bytes());
            msg = Message.recv(c, null);
            Assert.AreEqual(0, c.RemainingLength);
            Assert.AreEqual(5, msg.data_size);
            Assert.AreEqual(new byte[] { 1, 2, 3, 4, 5 }, msg.data);
            Assert.AreEqual(0, msg.annotations_size);
            Assert.AreEqual(0, msg.annotations.Count);
        }
Esempio n. 2
0
        public void testHmac()
        {
            byte[] hk = Config.HMAC_KEY;
            Stream c;

            byte[] data;

            try {
                Config.HMAC_KEY = Encoding.ASCII.GetBytes("test key");
                data            = new Message(Message.MSG_RESULT, new byte[] { 1, 2, 3, 4, 5 }, 42, 0, 1, null).to_bytes();
                c = new ConnectionMock(data);
            }
            finally {
                Config.HMAC_KEY = hk;
            }
            // test checking of different hmacs
            try {
                Message.recv(c, null);
                Assert.Fail("crash expected");
            }
            catch (PyroException x) {
                Assert.IsTrue(x.Message.Contains("hmac"));
            }
            c = new ConnectionMock(data);
            // test that it works again when resetting the key
            try {
                hk = Config.HMAC_KEY;
                Config.HMAC_KEY = Encoding.ASCII.GetBytes("test key");
                Message.recv(c, null);
            }
            finally {
                Config.HMAC_KEY = hk;
            }
            c = new ConnectionMock(data);
            // test that it doesn't work when no key is set
            try {
                hk = Config.HMAC_KEY;
                Config.HMAC_KEY = null;
                Message.recv(c, null);
                Assert.Fail("crash expected");
            }
            catch (PyroException x) {
                Assert.IsTrue(x.Message.Contains("hmac key config"));
            }
            finally {
                Config.HMAC_KEY = hk;
            }
        }
Esempio n. 3
0
        public void testRecvAnnotations()
        {
            var annotations = new Dictionary <string, byte[]>();

            annotations["TEST"] = new byte[] { 10, 20, 30, 40, 50 };
            var msg = new Message(Message.MSG_CONNECT, new byte[] { 1, 2, 3, 4, 5 }, this.serializer_id, 0, 0, annotations);
            var c   = new ConnectionMock();

            c.send(msg.to_bytes());
            msg = Message.recv(c, null);
            Assert.AreEqual(0, c.RemainingLength);
            Assert.AreEqual(5, msg.data_size);
            Assert.AreEqual(new byte[] { 1, 2, 3, 4, 5 }, msg.data);
            Assert.AreEqual(new byte[] { 10, 20, 30, 40, 50 }, msg.annotations["TEST"]);
            Assert.IsTrue(msg.annotations.ContainsKey("HMAC"));
        }
Esempio n. 4
0
        public void testChecksum()
        {
            var msg = new Message(Message.MSG_RESULT, new byte[] { 1, 2, 3, 4 }, 42, 0, 1, null);
            var c   = new ConnectionMock();

            c.send(msg.to_bytes());
            // corrupt the checksum bytes
            byte[] data = c.ReceivedData;
            data[Message.HEADER_SIZE - 2] = 0;
            data[Message.HEADER_SIZE - 1] = 0;
            c = new ConnectionMock(data);
            try {
                Message.recv(c, null);
                Assert.Fail("crash expected");
            }
            catch (PyroException x) {
                Assert.IsTrue(x.Message.Contains("checksum"));
            }
        }
Esempio n. 5
0
 public void testRecvNoAnnotations()
 {
     var msg = new Message(Message.MSG_CONNECT, new byte[]{1,2,3,4,5}, 42, 0, 0, null);
     var c = new ConnectionMock();
     c.send(msg.to_bytes());
     msg = Message.recv(c, null);
     Assert.AreEqual(0, c.RemainingLength);
     Assert.AreEqual(5, msg.data_size);
     Assert.AreEqual(new byte[]{1,2,3,4,5}, msg.data);
     Assert.AreEqual(0, msg.annotations_size);
     Assert.AreEqual(0, msg.annotations.Count);
 }
Esempio n. 6
0
 public void testRecvAnnotations()
 {
     var annotations = new Dictionary<string, byte[]>();
     annotations["TEST"] = new byte[]{10, 20,30,40,50};
     var msg = new Message(Message.MSG_CONNECT, new byte[]{1,2,3,4,5}, this.serializer_id, 0, 0, annotations);
     var c = new ConnectionMock();
     c.send(msg.to_bytes());
     msg = Message.recv(c, null);
     Assert.AreEqual(0, c.RemainingLength);
     Assert.AreEqual(5, msg.data_size);
     Assert.AreEqual(new byte[]{1,2,3,4,5}, msg.data);
     Assert.AreEqual(new byte[]{10,20,30,40,50}, msg.annotations["TEST"]);
     Assert.IsTrue(msg.annotations.ContainsKey("HMAC"));
 }
Esempio n. 7
0
        public void testHmac()
        {
            byte[] hk=Config.HMAC_KEY;
            Stream c;
            byte[] data;

            try {
            Config.HMAC_KEY = Encoding.ASCII.GetBytes("test key");
            data = new Message(Message.MSG_RESULT, new byte[]{1,2,3,4,5}, 42, 0, 1, null).to_bytes();
            c = new ConnectionMock(data);
            }
            finally {
            Config.HMAC_KEY = hk;
            }
            // test checking of different hmacs
            try {
            Message.recv(c, null);
            Assert.Fail("crash expected");
            }
            catch(PyroException x) {
            Assert.IsTrue(x.Message.Contains("hmac"));
            }
            c = new ConnectionMock(data);
            // test that it works again when resetting the key
            try {
            hk = Config.HMAC_KEY;
            Config.HMAC_KEY = Encoding.ASCII.GetBytes("test key");
            Message.recv(c, null);
            }
            finally {
            Config.HMAC_KEY = hk;
            }
            c = new ConnectionMock(data);
            // test that it doesn't work when no key is set
            try {
            hk = Config.HMAC_KEY;
            Config.HMAC_KEY = null;
            Message.recv(c, null);
            Assert.Fail("crash expected");
            }
            catch(PyroException x) {
            Assert.IsTrue(x.Message.Contains("hmac key config"));
            }
            finally {
            Config.HMAC_KEY = hk;
            }
        }
Esempio n. 8
0
 public void testChecksum()
 {
     var msg = new Message(Message.MSG_RESULT, new byte[]{1,2,3,4}, 42, 0, 1, null);
     var c = new ConnectionMock();
     c.send(msg.to_bytes());
     // corrupt the checksum bytes
     byte[] data = c.ReceivedData;
     data[Message.HEADER_SIZE-2] = 0;
     data[Message.HEADER_SIZE-1] = 0;
     c = new ConnectionMock(data);
     try {
     Message.recv(c, null);
     Assert.Fail("crash expected");
     }
     catch(PyroException x) {
     Assert.IsTrue(x.Message.Contains("checksum"));
     }
 }