diff --git a/p2p/protocol/autonatv2/autonat.go b/p2p/protocol/autonatv2/autonat.go index b0cbef613d..f220950d86 100644 --- a/p2p/protocol/autonatv2/autonat.go +++ b/p2p/protocol/autonatv2/autonat.go @@ -32,6 +32,7 @@ const ( var ( ErrNoValidPeers = errors.New("no valid peers for autonat v2") + ErrDialRefused = errors.New("dial refused") ) var ( @@ -146,28 +147,28 @@ type Result struct { // CheckReachability makes a single dial request for checking reachability. For highPriorityAddrs dial charge is paid // if the server asks for it. For lowPriorityAddrs dial charge is rejected. -func (an *AutoNAT) CheckReachability(ctx context.Context, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) (*Result, error) { +func (an *AutoNAT) CheckReachability(ctx context.Context, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) (Result, error) { if !an.allowAllAddrs { for _, a := range highPriorityAddrs { if !manet.IsPublicAddr(a) { - return nil, fmt.Errorf("private address cannot be verified by autonatv2: %s", a) + return Result{}, fmt.Errorf("private address cannot be verified by autonatv2: %s", a) } } for _, a := range lowPriorityAddrs { if !manet.IsPublicAddr(a) { - return nil, fmt.Errorf("private address cannot be verified by autonatv2: %s", a) + return Result{}, fmt.Errorf("private address cannot be verified by autonatv2: %s", a) } } } p := an.peers.GetRand() if p == "" { - return nil, ErrNoValidPeers + return Result{}, ErrNoValidPeers } res, err := an.cli.CheckReachability(ctx, p, highPriorityAddrs, lowPriorityAddrs) if err != nil { log.Debugf("reachability check with %s failed, err: %s", p, err) - return nil, fmt.Errorf("reachability check with %s failed: %w", p, err) + return Result{}, fmt.Errorf("reachability check with %s failed: %w", p, err) } log.Debugf("reachability check with %s successful", p) return res, nil diff --git a/p2p/protocol/autonatv2/autonat_test.go b/p2p/protocol/autonatv2/autonat_test.go index 402f81d565..7972c78ba2 100644 --- a/p2p/protocol/autonatv2/autonat_test.go +++ b/p2p/protocol/autonatv2/autonat_test.go @@ -79,7 +79,7 @@ func identify(t *testing.T, cli *AutoNAT, srv *AutoNAT) { func TestAutoNATPrivateAddr(t *testing.T) { an := newAutoNAT(t, nil) res, err := an.CheckReachability(context.Background(), []ma.Multiaddr{ma.StringCast("/ip4/192.168.0.1/udp/10/quic-v1")}, nil) - require.Nil(t, res) + require.Equal(t, res, Result{}) require.NotNil(t, err) } @@ -116,7 +116,7 @@ func TestClientRequest(t *testing.T) { waitForPeer(t, an) res, err := an.CheckReachability(context.Background(), addrs[:1], addrs[1:]) - require.Nil(t, res) + require.Equal(t, res, Result{}) require.NotNil(t, err) require.True(t, gotReq.Load()) } @@ -161,7 +161,7 @@ func TestClientServerError(t *testing.T) { t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) { p.SetStreamHandler(DialProtocol, tc.handler) res, err := an.CheckReachability(context.Background(), addrs[:1], addrs[1:]) - require.Nil(t, res) + require.Equal(t, res, Result{}) require.NotNil(t, err) <-done }) @@ -252,7 +252,7 @@ func TestClientDataRequest(t *testing.T) { t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) { p.SetStreamHandler(DialProtocol, tc.handler) res, err := an.CheckReachability(context.Background(), addrs[:1], addrs[1:]) - require.Nil(t, res) + require.Equal(t, res, Result{}) require.NotNil(t, err) <-done }) @@ -475,7 +475,7 @@ func TestClientDialBacks(t *testing.T) { res, err := an.CheckReachability(context.Background(), addrs[:1], addrs[1:]) if !tc.success { if tc.isError { - require.Nil(t, res) + require.Equal(t, res, Result{}) require.Error(t, err) } else { require.NoError(t, err) diff --git a/p2p/protocol/autonatv2/client.go b/p2p/protocol/autonatv2/client.go index 08223d6df0..92c4f6d29f 100644 --- a/p2p/protocol/autonatv2/client.go +++ b/p2p/protocol/autonatv2/client.go @@ -36,23 +36,23 @@ func NewClient(h host.Host) *Client { // CheckReachability verifies address reachability with a AutoNAT v2 server p. It'll provide dial data for dialing high // priority addresses and not for low priority addresses. -func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) (*Result, error) { +func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr) (Result, error) { ctx, cancel := context.WithTimeout(ctx, streamTimeout) defer cancel() s, err := ac.host.NewStream(ctx, p, DialProtocol) if err != nil { - return nil, fmt.Errorf("open %s stream: %w", DialProtocol, err) + return Result{}, fmt.Errorf("open %s stream: %w", DialProtocol, err) } if err := s.Scope().SetService(ServiceName); err != nil { s.Reset() - return nil, fmt.Errorf("attach stream %s to service %s: %w", DialProtocol, ServiceName, err) + return Result{}, fmt.Errorf("attach stream %s to service %s: %w", DialProtocol, ServiceName, err) } if err := s.Scope().ReserveMemory(maxMsgSize, network.ReservationPriorityAlways); err != nil { s.Reset() - return nil, fmt.Errorf("failed to reserve memory for stream %s: %w", DialProtocol, err) + return Result{}, fmt.Errorf("failed to reserve memory for stream %s: %w", DialProtocol, err) } defer s.Scope().ReleaseMemory(maxMsgSize) @@ -74,13 +74,13 @@ func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriority w := pbio.NewDelimitedWriter(s) if err := w.WriteMsg(&msg); err != nil { s.Reset() - return nil, fmt.Errorf("dial request write: %w", err) + return Result{}, fmt.Errorf("dial request write: %w", err) } r := pbio.NewDelimitedReader(s, maxMsgSize) if err := r.ReadMsg(&msg); err != nil { s.Reset() - return nil, fmt.Errorf("dial msg read: %w", err) + return Result{}, fmt.Errorf("dial msg read: %w", err) } switch { @@ -89,36 +89,40 @@ func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriority case msg.GetDialDataRequest() != nil: if int(msg.GetDialDataRequest().AddrIdx) >= len(highPriorityAddrs) { s.Reset() - return nil, fmt.Errorf("dial data requested for low priority address") + return Result{}, fmt.Errorf("dial data requested for low priority address") } if msg.GetDialDataRequest().NumBytes > maxHandshakeSizeBytes { s.Reset() - return nil, fmt.Errorf("dial data requested too high: %d", msg.GetDialDataRequest().NumBytes) + return Result{}, fmt.Errorf("dial data requested too high: %d", msg.GetDialDataRequest().NumBytes) } if err := ac.sendDialData(msg.GetDialDataRequest(), w, &msg); err != nil { s.Reset() - return nil, fmt.Errorf("dial data send: %w", err) + return Result{}, fmt.Errorf("dial data send: %w", err) } if err := r.ReadMsg(&msg); err != nil { s.Reset() - return nil, fmt.Errorf("dial response read: %w", err) + return Result{}, fmt.Errorf("dial response read: %w", err) } if msg.GetDialResponse() == nil { s.Reset() - return nil, fmt.Errorf("invalid response type: %T", msg.Msg) + return Result{}, fmt.Errorf("invalid response type: %T", msg.Msg) } default: s.Reset() - return nil, fmt.Errorf("invalid msg type: %T", msg.Msg) + return Result{}, fmt.Errorf("invalid msg type: %T", msg.Msg) } resp := msg.GetDialResponse() if resp.GetStatus() != pbv2.DialResponse_ResponseStatus_OK { - return nil, fmt.Errorf("dial request failed: status %d %s", resp.GetStatus(), + // server couldn't dial any requested address + if resp.GetStatus() == pbv2.DialResponse_E_DIAL_REFUSED { + return Result{}, fmt.Errorf("dial request: %w", ErrDialRefused) + } + return Result{}, fmt.Errorf("dial request: status %d %s", resp.GetStatus(), pbv2.DialStatus_name[int32(resp.GetStatus())]) } - if resp.GetDialStatus() == pbv2.DialStatus_E_INTERNAL_ERROR { - return nil, fmt.Errorf("dial request failed: received invalid dial status 0") + if resp.GetDialStatus() == pbv2.DialStatus_UNUSED { + return Result{}, fmt.Errorf("dial request failed: received invalid dial status 0") } var dialBackAddr ma.Multiaddr @@ -135,12 +139,9 @@ func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriority return ac.newResults(resp, highPriorityAddrs, lowPriorityAddrs, dialBackAddr) } -func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr, dialBackAddr ma.Multiaddr) (*Result, error) { - if resp.DialStatus == pbv2.DialStatus_E_DIAL_REFUSED { - return &Result{Idx: -1, Reachability: network.ReachabilityUnknown, Status: pbv2.DialStatus_E_DIAL_REFUSED}, nil - } +func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr, dialBackAddr ma.Multiaddr) (Result, error) { if int(resp.AddrIdx) >= len(highPriorityAddrs)+len(lowPriorityAddrs) { - return nil, fmt.Errorf("addrIdx out of range: %d 0-%d", resp.AddrIdx, len(highPriorityAddrs)+len(lowPriorityAddrs)-1) + return Result{}, fmt.Errorf("addrIdx out of range: %d 0-%d", resp.AddrIdx, len(highPriorityAddrs)+len(lowPriorityAddrs)-1) } idx := int(resp.AddrIdx) @@ -163,7 +164,7 @@ func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Mul case pbv2.DialStatus_E_DIAL_ERROR: rch = network.ReachabilityPrivate } - return &Result{ + return Result{ Idx: idx, Addr: addr, Reachability: rch, diff --git a/p2p/protocol/autonatv2/pbv2/autonat.pb.go b/p2p/protocol/autonatv2/pbv2/autonat.pb.go index 18e9da0e74..67e08fef15 100644 --- a/p2p/protocol/autonatv2/pbv2/autonat.pb.go +++ b/p2p/protocol/autonatv2/pbv2/autonat.pb.go @@ -23,27 +23,24 @@ const ( type DialStatus int32 const ( - DialStatus_E_INTERNAL_ERROR DialStatus = 0 // Default value to force servers to explicitly set the status - DialStatus_E_DIAL_REFUSED DialStatus = 100 - DialStatus_E_DIAL_ERROR DialStatus = 101 - DialStatus_E_DIAL_BACK_ERROR DialStatus = 102 + DialStatus_UNUSED DialStatus = 0 + DialStatus_E_DIAL_ERROR DialStatus = 100 + DialStatus_E_DIAL_BACK_ERROR DialStatus = 101 DialStatus_OK DialStatus = 200 ) // Enum value maps for DialStatus. var ( DialStatus_name = map[int32]string{ - 0: "E_INTERNAL_ERROR", - 100: "E_DIAL_REFUSED", - 101: "E_DIAL_ERROR", - 102: "E_DIAL_BACK_ERROR", + 0: "UNUSED", + 100: "E_DIAL_ERROR", + 101: "E_DIAL_BACK_ERROR", 200: "OK", } DialStatus_value = map[string]int32{ - "E_INTERNAL_ERROR": 0, - "E_DIAL_REFUSED": 100, - "E_DIAL_ERROR": 101, - "E_DIAL_BACK_ERROR": 102, + "UNUSED": 0, + "E_DIAL_ERROR": 100, + "E_DIAL_BACK_ERROR": 101, "OK": 200, } ) @@ -80,6 +77,7 @@ type DialResponse_ResponseStatus int32 const ( DialResponse_E_INTERNAL_ERROR DialResponse_ResponseStatus = 0 DialResponse_E_REQUEST_REJECTED DialResponse_ResponseStatus = 100 + DialResponse_E_DIAL_REFUSED DialResponse_ResponseStatus = 101 DialResponse_ResponseStatus_OK DialResponse_ResponseStatus = 200 ) @@ -88,11 +86,13 @@ var ( DialResponse_ResponseStatus_name = map[int32]string{ 0: "E_INTERNAL_ERROR", 100: "E_REQUEST_REJECTED", + 101: "E_DIAL_REFUSED", 200: "ResponseStatus_OK", } DialResponse_ResponseStatus_value = map[string]int32{ "E_INTERNAL_ERROR": 0, "E_REQUEST_REJECTED": 100, + "E_DIAL_REFUSED": 101, "ResponseStatus_OK": 200, } ) @@ -403,7 +403,7 @@ func (x *DialResponse) GetDialStatus() DialStatus { if x != nil { return x.DialStatus } - return DialStatus_E_INTERNAL_ERROR + return DialStatus_UNUSED } type DialDataResponse struct { @@ -532,7 +532,7 @@ var file_pbv2_autonat_proto_rawDesc = []byte{ 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x49, 0x64, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x49, 0x64, 0x78, 0x12, 0x1a, 0x0a, 0x08, 0x6e, 0x75, 0x6d, 0x42, 0x79, 0x74, 0x65, 0x73, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x04, 0x52, 0x08, 0x6e, 0x75, 0x6d, 0x42, 0x79, 0x74, 0x65, 0x73, 0x22, 0x81, 0x02, + 0x01, 0x28, 0x04, 0x52, 0x08, 0x6e, 0x75, 0x6d, 0x42, 0x79, 0x74, 0x65, 0x73, 0x22, 0x95, 0x02, 0x0a, 0x0c, 0x44, 0x69, 0x61, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x43, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x2b, 0x2e, 0x61, 0x75, 0x74, 0x6f, 0x6e, 0x61, 0x74, 0x76, 0x32, 0x2e, 0x70, 0x62, 0x76, 0x32, 0x2e, @@ -543,24 +543,24 @@ var file_pbv2_autonat_proto_rawDesc = []byte{ 0x0a, 0x64, 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1a, 0x2e, 0x61, 0x75, 0x74, 0x6f, 0x6e, 0x61, 0x74, 0x76, 0x32, 0x2e, 0x70, 0x62, 0x76, 0x32, 0x2e, 0x44, 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x0a, 0x64, - 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x56, 0x0a, 0x0e, 0x52, 0x65, 0x73, + 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x6a, 0x0a, 0x0e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x14, 0x0a, 0x10, 0x45, 0x5f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x4e, 0x41, 0x4c, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x00, 0x12, 0x16, 0x0a, 0x12, 0x45, 0x5f, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x5f, 0x52, - 0x45, 0x4a, 0x45, 0x43, 0x54, 0x45, 0x44, 0x10, 0x64, 0x12, 0x16, 0x0a, 0x11, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x4f, 0x4b, 0x10, 0xc8, - 0x01, 0x22, 0x26, 0x0a, 0x10, 0x44, 0x69, 0x61, 0x6c, 0x44, 0x61, 0x74, 0x61, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x20, 0x0a, 0x08, 0x44, 0x69, 0x61, - 0x6c, 0x42, 0x61, 0x63, 0x6b, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x06, 0x52, 0x05, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x2a, 0x68, 0x0a, 0x0a, 0x44, - 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x14, 0x0a, 0x10, 0x45, 0x5f, 0x49, - 0x4e, 0x54, 0x45, 0x52, 0x4e, 0x41, 0x4c, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x00, 0x12, - 0x12, 0x0a, 0x0e, 0x45, 0x5f, 0x44, 0x49, 0x41, 0x4c, 0x5f, 0x52, 0x45, 0x46, 0x55, 0x53, 0x45, - 0x44, 0x10, 0x64, 0x12, 0x10, 0x0a, 0x0c, 0x45, 0x5f, 0x44, 0x49, 0x41, 0x4c, 0x5f, 0x45, 0x52, - 0x52, 0x4f, 0x52, 0x10, 0x65, 0x12, 0x15, 0x0a, 0x11, 0x45, 0x5f, 0x44, 0x49, 0x41, 0x4c, 0x5f, - 0x42, 0x41, 0x43, 0x4b, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x66, 0x12, 0x07, 0x0a, 0x02, - 0x4f, 0x4b, 0x10, 0xc8, 0x01, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x45, 0x4a, 0x45, 0x43, 0x54, 0x45, 0x44, 0x10, 0x64, 0x12, 0x12, 0x0a, 0x0e, 0x45, 0x5f, 0x44, + 0x49, 0x41, 0x4c, 0x5f, 0x52, 0x45, 0x46, 0x55, 0x53, 0x45, 0x44, 0x10, 0x65, 0x12, 0x16, 0x0a, + 0x11, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, + 0x4f, 0x4b, 0x10, 0xc8, 0x01, 0x22, 0x26, 0x0a, 0x10, 0x44, 0x69, 0x61, 0x6c, 0x44, 0x61, 0x74, + 0x61, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x20, 0x0a, + 0x08, 0x44, 0x69, 0x61, 0x6c, 0x42, 0x61, 0x63, 0x6b, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x6f, 0x6e, + 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x06, 0x52, 0x05, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x2a, + 0x4a, 0x0a, 0x0a, 0x44, 0x69, 0x61, 0x6c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x0a, 0x0a, + 0x06, 0x55, 0x4e, 0x55, 0x53, 0x45, 0x44, 0x10, 0x00, 0x12, 0x10, 0x0a, 0x0c, 0x45, 0x5f, 0x44, + 0x49, 0x41, 0x4c, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x64, 0x12, 0x15, 0x0a, 0x11, 0x45, + 0x5f, 0x44, 0x49, 0x41, 0x4c, 0x5f, 0x42, 0x41, 0x43, 0x4b, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, + 0x10, 0x65, 0x12, 0x07, 0x0a, 0x02, 0x4f, 0x4b, 0x10, 0xc8, 0x01, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( diff --git a/p2p/protocol/autonatv2/pbv2/autonat.proto b/p2p/protocol/autonatv2/pbv2/autonat.proto index f12c65e25d..6d13966d65 100644 --- a/p2p/protocol/autonatv2/pbv2/autonat.proto +++ b/p2p/protocol/autonatv2/pbv2/autonat.proto @@ -24,10 +24,9 @@ message DialDataRequest { enum DialStatus { - E_INTERNAL_ERROR = 0; // Default value to force servers to explicitly set the status - E_DIAL_REFUSED = 100; - E_DIAL_ERROR = 101; - E_DIAL_BACK_ERROR = 102; + UNUSED = 0; + E_DIAL_ERROR = 100; + E_DIAL_BACK_ERROR = 101; OK = 200; } @@ -36,6 +35,7 @@ message DialResponse { enum ResponseStatus { E_INTERNAL_ERROR = 0; E_REQUEST_REJECTED = 100; + E_DIAL_REFUSED = 101; ResponseStatus_OK = 200; } diff --git a/p2p/protocol/autonatv2/server.go b/p2p/protocol/autonatv2/server.go index a45bcf68dd..79a6656999 100644 --- a/p2p/protocol/autonatv2/server.go +++ b/p2p/protocol/autonatv2/server.go @@ -111,10 +111,7 @@ func (as *Server) handleDialRequest(s network.Stream) { msg = pbv2.Message{ Msg: &pbv2.Message_DialResponse{ DialResponse: &pbv2.DialResponse{ - Status: pbv2.DialResponse_ResponseStatus_OK, - DialStatus: pbv2.DialStatus_E_DIAL_REFUSED, - // send an invalid index to prevent accidental misuse - AddrIdx: uint32(len(msg.GetDialRequest().Addrs)), + Status: pbv2.DialResponse_E_DIAL_REFUSED, }, }, } diff --git a/p2p/protocol/autonatv2/server_test.go b/p2p/protocol/autonatv2/server_test.go index 94d2d71273..d6f53f0efb 100644 --- a/p2p/protocol/autonatv2/server_test.go +++ b/p2p/protocol/autonatv2/server_test.go @@ -28,9 +28,8 @@ func TestServerAllAddrsInvalid(t *testing.T) { identify(t, c, an) res, err := c.CheckReachability(context.Background(), c.host.Addrs(), nil) - require.NoError(t, err) - require.Equal(t, res.Reachability, network.ReachabilityUnknown) - require.Equal(t, res.Status, pbv2.DialStatus_E_DIAL_REFUSED) + require.ErrorIs(t, err, ErrDialRefused) + require.Equal(t, Result{}, res) } func TestServerPrivateRejected(t *testing.T) { @@ -46,9 +45,8 @@ func TestServerPrivateRejected(t *testing.T) { identify(t, c, an) res, err := c.CheckReachability(context.Background(), c.host.Addrs(), nil) - require.NoError(t, err) - require.Equal(t, res.Status, pbv2.DialStatus_E_DIAL_REFUSED) - require.Equal(t, res.Reachability, network.ReachabilityUnknown) + require.ErrorIs(t, err, ErrDialRefused) + require.Equal(t, Result{}, res) } func TestServerDataRequest(t *testing.T) { @@ -87,12 +85,12 @@ func TestServerDataRequest(t *testing.T) { res, err := c.CheckReachability(context.Background(), []ma.Multiaddr{quicAddr}, []ma.Multiaddr{tcpAddr}) require.NoError(t, err) - require.Equal(t, res, &Result{ + require.Equal(t, Result{ Idx: 0, Addr: quicAddr, Reachability: network.ReachabilityPublic, Status: pbv2.DialStatus_OK, - }) + }, res) } func TestServerDial(t *testing.T) { @@ -110,7 +108,7 @@ func TestServerDial(t *testing.T) { hostAddrs := c.host.Addrs() res, err := c.CheckReachability(context.Background(), []ma.Multiaddr{randAddr}, hostAddrs) require.NoError(t, err) - require.Equal(t, &Result{ + require.Equal(t, Result{ Idx: 0, Addr: randAddr, Reachability: network.ReachabilityPrivate, @@ -119,7 +117,7 @@ func TestServerDial(t *testing.T) { res, err = c.CheckReachability(context.Background(), nil, c.host.Addrs()) require.NoError(t, err) - require.Equal(t, &Result{ + require.Equal(t, Result{ Idx: 0, Addr: hostAddrs[0], Reachability: network.ReachabilityPublic,