http_test.go 6.7 KB

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