From e4cd3e5306c57ea97edb15b0cae2a06273f739fe Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Sun, 27 Aug 2023 00:35:45 +0800 Subject: [PATCH] Chore: remove darwin code on test because orbstack can run it --- test/clash_test.go | 22 ++++--------- test/docker_test.go | 5 +-- test/go.mod | 2 +- test/util_darwin_test.go | 70 ---------------------------------------- test/util_other_test.go | 12 ------- 5 files changed, 9 insertions(+), 102 deletions(-) delete mode 100644 test/util_darwin_test.go delete mode 100644 test/util_other_test.go diff --git a/test/clash_test.go b/test/clash_test.go index 79437c2..e1c1c79 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -8,9 +8,9 @@ import ( "fmt" "io" "net" + "net/netip" "os" "path/filepath" - "runtime" "sync" "testing" "time" @@ -39,7 +39,7 @@ const ( var ( waitTime = time.Second - localIP = net.ParseIP("127.0.0.1") + localIP = netip.MustParseAddr("127.0.0.1") defaultExposedPorts = nat.PortSet{ "10002/tcp": struct{}{}, @@ -53,7 +53,6 @@ var ( {HostPort: "10002", HostIP: "0.0.0.0"}, }, } - isDarwin = runtime.GOOS == "darwin" ) func init() { @@ -64,13 +63,6 @@ func init() { homeDir := filepath.Join(currentDir, "config") C.SetHomeDir(homeDir) - if isDarwin { - localIP, err = defaultRouteIP() - if err != nil { - panic(err) - } - } - c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) @@ -302,7 +294,7 @@ func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error { require.NoError(t, err) defer l.Close() - rAddr := &net.UDPAddr{IP: localIP, Port: 10001} + rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001} pingCh, pongCh, test := newPingPongPair() go func() { @@ -437,7 +429,7 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { require.NoError(t, err) defer l.Close() - rAddr := &net.UDPAddr{IP: localIP, Port: 10001} + rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001} times := 50 chunkSize := int64(1024) @@ -570,7 +562,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { pc, err := proxy.ListenPacketContext(context.Background(), &C.Metadata{ NetWork: C.UDP, - DstIP: localIP, + DstIP: localIP.AsSlice(), DstPort: 10001, }) require.NoError(t, err) @@ -580,7 +572,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{ NetWork: C.UDP, - DstIP: localIP, + DstIP: localIP.AsSlice(), DstPort: 10001, }) require.NoError(t, err) @@ -590,7 +582,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{ NetWork: C.UDP, - DstIP: localIP, + DstIP: localIP.AsSlice(), DstPort: 10001, }) require.NoError(t, err) diff --git a/test/docker_test.go b/test/docker_test.go index 8513dde..d9ff2b4 100644 --- a/test/docker_test.go +++ b/test/docker_test.go @@ -15,10 +15,7 @@ func startContainer(cfg *container.Config, hostCfg *container.HostConfig, name s } defer c.Close() - if !isDarwin { - hostCfg.NetworkMode = "host" - } - + hostCfg.NetworkMode = "host" container, err := c.ContainerCreate(context.Background(), cfg, hostCfg, nil, nil, name) if err != nil { return "", err diff --git a/test/go.mod b/test/go.mod index 4d3dc7e..507b93c 100644 --- a/test/go.mod +++ b/test/go.mod @@ -9,7 +9,6 @@ require ( github.com/miekg/dns v1.1.55 github.com/stretchr/testify v1.8.4 go.uber.org/automaxprocs v1.5.3 - golang.org/x/net v0.14.0 ) replace github.com/Dreamacro/clash => ../ @@ -48,6 +47,7 @@ require ( golang.org/x/crypto v0.12.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/mod v0.8.0 // indirect + golang.org/x/net v0.14.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/text v0.12.0 // indirect diff --git a/test/util_darwin_test.go b/test/util_darwin_test.go deleted file mode 100644 index ab9b8b2..0000000 --- a/test/util_darwin_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "net" - "syscall" - - "golang.org/x/net/route" -) - -func defaultRouteIP() (net.IP, error) { - idx, err := defaultRouteInterfaceIndex() - if err != nil { - return nil, err - } - iface, err := net.InterfaceByIndex(idx) - if err != nil { - return nil, err - } - addrs, err := iface.Addrs() - if err != nil { - return nil, err - } - for _, addr := range addrs { - ip := addr.(*net.IPNet).IP - if ip.To4() != nil { - return ip, nil - } - } - - return nil, errors.New("no ipv4 addr") -} - -func defaultRouteInterfaceIndex() (int, error) { - rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_DUMP2, 0) - if err != nil { - return 0, fmt.Errorf("route.FetchRIB: %w", err) - } - msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib) - if err != nil { - return 0, fmt.Errorf("route.ParseRIB: %w", err) - } - for _, message := range msgs { - routeMessage := message.(*route.RouteMessage) - if routeMessage.Flags&(syscall.RTF_UP|syscall.RTF_GATEWAY|syscall.RTF_STATIC) == 0 { - continue - } - - addresses := routeMessage.Addrs - - destination, ok := addresses[0].(*route.Inet4Addr) - if !ok { - continue - } - - if destination.IP != [4]byte{0, 0, 0, 0} { - continue - } - - switch addresses[1].(type) { - case *route.Inet4Addr: - return routeMessage.Index, nil - default: - continue - } - } - - return 0, fmt.Errorf("ambiguous gateway interfaces found") -} diff --git a/test/util_other_test.go b/test/util_other_test.go deleted file mode 100644 index 708b609..0000000 --- a/test/util_other_test.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build !darwin - -package main - -import ( - "errors" - "net" -) - -func defaultRouteIP() (net.IP, error) { - return nil, errors.New("not supported") -}