outbound_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package link
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestParseVmessLink(t *testing.T) {
  7. // vmess:// + base64 of:
  8. // {"v":"2","ps":"test","add":"1.2.3.4","port":443,"id":"uuid","aid":"0","net":"ws","type":"","host":"ex.com","path":"/","tls":"tls"}
  9. link := "vmess://eyJ2IjoiMiIsInBzIjoidGVzdCIsImFkZCI6IjEuMi4zLjQiLCJwb3J0Ijo0NDMsImlkIjoidXVpZCIsImFpZCI6IjAiLCJuZXQiOiJ3cyIsInR5cGUiOiIiLCJob3N0IjoiZXguY29tIiwicGF0aCI6Ii8iLCJ0bHMiOiJ0bHMifQ=="
  10. res, err := ParseLink(link)
  11. if err != nil {
  12. t.Fatalf("parse vmess: %v", err)
  13. }
  14. if res.Outbound["protocol"] != "vmess" {
  15. t.Errorf("expected vmess protocol, got %v", res.Outbound["protocol"])
  16. }
  17. if res.Outbound["tag"] != "test" {
  18. t.Errorf("expected tag 'test', got %v", res.Outbound["tag"])
  19. }
  20. }
  21. func TestParseVlessLink(t *testing.T) {
  22. link := "vless://[email protected]:443?type=ws&security=tls&path=/&host=ex.com#node1"
  23. res, err := ParseLink(link)
  24. if err != nil {
  25. t.Fatalf("parse vless: %v", err)
  26. }
  27. if res.Outbound["protocol"] != "vless" {
  28. t.Fatalf("bad protocol")
  29. }
  30. if res.Outbound["tag"] != "node1" {
  31. t.Errorf("tag mismatch: %v", res.Outbound["tag"])
  32. }
  33. }
  34. func TestParseSubscriptionBody_Base64(t *testing.T) {
  35. // base64 of the two joined links:
  36. // vless://u@h:443?type=tcp#A\nvless://u2@h2:443?type=tcp#B
  37. b64 := "dmxlc3M6Ly91QGg6NDQzP3R5cGU9dGNwI0EKdmxlc3M6Ly91MkBoMjo0NDM/dHlwZT10Y3AjQg=="
  38. obs, ids, err := ParseSubscriptionBody([]byte(b64))
  39. if err != nil {
  40. t.Fatalf("parse sub body: %v", err)
  41. }
  42. if len(obs) != 2 {
  43. t.Fatalf("expected 2 outbounds, got %d", len(obs))
  44. }
  45. if !strings.HasPrefix(ids[0], "vless:") || !strings.HasPrefix(ids[1], "vless:") {
  46. t.Errorf("bad identities: %v", ids)
  47. }
  48. }
  49. func TestSlugAndSuggest(t *testing.T) {
  50. if SlugRemark("Hello World!") != "hello-world" {
  51. t.Errorf("slug failed")
  52. }
  53. tag := SuggestTag("hk-", " SG 01 !! ", 0)
  54. if tag != "hk-sg-01" {
  55. t.Errorf("suggest tag got %q", tag)
  56. }
  57. // Non-ASCII letters/digits are preserved rather than stripped.
  58. if got := SlugRemark("Москва 🇷🇺 01"); got != "москва-01" {
  59. t.Errorf("unicode slug got %q", got)
  60. }
  61. if got := SuggestTag("ru-", "Сервер 2", 0); got != "ru-сервер-2" {
  62. t.Errorf("unicode suggest tag got %q", got)
  63. }
  64. }