golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/internal/sockstest/server_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sockstest
     6  
     7  import (
     8  	"net"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"golang.org/x/net/internal/socks"
    13  )
    14  
    15  func TestParseAuthRequest(t *testing.T) {
    16  	for i, tt := range []struct {
    17  		wire []byte
    18  		req  *AuthRequest
    19  	}{
    20  		{
    21  			[]byte{0x05, 0x00},
    22  			&AuthRequest{
    23  				socks.Version5,
    24  				nil,
    25  			},
    26  		},
    27  		{
    28  			[]byte{0x05, 0x01, 0xff},
    29  			&AuthRequest{
    30  				socks.Version5,
    31  				[]socks.AuthMethod{
    32  					socks.AuthMethodNoAcceptableMethods,
    33  				},
    34  			},
    35  		},
    36  		{
    37  			[]byte{0x05, 0x02, 0x00, 0xff},
    38  			&AuthRequest{
    39  				socks.Version5,
    40  				[]socks.AuthMethod{
    41  					socks.AuthMethodNotRequired,
    42  					socks.AuthMethodNoAcceptableMethods,
    43  				},
    44  			},
    45  		},
    46  
    47  		// corrupted requests
    48  		{nil, nil},
    49  		{[]byte{0x00, 0x01}, nil},
    50  		{[]byte{0x06, 0x00}, nil},
    51  		{[]byte{0x05, 0x02, 0x00}, nil},
    52  	} {
    53  		req, err := ParseAuthRequest(tt.wire)
    54  		if !reflect.DeepEqual(req, tt.req) {
    55  			t.Errorf("#%d: got %v, %v; want %v", i, req, err, tt.req)
    56  			continue
    57  		}
    58  	}
    59  }
    60  
    61  func TestParseCmdRequest(t *testing.T) {
    62  	for i, tt := range []struct {
    63  		wire []byte
    64  		req  *CmdRequest
    65  	}{
    66  		{
    67  			[]byte{0x05, 0x01, 0x00, 0x01, 192, 0, 2, 1, 0x17, 0x4b},
    68  			&CmdRequest{
    69  				socks.Version5,
    70  				socks.CmdConnect,
    71  				socks.Addr{
    72  					IP:   net.IP{192, 0, 2, 1},
    73  					Port: 5963,
    74  				},
    75  			},
    76  		},
    77  		{
    78  			[]byte{0x05, 0x01, 0x00, 0x03, 0x04, 'F', 'Q', 'D', 'N', 0x17, 0x4b},
    79  			&CmdRequest{
    80  				socks.Version5,
    81  				socks.CmdConnect,
    82  				socks.Addr{
    83  					Name: "FQDN",
    84  					Port: 5963,
    85  				},
    86  			},
    87  		},
    88  
    89  		// corrupted requests
    90  		{nil, nil},
    91  		{[]byte{0x05}, nil},
    92  		{[]byte{0x06, 0x01, 0x00, 0x01, 192, 0, 2, 2, 0x17, 0x4b}, nil},
    93  		{[]byte{0x05, 0x01, 0xff, 0x01, 192, 0, 2, 3}, nil},
    94  		{[]byte{0x05, 0x01, 0x00, 0x01, 192, 0, 2, 4}, nil},
    95  		{[]byte{0x05, 0x01, 0x00, 0x03, 0x04, 'F', 'Q', 'D', 'N'}, nil},
    96  	} {
    97  		req, err := ParseCmdRequest(tt.wire)
    98  		if !reflect.DeepEqual(req, tt.req) {
    99  			t.Errorf("#%d: got %v, %v; want %v", i, req, err, tt.req)
   100  			continue
   101  		}
   102  	}
   103  }