github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/http/stream_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net/netip"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/database64128/shadowsocks-go/conn"
     9  	"github.com/database64128/shadowsocks-go/direct"
    10  	"github.com/database64128/shadowsocks-go/pipe"
    11  	"github.com/database64128/shadowsocks-go/zerocopy"
    12  	"go.uber.org/zap/zaptest"
    13  )
    14  
    15  func TestHttpStreamReadWriter(t *testing.T) {
    16  	logger := zaptest.NewLogger(t)
    17  	defer logger.Sync()
    18  
    19  	pl, pr := pipe.NewDuplexPipe()
    20  
    21  	clientTargetAddr := conn.AddrFromIPPort(netip.AddrPortFrom(netip.IPv6Unspecified(), 53))
    22  
    23  	var (
    24  		c                *direct.DirectStreamReadWriter
    25  		s                *direct.DirectStreamReadWriter
    26  		serverTargetAddr conn.Addr
    27  		cerr, serr       error
    28  	)
    29  
    30  	var wg sync.WaitGroup
    31  	wg.Add(2)
    32  
    33  	go func() {
    34  		c, cerr = NewHttpStreamClientReadWriter(pl, clientTargetAddr)
    35  		wg.Done()
    36  	}()
    37  
    38  	go func() {
    39  		s, serverTargetAddr, serr = NewHttpStreamServerReadWriter(pr, logger)
    40  		wg.Done()
    41  	}()
    42  
    43  	wg.Wait()
    44  	if cerr != nil {
    45  		t.Fatal(cerr)
    46  	}
    47  	if serr != nil {
    48  		t.Fatal(serr)
    49  	}
    50  
    51  	if !clientTargetAddr.Equals(serverTargetAddr) {
    52  		t.Errorf("Target address mismatch: c: %s, s: %s", clientTargetAddr, serverTargetAddr)
    53  	}
    54  
    55  	zerocopy.ReadWriterTestFunc(t, c, s)
    56  }
    57  
    58  func testHostHeaderToDomainPort(t *testing.T, host, expectedDomain string, expectedPort uint16) {
    59  	addr, err := hostHeaderToAddr(host)
    60  	if err != nil {
    61  		t.Errorf("Failed to parse %s: %s", host, err)
    62  	}
    63  	if domain := addr.Domain(); domain != expectedDomain {
    64  		t.Errorf("Expected domain %s, got %s", expectedDomain, domain)
    65  	}
    66  	if port := addr.Port(); port != expectedPort {
    67  		t.Errorf("Expected port %d, got %d", expectedPort, port)
    68  	}
    69  }
    70  
    71  func testHostHeaderToIPPort(t *testing.T, host string, expectedAddrPort netip.AddrPort) {
    72  	addr, err := hostHeaderToAddr(host)
    73  	if err != nil {
    74  		t.Errorf("Failed to parse %s: %s", host, err)
    75  	}
    76  	if addrPort := addr.IPPort(); addrPort != expectedAddrPort {
    77  		t.Errorf("Expected addrPort %s, got %s", expectedAddrPort, addrPort)
    78  	}
    79  }
    80  
    81  func testHostHeaderToError(t *testing.T, host string, expectedErr error) {
    82  	_, err := hostHeaderToAddr(host)
    83  	if err != expectedErr {
    84  		t.Errorf("Expected error %s, got %s", expectedErr, err)
    85  	}
    86  }
    87  
    88  func TestHostHeaderToAddr(t *testing.T) {
    89  	testHostHeaderToDomainPort(t, "example.com", "example.com", 80)
    90  	testHostHeaderToDomainPort(t, "example.com:443", "example.com", 443)
    91  
    92  	addr4 := netip.AddrFrom4([4]byte{1, 1, 1, 1})
    93  	testHostHeaderToIPPort(t, "1.1.1.1", netip.AddrPortFrom(addr4, 80))
    94  	testHostHeaderToIPPort(t, "1.1.1.1:443", netip.AddrPortFrom(addr4, 443))
    95  
    96  	addr6 := netip.AddrFrom16([16]byte{0x26, 0x06, 0x47, 0x00, 0x47, 0x00, 14: 0x11, 15: 0x11})
    97  	testHostHeaderToIPPort(t, "[2606:4700:4700::1111]", netip.AddrPortFrom(addr6, 80))
    98  	testHostHeaderToIPPort(t, "[2606:4700:4700::1111]:443", netip.AddrPortFrom(addr6, 443))
    99  
   100  	testHostHeaderToError(t, "", errEmptyHostHeader)
   101  }