api_reverse_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package xray
  2. import (
  3. "testing"
  4. "github.com/xtls/xray-core/proxy/vless"
  5. "google.golang.org/protobuf/proto"
  6. )
  7. // typedReverse stands in for model.ClientReverse, which this package cannot
  8. // import (model imports xray); it marshals to the same {"tag":"…"} shape.
  9. type typedReverse struct {
  10. Tag string `json:"tag"`
  11. }
  12. // A reverse client's account has to carry its tag: removing the user drops the
  13. // outbound handler, and only the tag lets the core rebuild it (GetReverse).
  14. func TestBuildUserAccountCarriesTheReverseTag(t *testing.T) {
  15. cases := []struct {
  16. name string
  17. reverse any
  18. want string
  19. }{
  20. {"settings json shape", map[string]any{"tag": "portal"}, "portal"},
  21. {"typed client field", &typedReverse{Tag: "portal"}, "portal"},
  22. {"blank tag", map[string]any{"tag": ""}, ""},
  23. {"typed nil", (*typedReverse)(nil), ""},
  24. {"absent", nil, ""},
  25. }
  26. for _, tc := range cases {
  27. t.Run(tc.name, func(t *testing.T) {
  28. user := map[string]any{
  29. "email": "[email protected]",
  30. "id": "5f2eb9d6-3a2f-4a55-9812-6ea1e2f7a333",
  31. "reverse": tc.reverse,
  32. }
  33. tm, err := buildUserAccount("vless", user)
  34. if err != nil {
  35. t.Fatalf("buildUserAccount: %v", err)
  36. }
  37. if tm == nil {
  38. t.Fatal("buildUserAccount returned no account for vless")
  39. }
  40. account := new(vless.Account)
  41. if err := proto.Unmarshal(tm.Value, account); err != nil {
  42. t.Fatalf("unmarshal vless account: %v", err)
  43. }
  44. if got := account.GetReverse().GetTag(); got != tc.want {
  45. t.Fatalf("the account carries reverse tag %q, want %q", got, tc.want)
  46. }
  47. })
  48. }
  49. }