コード例 #1
0
ファイル: handler_test.go プロジェクト: stratospark/torro
func TestHave(t *testing.T) {
	Convey("Receives Have Messages and update BitField", t, func(ctx C) {
		s := NewBTService(port, []byte(peerIDRemote))
		mc := NewMockConnectionFetcher()
		s.ConnectionFetcher = mc
		s.AddHash(hash)
		_ = s.StartListening()

		// TODO: check that peer data is saved within service data structure
		peers := make([]structure.Peer, 1)
		peers[0] = structure.Peer{IP: net.IPv4(192, 168, 1, 1), Port: 55557}
		//		peers[1] = structure.Peer{IP: net.IPv4(192, 168, 1, 2), Port: 55557}
		s.InitiateHandshakes(hash, peers)

		wg := sync.WaitGroup{}
		wg.Add(len(peers))
		for _, p := range peers {
			go func(pp structure.Peer) {
				c0 := mc.Conns[pp.AddrString()].Conn.(*MockConnection)
				hs, err := structure.ReadHandshake(bytes.NewReader(<-c0.ReceiveBytesChan))
				ctx.So(hs, ShouldNotBeNil)
				ctx.So(err, ShouldBeNil)

				hs, _ = structure.NewHandshake(hash, []byte(peerIDClient))
				c0.SendMessage(hs)

				bf := structure.BitFieldFromHexString("\x00\x00\x00\x01")
				msg0 := structure.NewBitFieldMessage(bf)
				c0.SendMessage(msg0)

				m, err := ReadMessageOrTimeout(c0, ctx)
				ctx.So(err, ShouldBeNil)
				ctx.So(m.GetType(), ShouldEqual, structure.MessageTypeInterested)

				haveMsg := structure.NewHaveMessage(0)
				c0.SendMessage(haveMsg)

				time.Sleep(time.Millisecond)

				// TODO: Check why byte equality doesn't work
				btc := s.LookupConn(pp.AddrString())
				ctx.Printf("BTC: %q\n", btc.BitField.String())
				ctx.Printf("BF : %q\n", bf.String())
				updatedBF := structure.BitFieldFromHexString("\x80\x00\x00\x01")
				ctx.Printf("UBF: %q\n", updatedBF.String())
				isEqual := updatedBF.String() == btc.BitField.String()
				ctx.So(isEqual, ShouldBeTrue)

				wg.Done()
			}(p)
		}
		wg.Wait()

		_ = s.StopListening()
		time.Sleep(time.Millisecond)
	})
}
コード例 #2
0
ファイル: handler_test.go プロジェクト: stratospark/torro
func TestConversation(t *testing.T) {
	Convey("Receives Bitfield message and sends Interested message", t, func(ctx C) {
		s := NewBTService(port, []byte(peerIDRemote))
		mc := NewMockConnectionFetcher()
		s.ConnectionFetcher = mc
		s.AddHash(hash)
		_ = s.StartListening()

		// TODO: check that peer data is saved within service data structure
		peers := make([]structure.Peer, 1)
		peers[0] = structure.Peer{IP: net.IPv4(192, 168, 1, 1), Port: 55556}
		//		peers[1] = structure.Peer{IP: net.IPv4(192, 168, 1, 2), Port: 55557}
		s.InitiateHandshakes(hash, peers)

		wg := sync.WaitGroup{}
		wg.Add(len(peers))
		for _, p := range peers {
			go func(pp structure.Peer) {
				c0 := mc.Conns[pp.AddrString()].Conn.(*MockConnection)
				hs, err := structure.ReadHandshake(bytes.NewReader(<-c0.ReceiveBytesChan))
				ctx.So(hs, ShouldNotBeNil)
				ctx.So(err, ShouldBeNil)

				hs, _ = structure.NewHandshake(hash, []byte(peerIDClient))
				t.Log("---TEST--- Send Handshake")
				c0.SendMessage(hs)

				bf := structure.BitFieldFromHexString("\xff\xff\xff\x01")
				msg0 := structure.NewBitFieldMessage(bf)
				c0.SendMessage(msg0)

				m, err := ReadMessageOrTimeout(c0, ctx)
				ctx.So(err, ShouldBeNil)
				ctx.So(m.GetType(), ShouldEqual, structure.MessageTypeInterested)

				// TODO: Check why byte equality doesn't work
				btc := s.LookupConn(pp.AddrString())
				t.Logf("BTC: %q\n", btc.BitField.String())
				t.Logf("BF : %q\n", bf.String())
				ctx.So(bf.String(), ShouldEqual, btc.BitField.String())

				ctx.So(btc.PeerChoking, ShouldBeTrue)
				msg1 := structure.NewUnchokeMessage()
				c0.SendMessage(msg1)

				m, err = ReadMessageOrTimeout(c0, ctx)
				ctx.So(btc.PeerChoking, ShouldBeFalse)
				ctx.So(err, ShouldBeNil)
				ctx.So(m.GetType(), ShouldEqual, structure.MessageTypeRequest)

				wg.Done()
			}(p)
		}
		wg.Wait()

		_ = s.StopListening()
	})
}