feat(wip): test reworks for app

This commit is contained in:
Toby 2023-07-27 13:13:07 -07:00
parent ddc7fa8456
commit fb7e6ed915
10 changed files with 177 additions and 251 deletions

View File

@ -45,18 +45,32 @@ func initClientFlags() {
type clientConfig struct { type clientConfig struct {
Server string `mapstructure:"server"` Server string `mapstructure:"server"`
Auth string `mapstructure:"auth"` Auth string `mapstructure:"auth"`
Obfs struct { Obfs clientConfigObfs `mapstructure:"obfs"`
Type string `mapstructure:"type"` TLS clientConfigTLS `mapstructure:"tls"`
Salamander struct { QUIC clientConfigQUIC `mapstructure:"quic"`
Bandwidth clientConfigBandwidth `mapstructure:"bandwidth"`
FastOpen bool `mapstructure:"fastOpen"`
SOCKS5 *socks5Config `mapstructure:"socks5"`
HTTP *httpConfig `mapstructure:"http"`
Forwarding []forwardingEntry `mapstructure:"forwarding"`
}
type clientConfigObfsSalamander struct {
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
} `mapstructure:"salamander"` }
} `mapstructure:"obfs"`
TLS struct { type clientConfigObfs struct {
Type string `mapstructure:"type"`
Salamander clientConfigObfsSalamander `mapstructure:"salamander"`
}
type clientConfigTLS struct {
SNI string `mapstructure:"sni"` SNI string `mapstructure:"sni"`
Insecure bool `mapstructure:"insecure"` Insecure bool `mapstructure:"insecure"`
CA string `mapstructure:"ca"` CA string `mapstructure:"ca"`
} `mapstructure:"tls"` }
QUIC struct {
type clientConfigQUIC struct {
InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"` InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"`
MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"` MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"`
InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"` InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"`
@ -64,15 +78,11 @@ type clientConfig struct {
MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"` MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"`
KeepAlivePeriod time.Duration `mapstructure:"keepAlivePeriod"` KeepAlivePeriod time.Duration `mapstructure:"keepAlivePeriod"`
DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"` DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"`
} `mapstructure:"quic"` }
Bandwidth struct {
type clientConfigBandwidth struct {
Up string `mapstructure:"up"` Up string `mapstructure:"up"`
Down string `mapstructure:"down"` Down string `mapstructure:"down"`
} `mapstructure:"bandwidth"`
FastOpen bool `mapstructure:"fastOpen"`
SOCKS5 *socks5Config `mapstructure:"socks5"`
HTTP *httpConfig `mapstructure:"http"`
Forwarding []forwardingEntry `mapstructure:"forwarding"`
} }
type socks5Config struct { type socks5Config struct {

View File

@ -1,10 +1,11 @@
package cmd package cmd
import ( import (
"reflect"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -12,47 +13,25 @@ import (
func TestClientConfig(t *testing.T) { func TestClientConfig(t *testing.T) {
viper.SetConfigFile("client_test.yaml") viper.SetConfigFile("client_test.yaml")
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { assert.NoError(t, err)
t.Fatal("failed to read client config", err)
}
var config clientConfig var config clientConfig
if err := viper.Unmarshal(&config); err != nil { err = viper.Unmarshal(&config)
t.Fatal("failed to parse client config", err) assert.NoError(t, err)
} assert.Equal(t, config, clientConfig{
if !reflect.DeepEqual(config, clientConfig{
Server: "example.com", Server: "example.com",
Auth: "weak_ahh_password", Auth: "weak_ahh_password",
Obfs: struct { Obfs: clientConfigObfs{
Type string `mapstructure:"type"`
Salamander struct {
Password string `mapstructure:"password"`
} `mapstructure:"salamander"`
}{
Type: "salamander", Type: "salamander",
Salamander: struct { Salamander: clientConfigObfsSalamander{
Password string `mapstructure:"password"`
}{
Password: "cry_me_a_r1ver", Password: "cry_me_a_r1ver",
}, },
}, },
TLS: struct { TLS: clientConfigTLS{
SNI string `mapstructure:"sni"`
Insecure bool `mapstructure:"insecure"`
CA string `mapstructure:"ca"`
}{
SNI: "another.example.com", SNI: "another.example.com",
Insecure: true, Insecure: true,
CA: "custom_ca.crt", CA: "custom_ca.crt",
}, },
QUIC: struct { QUIC: clientConfigQUIC{
InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"`
MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"`
InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"`
MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"`
MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"`
KeepAlivePeriod time.Duration `mapstructure:"keepAlivePeriod"`
DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"`
}{
InitStreamReceiveWindow: 1145141, InitStreamReceiveWindow: 1145141,
MaxStreamReceiveWindow: 1145142, MaxStreamReceiveWindow: 1145142,
InitConnectionReceiveWindow: 1145143, InitConnectionReceiveWindow: 1145143,
@ -61,10 +40,7 @@ func TestClientConfig(t *testing.T) {
KeepAlivePeriod: 4 * time.Second, KeepAlivePeriod: 4 * time.Second,
DisablePathMTUDiscovery: true, DisablePathMTUDiscovery: true,
}, },
Bandwidth: struct { Bandwidth: clientConfigBandwidth{
Up string `mapstructure:"up"`
Down string `mapstructure:"down"`
}{
Up: "200 mbps", Up: "200 mbps",
Down: "1 gbps", Down: "1 gbps",
}, },
@ -94,9 +70,7 @@ func TestClientConfig(t *testing.T) {
UDPTimeout: 50 * time.Second, UDPTimeout: 50 * time.Second,
}, },
}, },
}) { })
t.Fatal("parsed client config is not equal to expected")
}
} }
// TestClientConfigURI tests URI-related functions of clientConfig // TestClientConfigURI tests URI-related functions of clientConfig
@ -120,24 +94,13 @@ func TestClientConfigURI(t *testing.T) {
config: &clientConfig{ config: &clientConfig{
Server: "noauth.com", Server: "noauth.com",
Auth: "", Auth: "",
Obfs: struct { Obfs: clientConfigObfs{
Type string `mapstructure:"type"`
Salamander struct {
Password string `mapstructure:"password"`
} `mapstructure:"salamander"`
}{
Type: "salamander", Type: "salamander",
Salamander: struct { Salamander: clientConfigObfsSalamander{
Password string `mapstructure:"password"`
}{
Password: "66ccff", Password: "66ccff",
}, },
}, },
TLS: struct { TLS: clientConfigTLS{
SNI string `mapstructure:"sni"`
Insecure bool `mapstructure:"insecure"`
CA string `mapstructure:"ca"`
}{
SNI: "crap.cc", SNI: "crap.cc",
Insecure: true, Insecure: true,
}, },
@ -158,19 +121,13 @@ func TestClientConfigURI(t *testing.T) {
t.Run(test.uri, func(t *testing.T) { t.Run(test.uri, func(t *testing.T) {
// Test parseURI // Test parseURI
nc := &clientConfig{Server: test.uri} nc := &clientConfig{Server: test.uri}
if ok := nc.parseURI(); ok != test.uriOK { assert.Equal(t, nc.parseURI(), test.uriOK)
t.Fatal("unexpected parseURI ok result") if test.uriOK {
} assert.Equal(t, nc, test.config)
if test.uriOK && !reflect.DeepEqual(nc, test.config) {
t.Fatal("unexpected parsed client config from URI")
} }
// Test URI generation // Test URI generation
if test.config == nil { if test.config != nil {
// config is nil if parseURI is expected to fail assert.Equal(t, test.config.URI(), test.uri)
return
}
if uri := test.config.URI(); uri != test.uri {
t.Fatalf("generated URI mismatch: %s != %s", uri, test.uri)
} }
}) })
} }

View File

@ -33,42 +33,23 @@ func init() {
type serverConfig struct { type serverConfig struct {
Listen string `mapstructure:"listen"` Listen string `mapstructure:"listen"`
Obfs struct { Obfs serverConfigObfs `mapstructure:"obfs"`
Type string `mapstructure:"type"`
Salamander struct {
Password string `mapstructure:"password"`
} `mapstructure:"salamander"`
} `mapstructure:"obfs"`
TLS *serverConfigTLS `mapstructure:"tls"` TLS *serverConfigTLS `mapstructure:"tls"`
ACME *serverConfigACME `mapstructure:"acme"` ACME *serverConfigACME `mapstructure:"acme"`
QUIC struct { QUIC serverConfigQUIC `mapstructure:"quic"`
InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"` Bandwidth serverConfigBandwidth `mapstructure:"bandwidth"`
MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"`
InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"`
MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"`
MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"`
MaxIncomingStreams int64 `mapstructure:"maxIncomingStreams"`
DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"`
} `mapstructure:"quic"`
Bandwidth struct {
Up string `mapstructure:"up"`
Down string `mapstructure:"down"`
} `mapstructure:"bandwidth"`
DisableUDP bool `mapstructure:"disableUDP"` DisableUDP bool `mapstructure:"disableUDP"`
Auth struct { Auth serverConfigAuth `mapstructure:"auth"`
Type string `mapstructure:"type"` Masquerade serverConfigMasquerade `mapstructure:"masquerade"`
}
type serverConfigObfsSalamander struct {
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
} `mapstructure:"auth"` }
Masquerade struct {
type serverConfigObfs struct {
Type string `mapstructure:"type"` Type string `mapstructure:"type"`
File struct { Salamander serverConfigObfsSalamander `mapstructure:"salamander"`
Dir string `mapstructure:"dir"`
} `mapstructure:"file"`
Proxy struct {
URL string `mapstructure:"url"`
RewriteHost bool `mapstructure:"rewriteHost"`
} `mapstructure:"proxy"`
} `mapstructure:"masquerade"`
} }
type serverConfigTLS struct { type serverConfigTLS struct {
@ -87,6 +68,41 @@ type serverConfigACME struct {
Dir string `mapstructure:"dir"` Dir string `mapstructure:"dir"`
} }
type serverConfigQUIC struct {
InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"`
MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"`
InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"`
MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"`
MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"`
MaxIncomingStreams int64 `mapstructure:"maxIncomingStreams"`
DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"`
}
type serverConfigBandwidth struct {
Up string `mapstructure:"up"`
Down string `mapstructure:"down"`
}
type serverConfigAuth struct {
Type string `mapstructure:"type"`
Password string `mapstructure:"password"`
}
type serverConfigMasqueradeFile struct {
Dir string `mapstructure:"dir"`
}
type serverConfigMasqueradeProxy struct {
URL string `mapstructure:"url"`
RewriteHost bool `mapstructure:"rewriteHost"`
}
type serverConfigMasquerade struct {
Type string `mapstructure:"type"`
File serverConfigMasqueradeFile `mapstructure:"file"`
Proxy serverConfigMasqueradeProxy `mapstructure:"proxy"`
}
func (c *serverConfig) fillConn(hyConfig *server.Config) error { func (c *serverConfig) fillConn(hyConfig *server.Config) error {
listenAddr := c.Listen listenAddr := c.Listen
if listenAddr == "" { if listenAddr == "" {
@ -348,8 +364,8 @@ func (l *serverLogger) TCPError(addr net.Addr, id, reqAddr string, err error) {
} }
} }
func (l *serverLogger) UDPRequest(addr net.Addr, id string, sessionID uint32) { func (l *serverLogger) UDPRequest(addr net.Addr, id string, sessionID uint32, reqAddr string) {
logger.Debug("UDP request", zap.String("addr", addr.String()), zap.String("id", id), zap.Uint32("sessionID", sessionID)) logger.Debug("UDP request", zap.String("addr", addr.String()), zap.String("id", id), zap.Uint32("sessionID", sessionID), zap.String("reqAddr", reqAddr))
} }
func (l *serverLogger) UDPError(addr net.Addr, id string, sessionID uint32, err error) { func (l *serverLogger) UDPError(addr net.Addr, id string, sessionID uint32, err error) {

View File

@ -1,10 +1,11 @@
package cmd package cmd
import ( import (
"reflect"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -12,25 +13,15 @@ import (
func TestServerConfig(t *testing.T) { func TestServerConfig(t *testing.T) {
viper.SetConfigFile("server_test.yaml") viper.SetConfigFile("server_test.yaml")
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { assert.NoError(t, err)
t.Fatal("failed to read server config", err)
}
var config serverConfig var config serverConfig
if err := viper.Unmarshal(&config); err != nil { err = viper.Unmarshal(&config)
t.Fatal("failed to parse server config", err) assert.NoError(t, err)
} assert.Equal(t, config, serverConfig{
if !reflect.DeepEqual(config, serverConfig{
Listen: ":8443", Listen: ":8443",
Obfs: struct { Obfs: serverConfigObfs{
Type string `mapstructure:"type"`
Salamander struct {
Password string `mapstructure:"password"`
} `mapstructure:"salamander"`
}{
Type: "salamander", Type: "salamander",
Salamander: struct { Salamander: serverConfigObfsSalamander{
Password string `mapstructure:"password"`
}{
Password: "cry_me_a_r1ver", Password: "cry_me_a_r1ver",
}, },
}, },
@ -51,15 +42,7 @@ func TestServerConfig(t *testing.T) {
AltTLSALPNPort: 9443, AltTLSALPNPort: 9443,
Dir: "random_dir", Dir: "random_dir",
}, },
QUIC: struct { QUIC: serverConfigQUIC{
InitStreamReceiveWindow uint64 `mapstructure:"initStreamReceiveWindow"`
MaxStreamReceiveWindow uint64 `mapstructure:"maxStreamReceiveWindow"`
InitConnectionReceiveWindow uint64 `mapstructure:"initConnReceiveWindow"`
MaxConnectionReceiveWindow uint64 `mapstructure:"maxConnReceiveWindow"`
MaxIdleTimeout time.Duration `mapstructure:"maxIdleTimeout"`
MaxIncomingStreams int64 `mapstructure:"maxIncomingStreams"`
DisablePathMTUDiscovery bool `mapstructure:"disablePathMTUDiscovery"`
}{
InitStreamReceiveWindow: 77881, InitStreamReceiveWindow: 77881,
MaxStreamReceiveWindow: 77882, MaxStreamReceiveWindow: 77882,
InitConnectionReceiveWindow: 77883, InitConnectionReceiveWindow: 77883,
@ -68,46 +51,24 @@ func TestServerConfig(t *testing.T) {
MaxIncomingStreams: 256, MaxIncomingStreams: 256,
DisablePathMTUDiscovery: true, DisablePathMTUDiscovery: true,
}, },
Bandwidth: struct { Bandwidth: serverConfigBandwidth{
Up string `mapstructure:"up"`
Down string `mapstructure:"down"`
}{
Up: "500 mbps", Up: "500 mbps",
Down: "100 mbps", Down: "100 mbps",
}, },
DisableUDP: true, DisableUDP: true,
Auth: struct { Auth: serverConfigAuth{
Type string `mapstructure:"type"`
Password string `mapstructure:"password"`
}{
Type: "password", Type: "password",
Password: "goofy_ahh_password", Password: "goofy_ahh_password",
}, },
Masquerade: struct { Masquerade: serverConfigMasquerade{
Type string `mapstructure:"type"`
File struct {
Dir string `mapstructure:"dir"`
} `mapstructure:"file"`
Proxy struct {
URL string `mapstructure:"url"`
RewriteHost bool `mapstructure:"rewriteHost"`
} `mapstructure:"proxy"`
}{
Type: "proxy", Type: "proxy",
File: struct { File: serverConfigMasqueradeFile{
Dir string `mapstructure:"dir"`
}{
Dir: "/www/masq", Dir: "/www/masq",
}, },
Proxy: struct { Proxy: serverConfigMasqueradeProxy{
URL string `mapstructure:"url"`
RewriteHost bool `mapstructure:"rewriteHost"`
}{
URL: "https://some.site.net", URL: "https://some.site.net",
RewriteHost: true, RewriteHost: true,
}, },
}, },
}) { })
t.Fatal("parsed server config is not equal to expected")
}
} }

View File

@ -1,49 +1,39 @@
package forwarding package forwarding
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"net" "net"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/apernet/hysteria/app/internal/utils_test" "github.com/apernet/hysteria/app/internal/utils_test"
) )
func TestTCPTunnel(t *testing.T) { func TestTCPTunnel(t *testing.T) {
// Start the tunnel // Start the tunnel
l, err := net.Listen("tcp", "127.0.0.1:34567")
assert.NoError(t, err)
defer l.Close()
tunnel := &TCPTunnel{ tunnel := &TCPTunnel{
HyClient: &utils_test.MockEchoHyClient{}, HyClient: &utils_test.MockEchoHyClient{},
Remote: "whatever",
} }
l, err := net.Listen("tcp", "127.0.0.1:34567")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tunnel.Serve(l) go tunnel.Serve(l)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", "127.0.0.1:34567") conn, err := net.Dial("tcp", "127.0.0.1:34567")
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
data := make([]byte, 1024) data := make([]byte, 1024)
_, _ = rand.Read(data) _, _ = rand.Read(data)
_, err = conn.Write(data) _, err = conn.Write(data)
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
recv := make([]byte, 1024) recv := make([]byte, 1024)
_, err = conn.Read(recv) _, err = conn.Read(recv)
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
if !bytes.Equal(data, recv) {
t.Fatalf("connection %d: data mismatch", i)
}
assert.Equal(t, data, recv)
_ = conn.Close() _ = conn.Close()
} }
} }

View File

@ -1,49 +1,39 @@
package forwarding package forwarding
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"net" "net"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/apernet/hysteria/app/internal/utils_test" "github.com/apernet/hysteria/app/internal/utils_test"
) )
func TestUDPTunnel(t *testing.T) { func TestUDPTunnel(t *testing.T) {
// Start the tunnel // Start the tunnel
l, err := net.ListenPacket("udp", "127.0.0.1:34567")
assert.NoError(t, err)
defer l.Close()
tunnel := &UDPTunnel{ tunnel := &UDPTunnel{
HyClient: &utils_test.MockEchoHyClient{}, HyClient: &utils_test.MockEchoHyClient{},
Remote: "whatever",
} }
l, err := net.ListenPacket("udp", "127.0.0.1:34567")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go tunnel.Serve(l) go tunnel.Serve(l)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
conn, err := net.Dial("udp", "127.0.0.1:34567") conn, err := net.Dial("udp", "127.0.0.1:34567")
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
data := make([]byte, 1024) data := make([]byte, 1024)
_, _ = rand.Read(data) _, _ = rand.Read(data)
_, err = conn.Write(data) _, err = conn.Write(data)
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
recv := make([]byte, 1024) recv := make([]byte, 1024)
_, err = conn.Read(recv) _, err = conn.Read(recv)
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
if !bytes.Equal(data, recv) {
t.Fatalf("connection %d: data mismatch", i)
}
assert.Equal(t, data, recv)
_ = conn.Close() _ = conn.Close()
} }
} }

View File

@ -5,8 +5,11 @@ import (
"net" "net"
"net/http" "net/http"
"os/exec" "os/exec"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/apernet/hysteria/core/client" "github.com/apernet/hysteria/core/client"
) )
@ -22,7 +25,6 @@ func (c *mockHyClient) TCP(addr string) (net.Conn, error) {
} }
func (c *mockHyClient) UDP() (client.HyUDPConn, error) { func (c *mockHyClient) UDP() (client.HyUDPConn, error) {
// Not implemented
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }
@ -32,14 +34,12 @@ func (c *mockHyClient) Close() error {
func TestServer(t *testing.T) { func TestServer(t *testing.T) {
// Start the server // Start the server
l, err := net.Listen("tcp", "127.0.0.1:18080")
assert.NoError(t, err)
defer l.Close()
s := &Server{ s := &Server{
HyClient: &mockHyClient{}, HyClient: &mockHyClient{},
} }
l, err := net.Listen("tcp", "127.0.0.1:18080")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go s.Serve(l) go s.Serve(l)
// Start a test HTTP & HTTPS server // Start a test HTTP & HTTPS server
@ -51,8 +51,9 @@ func TestServer(t *testing.T) {
// Run the Python test script // Run the Python test script
cmd := exec.Command("python", "server_test.py") cmd := exec.Command("python", "server_test.py")
// Suppress HTTPS warning text from Python
cmd.Env = append(cmd.Env, "PYTHONWARNINGS=ignore:Unverified HTTPS request")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { assert.NoError(t, err)
t.Fatalf("Failed to run test script: %v\n%s", err, out) assert.Equal(t, "OK", strings.TrimSpace(string(out)))
}
} }

View File

@ -1,24 +1,24 @@
import requests import requests
proxies = { proxies = {
'http': 'http://127.0.0.1:18080', "http": "http://127.0.0.1:18080",
'https': 'http://127.0.0.1:18080', "https": "http://127.0.0.1:18080",
} }
def test_http(it): def test_http(it):
for i in range(it): for i in range(it):
r = requests.get('http://127.0.0.1:18081', proxies=proxies) r = requests.get("http://127.0.0.1:18081", proxies=proxies)
assert r.status_code == 200 and r.text == 'control is an illusion' assert r.status_code == 200 and r.text == "control is an illusion"
def test_https(it): def test_https(it):
for i in range(it): for i in range(it):
r = requests.get('https://127.0.0.1:18082', r = requests.get("https://127.0.0.1:18082", proxies=proxies, verify=False)
proxies=proxies, verify=False) assert r.status_code == 200 and r.text == "control is an illusion"
assert r.status_code == 200 and r.text == 'control is an illusion'
if __name__ == '__main__': if __name__ == "__main__":
test_http(10) test_http(10)
test_https(10) test_https(10)
print("OK")

View File

@ -3,27 +3,27 @@ package socks5
import ( import (
"net" "net"
"os/exec" "os/exec"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/apernet/hysteria/app/internal/utils_test" "github.com/apernet/hysteria/app/internal/utils_test"
) )
func TestServer(t *testing.T) { func TestServer(t *testing.T) {
// Start the server // Start the server
l, err := net.Listen("tcp", "127.0.0.1:11080")
assert.NoError(t, err)
defer l.Close()
s := &Server{ s := &Server{
HyClient: &utils_test.MockEchoHyClient{}, HyClient: &utils_test.MockEchoHyClient{},
} }
l, err := net.Listen("tcp", "127.0.0.1:11080")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go s.Serve(l) go s.Serve(l)
// Run the Python test script // Run the Python test script
cmd := exec.Command("python", "server_test.py") cmd := exec.Command("python", "server_test.py")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { assert.NoError(t, err)
t.Fatalf("Failed to run test script: %v\n%s", err, out) assert.Equal(t, "OK", strings.TrimSpace(string(out)))
}
} }

View File

@ -54,3 +54,4 @@ if __name__ == "__main__":
test_tcp(1024, 1024, 10, domain=True) test_tcp(1024, 1024, 10, domain=True)
test_udp(1024, 1024, 10, domain=False) test_udp(1024, 1024, 10, domain=False)
test_udp(1024, 1024, 10, domain=True) test_udp(1024, 1024, 10, domain=True)
print("OK")