| 1 |
/* |
| 2 |
Copyright 2020 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package util //nolint:revive |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/tls" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
const ( |
| 25 |
failed = "\u2717" |
| 26 |
succeed = "\u2713" |
| 27 |
) |
| 28 |
|
| 29 |
func TestRemovePortFromHost(t *testing.T) { |
| 30 |
tests := []struct { |
| 31 |
name string |
| 32 |
origHost string |
| 33 |
expect string |
| 34 |
}{ |
| 35 |
{"Domain&Port", "localhost:8080", "localhost"}, |
| 36 |
{"Domain", "localhost", "localhost"}, |
| 37 |
{"IPv4&Port", "192.168.0.1:8080", "192.168.0.1"}, |
| 38 |
{"ShortestIPv6", "::", "::"}, |
| 39 |
{"IPv6", "9878::7675:1292:9183:7562", "9878::7675:1292:9183:7562"}, |
| 40 |
{"IPv6&Port", "[alsk:1204:1020::1292]:8080", "alsk:1204:1020::1292"}, |
| 41 |
{"FQDN", " www.kubernetes.test:8080", " www.kubernetes.test"}, |
| 42 |
} |
| 43 |
|
| 44 |
for _, tt := range tests { |
| 45 |
st := tt |
| 46 |
tf := func(t *testing.T) { |
| 47 |
t.Parallel() |
| 48 |
t.Logf("\tTestCase: %s", st.name) |
| 49 |
{ |
| 50 |
get := RemovePortFromHost(st.origHost) |
| 51 |
if get != st.expect { |
| 52 |
t.Fatalf("\t%s\texpect %v, but get %v", failed, st.expect, get) |
| 53 |
} |
| 54 |
t.Logf("\t%s\texpect %v, get %v", succeed, st.expect, get) |
| 55 |
|
| 56 |
} |
| 57 |
} |
| 58 |
t.Run(st.name, tf) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestGetTLSVersion(t *testing.T) { |
| 63 |
tests := []struct { |
| 64 |
name string |
| 65 |
versionName string |
| 66 |
expected uint16 |
| 67 |
expectError bool |
| 68 |
}{ |
| 69 |
{"EmptyDefaultsToTLS12", "", tls.VersionTLS12, false}, |
| 70 |
{"VersionTLS10", "VersionTLS10", tls.VersionTLS10, false}, |
| 71 |
{"VersionTLS11", "VersionTLS11", tls.VersionTLS11, false}, |
| 72 |
{"VersionTLS12", "VersionTLS12", tls.VersionTLS12, false}, |
| 73 |
{"VersionTLS13", "VersionTLS13", tls.VersionTLS13, false}, |
| 74 |
{"InvalidVersion", "VersionTLS99", 0, true}, |
| 75 |
} |
| 76 |
|
| 77 |
for _, tt := range tests { |
| 78 |
st := tt |
| 79 |
t.Run(st.name, func(t *testing.T) { |
| 80 |
t.Parallel() |
| 81 |
got, err := GetTLSVersion(st.versionName) |
| 82 |
if st.expectError { |
| 83 |
if err == nil { |
| 84 |
t.Fatalf("\t%s\texpected error for input %q, but got none", failed, st.versionName) |
| 85 |
} |
| 86 |
t.Logf("\t%s\tgot expected error: %v", succeed, err) |
| 87 |
return |
| 88 |
} |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("\t%s\tunexpected error for input %q: %v", failed, st.versionName, err) |
| 91 |
} |
| 92 |
if got != st.expected { |
| 93 |
t.Fatalf("\t%s\texpect %v, but got %v", failed, st.expected, got) |
| 94 |
} |
| 95 |
t.Logf("\t%s\texpect %v, got %v", succeed, st.expected, got) |
| 96 |
}) |
| 97 |
} |
| 98 |
} |