udpproxy_direct_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //go:build !wasm
  2. // +build !wasm
  3. package vnet
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "net"
  9. "sync"
  10. "testing"
  11. "time"
  12. "github.com/pion/logging"
  13. )
  14. // The vnet client:
  15. //
  16. // 10.0.0.11:5787
  17. //
  18. // which proxy to real server:
  19. //
  20. // 192.168.1.10:8000
  21. //
  22. // We should get a reply if directly deliver to proxy.
  23. func TestUDPProxyDirectDeliverTypical(t *testing.T) {
  24. ctx, cancel := context.WithCancel(context.Background())
  25. var r0, r1, r2 error
  26. defer func() {
  27. if r0 != nil || r1 != nil || r2 != nil {
  28. t.Errorf("fail for ctx=%v, r0=%v, r1=%v, r2=%v", ctx.Err(), r0, r1, r2)
  29. }
  30. }()
  31. var wg sync.WaitGroup
  32. defer wg.Wait()
  33. // Timeout, fail
  34. wg.Add(1)
  35. go func() {
  36. defer wg.Done()
  37. defer cancel()
  38. select {
  39. case <-ctx.Done():
  40. case <-time.After(time.Duration(*testTimeout) * time.Millisecond):
  41. r2 = fmt.Errorf("timeout") // nolint:goerr113
  42. }
  43. }()
  44. // For utest, we always proxy vnet packets to the random port we listen to.
  45. mockServer := NewMockUDPEchoServer()
  46. wg.Add(1)
  47. go func() {
  48. defer wg.Done()
  49. defer cancel()
  50. if err := mockServer.doMockUDPServer(ctx); err != nil {
  51. r0 = err
  52. }
  53. }()
  54. // Create a vent and proxy.
  55. wg.Add(1)
  56. go func() {
  57. defer wg.Done()
  58. defer cancel()
  59. // When real server is ready, start the vnet test.
  60. select {
  61. case <-ctx.Done():
  62. return
  63. case <-mockServer.realServerReady.Done():
  64. }
  65. doVnetProxy := func() error {
  66. router, err := NewRouter(&RouterConfig{
  67. CIDR: "0.0.0.0/0",
  68. LoggerFactory: logging.NewDefaultLoggerFactory(),
  69. })
  70. if err != nil {
  71. return err
  72. }
  73. clientNetwork := NewNet(&NetConfig{
  74. StaticIP: "10.0.0.11",
  75. })
  76. if err = router.AddNet(clientNetwork); err != nil {
  77. return err
  78. }
  79. if err = router.Start(); err != nil {
  80. return err
  81. }
  82. defer router.Stop() // nolint:errcheck
  83. proxy, err := NewProxy(router)
  84. if err != nil {
  85. return err
  86. }
  87. defer proxy.Close() // nolint:errcheck
  88. // For utest, mock the target real server.
  89. proxy.mockRealServerAddr = mockServer.realServerAddr
  90. // The real server address to proxy to.
  91. // Note that for utest, we will proxy to a local address.
  92. serverAddr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8000")
  93. if err != nil {
  94. return err
  95. }
  96. if err = proxy.Proxy(clientNetwork, serverAddr); err != nil {
  97. return err
  98. }
  99. // Now, all packets from client, will be proxy to real server, vice versa.
  100. client, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5787")
  101. if err != nil {
  102. return err
  103. }
  104. // When system quit, interrupt client.
  105. selfKill, selfKillCancel := context.WithCancel(context.Background())
  106. go func() {
  107. <-ctx.Done()
  108. selfKillCancel()
  109. _ = client.Close()
  110. }()
  111. // Write by vnet client.
  112. if _, err = client.WriteTo([]byte("Hello"), serverAddr); err != nil {
  113. return err
  114. }
  115. buf := make([]byte, 1500)
  116. if n, addr, err := client.ReadFrom(buf); err != nil { // nolint:gocritic,govet
  117. if errors.Is(selfKill.Err(), context.Canceled) {
  118. return nil
  119. }
  120. return err
  121. } else if n != 5 || addr == nil {
  122. return fmt.Errorf("n=%v, addr=%v", n, addr) // nolint:goerr113
  123. } else if string(buf[:n]) != "Hello" { // nolint:goconst
  124. return fmt.Errorf("data %v", buf[:n]) // nolint:goerr113
  125. }
  126. // Directly write, simulate the ARQ packet.
  127. // We should got the echo packet also.
  128. if _, err = proxy.Deliver(client.LocalAddr(), serverAddr, []byte("Hello")); err != nil {
  129. return err
  130. }
  131. if n, addr, err := client.ReadFrom(buf); err != nil { // nolint:gocritic,govet
  132. if errors.Is(selfKill.Err(), context.Canceled) {
  133. return nil
  134. }
  135. return err
  136. } else if n != 5 || addr == nil {
  137. return fmt.Errorf("n=%v, addr=%v", n, addr) // nolint:goerr113
  138. } else if string(buf[:n]) != "Hello" {
  139. return fmt.Errorf("data %v", buf[:n]) // nolint:goerr113
  140. }
  141. return err
  142. }
  143. if err := doVnetProxy(); err != nil {
  144. r1 = err
  145. }
  146. }()
  147. }
  148. // Error if deliver to invalid address.
  149. func TestUDPProxyDirectDeliverBadcase(t *testing.T) {
  150. ctx, cancel := context.WithCancel(context.Background())
  151. var r0, r1, r2 error
  152. defer func() {
  153. if r0 != nil || r1 != nil || r2 != nil {
  154. t.Errorf("fail for ctx=%v, r0=%v, r1=%v, r2=%v", ctx.Err(), r0, r1, r2)
  155. }
  156. }()
  157. var wg sync.WaitGroup
  158. defer wg.Wait()
  159. // Timeout, fail
  160. wg.Add(1)
  161. go func() {
  162. defer wg.Done()
  163. defer cancel()
  164. select {
  165. case <-ctx.Done():
  166. case <-time.After(time.Duration(*testTimeout) * time.Millisecond):
  167. r2 = fmt.Errorf("timeout") // nolint:goerr113
  168. }
  169. }()
  170. // For utest, we always proxy vnet packets to the random port we listen to.
  171. mockServer := NewMockUDPEchoServer()
  172. wg.Add(1)
  173. go func() {
  174. defer wg.Done()
  175. defer cancel()
  176. if err := mockServer.doMockUDPServer(ctx); err != nil {
  177. r0 = err
  178. }
  179. }()
  180. // Create a vent and proxy.
  181. wg.Add(1)
  182. go func() {
  183. defer wg.Done()
  184. defer cancel()
  185. // When real server is ready, start the vnet test.
  186. select {
  187. case <-ctx.Done():
  188. return
  189. case <-mockServer.realServerReady.Done():
  190. }
  191. doVnetProxy := func() error {
  192. router, err := NewRouter(&RouterConfig{
  193. CIDR: "0.0.0.0/0",
  194. LoggerFactory: logging.NewDefaultLoggerFactory(),
  195. })
  196. if err != nil {
  197. return err
  198. }
  199. clientNetwork := NewNet(&NetConfig{
  200. StaticIP: "10.0.0.11",
  201. })
  202. if err = router.AddNet(clientNetwork); err != nil {
  203. return err
  204. }
  205. if err = router.Start(); err != nil {
  206. return err
  207. }
  208. defer router.Stop() // nolint:errcheck
  209. proxy, err := NewProxy(router)
  210. if err != nil {
  211. return err
  212. }
  213. defer proxy.Close() // nolint:errcheck
  214. // For utest, mock the target real server.
  215. proxy.mockRealServerAddr = mockServer.realServerAddr
  216. // The real server address to proxy to.
  217. // Note that for utest, we will proxy to a local address.
  218. serverAddr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8000")
  219. if err != nil {
  220. return err
  221. }
  222. if err = proxy.Proxy(clientNetwork, serverAddr); err != nil {
  223. return err
  224. }
  225. // Now, all packets from client, will be proxy to real server, vice versa.
  226. client, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5787")
  227. if err != nil {
  228. return err
  229. }
  230. // When system quit, interrupt client.
  231. selfKill, selfKillCancel := context.WithCancel(context.Background())
  232. go func() {
  233. <-ctx.Done()
  234. selfKillCancel()
  235. _ = client.Close()
  236. }()
  237. // Write by vnet client.
  238. if _, err = client.WriteTo([]byte("Hello"), serverAddr); err != nil {
  239. return err
  240. }
  241. buf := make([]byte, 1500)
  242. if n, addr, err := client.ReadFrom(buf); err != nil { // nolint:gocritic,govet
  243. if errors.Is(selfKill.Err(), context.Canceled) {
  244. return nil
  245. }
  246. return err
  247. } else if n != 5 || addr == nil {
  248. return fmt.Errorf("n=%v, addr=%v", n, addr) // nolint:goerr113
  249. } else if string(buf[:n]) != "Hello" { // nolint:goconst
  250. return fmt.Errorf("data %v", buf[:n]) // nolint:goerr113
  251. }
  252. // BadCase: Invalid address, error and ignore.
  253. tcpAddr, err := net.ResolveTCPAddr("tcp4", "192.168.1.10:8000")
  254. if err != nil {
  255. return err
  256. }
  257. if _, err = proxy.Deliver(tcpAddr, serverAddr, []byte("Hello")); err == nil {
  258. return fmt.Errorf("should err") // nolint:goerr113
  259. }
  260. // BadCase: Invalid target address, ignore.
  261. udpAddr, err := net.ResolveUDPAddr("udp4", "10.0.0.12:5788")
  262. if err != nil {
  263. return err
  264. }
  265. if nn, err := proxy.Deliver(udpAddr, serverAddr, []byte("Hello")); err != nil { // nolint:govet
  266. return err
  267. } else if nn != 0 {
  268. return fmt.Errorf("invalid %v", nn) // nolint:goerr113
  269. }
  270. // BadCase: Write on closed socket, error and ignore.
  271. proxy.workers.Range(func(key, value interface{}) bool {
  272. value.(*aUDPProxyWorker).endpoints.Range(func(key, value interface{}) bool {
  273. _ = value.(*net.UDPConn).Close()
  274. return true
  275. })
  276. return true
  277. })
  278. if _, err = proxy.Deliver(client.LocalAddr(), serverAddr, []byte("Hello")); err == nil {
  279. return fmt.Errorf("should error") // nolint:goerr113
  280. }
  281. return nil
  282. }
  283. if err := doVnetProxy(); err != nil {
  284. r1 = err
  285. }
  286. }()
  287. }