httpupgrade_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package scenarios
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/v2fly/v2ray-core/v5/common"
  8. "github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
  9. _ "github.com/v2fly/v2ray-core/v5/main/distro/all"
  10. )
  11. func TestHTTPUpgrade(t *testing.T) {
  12. tcpServer := tcp.Server{
  13. MsgProcessor: xor,
  14. }
  15. dest, err := tcpServer.Start()
  16. common.Must(err)
  17. defer tcpServer.Close()
  18. coreInst, InstMgrIfce := NewInstanceManagerCoreInstance()
  19. defer coreInst.Close()
  20. common.Must(InstMgrIfce.AddInstance(
  21. context.TODO(),
  22. "httpupgrade_client",
  23. common.Must2(os.ReadFile("config/httpupgrade_client.json")).([]byte),
  24. "jsonv5"))
  25. common.Must(InstMgrIfce.AddInstance(
  26. context.TODO(),
  27. "httpupgrade_server",
  28. common.Must2(os.ReadFile("config/httpupgrade_server.json")).([]byte),
  29. "jsonv5"))
  30. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_server"))
  31. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_client"))
  32. defer func() {
  33. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_server"))
  34. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_client"))
  35. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_server"))
  36. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_client"))
  37. coreInst.Close()
  38. }()
  39. if err := testTCPConnViaSocks(17794, dest.Port, 1024, time.Second*2)(); err != nil {
  40. t.Error(err)
  41. }
  42. }
  43. func TestHTTPUpgradeWithEarlyData(t *testing.T) {
  44. tcpServer := tcp.Server{
  45. MsgProcessor: xor,
  46. }
  47. dest, err := tcpServer.Start()
  48. common.Must(err)
  49. defer tcpServer.Close()
  50. coreInst, InstMgrIfce := NewInstanceManagerCoreInstance()
  51. defer coreInst.Close()
  52. common.Must(InstMgrIfce.AddInstance(
  53. context.TODO(),
  54. "httpupgrade_client",
  55. common.Must2(os.ReadFile("config/httpupgrade_earlydata_client.json")).([]byte),
  56. "jsonv5"))
  57. common.Must(InstMgrIfce.AddInstance(
  58. context.TODO(),
  59. "httpupgrade_server",
  60. common.Must2(os.ReadFile("config/httpupgrade_earlydata_server.json")).([]byte),
  61. "jsonv5"))
  62. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_server"))
  63. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_client"))
  64. defer func() {
  65. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_server"))
  66. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_client"))
  67. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_server"))
  68. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_client"))
  69. coreInst.Close()
  70. }()
  71. if err := testTCPConnViaSocks(17794, dest.Port, 1024, time.Second*2)(); err != nil {
  72. t.Error(err)
  73. }
  74. }
  75. func TestHTTPUpgradeWithShortEarlyData(t *testing.T) {
  76. tcpServer := tcp.Server{
  77. MsgProcessor: xor,
  78. }
  79. dest, err := tcpServer.Start()
  80. common.Must(err)
  81. defer tcpServer.Close()
  82. coreInst, InstMgrIfce := NewInstanceManagerCoreInstance()
  83. defer coreInst.Close()
  84. common.Must(InstMgrIfce.AddInstance(
  85. context.TODO(),
  86. "httpupgrade_client",
  87. common.Must2(os.ReadFile("config/httpupgrade_earlydataShortEarlyData_client.json")).([]byte),
  88. "jsonv5"))
  89. common.Must(InstMgrIfce.AddInstance(
  90. context.TODO(),
  91. "httpupgrade_server",
  92. common.Must2(os.ReadFile("config/httpupgrade_earlydataShortEarlyData_server.json")).([]byte),
  93. "jsonv5"))
  94. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_server"))
  95. common.Must(InstMgrIfce.StartInstance(context.TODO(), "httpupgrade_client"))
  96. defer func() {
  97. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_server"))
  98. common.Must(InstMgrIfce.StopInstance(context.TODO(), "httpupgrade_client"))
  99. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_server"))
  100. common.Must(InstMgrIfce.UntrackInstance(context.TODO(), "httpupgrade_client"))
  101. coreInst.Close()
  102. }()
  103. if err := testTCPConnViaSocks(17794, dest.Port, 1024, time.Second*2)(); err != nil {
  104. t.Error(err)
  105. }
  106. }