1
0

reality_scan_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "crypto/tls"
  4. "testing"
  5. )
  6. func TestTLSVersionName(t *testing.T) {
  7. cases := map[uint16]string{
  8. tls.VersionTLS13: "1.3",
  9. tls.VersionTLS12: "1.2",
  10. tls.VersionTLS11: "1.1",
  11. tls.VersionTLS10: "1.0",
  12. 0: "unknown",
  13. }
  14. for in, want := range cases {
  15. if got := tlsVersionName(in); got != want {
  16. t.Errorf("tlsVersionName(%d) = %q, want %q", in, got, want)
  17. }
  18. }
  19. }
  20. func TestRealityCurveName(t *testing.T) {
  21. cases := map[tls.CurveID]string{
  22. tls.X25519: "X25519",
  23. tls.X25519MLKEM768: "X25519MLKEM768",
  24. tls.CurveP256: "P-256",
  25. 0: "",
  26. }
  27. for in, want := range cases {
  28. if got := realityCurveName(in); got != want {
  29. t.Errorf("realityCurveName(%d) = %q, want %q", in, got, want)
  30. }
  31. }
  32. }
  33. func TestFilterUsableSANs(t *testing.T) {
  34. got := filterUsableSANs([]string{"example.com", "*.example.com", "", " a.com "})
  35. want := []string{"example.com", "a.com"}
  36. if len(got) != len(want) {
  37. t.Fatalf("filterUsableSANs = %v, want %v", got, want)
  38. }
  39. for i := range want {
  40. if got[i] != want[i] {
  41. t.Errorf("filterUsableSANs[%d] = %q, want %q", i, got[i], want[i])
  42. }
  43. }
  44. }
  45. func TestSplitRealityTarget(t *testing.T) {
  46. okCases := []struct {
  47. in string
  48. wantHost string
  49. wantPort int
  50. }{
  51. {"example.com", "example.com", 443},
  52. {"example.com:8443", "example.com", 8443},
  53. {"1.1.1.1:443", "1.1.1.1", 443},
  54. }
  55. for _, c := range okCases {
  56. host, port, err := splitRealityTarget(c.in)
  57. if err != nil {
  58. t.Errorf("splitRealityTarget(%q) unexpected error: %v", c.in, err)
  59. continue
  60. }
  61. if host != c.wantHost || port != c.wantPort {
  62. t.Errorf("splitRealityTarget(%q) = (%q, %d), want (%q, %d)", c.in, host, port, c.wantHost, c.wantPort)
  63. }
  64. }
  65. badCases := []string{"", " ", "example.com:0", "example.com:70000", "bad host!"}
  66. for _, in := range badCases {
  67. if _, _, err := splitRealityTarget(in); err == nil {
  68. t.Errorf("splitRealityTarget(%q) expected error, got nil", in)
  69. }
  70. }
  71. }
  72. func TestScanRealityTargetInputValidation(t *testing.T) {
  73. if _, err := (&ServerService{}).ScanRealityTarget(""); err == nil {
  74. t.Error("ScanRealityTarget(empty) expected error, got nil")
  75. }
  76. }
  77. func TestScanRealityTargetBlocksPrivate(t *testing.T) {
  78. res, err := (&ServerService{}).ScanRealityTarget("127.0.0.1:443")
  79. if err != nil {
  80. t.Fatalf("ScanRealityTarget(loopback) unexpected error: %v", err)
  81. }
  82. if res.Feasible {
  83. t.Error("ScanRealityTarget(loopback) should not be feasible")
  84. }
  85. if res.Reason == "" {
  86. t.Error("ScanRealityTarget(loopback) should set a reason")
  87. }
  88. }
  89. func TestScanRealityTargetsHandlesPrivateAndBadInput(t *testing.T) {
  90. results, err := (&ServerService{}).ScanRealityTargets("127.0.0.1:443,10.0.0.1:443,bad host!")
  91. if err != nil {
  92. t.Fatalf("ScanRealityTargets unexpected error: %v", err)
  93. }
  94. if len(results) != 3 {
  95. t.Fatalf("ScanRealityTargets returned %d results, want 3", len(results))
  96. }
  97. for _, r := range results {
  98. if r.Feasible {
  99. t.Errorf("result %q unexpectedly feasible", r.Target)
  100. }
  101. }
  102. }