http_test.go 6.6 KB

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