]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client_test.go
Rename tracker/http package
[btrtrc.git] / client_test.go
index a385521a1f89258e776bdddecbbb6d47254fa535..65182d58be84bfe5a880ab2071f2525e5dfee851 100644 (file)
@@ -4,6 +4,8 @@ import (
        "encoding/binary"
        "fmt"
        "io"
+       "net"
+       "net/netip"
        "os"
        "path/filepath"
        "reflect"
@@ -15,6 +17,8 @@ import (
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
 
+       "github.com/anacrolix/log"
+
        "github.com/anacrolix/dht/v2"
        "github.com/anacrolix/missinggo/v2"
        "github.com/anacrolix/missinggo/v2/filecache"
@@ -489,8 +493,7 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
                }
        }()
        for !reflect.DeepEqual(completes, expected) {
-               _v := <-psc.Values
-               v := _v.(PieceStateChange)
+               v := <-psc.Values
                completes[v.Index] = v.Complete
        }
 }
@@ -741,7 +744,7 @@ func TestObfuscatedHeaderFallbackSeederRequiresLeecherPrefersNot(t *testing.T) {
 }
 
 func TestClientAddressInUse(t *testing.T) {
-       s, _ := NewUtpSocket("udp", ":50007", nil)
+       s, _ := NewUtpSocket("udp", ":50007", nil, log.Default)
        if s != nil {
                defer s.Close()
        }
@@ -772,3 +775,75 @@ func TestClientDisabledImplicitNetworksButDhtEnabled(t *testing.T) {
        assert.Empty(t, cl.listeners)
        assert.NotEmpty(t, cl.DhtServers())
 }
+
+func TestBadPeerIpPort(t *testing.T) {
+       for _, tc := range []struct {
+               title      string
+               ip         net.IP
+               port       int
+               expectedOk bool
+               setup      func(*Client)
+       }{
+               {"empty both", nil, 0, true, func(*Client) {}},
+               {"empty/nil ip", nil, 6666, true, func(*Client) {}},
+               {
+                       "empty port",
+                       net.ParseIP("127.0.0.1/32"),
+                       0, true,
+                       func(*Client) {},
+               },
+               {
+                       "in doppleganger addresses",
+                       net.ParseIP("127.0.0.1/32"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               cl.dopplegangerAddrs["10.0.0.1:2322"] = struct{}{}
+                       },
+               },
+               {
+                       "in IP block list",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               cl.ipBlockList = iplist.New([]iplist.Range{
+                                       iplist.Range{First: net.ParseIP("10.0.0.1"), Last: net.ParseIP("10.0.0.255")},
+                               })
+                       },
+               },
+               {
+                       "in bad peer IPs",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       true,
+                       func(cl *Client) {
+                               ipAddr, ok := netip.AddrFromSlice(net.ParseIP("10.0.0.1"))
+                               require.True(t, ok)
+                               cl.badPeerIPs = map[netip.Addr]struct{}{}
+                               cl.badPeerIPs[ipAddr] = struct{}{}
+                       },
+               },
+               {
+                       "good",
+                       net.ParseIP("10.0.0.1"),
+                       2322,
+                       false,
+                       func(cl *Client) {},
+               },
+       } {
+               t.Run(tc.title, func(t *testing.T) {
+                       cfg := TestingConfig(t)
+                       cfg.DisableTCP = true
+                       cfg.DisableUTP = true
+                       cfg.NoDHT = false
+                       cl, err := NewClient(cfg)
+                       require.NoError(t, err)
+                       defer cl.Close()
+
+                       tc.setup(cl)
+                       require.Equal(t, tc.expectedOk, cl.badPeerIPPort(tc.ip, tc.port))
+               })
+       }
+
+}