]> Sergey Matveev's repositories - btrtrc.git/blobdiff - tracker/udp_test.go
gorond test files
[btrtrc.git] / tracker / udp_test.go
index 56d358c569894205e83bfd2a95705e8e58d1307c..cf18e0de9f2dc20fc07f7f13fd9a6b99fc48fe03 100644 (file)
@@ -4,10 +4,8 @@ import (
        "bytes"
        "context"
        "crypto/rand"
-       "encoding/binary"
        "errors"
        "fmt"
-       "io"
        "io/ioutil"
        "net"
        "net/url"
@@ -17,77 +15,16 @@ import (
 
        "github.com/anacrolix/dht/v2/krpc"
        _ "github.com/anacrolix/envpprof"
-       "github.com/anacrolix/torrent/tracker/udp"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
+
+       "github.com/anacrolix/torrent/tracker/udp"
 )
 
 var trackers = []string{
-       "udp://tracker.coppersurfer.tk:6969",
-       "udp://tracker.leechers-paradise.org:6969",
-}
-
-// Ensure net.IPs are stored big-endian, to match the way they're read from
-// the wire.
-func TestNetIPv4Bytes(t *testing.T) {
-       ip := net.IP([]byte{127, 0, 0, 1})
-       if ip.String() != "127.0.0.1" {
-               t.FailNow()
-       }
-       if string(ip) != "\x7f\x00\x00\x01" {
-               t.Fatal([]byte(ip))
-       }
-}
-
-func TestMarshalAnnounceResponse(t *testing.T) {
-       peers := krpc.CompactIPv4NodeAddrs{
-               {[]byte{127, 0, 0, 1}, 2},
-               {[]byte{255, 0, 0, 3}, 4},
-       }
-       b, err := peers.MarshalBinary()
-       require.NoError(t, err)
-       require.EqualValues(t,
-               "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
-               b)
-       require.EqualValues(t, 12, binary.Size(udp.AnnounceResponseHeader{}))
-}
-
-// Failure to write an entire packet to UDP is expected to given an error.
-func TestLongWriteUDP(t *testing.T) {
-       t.Parallel()
-       l, err := net.ListenUDP("udp4", nil)
-       require.NoError(t, err)
-       defer l.Close()
-       c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer c.Close()
-       for msgLen := 1; ; msgLen *= 2 {
-               n, err := c.Write(make([]byte, msgLen))
-               if err != nil {
-                       require.Contains(t, err.Error(), "message too long")
-                       return
-               }
-               if n < msgLen {
-                       t.FailNow()
-               }
-       }
-}
-
-func TestShortBinaryRead(t *testing.T) {
-       var data udp.ResponseHeader
-       err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
-       if err != io.ErrUnexpectedEOF {
-               t.FailNow()
-       }
-}
-
-func TestConvertInt16ToInt(t *testing.T) {
-       i := 50000
-       if int(uint16(int16(i))) != 50000 {
-               t.FailNow()
-       }
+       "udp://tracker.opentrackr.org:1337/announce",
+       "udp://tracker.openbittorrent.com:6969/announce",
+       "udp://localhost:42069",
 }
 
 func TestAnnounceLocalhost(t *testing.T) {
@@ -105,7 +42,7 @@ func TestAnnounceLocalhost(t *testing.T) {
                },
        }
        var err error
-       srv.pc, err = net.ListenPacket("udp", ":0")
+       srv.pc, err = net.ListenPacket("udp", "localhost:0")
        require.NoError(t, err)
        defer srv.pc.Close()
        go func() {
@@ -157,7 +94,7 @@ func TestUDPTracker(t *testing.T) {
                t.Skip(err)
        }
        require.NoError(t, err)
-       t.Log(ar)
+       t.Logf("%+v", ar)
 }
 
 func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
@@ -208,7 +145,7 @@ func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
 
 // Check that URLPath option is done correctly.
 func TestURLPathOption(t *testing.T) {
-       conn, err := net.ListenUDP("udp", nil)
+       conn, err := net.ListenPacket("udp", "localhost:0")
        if err != nil {
                panic(err)
        }
@@ -226,6 +163,7 @@ func TestURLPathOption(t *testing.T) {
                announceErr <- err
        }()
        var b [512]byte
+       // conn.SetReadDeadline(time.Now().Add(time.Second))
        _, addr, _ := conn.ReadFrom(b[:])
        r := bytes.NewReader(b[:])
        var h udp.RequestHeader