service_property_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package sub
  2. import (
  3. "net"
  4. "net/url"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "pgregory.net/rapid"
  9. )
  10. // TestProp_JoinHostPort_Bracketing asserts the RFC-3986 authority contract for any
  11. // host/port: SplitHostPort must recover the (un-bracketed) host and the exact port,
  12. // and an IPv6 literal is bracketed exactly once regardless of input brackets.
  13. func TestProp_JoinHostPort_Bracketing(t *testing.T) {
  14. hosts := []string{
  15. "1.2.3.4", "example.com", "sub.host.test",
  16. "2001:db8::1", "[2001:db8::1]", "::1", "[::1]", "fe80::1%eth0",
  17. }
  18. rapid.Check(t, func(t *rapid.T) {
  19. host := rapid.SampledFrom(hosts).Draw(t, "host")
  20. port := rapid.IntRange(0, 65535).Draw(t, "port")
  21. out := joinHostPort(host, port)
  22. gotHost, gotPort, err := net.SplitHostPort(out)
  23. if err != nil {
  24. t.Fatalf("SplitHostPort(%q) failed: %v", out, err)
  25. }
  26. wantHost := strings.Trim(host, "[]")
  27. if gotHost != wantHost {
  28. t.Fatalf("host round-trip: joinHostPort(%q,%d)=%q -> host %q, want %q", host, port, out, gotHost, wantHost)
  29. }
  30. if gotPort != strconv.Itoa(port) {
  31. t.Fatalf("port round-trip: got %q, want %d (out=%q)", gotPort, port, out)
  32. }
  33. // An IPv6 literal (contains a colon in the host) must be bracketed once.
  34. if strings.Contains(wantHost, ":") {
  35. if strings.Count(out, "[") != 1 || strings.Count(out, "]") != 1 {
  36. t.Fatalf("IPv6 host not bracketed exactly once: %q", out)
  37. }
  38. }
  39. })
  40. }
  41. // TestProp_EncodeUserinfo_RoundTrip asserts encodeUserinfo produces a userinfo token
  42. // that net/url parses back to the original password for ANY input — the contract that
  43. // trojan/ss links rely on. A field-mapping mutant that mangles escaping breaks this.
  44. func TestProp_EncodeUserinfo_RoundTrip(t *testing.T) {
  45. rapid.Check(t, func(t *rapid.T) {
  46. pw := rapid.String().Draw(t, "pw")
  47. raw := "trojan://" + encodeUserinfo(pw) + "@example.com:443"
  48. u, err := url.Parse(raw)
  49. if err != nil {
  50. t.Fatalf("url.Parse(%q) failed for pw=%q: %v", raw, pw, err)
  51. }
  52. if got := u.User.Username(); got != pw {
  53. t.Fatalf("userinfo round-trip mismatch: pw=%q got=%q", pw, got)
  54. }
  55. })
  56. }
  57. // TestProp_SplitLinkLines_Invariants asserts splitLinkLines never emits empty or
  58. // untrimmed lines, and that re-splitting its own joined output is a fixed point.
  59. func TestProp_SplitLinkLines_Invariants(t *testing.T) {
  60. rapid.Check(t, func(t *rapid.T) {
  61. raw := rapid.String().Draw(t, "raw")
  62. out := splitLinkLines(raw)
  63. for i, line := range out {
  64. if line == "" {
  65. t.Fatalf("splitLinkLines emitted an empty line at %d for %q", i, raw)
  66. }
  67. if line != strings.TrimSpace(line) {
  68. t.Fatalf("splitLinkLines emitted an untrimmed line %q", line)
  69. }
  70. }
  71. rejoined := splitLinkLines(strings.Join(out, "\n"))
  72. if len(rejoined) != len(out) {
  73. t.Fatalf("not a fixed point: %d -> %d lines", len(out), len(rejoined))
  74. }
  75. for i := range out {
  76. if rejoined[i] != out[i] {
  77. t.Fatalf("fixed-point mismatch at %d: %q vs %q", i, out[i], rejoined[i])
  78. }
  79. }
  80. })
  81. }