inbound_clients_tuic_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  7. )
  8. func TestBuildTargetClientFromSourceTuic(t *testing.T) {
  9. s := &InboundService{}
  10. source := model.Client{
  11. Email: "[email protected]",
  12. ID: "old-uuid",
  13. Password: "old-password",
  14. }
  15. targetInbound := &model.Inbound{
  16. Protocol: model.TUIC,
  17. }
  18. target, err := s.buildTargetClientFromSource(source, targetInbound, "[email protected]", "")
  19. if err != nil {
  20. t.Fatalf("buildTargetClientFromSource failed: %v", err)
  21. }
  22. if target.ID == "" || target.ID == "old-uuid" {
  23. t.Fatalf("expected new UUID for TUIC client, got %q", target.ID)
  24. }
  25. if target.Password == "" || target.Password == "old-password" {
  26. t.Fatalf("expected new password for TUIC client, got %q", target.Password)
  27. }
  28. }
  29. func TestAddInboundTuicClientValidation(t *testing.T) {
  30. setupConflictDB(t)
  31. s := &InboundService{}
  32. ib := &model.Inbound{
  33. Tag: "tuic-test-1",
  34. Protocol: model.TUIC,
  35. Settings: `{"clients":[{"id":"uuid-1","password":""}]}`,
  36. }
  37. _, _, err := s.AddInbound(ib)
  38. if err == nil || strings.TrimSpace(err.Error()) != "tuic client requires a password" {
  39. t.Fatalf("expected 'tuic client requires a password' error, got %v", err)
  40. }
  41. ibNoID := &model.Inbound{
  42. Tag: "tuic-test-2",
  43. Protocol: model.TUIC,
  44. Settings: `{"clients":[{"id":"","password":"pass"}]}`,
  45. }
  46. _, _, errNoID := s.AddInbound(ibNoID)
  47. if errNoID == nil || strings.TrimSpace(errNoID.Error()) != "empty client ID" {
  48. t.Fatalf("expected 'empty client ID' error, got %v", errNoID)
  49. }
  50. ibNoEmail := &model.Inbound{
  51. Tag: "tuic-test-3",
  52. Protocol: model.TUIC,
  53. Settings: `{"clients":[{"id":"uuid-3","password":"pass","email":""}]}`,
  54. }
  55. _, _, errNoEmail := s.AddInbound(ibNoEmail)
  56. if errNoEmail == nil || strings.TrimSpace(errNoEmail.Error()) != "empty client email" {
  57. t.Fatalf("expected 'empty client email' error, got %v", errNoEmail)
  58. }
  59. }
  60. func TestTuicClientMutationsDoNotRequireRestart(t *testing.T) {
  61. setupConflictDB(t)
  62. mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}})
  63. runtime.SetManager(mgr)
  64. t.Cleanup(func() { runtime.SetManager(nil) })
  65. inboundSvc := &InboundService{}
  66. clientSvc := &ClientService{}
  67. ib := &model.Inbound{
  68. Tag: "tuic-restart-test",
  69. Protocol: model.TUIC,
  70. Port: 54321,
  71. Enable: true,
  72. Settings: `{
  73. "certificate": "dummy-cert",
  74. "private_key": "dummy-key",
  75. "clients":[{"id":"a0000000-0000-0000-0000-000000000001","password":"p1","email":"[email protected]","enable":true}]
  76. }`,
  77. }
  78. created, _, err := inboundSvc.AddInbound(ib)
  79. if err != nil {
  80. t.Fatalf("AddInbound failed: %v", err)
  81. }
  82. addPayload := &model.Inbound{
  83. Id: created.Id,
  84. Settings: `{"clients":[{"id":"a0000000-0000-0000-0000-000000000002","password":"p2","email":"[email protected]","enable":true}]}`,
  85. }
  86. needRestart, err := clientSvc.AddInboundClient(inboundSvc, addPayload)
  87. if err != nil {
  88. t.Fatalf("AddInboundClient failed: %v", err)
  89. }
  90. if needRestart {
  91. t.Fatal("expected needRestart = false when adding TUIC client")
  92. }
  93. updatePayload := &model.Inbound{
  94. Id: created.Id,
  95. Settings: `{"clients":[{"id":"a0000000-0000-0000-0000-000000000002","password":"new-p2","email":"[email protected]","enable":true}]}`,
  96. }
  97. needRestart, err = clientSvc.UpdateInboundClient(inboundSvc, updatePayload, "[email protected]")
  98. if err != nil {
  99. t.Fatalf("UpdateInboundClient failed: %v", err)
  100. }
  101. if needRestart {
  102. t.Fatal("expected needRestart = false when updating TUIC client")
  103. }
  104. needRestart, err = clientSvc.DelInboundClientByEmail(inboundSvc, created.Id, "[email protected]", false, true)
  105. if err != nil {
  106. t.Fatalf("DelInboundClientByEmail failed: %v", err)
  107. }
  108. if needRestart {
  109. t.Fatal("expected needRestart = false when deleting TUIC client")
  110. }
  111. }