| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestNormalizeRelayURLs(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | |
| 13 | got, err := NormalizeRelayURLs( |
| 14 | " localhost:4017 , https://relay.example.com/base/relay?x=1#frag ", |
| 15 | "https://relay.example.com/base", |
| 16 | ) |
| 17 | if err != nil { |
| 18 | t.Fatalf("NormalizeRelayURLs() error = %v", err) |
| 19 | } |
| 20 | |
| 21 | want := []string{ |
| 22 | "https://localhost:4017", |
| 23 | "https://relay.example.com/base", |
| 24 | } |
| 25 | if !reflect.DeepEqual(got, want) { |
| 26 | t.Fatalf("NormalizeRelayURLs() = %v, want %v", got, want) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestNormalizeURLPath(t *testing.T) { |
| 31 | t.Parallel() |
| 32 | |
| 33 | cases := []struct { |
| 34 | input string |
| 35 | want string |
| 36 | }{ |
| 37 | {input: "", want: "/"}, |
| 38 | {input: " ", want: "/"}, |
| 39 | {input: "api", want: "/api"}, |
| 40 | {input: "/api/", want: "/api"}, |
| 41 | {input: "/api/../v1//", want: "/v1"}, |
| 42 | {input: "/", want: "/"}, |
| 43 | } |
| 44 | |
| 45 | for _, tc := range cases { |
| 46 | if got := NormalizeURLPath(tc.input); got != tc.want { |
| 47 | t.Fatalf("NormalizeURLPath(%q) = %q, want %q", tc.input, got, tc.want) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestFilterRelayURLs(t *testing.T) { |
| 53 | t.Parallel() |
| 54 | |
| 55 | got := FilterRelayURLs( |
| 56 | []string{"https://relay-a.example", "https://relay-b.example"}, |
| 57 | []string{"https://relay-b.example"}, |
| 58 | ) |
| 59 | |
| 60 | want := []string{"https://relay-a.example"} |
| 61 | if !reflect.DeepEqual(got, want) { |
| 62 | t.Fatalf("FilterRelayURLs() = %v, want %v", got, want) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestRemoveRelayURL(t *testing.T) { |
| 67 | t.Parallel() |
| 68 | |
| 69 | got := RemoveRelayURL( |
| 70 | []string{"https://relay-a.example", "https://relay-b.example"}, |
| 71 | "https://relay-a.example", |
| 72 | ) |
| 73 | |
| 74 | want := []string{"https://relay-b.example"} |
| 75 | if !reflect.DeepEqual(got, want) { |
| 76 | t.Fatalf("RemoveRelayURL() = %v, want %v", got, want) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestExcludeLocalRelayURLs(t *testing.T) { |
| 81 | t.Parallel() |
| 82 | |
| 83 | got, err := ExcludeLocalRelayURLs( |
| 84 | "https://localhost:4017", |
| 85 | "https://127.0.0.1:4017", |
| 86 | "https://relay.example.com/base", |
| 87 | "https://demo.localhost", |
| 88 | ) |
| 89 | if err != nil { |
| 90 | t.Fatalf("ExcludeLocalRelayURLs() error = %v", err) |
| 91 | } |
| 92 | |
| 93 | want := []string{"https://relay.example.com/base"} |
| 94 | if !reflect.DeepEqual(got, want) { |
| 95 | t.Fatalf("ExcludeLocalRelayURLs() = %v, want %v", got, want) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestParseCIDRs(t *testing.T) { |
| 100 | t.Parallel() |
| 101 | |
| 102 | got, err := ParseCIDRs("10.0.0.0/8, 10.0.0.0/8, 192.168.0.0/16") |
| 103 | if err != nil { |
| 104 | t.Fatalf("ParseCIDRs() error = %v", err) |
| 105 | } |
| 106 | if len(got) != 2 { |
| 107 | t.Fatalf("ParseCIDRs() len = %d, want %d", len(got), 2) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestParseCIDRsRejectsInvalidValue(t *testing.T) { |
| 112 | t.Parallel() |
| 113 | |
| 114 | if _, err := ParseCIDRs("not-a-cidr"); err == nil { |
| 115 | t.Fatal("ParseCIDRs() error = nil, want invalid cidr error") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestDomainCandidates(t *testing.T) { |
| 120 | t.Parallel() |
| 121 | |
| 122 | got := DomainCandidates("portal.example.com") |
| 123 | want := []string{"portal.example.com", "example.com"} |
| 124 | if !reflect.DeepEqual(got, want) { |
| 125 | t.Fatalf("DomainCandidates() = %v, want %v", got, want) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestNormalizeTargetAddr(t *testing.T) { |
| 130 | t.Parallel() |
| 131 | |
| 132 | got, err := NormalizeTargetAddr("http://127.0.0.1") |
| 133 | if err != nil { |
| 134 | t.Fatalf("NormalizeTargetAddr() error = %v", err) |
| 135 | } |
| 136 | if got != "127.0.0.1:80" { |
| 137 | t.Fatalf("NormalizeTargetAddr() = %q, want %q", got, "127.0.0.1:80") |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestValidateIPv4(t *testing.T) { |
| 142 | t.Parallel() |
| 143 | |
| 144 | if err := ValidateIPv4("203.0.113.10"); err != nil { |
| 145 | t.Fatalf("ValidateIPv4() error = %v", err) |
| 146 | } |
| 147 | if err := ValidateIPv4("not-an-ip"); err == nil { |
| 148 | t.Fatal("ValidateIPv4() error = nil, want invalid ip error") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestNormalizeDNSLabel(t *testing.T) { |
| 153 | t.Parallel() |
| 154 | |
| 155 | got, err := NormalizeDNSLabel("Demo-App") |
| 156 | if err != nil { |
| 157 | t.Fatalf("NormalizeDNSLabel() error = %v", err) |
| 158 | } |
| 159 | if got != "demo-app" { |
| 160 | t.Fatalf("NormalizeDNSLabel() = %q, want %q", got, "demo-app") |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestLeaseHostname(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | |
| 167 | got, err := LeaseHostname("Demo-App", "portal.example.com") |
| 168 | if err != nil { |
| 169 | t.Fatalf("LeaseHostname() error = %v", err) |
| 170 | } |
| 171 | if got != "demo-app.portal.example.com" { |
| 172 | t.Fatalf("LeaseHostname() = %q, want %q", got, "demo-app.portal.example.com") |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestDecodeBase64URLString(t *testing.T) { |
| 177 | t.Parallel() |
| 178 | |
| 179 | cases := []struct { |
| 180 | encoded string |
| 181 | want string |
| 182 | }{ |
| 183 | {encoded: "bGVhc2UtMTIz", want: "lease-123"}, |
| 184 | {encoded: "bGVhc2UtMTIzZA==", want: "lease-123d"}, |
| 185 | {encoded: "bGVhc2UtMTIzZA", want: "lease-123d"}, |
| 186 | } |
| 187 | |
| 188 | for _, tc := range cases { |
| 189 | encoded, want := tc.encoded, tc.want |
| 190 | got, err := DecodeBase64URLString(encoded) |
| 191 | if err != nil { |
| 192 | t.Fatalf("DecodeBase64URLString(%q) error = %v", encoded, err) |
| 193 | } |
| 194 | if got != want { |
| 195 | t.Fatalf("DecodeBase64URLString(%q) = %q, want %q", encoded, got, want) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestDecodeBase64URLStringRejectsInvalidValue(t *testing.T) { |
| 201 | t.Parallel() |
| 202 | |
| 203 | if _, err := DecodeBase64URLString("%%%"); err == nil { |
| 204 | t.Fatal("DecodeBase64URLString() error = nil, want invalid base64 error") |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestSleepOrDoneCanceled(t *testing.T) { |
| 209 | t.Parallel() |
| 210 | |
| 211 | ctx, cancel := context.WithCancel(context.Background()) |
| 212 | cancel() |
| 213 | |
| 214 | if SleepOrDone(ctx, time.Second) { |
| 215 | t.Fatal("SleepOrDone() = true, want false for canceled context") |
| 216 | } |
| 217 | } |