public void LoginReply_TestEverything()
        {
            LoginReply r1 = new LoginReply();
            Assert.IsFalse(r1.Success);
            Assert.IsNull(r1.ProcessInfo);
            Assert.IsNull(r1.Note);

            ProcessInfo p1 = new ProcessInfo()
            {
                ProcessId = 10,
                Label = "Joey's player",
                EndPoint = new PublicEndPoint { Host = "127.0.0.1", Port = 10030 }
            };

            LoginReply r2 = new LoginReply()
            {
                Success = true,
                ProcessInfo = p1,
                Note = "Testing"
            };

            Assert.IsTrue(r2.Success);
            Assert.AreSame(p1, r2.ProcessInfo);
            Assert.AreEqual("Testing", r2.Note);

            byte[] bytes = r2.Encode();
            // string tmp = Encoding.ASCII.GetString(bytes);

            Message m2 = Message.Decode(bytes);
            LoginReply r3 = m2 as LoginReply;
            Assert.IsNotNull(r3);
            Assert.AreNotSame(r2, r3);
            Assert.IsTrue(r3.Success);
            Assert.AreNotSame(r2.ProcessInfo, r3.ProcessInfo);
            Assert.AreEqual(r2.ProcessInfo.ProcessId, r3.ProcessInfo.ProcessId);
            Assert.AreEqual(r2.ProcessInfo.Label, r3.ProcessInfo.Label);
            Assert.AreEqual(r2.Note, r3.Note);
        }
        public void TestLogin()
        {
            // Create udp client to mock registry
            UdpClient mockClient = new UdpClient(0);
            int mockClientPort = ((IPEndPoint)mockClient.Client.LocalEndPoint).Port;
            PublicEndPoint mockClientEP = new PublicEndPoint()
            {
                Host = "127.0.0.1",
                Port = mockClientPort
            };

            // Create fake player
            TestablePlayer player = new TestablePlayer()
            {
                RegistryEndPoint = mockClientEP,
                IdentityInfo = new IdentityInfo()
                {
                    FirstName = "Test",
                    LastName = "Player",
                    ANumber = "A01234983",
                    Alias = "TP"
                },
                ProcessLabel = "Test TP"
            };

            // Run player
            Thread loginThread = new Thread(new ThreadStart(player.Start));
            loginThread.Start();

            // Get message from registry client
            IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
            byte[] bytes = mockClient.Receive(ref senderEP);
            Assert.IsNotNull(bytes);
            Assert.AreNotEqual(0, bytes.Length);

            // Assert on LoginRequest
            Message msg = Message.Decode(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is LoginRequest);
            LoginRequest request = msg as LoginRequest;
            Assert.AreEqual(player.IdentityInfo.Alias, request.Identity.Alias);
            Assert.AreEqual(player.IdentityInfo.ANumber, request.Identity.ANumber);
            Assert.AreEqual(player.IdentityInfo.FirstName, request.Identity.FirstName);
            Assert.AreEqual(player.IdentityInfo.LastName, request.Identity.LastName);
            Assert.AreEqual(player.ProcessLabel, request.ProcessLabel);
            Assert.AreEqual(ProcessInfo.ProcessType.Player,request.ProcessType);

            // Send LoginReply from registry to player
            LoginReply reply = new LoginReply()
            {
                ProcessInfo = new ProcessInfo()
                {
                    ProcessId = 5,
                    Type = ProcessInfo.ProcessType.Player,
                    EndPoint = new PublicEndPoint()
                    {
                        HostAndPort = senderEP.ToString()
                    },
                    Label = "Test Player",
                    Status = ProcessInfo.StatusCode.Initializing
                },
                Success = true
            };
            bytes = reply.Encode();
            mockClient.Send(bytes, bytes.Length, senderEP);

            // Assert on LoginReply, player state
            Thread.Sleep(2000);
            player.Stop();
            loginThread.Join();
            Assert.AreEqual(player.ProcessInfo.ProcessId, reply.ProcessInfo.ProcessId);
            Assert.AreEqual(player.ProcessInfo.Type, reply.ProcessInfo.Type);
            Assert.AreEqual(player.ProcessInfo.EndPoint.HostAndPort, reply.ProcessInfo.EndPoint.HostAndPort);
            Assert.AreEqual(player.ProcessInfo.Label, reply.ProcessInfo.Label);
            Assert.AreEqual(player.ProcessInfo.Status, reply.ProcessInfo.Status);
            Assert.IsTrue(player.CurrentState is GetGamesState);
        }