reality_scan_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package service
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "testing"
  6. )
  7. func TestTLSVersionName(t *testing.T) {
  8. cases := map[uint16]string{
  9. tls.VersionTLS13: "1.3",
  10. tls.VersionTLS12: "1.2",
  11. tls.VersionTLS11: "1.1",
  12. tls.VersionTLS10: "1.0",
  13. 0: "unknown",
  14. }
  15. for in, want := range cases {
  16. if got := tlsVersionName(in); got != want {
  17. t.Errorf("tlsVersionName(%d) = %q, want %q", in, got, want)
  18. }
  19. }
  20. }
  21. func TestRealityCurveName(t *testing.T) {
  22. cases := map[tls.CurveID]string{
  23. tls.X25519: "X25519",
  24. tls.X25519MLKEM768: "X25519MLKEM768",
  25. tls.CurveP256: "P-256",
  26. 0: "",
  27. }
  28. for in, want := range cases {
  29. if got := realityCurveName(in); got != want {
  30. t.Errorf("realityCurveName(%d) = %q, want %q", in, got, want)
  31. }
  32. }
  33. }
  34. func TestFilterUsableSANs(t *testing.T) {
  35. got := filterUsableSANs([]string{"example.com", "*.example.com", "", " a.com "})
  36. want := []string{"example.com", "a.com"}
  37. if len(got) != len(want) {
  38. t.Fatalf("filterUsableSANs = %v, want %v", got, want)
  39. }
  40. for i := range want {
  41. if got[i] != want[i] {
  42. t.Errorf("filterUsableSANs[%d] = %q, want %q", i, got[i], want[i])
  43. }
  44. }
  45. }
  46. func TestSplitRealityTarget(t *testing.T) {
  47. okCases := []struct {
  48. in string
  49. wantHost string
  50. wantPort int
  51. }{
  52. {"example.com", "example.com", 443},
  53. {"example.com:8443", "example.com", 8443},
  54. {"1.1.1.1:443", "1.1.1.1", 443},
  55. }
  56. for _, c := range okCases {
  57. host, port, err := splitRealityTarget(c.in)
  58. if err != nil {
  59. t.Errorf("splitRealityTarget(%q) unexpected error: %v", c.in, err)
  60. continue
  61. }
  62. if host != c.wantHost || port != c.wantPort {
  63. t.Errorf("splitRealityTarget(%q) = (%q, %d), want (%q, %d)", c.in, host, port, c.wantHost, c.wantPort)
  64. }
  65. }
  66. badCases := []string{"", " ", "example.com:0", "example.com:70000", "bad host!"}
  67. for _, in := range badCases {
  68. if _, _, err := splitRealityTarget(in); err == nil {
  69. t.Errorf("splitRealityTarget(%q) expected error, got nil", in)
  70. }
  71. }
  72. }
  73. func TestScanRealityTargetInputValidation(t *testing.T) {
  74. if _, err := (&ServerService{}).ScanRealityTarget("", 0); err == nil {
  75. t.Error("ScanRealityTarget(empty) expected error, got nil")
  76. }
  77. }
  78. func TestScanRealityTargetBlocksPrivate(t *testing.T) {
  79. res, err := (&ServerService{}).ScanRealityTarget("127.0.0.1:443", 0)
  80. if err != nil {
  81. t.Fatalf("ScanRealityTarget(loopback) unexpected error: %v", err)
  82. }
  83. if res.Feasible {
  84. t.Error("ScanRealityTarget(loopback) should not be feasible")
  85. }
  86. if res.Reason == "" {
  87. t.Error("ScanRealityTarget(loopback) should set a reason")
  88. }
  89. }
  90. func TestScanRealityTargetsHandlesPrivateAndBadInput(t *testing.T) {
  91. results, err := (&ServerService{}).ScanRealityTargets("127.0.0.1:443,10.0.0.1:443,bad host!")
  92. if err != nil {
  93. t.Fatalf("ScanRealityTargets unexpected error: %v", err)
  94. }
  95. if len(results) != 3 {
  96. t.Fatalf("ScanRealityTargets returned %d results, want 3", len(results))
  97. }
  98. for _, r := range results {
  99. if r.Feasible {
  100. t.Errorf("result %q unexpectedly feasible", r.Target)
  101. }
  102. }
  103. }
  104. func TestWriteProxyProtocolV1(t *testing.T) {
  105. server, client := net.Pipe()
  106. defer client.Close()
  107. local := &net.TCPAddr{IP: net.ParseIP("192.0.2.10"), Port: 51234}
  108. remote := &net.TCPAddr{IP: net.ParseIP("203.0.113.5"), Port: 443}
  109. got := make(chan string, 1)
  110. go func() {
  111. buf := make([]byte, 128)
  112. n, _ := server.Read(buf)
  113. got <- string(buf[:n])
  114. }()
  115. if err := writeProxyProtocolV1(client, local, remote); err != nil {
  116. t.Fatalf("writeProxyProtocolV1: %v", err)
  117. }
  118. line := <-got
  119. want := "PROXY TCP4 192.0.2.10 203.0.113.5 51234 443\r\n"
  120. if line != want {
  121. t.Fatalf("v1 header = %q, want %q", line, want)
  122. }
  123. }
  124. func TestWriteProxyProtocolV2Signature(t *testing.T) {
  125. server, client := net.Pipe()
  126. defer client.Close()
  127. local := &net.TCPAddr{IP: net.ParseIP("192.0.2.10"), Port: 51234}
  128. remote := &net.TCPAddr{IP: net.ParseIP("203.0.113.5"), Port: 443}
  129. got := make(chan []byte, 1)
  130. go func() {
  131. buf := make([]byte, 128)
  132. n, _ := server.Read(buf)
  133. got <- append([]byte(nil), buf[:n]...)
  134. }()
  135. if err := writeProxyProtocolV2(client, local, remote); err != nil {
  136. t.Fatalf("writeProxyProtocolV2: %v", err)
  137. }
  138. hdr := <-got
  139. sig := []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A}
  140. if len(hdr) < 16 || string(hdr[:12]) != string(sig) {
  141. t.Fatalf("v2 header missing the protocol signature: %v", hdr)
  142. }
  143. if hdr[12] != 0x21 {
  144. t.Fatalf("v2 version/command byte = 0x%02x, want 0x21", hdr[12])
  145. }
  146. if hdr[13] != 0x11 {
  147. t.Fatalf("v2 family/protocol byte = 0x%02x, want 0x11 (TCP over IPv4)", hdr[13])
  148. }
  149. }