sockopt_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package internet_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/v2fly/v2ray-core/v5/common"
  7. "github.com/v2fly/v2ray-core/v5/common/buf"
  8. "github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
  9. . "github.com/v2fly/v2ray-core/v5/transport/internet"
  10. )
  11. func TestTCPFastOpen(t *testing.T) {
  12. tcpServer := tcp.Server{
  13. MsgProcessor: func(b []byte) []byte {
  14. return b
  15. },
  16. }
  17. dest, err := tcpServer.StartContext(context.Background(), &SocketConfig{Tfo: SocketConfig_Enable})
  18. common.Must(err)
  19. defer tcpServer.Close()
  20. ctx := context.Background()
  21. dialer := DefaultSystemDialer{}
  22. conn, err := dialer.Dial(ctx, nil, dest, &SocketConfig{
  23. Tfo: SocketConfig_Enable,
  24. })
  25. common.Must(err)
  26. defer conn.Close()
  27. _, err = conn.Write([]byte("abcd"))
  28. common.Must(err)
  29. b := buf.New()
  30. common.Must2(b.ReadFrom(conn))
  31. if r := cmp.Diff(b.Bytes(), []byte("abcd")); r != "" {
  32. t.Fatal(r)
  33. }
  34. }
  35. // Currently, Multipath TCP is only supported on Linux.
  36. // We test the Multipath TCP Settings on other platforms for ensure code will not have any negative impact on other platforms.
  37. func TestMultipathTCP(t *testing.T) {
  38. tcpServer := tcp.Server{
  39. MsgProcessor: func(b []byte) []byte {
  40. return b
  41. },
  42. }
  43. dest, err := tcpServer.StartContext(context.Background(), &SocketConfig{Mptcp: MPTCPState_Enable})
  44. common.Must(err)
  45. defer tcpServer.Close()
  46. ctx := context.Background()
  47. dialer := DefaultSystemDialer{}
  48. conn, err := dialer.Dial(ctx, nil, dest, &SocketConfig{
  49. Mptcp: MPTCPState_Enable,
  50. })
  51. common.Must(err)
  52. defer conn.Close()
  53. _, err = conn.Write([]byte("abcd"))
  54. common.Must(err)
  55. b := buf.New()
  56. common.Must2(b.ReadFrom(conn))
  57. if r := cmp.Diff(b.Bytes(), []byte("abcd")); r != "" {
  58. t.Fatal(r)
  59. }
  60. }