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

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

View file

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

View file

@ -5,8 +5,11 @@ import (
"net"
"net/http"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"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) {
// Not implemented
return nil, errors.New("not implemented")
}
@ -32,14 +34,12 @@ func (c *mockHyClient) Close() error {
func TestServer(t *testing.T) {
// Start the server
l, err := net.Listen("tcp", "127.0.0.1:18080")
assert.NoError(t, err)
defer l.Close()
s := &Server{
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)
// Start a test HTTP & HTTPS server
@ -51,8 +51,9 @@ func TestServer(t *testing.T) {
// Run the Python test script
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()
if err != nil {
t.Fatalf("Failed to run test script: %v\n%s", err, out)
}
assert.NoError(t, err)
assert.Equal(t, "OK", strings.TrimSpace(string(out)))
}

View file

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

View file

@ -3,27 +3,27 @@ package socks5
import (
"net"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/apernet/hysteria/app/internal/utils_test"
)
func TestServer(t *testing.T) {
// Start the server
l, err := net.Listen("tcp", "127.0.0.1:11080")
assert.NoError(t, err)
defer l.Close()
s := &Server{
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)
// Run the Python test script
cmd := exec.Command("python", "server_test.py")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to run test script: %v\n%s", err, out)
}
assert.NoError(t, err)
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_udp(1024, 1024, 10, domain=False)
test_udp(1024, 1024, 10, domain=True)
print("OK")