1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 06:25:24 +00:00
v2fly/transport/internet/headers/http/http_test.go

306 lines
6.4 KiB
Go
Raw Normal View History

2016-11-04 20:59:19 +00:00
package http_test
import (
"bufio"
2018-11-07 16:16:57 +00:00
"bytes"
2017-01-13 12:47:44 +00:00
"context"
2018-11-07 16:16:57 +00:00
"crypto/rand"
"strings"
2016-11-04 20:59:19 +00:00
"testing"
2016-12-16 15:52:11 +00:00
"time"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
. "github.com/v2fly/v2ray-core/v5/transport/internet/headers/http"
2016-11-04 20:59:19 +00:00
)
func TestReaderWriter(t *testing.T) {
2016-12-09 11:08:25 +00:00
cache := buf.New()
2018-08-16 10:05:33 +00:00
b := buf.New()
2018-11-02 20:34:04 +00:00
common.Must2(b.WriteString("abcd" + ENDING))
2016-12-06 10:03:42 +00:00
writer := NewHeaderWriter(b)
2016-12-16 15:52:11 +00:00
err := writer.Write(cache)
2019-02-02 21:19:30 +00:00
common.Must(err)
2019-02-03 18:46:53 +00:00
if v := cache.Len(); v != 8 {
t.Error("cache len: ", v)
}
2016-12-16 15:52:11 +00:00
_, err = cache.Write([]byte{'e', 'f', 'g'})
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-11-04 20:59:19 +00:00
reader := &HeaderReader{}
2020-11-19 17:02:52 +00:00
_, err = reader.Read(cache)
if err != nil && !strings.HasPrefix(err.Error(), "malformed HTTP request") {
t.Error("unknown error ", err)
2019-02-03 18:46:53 +00:00
}
2016-11-04 20:59:19 +00:00
}
2016-11-07 09:47:30 +00:00
func TestRequestHeader(t *testing.T) {
auth, err := NewAuthenticator(context.Background(), &Config{
2016-11-07 09:47:30 +00:00
Request: &RequestConfig{
Uri: []string{"/"},
Header: []*Header{
{
Name: "Test",
Value: []string{"Value"},
},
},
},
2017-01-13 12:47:44 +00:00
})
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-11-07 09:47:30 +00:00
2016-12-09 11:08:25 +00:00
cache := buf.New()
2017-01-13 12:47:44 +00:00
err = auth.GetClientWriter().Write(cache)
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-11-07 09:47:30 +00:00
2019-02-03 18:46:53 +00:00
if cache.String() != "GET / HTTP/1.1\r\nTest: Value\r\n\r\n" {
t.Error("cache: ", cache.String())
}
2016-11-07 09:47:30 +00:00
}
2016-12-16 15:52:11 +00:00
2018-11-07 16:16:57 +00:00
func TestLongRequestHeader(t *testing.T) {
payload := make([]byte, buf.Size+2)
common.Must2(rand.Read(payload[:buf.Size-2]))
copy(payload[buf.Size-2:], ENDING)
2018-11-07 16:16:57 +00:00
payload = append(payload, []byte("abcd")...)
reader := HeaderReader{}
2020-11-19 17:02:52 +00:00
_, err := reader.Read(bytes.NewReader(payload))
if err != nil && !(strings.HasPrefix(err.Error(), "invalid") || strings.HasPrefix(err.Error(), "malformed")) {
t.Error("unknown error ", err)
2018-11-07 16:16:57 +00:00
}
}
2016-12-16 15:52:11 +00:00
func TestConnection(t *testing.T) {
auth, err := NewAuthenticator(context.Background(), &Config{
2017-12-03 22:41:01 +00:00
Request: &RequestConfig{
Method: &Method{Value: "Post"},
Uri: []string{"/testpath"},
Header: []*Header{
{
Name: "Host",
2021-02-16 20:31:50 +00:00
Value: []string{"www.v2fly.org", "www.google.com"},
2017-12-03 22:41:01 +00:00
},
{
Name: "User-Agent",
Value: []string{"Test-Agent"},
},
},
},
Response: &ResponseConfig{
Version: &Version{
Value: "1.1",
},
Status: &Status{
Code: "404",
Reason: "Not Found",
},
},
})
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
go func() {
conn, err := listener.Accept()
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
2019-02-03 18:46:53 +00:00
if err != nil {
break
}
2016-12-16 15:52:11 +00:00
_, err = authConn.Write(b[:n])
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
authConn := auth.Client(conn)
2019-02-03 18:46:53 +00:00
defer authConn.Close()
2016-12-16 15:52:11 +00:00
authConn.Write([]byte("Test payload"))
authConn.Write([]byte("Test payload 2"))
expectedResponse := "Test payloadTest payload 2"
actualResponse := make([]byte, 256)
deadline := time.Now().Add(time.Second * 5)
totalBytes := 0
for {
n, err := authConn.Read(actualResponse[totalBytes:])
2019-02-02 21:19:30 +00:00
common.Must(err)
2016-12-16 15:52:11 +00:00
totalBytes += n
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
break
}
}
2019-02-03 18:46:53 +00:00
if string(actualResponse[:totalBytes]) != expectedResponse {
t.Error("response: ", string(actualResponse[:totalBytes]))
}
2016-12-16 15:52:11 +00:00
}
func TestConnectionInvPath(t *testing.T) {
auth, err := NewAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Method: &Method{Value: "Post"},
Uri: []string{"/testpath"},
Header: []*Header{
{
Name: "Host",
2021-02-16 20:31:50 +00:00
Value: []string{"www.v2fly.org", "www.google.com"},
},
{
Name: "User-Agent",
Value: []string{"Test-Agent"},
},
},
},
Response: &ResponseConfig{
Version: &Version{
Value: "1.1",
},
Status: &Status{
Code: "404",
Reason: "Not Found",
},
},
})
common.Must(err)
authR, err := NewAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Method: &Method{Value: "Post"},
Uri: []string{"/testpathErr"},
Header: []*Header{
{
Name: "Host",
2021-02-16 20:31:50 +00:00
Value: []string{"www.v2fly.org", "www.google.com"},
},
{
Name: "User-Agent",
Value: []string{"Test-Agent"},
},
},
},
Response: &ResponseConfig{
Version: &Version{
Value: "1.1",
},
Status: &Status{
Code: "404",
Reason: "Not Found",
},
},
})
common.Must(err)
listener, err := net.Listen("tcp", "127.0.0.1:0")
common.Must(err)
go func() {
conn, err := listener.Accept()
common.Must(err)
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
if err != nil {
authConn.Close()
break
}
_, err = authConn.Write(b[:n])
common.Must(err)
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
common.Must(err)
authConn := authR.Client(conn)
defer authConn.Close()
authConn.Write([]byte("Test payload"))
authConn.Write([]byte("Test payload 2"))
expectedResponse := "Test payloadTest payload 2"
actualResponse := make([]byte, 256)
deadline := time.Now().Add(time.Second * 5)
totalBytes := 0
for {
n, err := authConn.Read(actualResponse[totalBytes:])
2020-06-03 08:32:58 +00:00
if err == nil {
t.Error("Error Expected", err)
} else {
return
}
totalBytes += n
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
break
}
}
}
func TestConnectionInvReq(t *testing.T) {
auth, err := NewAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Method: &Method{Value: "Post"},
Uri: []string{"/testpath"},
Header: []*Header{
{
Name: "Host",
2021-02-16 20:31:50 +00:00
Value: []string{"www.v2fly.org", "www.google.com"},
},
{
Name: "User-Agent",
Value: []string{"Test-Agent"},
},
},
},
Response: &ResponseConfig{
Version: &Version{
Value: "1.1",
},
Status: &Status{
Code: "404",
Reason: "Not Found",
},
},
})
common.Must(err)
listener, err := net.Listen("tcp", "127.0.0.1:0")
common.Must(err)
go func() {
conn, err := listener.Accept()
common.Must(err)
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
if err != nil {
authConn.Close()
break
}
_, err = authConn.Write(b[:n])
common.Must(err)
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
common.Must(err)
conn.Write([]byte("ABCDEFGHIJKMLN\r\n\r\n"))
l, _, err := bufio.NewReader(conn).ReadLine()
common.Must(err)
if !strings.HasPrefix(string(l), "HTTP/1.1 400 Bad Request") {
t.Error("Resp to non http conn", string(l))
}
}