public void TestMessageSerializations() { // Test Serialization // Setup Message expected = new SendMessageRequest("my group id", "Hello World", "Me"); string jsontext = expected.JSON(); Message m = MessageFromJSONFactory.GetMessage(jsontext); // Tests Trace.WriteLine(m.MessageId); Trace.WriteLine(expected.MessageId); Assert.IsNotNull(jsontext); Assert.IsNotNull(m); Assert.IsNotNull(expected); Assert.AreEqual(m.MessageId, expected.MessageId); // Setup Message m1 = new RegisterServerRequest(new Server()); Message m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.IsNotNull(m1.Server); Assert.IsNotNull(m2.Server); Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(RegisterServerRequest))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(RegisterServerRequest))); // Setup List <Tuple <string, UserKey> > keys = new List <Tuple <string, UserKey> >(); keys.Add(new Tuple <string, UserKey>("1", new UserKey("Bob", "BobsKey"))); keys.Add(new Tuple <string, UserKey>("2", new UserKey("Joe", "JoesKey"))); m1 = new PublicKeysResponse(keys); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m2.Keys[0].Item2.Publickey, "BobsKey"); Assert.AreEqual(m2.Keys[1].Item2.Publickey, "JoesKey"); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(PublicKeysResponse))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(PublicKeysResponse))); // Setup m1 = new RegisterKeyRequest("1", new UserKey("Bob", "BobsKey")); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); Assert.AreEqual(m1.UserKey.Username, m2.UserKey.Username); Assert.AreEqual(m1.UserKey.Publickey, m2.UserKey.Publickey); Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.MessageType, m2.MessageType); // Setup m1 = new Acknowledge(ResponseCodes.SUCCESS, "You win!"); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.Code, ResponseCodes.SUCCESS); Assert.AreEqual(m1.ResponseMessage, "You win!"); // Setup List <string> users = new List <string>(); users.Add("Billy"); users.Add("Joe"); users.Add("Bob"); m1 = new PublicKeysRequest(users); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(PublicKeysRequest))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(PublicKeysRequest))); Assert.AreEqual(m1.Users[0], m2.Users[0]); Assert.AreEqual(m1.Users[1], m2.Users[1]); Assert.AreEqual(m1.Users[2], m2.Users[2]); Assert.AreEqual(m1.Users.Count, 3); Assert.AreEqual(m1.Users.Count, m2.Users.Count); // Setup m1 = new HeartBeat(300); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.KeepAlive, 300); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(HeartBeat))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(HeartBeat))); Assert.AreEqual(m1.MessageId, m2.MessageId); // Setup m1 = new NewMessagesRequest("MyId", "Last message ID"); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.LastMessage, m2.LastMessage); Assert.AreEqual(m2.LastMessage, "Last message ID"); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(NewMessagesRequest))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(NewMessagesRequest))); // Setup List <EncryptedMessage> list = new List <EncryptedMessage>(); EncryptedMessage cm1 = new EncryptedMessage(); cm1.MessageId = "message id 1"; cm1.SenderId = "Bob"; cm1.Timestamp = 101010101010; cm1.PlainText = "Dead men tell no tales"; cm1.GroupId = "My special group"; list.Add(cm1); m1 = new NewMessagesResponse(list); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(NewMessagesResponse))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(NewMessagesResponse))); Assert.AreEqual(m1.Messages[0].MessageId, m2.Messages[0].MessageId); Assert.AreEqual(m1.Messages[0].Timestamp, m2.Messages[0].Timestamp); Assert.AreEqual(m1.Messages[0].GroupId, m2.Messages[0].GroupId); Assert.AreEqual(m1.Messages[0].SenderId, m2.Messages[0].SenderId); Assert.IsNull(m2.Messages[0].PlainText); // Make sure message isn't passed in plain text // Setup Server s1 = new Server(); s1.ChatServerEndpoint = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 12345); s1.ActiveUsers = 38; List <Server> l1 = new List <Server>(); l1.Add(s1); m1 = new ListServersResponse(l1); m2 = MessageFromJSONFactory.GetMessage(m1.JSON()); // Tests Assert.AreEqual(m1.MessageType, MessageTypes.ToInt(typeof(ListServersResponse))); Assert.AreEqual(m2.MessageType, MessageTypes.ToInt(typeof(ListServersResponse))); Assert.AreEqual(m1.MessageId, m2.MessageId); Assert.AreEqual(m1.Servers[0].ChatServerEndpoint, m2.Servers[0].ChatServerEndpoint); Assert.AreEqual(m1.Servers[0].ChatServerEndpoint, m2.Servers[0].ChatServerEndpoint); Assert.AreEqual(m2.Servers[0].ChatServerEndpoint.ToString(), "192.168.0.1:12345"); Assert.AreEqual(m1.Servers[0].ActiveUsers, m2.Servers[0].ActiveUsers); Assert.AreEqual(m2.Servers[0].ActiveUsers, 38); }