예제 #1
0
        public void ShouldSentInterestedWhenPeerHasSomethingToOffer()
        {
            PeerHash other = PeerHash.Random();

            Action <Action <PeerHash, Bitfield, PeerState> > handle = callback =>
            {
                callback.Invoke(other, Bitfield.Complete(1), new PeerState());
            };

            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    DataVerified verified = new DataVerified
                    {
                        Hash     = session.Service.Parameters.Hash,
                        Bitfield = new Bitfield(1)
                    };

                    A.CallTo(() => session.DataMap.Query(A <Action <PeerHash, Bitfield, PeerState> > .Ignored)).Invokes(handle);

                    session.Service.Start();
                    session.Service.Handle(verified);

                    session.Pipeline.Tick();
                    session.Pipeline.Process();

                    A.CallTo(() => session.Glue.SendInterested(other)).MustHaveHappened();
                }
        }
예제 #2
0
        public void ShouldTriggerBlockRequested()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    BlockReserved reserved = new BlockReserved
                    {
                        Hash  = session.Service.Hash,
                        Peer  = PeerHash.Random(),
                        Block = new BlockIndex(1, 0, 3616)
                    };

                    Trigger handler = Trigger.Bind(ref session.Service.Hooks.OnBlockRequested, data =>
                    {
                        data.Hash.Should().Be(session.Service.Hash);
                        data.Peer.Should().Be(reserved.Peer);
                        data.Block.Should().Be(reserved.Block);
                        data.Block.Size.Should().Be(3616);
                    });

                    session.Service.Start();
                    session.Service.Handle(reserved);

                    session.Pipeline.Process();
                    handler.Wait().Should().BeTrue();
                }
        }
예제 #3
0
        public void ShouldCompleteBlockWhenBlockIsWritten()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    BlockWritten written = new BlockWritten
                    {
                        Hash  = session.Service.Hash,
                        Block = new BlockIndex(1, 0, 3616)
                    };

                    session.Service.Start();
                    session.Service.Handle(written);
                    session.Pipeline.Process();

                    A.CallTo(() => session.DataMap.Complete(written.Block)).MustHaveHappened();
                }
        }
예제 #4
0
        public void ShouldInvalidatePieceWhenPieceIsRejected()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    PieceRejected rejected = new PieceRejected
                    {
                        Hash  = session.Service.Hash,
                        Piece = new PieceInfo(1)
                    };

                    session.Service.Start();
                    session.Service.Handle(rejected);
                    session.Pipeline.Process();

                    A.CallTo(() => session.DataMap.Invalidate(rejected.Piece)).MustHaveHappened();
                }
        }
예제 #5
0
        public void ShouldCompletePieceWhenPieceIsAccepted()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    PieceAccepted accepted = new PieceAccepted
                    {
                        Hash  = session.Service.Hash,
                        Piece = new PieceInfo(1)
                    };

                    session.Service.Start();
                    session.Service.Handle(accepted);
                    session.Pipeline.Process();

                    A.CallTo(() => session.DataMap.Complete(accepted.Piece)).MustHaveHappened();
                }
        }
예제 #6
0
        public void ShouldVerifyPieceWhenPieceIsReady()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    PieceReady ready = new PieceReady
                    {
                        Hash  = session.Service.Hash,
                        Piece = new PieceInfo(1)
                    };

                    session.Service.Start();
                    session.Service.Handle(ready);
                    session.Pipeline.Process();

                    A.CallTo(() => session.DataStore.Verify(ready.Piece)).MustHaveHappened();
                }
        }
예제 #7
0
        public void ShouldSendRequestWhenBlockReserved()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    BlockReserved received = new BlockReserved
                    {
                        Peer  = PeerHash.Random(),
                        Hash  = session.Service.Hash,
                        Block = new BlockIndex(1, 0, 3616)
                    };

                    session.Service.Start();
                    session.Service.Handle(received);
                    session.Pipeline.Process();

                    A.CallTo(() => session.Glue.SendRequest(received.Peer, received.Block)).MustHaveHappened();
                }
        }
예제 #8
0
        public void ShouldWriteToRepositoryWhenBlockReceived()
        {
            using (DataGetFixture fixture = new DataGetFixture())
                using (DataGetSession session = fixture.Start())
                {
                    BlockReceived received = new BlockReceived
                    {
                        Payload = new DataGetBlock(),
                        Peer    = PeerHash.Random(),
                        Hash    = session.Service.Hash,
                        Block   = new BlockIndex(1, 0, 3616)
                    };

                    session.Service.Start();
                    session.Service.Handle(received);
                    session.Pipeline.Process();

                    A.CallTo(() => session.DataStore.Write(received.Block, received.Payload)).MustHaveHappened();
                }
        }