예제 #1
0
        public void TestSSUOutOfOrderFragmentation()
        {
            var fragmenter = new DataFragmenter();

            var smalldata        = new BufLen(BufUtils.RandomBytes(4 + BufUtils.RandomInt(4)));
            var smalldatamessage = new DataMessage(smalldata);

            var smalldata1        = new BufLen(BufUtils.RandomBytes(40 + BufUtils.RandomInt(14)));
            var smalldatamessage1 = new DataMessage(smalldata1);

            var smalldata2        = new BufLen(BufUtils.RandomBytes(130 + BufUtils.RandomInt(39)));
            var smalldatamessage2 = new DataMessage(smalldata2);

            var smalldata3        = new BufLen(BufUtils.RandomBytes(770 + BufUtils.RandomInt(220)));
            var smalldatamessage3 = new DataMessage(smalldata3);

            var data        = new BufLen(BufUtils.RandomBytes(30000 + BufUtils.RandomInt(30)));
            var datamessage = new DataMessage(data);

            var data2        = new BufLen(BufUtils.RandomBytes(20000 + BufUtils.RandomInt(1040)));
            var datamessage2 = new DataMessage(data2);

            var dest   = new byte[MTUConfig.BufferSize];
            var start  = new BufLen(dest);
            var writer = new BufRefLen(dest);

            var tosend = new LinkedList <II2NPHeader16>();

            tosend.AddLast(smalldatamessage.CreateHeader16);
            tosend.AddLast(datamessage.CreateHeader16);
            tosend.AddLast(smalldatamessage1.CreateHeader16);
            tosend.AddLast(smalldatamessage2.CreateHeader16);
            tosend.AddLast(smalldatamessage3.CreateHeader16);
            tosend.AddLast(datamessage2.CreateHeader16);

            var tosendshuffle  = tosend.Shuffle();
            var tosendshuffled = new ConcurrentQueue <II2NPHeader16>(tosendshuffle);

            var sent = new LinkedList <II2NPHeader16>();

            foreach (var one in tosend)
            {
                sent.AddLast(one);
            }

            var sentdata = new LinkedList <BufLen>();

            while (true)
            {
                var flagbuf      = writer.ReadBufLen(1);
                var fragcountbuf = writer.ReadBufLen(1);

                var fragments = fragmenter.Send(writer, tosendshuffled);
                if (fragments == 0)
                {
                    break;
                }

                flagbuf[0] |= (byte)SSUDataMessage.DataMessageFlags.WantReply;
                // no ACKs
                fragcountbuf[0] = (byte)fragments;

                sentdata.AddLast(new BufLen(start, 0, writer - start));
                dest   = new byte[MTUConfig.BufferSize];
                start  = new BufLen(dest);
                writer = new BufRefLen(dest);
            }

            var shuffled = sentdata.Shuffle();

            var receivedmessages = new LinkedList <II2NPHeader16>();

            var defragmenter = new DataDefragmenter();

            foreach (var frag in shuffled)
            {
                var datamsg = new SSUDataMessage(new BufRefLen(frag), defragmenter);
                if (datamsg.NewMessages != null)
                {
                    foreach (var msg in datamsg.NewMessages)
                    {
                        var i2npmsg = I2NPMessage.ReadHeader16((BufRefLen)msg.GetPayload());
                        receivedmessages.AddLast(i2npmsg);
                    }
                }
            }

            Assert.IsTrue(receivedmessages.Count == sent.Count);

            foreach (var sentmsg in sent)
            {
                Assert.IsTrue(receivedmessages.SingleOrDefault(m => ((DataMessage)m.Message).Payload ==
                                                               ((DataMessage)sentmsg.Message).Payload) != null);
            }
        }
예제 #2
0
        public void TestSSUFragmentation1()
        {
            var fragmenter = new DataFragmenter();

            var smalldata        = new BufLen(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, });
            var smalldatamessage = new DataMessage(smalldata);

            var dest   = new byte[MTUConfig.BufferSize];
            var start  = new BufLen(dest);
            var writer = new BufRefLen(dest);

            var tosend = new ConcurrentQueue <II2NPHeader16>();

            tosend.Enqueue(smalldatamessage.CreateHeader16);

            var sent = new LinkedList <II2NPHeader16>();

            foreach (var one in tosend)
            {
                sent.AddLast(one);
            }

            var sentdata = new LinkedList <BufLen>();

            while (true)
            {
                var flagbuf      = writer.ReadBufLen(1);
                var fragcountbuf = writer.ReadBufLen(1);

                var fragments = fragmenter.Send(writer, tosend);
                if (fragments == 0)
                {
                    break;
                }

                flagbuf[0] |= (byte)SSUDataMessage.DataMessageFlags.WantReply;
                // no ACKs
                fragcountbuf[0] = (byte)fragments;

                sentdata.AddLast(new BufLen(start, 0, writer - start));
                dest   = new byte[MTUConfig.BufferSize];
                start  = new BufLen(dest);
                writer = new BufRefLen(dest);
            }

            var receivedmessages = new LinkedList <II2NPHeader16>();

            var defragmenter = new DataDefragmenter();

            foreach (var frag in sentdata)
            {
                var datamsg = new SSUDataMessage(new BufRefLen(frag), defragmenter);
                if (datamsg.NewMessages != null)
                {
                    foreach (var msg in datamsg.NewMessages)
                    {
                        var i2npmsg = I2NPMessage.ReadHeader16((BufRefLen)msg.GetPayload());
                        receivedmessages.AddLast(i2npmsg);
                    }
                }
            }

            Assert.IsTrue(receivedmessages.Count == sent.Count);

            /*
             *
             * var st1 = FreenetBase64.Encode( sent.First.Value.Message.Payload );
             * var st2 = FreenetBase64.Encode( receivedmessages.First.Value.Message.Payload );
             *
             * Console.WriteLine( st1 );
             * Console.WriteLine( st2 );
             *
             * Console.WriteLine( sent.First.Value.Message );
             * Console.WriteLine( receivedmessages.First.Value.Message );
             *
             * Console.WriteLine( sent.First.Value.Message.Payload.ToString( "I500" ) );
             * Console.WriteLine( receivedmessages.First.Value.Message.Payload.ToString( "I500" ) );
             */

            foreach (var sentmsg in sent)
            {
                Assert.IsTrue(receivedmessages.Any(m => m.Message.Payload ==
                                                   sentmsg.Message.Payload));
            }
        }
예제 #3
0
        public void TestSSUFragmentation()
        {
            var fragmenter = new DataFragmenter();

            var smalldata        = new BufLen(BufUtils.RandomBytes(30));
            var smalldatamessage = new DataMessage(smalldata);

            var data        = new BufLen(BufUtils.RandomBytes(30000));
            var datamessage = new DataMessage(data);

            var data2        = new BufLen(BufUtils.RandomBytes(30000));
            var datamessage2 = new DataMessage(data2);

            var dest   = new byte[MTUConfig.BufferSize];
            var start  = new BufLen(dest);
            var writer = new BufRefLen(dest);

            var tosend = new ConcurrentQueue <II2NPHeader16>();

            tosend.Enqueue(smalldatamessage.CreateHeader16);
            tosend.Enqueue(datamessage.CreateHeader16);
            tosend.Enqueue(datamessage2.CreateHeader16);

            var sent = new LinkedList <II2NPHeader16>();

            foreach (var one in tosend)
            {
                sent.AddLast(one);
            }

            var sentdata = new LinkedList <BufLen>();

            while (true)
            {
                var flagbuf      = writer.ReadBufLen(1);
                var fragcountbuf = writer.ReadBufLen(1);

                var fragments = fragmenter.Send(writer, tosend);
                if (fragments == 0)
                {
                    break;
                }

                flagbuf[0] |= (byte)SSUDataMessage.DataMessageFlags.WantReply;
                // no ACKs
                fragcountbuf[0] = (byte)fragments;

                sentdata.AddLast(new BufLen(start, 0, writer - start));
                dest   = new byte[MTUConfig.BufferSize];
                start  = new BufLen(dest);
                writer = new BufRefLen(dest);
            }

            var receivedmessages = new LinkedList <II2NPHeader16>();

            var defragmenter = new DataDefragmenter();

            foreach (var frag in sentdata)
            {
                var datamsg = new SSUDataMessage(new BufRefLen(frag), defragmenter);
                if (datamsg.NewMessages != null)
                {
                    foreach (var msg in datamsg.NewMessages)
                    {
                        var i2npmsg = I2NPMessage.ReadHeader16((BufRefLen)msg.GetPayload());
                        receivedmessages.AddLast(i2npmsg);
                    }
                }
            }

            Assert.IsTrue(receivedmessages.Count == sent.Count);

            foreach (var sentmsg in sent)
            {
                Assert.IsTrue(receivedmessages.Any(m => m.Message.Payload ==
                                                   sentmsg.Message.Payload));
            }
        }
예제 #4
0
        public void TestSSUFragmentation()
        {
            var fragmenter = new DataFragmenter();

            var smalldata        = new BufLen(BufUtils.Random(30));
            var smalldatamessage = new I2PCore.Tunnel.I2NP.Messages.DataMessage(smalldata);

            var data        = new BufLen(BufUtils.Random(30000));
            var datamessage = new I2PCore.Tunnel.I2NP.Messages.DataMessage(data);

            var data2        = new BufLen(BufUtils.Random(30000));
            var datamessage2 = new I2PCore.Tunnel.I2NP.Messages.DataMessage(data2);

            var dest   = new byte[MTUConfig.BufferSize];
            var start  = new BufLen(dest);
            var writer = new BufRefLen(dest);

            var tosend = new LinkedList <II2NPHeader5>();

            tosend.AddLast(smalldatamessage.Header5);
            tosend.AddLast(datamessage.Header5);
            tosend.AddLast(datamessage2.Header5);

            var sent = new LinkedList <II2NPHeader5>();

            foreach (var one in tosend)
            {
                sent.AddLast(one);
            }

            var sentdata = new LinkedList <BufLen>();

            while (true)
            {
                var flagbuf      = writer.ReadBufLen(1);
                var fragcountbuf = writer.ReadBufLen(1);

                var fragments = fragmenter.Send(writer, tosend);
                if (fragments == 0)
                {
                    break;
                }

                flagbuf[0] |= (byte)I2PCore.Transport.SSU.SSUDataMessage.DataMessageFlags.WantReply;
                // no ACKs
                fragcountbuf[0] = (byte)fragments;

                sentdata.AddLast(new BufLen(start, 0, writer - start));
                dest   = new byte[MTUConfig.BufferSize];
                start  = new BufLen(dest);
                writer = new BufRefLen(dest);
            }

            var receivedmessages = new LinkedList <II2NPHeader5>();

            var defragmenter = new DataDefragmenter();

            foreach (var frag in sentdata)
            {
                var datamsg = new I2PCore.Transport.SSU.SSUDataMessage(new BufRefLen(frag), defragmenter);
                if (datamsg.NewMessages != null)
                {
                    foreach (var msg in datamsg.NewMessages)
                    {
                        var i2npmsg = I2NPMessage.ReadHeader5((BufRefLen)msg.GetPayload());
                        receivedmessages.AddLast(i2npmsg);
                    }
                }
            }

            Assert.IsTrue(receivedmessages.Count == sent.Count);

            foreach (var sentmsg in sent)
            {
                Assert.IsTrue(receivedmessages.SingleOrDefault(m => ((I2PCore.Tunnel.I2NP.Messages.DataMessage)m.Message).Payload ==
                                                               ((I2PCore.Tunnel.I2NP.Messages.DataMessage)sentmsg.Message).Payload) != null);
            }
        }