http_test.go 6.6 KB

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