outbound_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }