コード例 #1
0
        public static void Run(NetPeer peer)
        {
            NetOutgoingMessage msg = peer.CreateMessage();

            msg.Write(false);
            msg.Write(-3, 6);
            msg.Write(42);
            msg.Write("duke of earl");
            msg.Write((byte)43);
            msg.Write((ushort)44);
            msg.Write(UInt64.MaxValue, 64);
            msg.Write(true);

            msg.WritePadBits();

            int bcnt = 0;

            msg.Write(567845.0f);
            msg.WriteVariableInt32(2115998022);
            msg.Write(46.0);
            msg.Write((ushort)14, 9);
            bcnt += msg.WriteVariableInt32(-47);
            msg.WriteVariableInt32(470000);
            msg.WriteVariableUInt32(48);
            bcnt += msg.WriteVariableInt64(-49);

            if (bcnt != 2)
            {
                throw new NetException("WriteVariable* wrote too many bytes!");
            }

            byte[] data = msg.Data;

            NetIncomingMessage inc = MiscTests.CreateIncomingMessage(data, msg.LengthBits);

            StringBuilder bdr = new StringBuilder();

            bdr.Append(inc.ReadBoolean());
            bdr.Append(inc.ReadInt32(6));
            bdr.Append(inc.ReadInt32());

            string strResult;
            bool   ok = inc.ReadString(out strResult);

            Assert.IsTrue(ok, "ReadWriteTests failed");

            if (ok == false)
            {
                throw new NetException("Read/write failure");
            }
            bdr.Append(strResult);

            bdr.Append(inc.ReadByte());

            if (inc.PeekUInt16() != (ushort)44)
            {
                throw new NetException("Read/write failure");
            }

            bdr.Append(inc.ReadUInt16());

            if (inc.PeekUInt64(64) != UInt64.MaxValue)
            {
                throw new NetException("Read/write failure");
            }

            bdr.Append(inc.ReadUInt64());
            bdr.Append(inc.ReadBoolean());

            inc.SkipPadBits();

            bdr.Append(inc.ReadSingle());
            bdr.Append(inc.ReadVariableInt32());
            bdr.Append(inc.ReadDouble());
            bdr.Append(inc.ReadUInt32(9));
            bdr.Append(inc.ReadVariableInt32());
            bdr.Append(inc.ReadVariableInt32());
            bdr.Append(inc.ReadVariableUInt32());
            bdr.Append(inc.ReadVariableInt64());

            if (bdr.ToString().Equals("False-342duke of earl434418446744073709551615True56784521159980224614-4747000048-49"))
            {
                TestContext.Out.WriteLine("Read/write tests OK");
            }
            else
            {
                throw new NetException("Read/write tests FAILED!");
            }

            msg = peer.CreateMessage();

            NetOutgoingMessage tmp = peer.CreateMessage();

            tmp.Write((int)42, 14);

            msg.Write(tmp);
            msg.Write(tmp);

            if (msg.LengthBits != tmp.LengthBits * 2)
            {
                throw new NetException("NetOutgoingMessage.Write(NetOutgoingMessage) failed!");
            }

            tmp = peer.CreateMessage();

            Test test = new Test();

            test.Number = 42;
            test.Name   = "Hallon";
            test.Age    = 8.2f;

            tmp.WriteAllFields(test, BindingFlags.Public | BindingFlags.Instance);

            data = tmp.Data;

            inc = MiscTests.CreateIncomingMessage(data, tmp.LengthBits);

            Test readTest = new Test();

            inc.ReadAllFields(readTest, BindingFlags.Public | BindingFlags.Instance);

            NetException.Assert(readTest.Number == 42);
            NetException.Assert(readTest.Name == "Hallon");
            NetException.Assert(readTest.Age == 8.2f);

            // test aligned WriteBytes/ReadBytes
            msg = peer.CreateMessage();
            byte[] tmparr = new byte[] { 5, 6, 7, 8, 9 };
            msg.Write(tmparr);

            inc = MiscTests.CreateIncomingMessage(msg.Data, msg.LengthBits);
            byte[] result = inc.ReadBytes(tmparr.Length);

            for (int i = 0; i < tmparr.Length; i++)
            {
                if (tmparr[i] != result[i])
                {
                    throw new Exception("readbytes fail");
                }
            }
        }
コード例 #2
0
        public static void Run(NetPeer peer)
        {
            //
            // Test encryption
            //
            List <NetEncryption> algos = new List <NetEncryption>();

            algos.Add(new NetXorEncryption(peer, "TopSecret"));
            algos.Add(new NetXtea(peer, "TopSecret"));
            algos.Add(new NetAESEncryption(peer, "TopSecret"));
            algos.Add(new NetRC2Encryption(peer, "TopSecret"));
            algos.Add(new NetDESEncryption(peer, "TopSecret"));
            algos.Add(new NetTripleDESEncryption(peer, "TopSecret"));

            foreach (var algo in algos)
            {
                NetOutgoingMessage om = peer.CreateMessage();
                om.Write("Hallon");
                om.Write(42);
                om.Write(5, 5);
                om.Write(true);
                om.Write("kokos");
                int unencLen = om.LengthBits;
                om.Encrypt(algo);

                // convert to incoming message
                NetIncomingMessage im = MiscTests.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
                if (im.Data == null || im.Data.Length == 0)
                {
                    throw new NetException("bad im!");
                }

                im.Decrypt(algo);

                if (im.Data == null || im.Data.Length == 0 || im.LengthBits != unencLen)
                {
                    throw new NetException("Length fail");
                }

                var str = im.ReadString();
                if (str != "Hallon")
                {
                    throw new NetException("fail");
                }
                if (im.ReadInt32() != 42)
                {
                    throw new NetException("fail");
                }
                if (im.ReadInt32(5) != 5)
                {
                    throw new NetException("fail");
                }
                if (im.ReadBoolean() != true)
                {
                    throw new NetException("fail");
                }
                if (im.ReadString() != "kokos")
                {
                    throw new NetException("fail");
                }

                TestContext.Out.WriteLine(algo.GetType().Name + " encryption verified");
            }

            for (int i = 0; i < 100; i++)
            {
                byte[] salt = NetSRP.CreateRandomSalt();
                byte[] x    = NetSRP.ComputePrivateKey("user", "password", salt);

                byte[] v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                byte[] a = NetSRP.CreateRandomEphemeral();                 //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                byte[] A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                byte[] b = NetSRP.CreateRandomEphemeral();                 // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                byte[] B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                byte[] u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss.Length != Sc.Length)
                {
                    throw new NetException("SRP non matching lengths!");
                }

                for (int j = 0; j < Ss.Length; j++)
                {
                    if (Ss[j] != Sc[j])
                    {
                        throw new NetException("SRP non matching session values!");
                    }
                }

                var test = NetSRP.CreateEncryption(peer, Ss);
            }

            TestContext.Out.WriteLine("Message encryption OK");
        }