You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Implement handshake.New()
This commit is contained in:
@@ -12,6 +12,15 @@ type Handshake struct {
|
||||
PeerID [20]byte
|
||||
}
|
||||
|
||||
// New creates a new handshake with the standard pstr
|
||||
func New(infoHash, peerID [20]byte) *Handshake {
|
||||
return &Handshake{
|
||||
Pstr: "BitTorrent protocol",
|
||||
InfoHash: infoHash,
|
||||
PeerID: peerID,
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize serializes the handshake to a buffer
|
||||
func (h *Handshake) Serialize() []byte {
|
||||
pstrlen := len(h.Pstr)
|
||||
|
||||
@@ -7,6 +7,18 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
infoHash := [20]byte{134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116}
|
||||
peerID := [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
|
||||
h := New(infoHash, peerID)
|
||||
expected := &Handshake{
|
||||
Pstr: "BitTorrent protocol",
|
||||
InfoHash: [20]byte{134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116},
|
||||
PeerID: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
|
||||
}
|
||||
assert.Equal(t, expected, h)
|
||||
}
|
||||
|
||||
func TestSerialize(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
input *Handshake
|
||||
|
||||
@@ -100,11 +100,7 @@ func (p *Peer) connect(peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
||||
}
|
||||
|
||||
func (d *Downloader) handshake(conn net.Conn) (*handshake.Handshake, error) {
|
||||
req := handshake.Handshake{
|
||||
Pstr: "BitTorrent protocol",
|
||||
InfoHash: d.InfoHash,
|
||||
PeerID: d.PeerID,
|
||||
}
|
||||
req := handshake.New(d.InfoHash, d.PeerID)
|
||||
_, err := conn.Write(req.Serialize())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user