public void TestAnnotations() { byte[] hmac=Encoding.UTF8.GetBytes("secret"); var annotations = new Dictionary<string,byte[]>(); annotations["TES1"]=new byte[]{10,20,30,40,50}; annotations["TES2"]=new byte[]{20,30,40,50,60}; annotations["TES3"]=new byte[]{30,40,50,60,70}; annotations["TES4"]=new byte[]{40,50,60,70,80}; var msg = new Message(Message.MSG_CONNECT, new byte[]{1,2,3,4,5}, this.serializer_id, 0, 0, annotations, hmac); byte[] data = msg.to_bytes(); int annotations_size = 4+2+20 + (4+2+5)*4; Assert.AreEqual(Message.HEADER_SIZE + 5 + annotations_size, data.Length); Assert.AreEqual(annotations_size, msg.annotations_size); Assert.AreEqual(5, msg.annotations.Count); Assert.AreEqual(new byte[]{10,20,30,40,50}, msg.annotations["TES1"]); Assert.AreEqual(new byte[]{20,30,40,50,60}, msg.annotations["TES2"]); Assert.AreEqual(new byte[]{30,40,50,60,70}, msg.annotations["TES3"]); Assert.AreEqual(new byte[]{40,50,60,70,80}, msg.annotations["TES4"]); byte[] mac = msg.hmac(hmac); Assert.AreEqual(mac, msg.annotations["HMAC"]); annotations = new Dictionary<string,byte[]>(); annotations["TES4"]=new byte[]{40,50,60,70,80}; annotations["TES3"]=new byte[]{30,40,50,60,70}; annotations["TES2"]=new byte[]{20,30,40,50,60}; annotations["TES1"]=new byte[]{10,20,30,40,50}; var msg2 = new Message(Message.MSG_CONNECT, new byte[]{1,2,3,4,5}, this.serializer_id, 0, 0, annotations, hmac); Assert.AreEqual(msg.hmac(hmac), msg2.hmac(hmac)); annotations = new Dictionary<string,byte[]>(); annotations["TES4"]=new byte[]{40,50,60,70,80}; var msg3 = new Message(Message.MSG_CONNECT, new byte[]{1,2,3,4,5}, this.serializer_id, 0, 0, annotations, hmac); Assert.AreNotEqual(msg.hmac(hmac), msg3.hmac(hmac)); }
public void TestMessage() { byte[] hmac = Encoding.UTF8.GetBytes("secret"); new Message(99, new byte[0], this.serializer_id, 0, 0, null, hmac); // doesn't check msg type here Assert.Throws(typeof(PyroException), ()=>Message.from_header(Encoding.ASCII.GetBytes("FOOBAR"))); var msg = new Message(Message.MSG_CONNECT, Encoding.ASCII.GetBytes("hello"), this.serializer_id, 0, 0, null, hmac); Assert.AreEqual(Message.MSG_CONNECT, msg.type); Assert.AreEqual(5, msg.data_size); Assert.AreEqual(new byte[]{(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'}, msg.data); Assert.AreEqual(4+2+20, msg.annotations_size); byte[] mac = msg.hmac(hmac); var expected = new Dictionary<string, byte[]>(); expected["HMAC"] = mac; CollectionAssert.AreEqual(expected, msg.annotations); byte[] hdr = msg.to_bytes().Take(Message.HEADER_SIZE).ToArray(); msg = Message.from_header(hdr); Assert.AreEqual(Message.MSG_CONNECT, msg.type); Assert.AreEqual(4+2+20, msg.annotations_size); Assert.AreEqual(5, msg.data_size); hdr = new Message(Message.MSG_RESULT, new byte[0], this.serializer_id, 0, 0, null, hmac).to_bytes().Take(Message.HEADER_SIZE).ToArray(); msg = Message.from_header(hdr); Assert.AreEqual(Message.MSG_RESULT, msg.type); Assert.AreEqual(4+2+20, msg.annotations_size); Assert.AreEqual(0, msg.data_size); hdr = new Message(Message.MSG_RESULT, Encoding.ASCII.GetBytes("hello"), 12345, 60006, 30003, null, hmac).to_bytes().Take(Message.HEADER_SIZE).ToArray(); msg = Message.from_header(hdr); Assert.AreEqual(Message.MSG_RESULT, msg.type); Assert.AreEqual(60006, msg.flags); Assert.AreEqual(5, msg.data_size); Assert.AreEqual(12345, msg.serializer_id); Assert.AreEqual(30003, msg.seq); byte[] data = new Message(255, new byte[0], this.serializer_id, 0, 255, null, hmac).to_bytes(); Assert.AreEqual(50, data.Length); data = new Message(1, new byte[0], this.serializer_id, 0, 255, null, hmac).to_bytes(); Assert.AreEqual(50, data.Length); data = new Message(1, new byte[0], this.serializer_id, 253, 254, null, hmac).to_bytes(); Assert.AreEqual(50, data.Length); // compression is a job of the code supplying the data, so the messagefactory should leave it untouched data = new byte[1000]; byte[] msg_bytes1 = new Message(Message.MSG_INVOKE, data, this.serializer_id, 0, 0, null, hmac).to_bytes(); byte[] msg_bytes2 = new Message(Message.MSG_INVOKE, data, this.serializer_id, Message.FLAGS_COMPRESSED, 0, null, hmac).to_bytes(); Assert.AreEqual(msg_bytes1.Length, msg_bytes2.Length); }