github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/headers/http/http_test.go (about)

     1  package http_test
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"context"
     7  	"crypto/rand"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"v2ray.com/core/common"
    13  	"v2ray.com/core/common/buf"
    14  	"v2ray.com/core/common/net"
    15  	. "v2ray.com/core/transport/internet/headers/http"
    16  )
    17  
    18  func TestReaderWriter(t *testing.T) {
    19  	cache := buf.New()
    20  	b := buf.New()
    21  	common.Must2(b.WriteString("abcd" + ENDING))
    22  	writer := NewHeaderWriter(b)
    23  	err := writer.Write(cache)
    24  	common.Must(err)
    25  	if v := cache.Len(); v != 8 {
    26  		t.Error("cache len: ", v)
    27  	}
    28  	_, err = cache.Write([]byte{'e', 'f', 'g'})
    29  	common.Must(err)
    30  
    31  	reader := &HeaderReader{}
    32  	buffer, err := reader.Read(cache)
    33  	if err != nil && !strings.HasPrefix(err.Error(), "malformed HTTP request") {
    34  		t.Error("unknown error ", err)
    35  	}
    36  	_ = buffer
    37  	/*
    38  		if buffer.String() != "efg" {
    39  			t.Error("buffer: ", buffer.String())
    40  		}*/
    41  }
    42  
    43  func TestRequestHeader(t *testing.T) {
    44  	auth, err := NewHttpAuthenticator(context.Background(), &Config{
    45  		Request: &RequestConfig{
    46  			Uri: []string{"/"},
    47  			Header: []*Header{
    48  				{
    49  					Name:  "Test",
    50  					Value: []string{"Value"},
    51  				},
    52  			},
    53  		},
    54  	})
    55  	common.Must(err)
    56  
    57  	cache := buf.New()
    58  	err = auth.GetClientWriter().Write(cache)
    59  	common.Must(err)
    60  
    61  	if cache.String() != "GET / HTTP/1.1\r\nTest: Value\r\n\r\n" {
    62  		t.Error("cache: ", cache.String())
    63  	}
    64  }
    65  
    66  func TestLongRequestHeader(t *testing.T) {
    67  	payload := make([]byte, buf.Size+2)
    68  	common.Must2(rand.Read(payload[:buf.Size-2]))
    69  	copy(payload[buf.Size-2:], []byte(ENDING))
    70  	payload = append(payload, []byte("abcd")...)
    71  
    72  	reader := HeaderReader{}
    73  	b, err := reader.Read(bytes.NewReader(payload))
    74  
    75  	if err != nil && !(strings.HasPrefix(err.Error(), "invalid") || strings.HasPrefix(err.Error(), "malformed")) {
    76  		t.Error("unknown error ", err)
    77  	}
    78  	_ = b
    79  	/*
    80  		common.Must(err)
    81  		if b.String() != "abcd" {
    82  			t.Error("expect content abcd, but actually ", b.String())
    83  		}*/
    84  }
    85  
    86  func TestConnection(t *testing.T) {
    87  	auth, err := NewHttpAuthenticator(context.Background(), &Config{
    88  		Request: &RequestConfig{
    89  			Method: &Method{Value: "Post"},
    90  			Uri:    []string{"/testpath"},
    91  			Header: []*Header{
    92  				{
    93  					Name:  "Host",
    94  					Value: []string{"www.v2ray.com", "www.google.com"},
    95  				},
    96  				{
    97  					Name:  "User-Agent",
    98  					Value: []string{"Test-Agent"},
    99  				},
   100  			},
   101  		},
   102  		Response: &ResponseConfig{
   103  			Version: &Version{
   104  				Value: "1.1",
   105  			},
   106  			Status: &Status{
   107  				Code:   "404",
   108  				Reason: "Not Found",
   109  			},
   110  		},
   111  	})
   112  	common.Must(err)
   113  
   114  	listener, err := net.Listen("tcp", "127.0.0.1:0")
   115  	common.Must(err)
   116  
   117  	go func() {
   118  		conn, err := listener.Accept()
   119  		common.Must(err)
   120  		authConn := auth.Server(conn)
   121  		b := make([]byte, 256)
   122  		for {
   123  			n, err := authConn.Read(b)
   124  			if err != nil {
   125  				break
   126  			}
   127  			_, err = authConn.Write(b[:n])
   128  			common.Must(err)
   129  		}
   130  	}()
   131  
   132  	conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
   133  	common.Must(err)
   134  
   135  	authConn := auth.Client(conn)
   136  	defer authConn.Close()
   137  
   138  	authConn.Write([]byte("Test payload"))
   139  	authConn.Write([]byte("Test payload 2"))
   140  
   141  	expectedResponse := "Test payloadTest payload 2"
   142  	actualResponse := make([]byte, 256)
   143  	deadline := time.Now().Add(time.Second * 5)
   144  	totalBytes := 0
   145  	for {
   146  		n, err := authConn.Read(actualResponse[totalBytes:])
   147  		common.Must(err)
   148  		totalBytes += n
   149  		if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
   150  			break
   151  		}
   152  	}
   153  
   154  	if string(actualResponse[:totalBytes]) != expectedResponse {
   155  		t.Error("response: ", string(actualResponse[:totalBytes]))
   156  	}
   157  }
   158  
   159  func TestConnectionInvPath(t *testing.T) {
   160  	auth, err := NewHttpAuthenticator(context.Background(), &Config{
   161  		Request: &RequestConfig{
   162  			Method: &Method{Value: "Post"},
   163  			Uri:    []string{"/testpath"},
   164  			Header: []*Header{
   165  				{
   166  					Name:  "Host",
   167  					Value: []string{"www.v2ray.com", "www.google.com"},
   168  				},
   169  				{
   170  					Name:  "User-Agent",
   171  					Value: []string{"Test-Agent"},
   172  				},
   173  			},
   174  		},
   175  		Response: &ResponseConfig{
   176  			Version: &Version{
   177  				Value: "1.1",
   178  			},
   179  			Status: &Status{
   180  				Code:   "404",
   181  				Reason: "Not Found",
   182  			},
   183  		},
   184  	})
   185  	common.Must(err)
   186  
   187  	authR, err := NewHttpAuthenticator(context.Background(), &Config{
   188  		Request: &RequestConfig{
   189  			Method: &Method{Value: "Post"},
   190  			Uri:    []string{"/testpathErr"},
   191  			Header: []*Header{
   192  				{
   193  					Name:  "Host",
   194  					Value: []string{"www.v2ray.com", "www.google.com"},
   195  				},
   196  				{
   197  					Name:  "User-Agent",
   198  					Value: []string{"Test-Agent"},
   199  				},
   200  			},
   201  		},
   202  		Response: &ResponseConfig{
   203  			Version: &Version{
   204  				Value: "1.1",
   205  			},
   206  			Status: &Status{
   207  				Code:   "404",
   208  				Reason: "Not Found",
   209  			},
   210  		},
   211  	})
   212  	common.Must(err)
   213  
   214  	listener, err := net.Listen("tcp", "127.0.0.1:0")
   215  	common.Must(err)
   216  
   217  	go func() {
   218  		conn, err := listener.Accept()
   219  		common.Must(err)
   220  		authConn := auth.Server(conn)
   221  		b := make([]byte, 256)
   222  		for {
   223  			n, err := authConn.Read(b)
   224  			if err != nil {
   225  				authConn.Close()
   226  				break
   227  			}
   228  			_, err = authConn.Write(b[:n])
   229  			common.Must(err)
   230  		}
   231  	}()
   232  
   233  	conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
   234  	common.Must(err)
   235  
   236  	authConn := authR.Client(conn)
   237  	defer authConn.Close()
   238  
   239  	authConn.Write([]byte("Test payload"))
   240  	authConn.Write([]byte("Test payload 2"))
   241  
   242  	expectedResponse := "Test payloadTest payload 2"
   243  	actualResponse := make([]byte, 256)
   244  	deadline := time.Now().Add(time.Second * 5)
   245  	totalBytes := 0
   246  	for {
   247  		n, err := authConn.Read(actualResponse[totalBytes:])
   248  		if err == nil {
   249  			t.Error("Error Expected", err)
   250  		} else {
   251  			return
   252  		}
   253  		totalBytes += n
   254  		if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
   255  			break
   256  		}
   257  	}
   258  }
   259  
   260  func TestConnectionInvReq(t *testing.T) {
   261  	auth, err := NewHttpAuthenticator(context.Background(), &Config{
   262  		Request: &RequestConfig{
   263  			Method: &Method{Value: "Post"},
   264  			Uri:    []string{"/testpath"},
   265  			Header: []*Header{
   266  				{
   267  					Name:  "Host",
   268  					Value: []string{"www.v2ray.com", "www.google.com"},
   269  				},
   270  				{
   271  					Name:  "User-Agent",
   272  					Value: []string{"Test-Agent"},
   273  				},
   274  			},
   275  		},
   276  		Response: &ResponseConfig{
   277  			Version: &Version{
   278  				Value: "1.1",
   279  			},
   280  			Status: &Status{
   281  				Code:   "404",
   282  				Reason: "Not Found",
   283  			},
   284  		},
   285  	})
   286  	common.Must(err)
   287  
   288  	listener, err := net.Listen("tcp", "127.0.0.1:0")
   289  	common.Must(err)
   290  
   291  	go func() {
   292  		conn, err := listener.Accept()
   293  		common.Must(err)
   294  		authConn := auth.Server(conn)
   295  		b := make([]byte, 256)
   296  		for {
   297  			n, err := authConn.Read(b)
   298  			if err != nil {
   299  				authConn.Close()
   300  				break
   301  			}
   302  			_, err = authConn.Write(b[:n])
   303  			common.Must(err)
   304  		}
   305  	}()
   306  
   307  	conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
   308  	common.Must(err)
   309  
   310  	conn.Write([]byte("ABCDEFGHIJKMLN\r\n\r\n"))
   311  	l, _, err := bufio.NewReader(conn).ReadLine()
   312  	common.Must(err)
   313  	if !strings.HasPrefix(string(l), "HTTP/1.1 400 Bad Request") {
   314  		t.Error("Resp to non http conn", string(l))
   315  	}
   316  }