| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package http_test
- import (
- "bufio"
- "bytes"
- "context"
- "crypto/rand"
- "strings"
- "testing"
- "time"
- "v2ray.com/core/common"
- "v2ray.com/core/common/buf"
- "v2ray.com/core/common/net"
- . "v2ray.com/core/transport/internet/headers/http"
- )
- func TestReaderWriter(t *testing.T) {
- cache := buf.New()
- b := buf.New()
- common.Must2(b.WriteString("abcd" + ENDING))
- writer := NewHeaderWriter(b)
- err := writer.Write(cache)
- common.Must(err)
- if v := cache.Len(); v != 8 {
- t.Error("cache len: ", v)
- }
- _, err = cache.Write([]byte{'e', 'f', 'g'})
- common.Must(err)
- reader := &HeaderReader{}
- _, err = reader.Read(cache)
- if err != nil && !strings.HasPrefix(err.Error(), "malformed HTTP request") {
- t.Error("unknown error ", err)
- }
- }
- func TestRequestHeader(t *testing.T) {
- auth, err := NewAuthenticator(context.Background(), &Config{
- Request: &RequestConfig{
- Uri: []string{"/"},
- Header: []*Header{
- {
- Name: "Test",
- Value: []string{"Value"},
- },
- },
- },
- })
- common.Must(err)
- cache := buf.New()
- err = auth.GetClientWriter().Write(cache)
- common.Must(err)
- if cache.String() != "GET / HTTP/1.1\r\nTest: Value\r\n\r\n" {
- t.Error("cache: ", cache.String())
- }
- }
- 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)
- payload = append(payload, []byte("abcd")...)
- reader := HeaderReader{}
- _, err := reader.Read(bytes.NewReader(payload))
- if err != nil && !(strings.HasPrefix(err.Error(), "invalid") || strings.HasPrefix(err.Error(), "malformed")) {
- t.Error("unknown error ", err)
- }
- }
- func TestConnection(t *testing.T) {
- auth, err := NewAuthenticator(context.Background(), &Config{
- Request: &RequestConfig{
- Method: &Method{Value: "Post"},
- Uri: []string{"/testpath"},
- Header: []*Header{
- {
- Name: "Host",
- Value: []string{"www.v2ray.com", "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 {
- break
- }
- _, err = authConn.Write(b[:n])
- common.Must(err)
- }
- }()
- conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
- common.Must(err)
- authConn := auth.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:])
- common.Must(err)
- totalBytes += n
- if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
- break
- }
- }
- if string(actualResponse[:totalBytes]) != expectedResponse {
- t.Error("response: ", string(actualResponse[:totalBytes]))
- }
- }
- func TestConnectionInvPath(t *testing.T) {
- auth, err := NewAuthenticator(context.Background(), &Config{
- Request: &RequestConfig{
- Method: &Method{Value: "Post"},
- Uri: []string{"/testpath"},
- Header: []*Header{
- {
- Name: "Host",
- Value: []string{"www.v2ray.com", "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",
- Value: []string{"www.v2ray.com", "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:])
- 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",
- Value: []string{"www.v2ray.com", "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))
- }
- }
|