http_test.go 6.4 KB

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