public void TestReliability()
        {
            base.SetupBaseTest();
            UInt16 interval  = 20;
            UInt16 dgramSize = 576;

            TunnelSocketSendIntercept sock1 = new TunnelSocketSendIntercept();
            TunnelSocketSendIntercept sock2 = new TunnelSocketSendIntercept();

            AIMDCongestionControl control1 = new AIMDCongestionControl(sock1, interval, dgramSize);
            AIMDCongestionControl control2 = new AIMDCongestionControl(sock2, interval, dgramSize);

            TunnelCongestionControllerMock t1 = new TunnelCongestionControllerMock(control1, sock1);
            TunnelCongestionControllerMock t2 = new TunnelCongestionControllerMock(control2, sock2);

            base.TestReliability(t1, sock1, t2, sock2, 20, dgramSize);
        }
Esempio n. 2
0
        public void TestOpenNewTunnelRequest()
        {
            //this actually needs to be done by the tunnel socket itself
            //if an EPK is recieved on a TID that doesn't exist, we should
            //respond by creating a tunnel to receive it

            TunnelSocketSendIntercept sendIntercept = new TunnelSocketSendIntercept();
            SecureTunnel t = new SecureTunnel(sendIntercept);

            t.ID = 1;
            UInt64          _id       = t.ID;
            bool            triggered = false;
            EncryptedPacket hello     = t.MakeHelloPacket();

            hello.TID = hello.TID |= Common.PUBLIC_KEY_FLAG; //this should be done during packing
            sendIntercept.SetSendInterceptor(p =>
            {
                //socket.HandlePacket ((EncryptedPacket)p);
                Assert.IsTrue(t.ID == (p.TID & ~Common.TID_FLAGS), String.Format("Key was: {0} but was supposed to be {1}", p.TID, t.ID));
                TunnelBase ta;
                Assert.IsFalse(socket.mTunnelDirectory.Get(t.ID, out ta), "Tunnel ID couldn't be found");
                triggered = true;
            });

            IPEndPoint ep = new IPEndPoint(IPAddress.Any, 5000);

            sendIntercept.HandlePacket(hello);
            //t.CommunicateWith (ep);
            //t.CommunicateWith (ep);
            Assert.IsTrue(triggered);

            triggered = false;

            //TODO: When a decryption fails due to a packet
            //being sent with an incorrect key we should
            //add that ip address to a "caution" list. If there
            //are repeated attepts at opening tunnels or
            //adding tunnels then we should  challenge with a puzzle
            //or blacklist the user altogether.
        }
        /// <summary>
        /// This test launches multiple tests and sends a series of packets through an object
        /// that simulates packet loss. The packet loss will medium will randomly simulate
        /// RTT delays, MTU size differences and just packet loss.
        /// </summary>
        internal void TestReliability(TunnelCongestionControllerMock ep1,
                                      TunnelSocketSendIntercept sock1,
                                      TunnelCongestionControllerMock ep2,
                                      TunnelSocketSendIntercept sock2,
                                      int lossPercentage,
                                      UInt32 dgramSize)
        {
            LossyConnection connection = new LossyConnection(lossPercentage);

            //hook ep1 to ep2
            //sending from ep1 to ep2
            sock1.SetSendInterceptor(p =>
            {
                //outgoing packet needs to be bubbled into sock2
                connection.SendPacket(p, ep2);
            });

            //ep2 sends an ack to ep1
            sock2.SetSendInterceptor(p =>
            {
                //add test for acks (they should always come in order)
                connection.SendPacket(p, ep1);
            });
            int curAck = 1;

            ep1.SetIncomingPacketInterceptor(p =>
            {
                //should receive Ack's in order
                Assert.IsTrue(curAck == p.Ack, "Acks should be received in order");
            });
            StringBuilder buildString = new StringBuilder();

            ep2.SetIncomingPacketInterceptor(p =>
            {
                GenericPacketMock gp = (GenericPacketMock)p;
                buildString.Append(Encoding.ASCII.GetString(gp.GetPayload()));
            });

            //the test executes on a seperate thread
            Thread testThread = new Thread(new ThreadStart(() =>
            {
                byte[] txt = Encoding.ASCII.GetBytes(this.sendText);
                int size   = 576 - 8 - 80;
                uint seq   = 1;
                ArrayList <GenericPacketMock> packets = new ArrayList <GenericPacketMock>();
                int numPackets = txt.Length / size;
                int overFlow   = txt.Length % size;
                if (overFlow > 0)
                {
                    numPackets += 1;
                }
                for (int i = 0; i < numPackets; i++)
                {
                    int sizeSubArray = Math.Min(size, txt.Length - (i * size));
                    byte[] sub       = new byte[sizeSubArray];
                    Array.Copy(txt, i * size, sub, 0, sizeSubArray);
                    GenericPacketMock p = new GenericPacketMock(seq);
                    p.SetPayload(sub);
                    seq++;
                    packets.Add(p);
                }
                foreach (GenericPacket p in packets)
                {
                    ep1.SendPacket(p);
                }
            }));

            testThread.Start();
            Thread.Sleep(1000);
            Assert.IsTrue(this.sendText.Equals(buildString.ToString()), buildString.ToString());
        }