| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package service
- import (
- "crypto/tls"
- "testing"
- )
- func TestTLSVersionName(t *testing.T) {
- cases := map[uint16]string{
- tls.VersionTLS13: "1.3",
- tls.VersionTLS12: "1.2",
- tls.VersionTLS11: "1.1",
- tls.VersionTLS10: "1.0",
- 0: "unknown",
- }
- for in, want := range cases {
- if got := tlsVersionName(in); got != want {
- t.Errorf("tlsVersionName(%d) = %q, want %q", in, got, want)
- }
- }
- }
- func TestRealityCurveName(t *testing.T) {
- cases := map[tls.CurveID]string{
- tls.X25519: "X25519",
- tls.X25519MLKEM768: "X25519MLKEM768",
- tls.CurveP256: "P-256",
- 0: "",
- }
- for in, want := range cases {
- if got := realityCurveName(in); got != want {
- t.Errorf("realityCurveName(%d) = %q, want %q", in, got, want)
- }
- }
- }
- func TestFilterUsableSANs(t *testing.T) {
- got := filterUsableSANs([]string{"example.com", "*.example.com", "", " a.com "})
- want := []string{"example.com", "a.com"}
- if len(got) != len(want) {
- t.Fatalf("filterUsableSANs = %v, want %v", got, want)
- }
- for i := range want {
- if got[i] != want[i] {
- t.Errorf("filterUsableSANs[%d] = %q, want %q", i, got[i], want[i])
- }
- }
- }
- func TestSplitRealityTarget(t *testing.T) {
- okCases := []struct {
- in string
- wantHost string
- wantPort int
- }{
- {"example.com", "example.com", 443},
- {"example.com:8443", "example.com", 8443},
- {"1.1.1.1:443", "1.1.1.1", 443},
- }
- for _, c := range okCases {
- host, port, err := splitRealityTarget(c.in)
- if err != nil {
- t.Errorf("splitRealityTarget(%q) unexpected error: %v", c.in, err)
- continue
- }
- if host != c.wantHost || port != c.wantPort {
- t.Errorf("splitRealityTarget(%q) = (%q, %d), want (%q, %d)", c.in, host, port, c.wantHost, c.wantPort)
- }
- }
- badCases := []string{"", " ", "example.com:0", "example.com:70000", "bad host!"}
- for _, in := range badCases {
- if _, _, err := splitRealityTarget(in); err == nil {
- t.Errorf("splitRealityTarget(%q) expected error, got nil", in)
- }
- }
- }
- func TestScanRealityTargetInputValidation(t *testing.T) {
- if _, err := (&ServerService{}).ScanRealityTarget(""); err == nil {
- t.Error("ScanRealityTarget(empty) expected error, got nil")
- }
- }
- func TestScanRealityTargetBlocksPrivate(t *testing.T) {
- res, err := (&ServerService{}).ScanRealityTarget("127.0.0.1:443")
- if err != nil {
- t.Fatalf("ScanRealityTarget(loopback) unexpected error: %v", err)
- }
- if res.Feasible {
- t.Error("ScanRealityTarget(loopback) should not be feasible")
- }
- if res.Reason == "" {
- t.Error("ScanRealityTarget(loopback) should set a reason")
- }
- }
- func TestScanRealityTargetsHandlesPrivateAndBadInput(t *testing.T) {
- results, err := (&ServerService{}).ScanRealityTargets("127.0.0.1:443,10.0.0.1:443,bad host!")
- if err != nil {
- t.Fatalf("ScanRealityTargets unexpected error: %v", err)
- }
- if len(results) != 3 {
- t.Fatalf("ScanRealityTargets returned %d results, want 3", len(results))
- }
- for _, r := range results {
- if r.Feasible {
- t.Errorf("result %q unexpectedly feasible", r.Target)
- }
- }
- }
|