storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/net/host_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 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 net 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestHostIsEmpty(t *testing.T) { 25 testCases := []struct { 26 host Host 27 expectedResult bool 28 }{ 29 {Host{"", 0, false}, true}, 30 {Host{"", 0, true}, true}, 31 {Host{"play", 9000, false}, false}, 32 {Host{"play", 9000, true}, false}, 33 } 34 35 for i, testCase := range testCases { 36 result := testCase.host.IsEmpty() 37 38 if result != testCase.expectedResult { 39 t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) 40 } 41 } 42 } 43 44 func TestHostString(t *testing.T) { 45 testCases := []struct { 46 host Host 47 expectedStr string 48 }{ 49 {Host{"", 0, false}, ""}, 50 {Host{"", 0, true}, ":0"}, 51 {Host{"play", 9000, false}, "play"}, 52 {Host{"play", 9000, true}, "play:9000"}, 53 } 54 55 for i, testCase := range testCases { 56 str := testCase.host.String() 57 58 if str != testCase.expectedStr { 59 t.Fatalf("test %v: string: expected: %v, got: %v", i+1, testCase.expectedStr, str) 60 } 61 } 62 } 63 64 func TestHostEqual(t *testing.T) { 65 testCases := []struct { 66 host Host 67 compHost Host 68 expectedResult bool 69 }{ 70 {Host{"", 0, false}, Host{"", 0, true}, false}, 71 {Host{"play", 9000, true}, Host{"play", 9000, false}, false}, 72 {Host{"", 0, true}, Host{"", 0, true}, true}, 73 {Host{"play", 9000, false}, Host{"play", 9000, false}, true}, 74 {Host{"play", 9000, true}, Host{"play", 9000, true}, true}, 75 } 76 77 for i, testCase := range testCases { 78 result := testCase.host.Equal(testCase.compHost) 79 80 if result != testCase.expectedResult { 81 t.Fatalf("test %v: string: expected: %v, got: %v", i+1, testCase.expectedResult, result) 82 } 83 } 84 } 85 86 func TestHostMarshalJSON(t *testing.T) { 87 testCases := []struct { 88 host Host 89 expectedData []byte 90 expectErr bool 91 }{ 92 {Host{}, []byte(`""`), false}, 93 {Host{"play", 0, false}, []byte(`"play"`), false}, 94 {Host{"play", 0, true}, []byte(`"play:0"`), false}, 95 {Host{"play", 9000, true}, []byte(`"play:9000"`), false}, 96 {Host{"play.min.io", 0, false}, []byte(`"play.min.io"`), false}, 97 {Host{"play.min.io", 9000, true}, []byte(`"play.min.io:9000"`), false}, 98 {Host{"147.75.201.93", 0, false}, []byte(`"147.75.201.93"`), false}, 99 {Host{"147.75.201.93", 9000, true}, []byte(`"147.75.201.93:9000"`), false}, 100 {Host{"play12", 0, false}, []byte(`"play12"`), false}, 101 {Host{"12play", 0, false}, []byte(`"12play"`), false}, 102 {Host{"play-minio-io", 0, false}, []byte(`"play-minio-io"`), false}, 103 {Host{"play--min.io", 0, false}, []byte(`"play--min.io"`), false}, 104 } 105 106 for i, testCase := range testCases { 107 data, err := testCase.host.MarshalJSON() 108 expectErr := (err != nil) 109 110 if expectErr != testCase.expectErr { 111 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 112 } 113 114 if !testCase.expectErr { 115 if !reflect.DeepEqual(data, testCase.expectedData) { 116 t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data)) 117 } 118 } 119 } 120 } 121 122 func TestHostUnmarshalJSON(t *testing.T) { 123 testCases := []struct { 124 data []byte 125 expectedHost *Host 126 expectErr bool 127 }{ 128 {[]byte(`""`), &Host{}, false}, 129 {[]byte(`"play"`), &Host{"play", 0, false}, false}, 130 {[]byte(`"play:0"`), &Host{"play", 0, true}, false}, 131 {[]byte(`"play:9000"`), &Host{"play", 9000, true}, false}, 132 {[]byte(`"play.min.io"`), &Host{"play.min.io", 0, false}, false}, 133 {[]byte(`"play.min.io:9000"`), &Host{"play.min.io", 9000, true}, false}, 134 {[]byte(`"147.75.201.93"`), &Host{"147.75.201.93", 0, false}, false}, 135 {[]byte(`"147.75.201.93:9000"`), &Host{"147.75.201.93", 9000, true}, false}, 136 {[]byte(`"play12"`), &Host{"play12", 0, false}, false}, 137 {[]byte(`"12play"`), &Host{"12play", 0, false}, false}, 138 {[]byte(`"play-minio-io"`), &Host{"play-minio-io", 0, false}, false}, 139 {[]byte(`"play--min.io"`), &Host{"play--min.io", 0, false}, false}, 140 {[]byte(`":9000"`), &Host{"", 9000, true}, false}, 141 {[]byte(`"[fe80::8097:76eb:b397:e067%wlp2s0]"`), &Host{"fe80::8097:76eb:b397:e067%wlp2s0", 0, false}, false}, 142 {[]byte(`"[fe80::8097:76eb:b397:e067]:9000"`), &Host{"fe80::8097:76eb:b397:e067", 9000, true}, false}, 143 {[]byte(`"fe80::8097:76eb:b397:e067%wlp2s0"`), nil, true}, 144 {[]byte(`"fe80::8097:76eb:b397:e067%wlp2s0]"`), nil, true}, 145 {[]byte(`"[fe80::8097:76eb:b397:e067%wlp2s0"`), nil, true}, 146 {[]byte(`"[[fe80::8097:76eb:b397:e067%wlp2s0]]"`), nil, true}, 147 {[]byte(`"[[fe80::8097:76eb:b397:e067%wlp2s0"`), nil, true}, 148 {[]byte(`"play:"`), nil, true}, 149 {[]byte(`"play::"`), nil, true}, 150 {[]byte(`"play:90000"`), nil, true}, 151 {[]byte(`"play:-10"`), nil, true}, 152 {[]byte(`"play-"`), nil, true}, 153 {[]byte(`"play.minio..io"`), nil, true}, 154 {[]byte(`":"`), nil, true}, 155 } 156 157 for _, testCase := range testCases { 158 testCase := testCase 159 t.Run("", func(t *testing.T) { 160 var host Host 161 err := host.UnmarshalJSON(testCase.data) 162 expectErr := (err != nil) 163 164 if expectErr != testCase.expectErr { 165 t.Errorf("error: expected: %v, got: %v", testCase.expectErr, expectErr) 166 } 167 168 if !testCase.expectErr { 169 if !reflect.DeepEqual(&host, testCase.expectedHost) { 170 t.Errorf("host: expected: %#v, got: %#v", testCase.expectedHost, host) 171 } 172 } 173 }) 174 } 175 } 176 177 func TestParseHost(t *testing.T) { 178 testCases := []struct { 179 s string 180 expectedHost *Host 181 expectErr bool 182 }{ 183 {"play", &Host{"play", 0, false}, false}, 184 {"play:0", &Host{"play", 0, true}, false}, 185 {"play:9000", &Host{"play", 9000, true}, false}, 186 {"play.min.io", &Host{"play.min.io", 0, false}, false}, 187 {"play.min.io:9000", &Host{"play.min.io", 9000, true}, false}, 188 {"147.75.201.93", &Host{"147.75.201.93", 0, false}, false}, 189 {"147.75.201.93:9000", &Host{"147.75.201.93", 9000, true}, false}, 190 {"play12", &Host{"play12", 0, false}, false}, 191 {"12play", &Host{"12play", 0, false}, false}, 192 {"play-minio-io", &Host{"play-minio-io", 0, false}, false}, 193 {"play--min.io", &Host{"play--min.io", 0, false}, false}, 194 {":9000", &Host{"", 9000, true}, false}, 195 {"play:", nil, true}, 196 {"play::", nil, true}, 197 {"play:90000", nil, true}, 198 {"play:-10", nil, true}, 199 {"play-", nil, true}, 200 {"play.minio..io", nil, true}, 201 {":", nil, true}, 202 {"", nil, true}, 203 } 204 205 for _, testCase := range testCases { 206 testCase := testCase 207 t.Run("", func(t *testing.T) { 208 host, err := ParseHost(testCase.s) 209 expectErr := (err != nil) 210 211 if expectErr != testCase.expectErr { 212 t.Errorf("error: expected: %v, got: %v", testCase.expectErr, expectErr) 213 } 214 215 if !testCase.expectErr { 216 if !reflect.DeepEqual(host, testCase.expectedHost) { 217 t.Errorf("host: expected: %#v, got: %#v", testCase.expectedHost, host) 218 } 219 } 220 }) 221 } 222 } 223 224 func TestTrimIPv6(t *testing.T) { 225 testCases := []struct { 226 IP string 227 expectedIP string 228 expectErr bool 229 }{ 230 {"[fe80::8097:76eb:b397:e067%wlp2s0]", "fe80::8097:76eb:b397:e067%wlp2s0", false}, 231 {"fe80::8097:76eb:b397:e067%wlp2s0]", "fe80::8097:76eb:b397:e067%wlp2s0", true}, 232 {"[fe80::8097:76eb:b397:e067%wlp2s0]]", "fe80::8097:76eb:b397:e067%wlp2s0]", false}, 233 {"[[fe80::8097:76eb:b397:e067%wlp2s0]]", "[fe80::8097:76eb:b397:e067%wlp2s0]", false}, 234 } 235 236 for i, testCase := range testCases { 237 ip, err := trimIPv6(testCase.IP) 238 expectErr := (err != nil) 239 240 if expectErr != testCase.expectErr { 241 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 242 } 243 244 if !testCase.expectErr { 245 if ip != testCase.expectedIP { 246 t.Fatalf("test %v: IP: expected: %#v, got: %#v", i+1, testCase.expectedIP, ip) 247 } 248 } 249 } 250 }