go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/addr_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth 16 17 import ( 18 "fmt" 19 "net" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 . "go.chromium.org/luci/common/testing/assertions" 24 ) 25 26 func TestParseRemoteIP(t *testing.T) { 27 t.Parallel() 28 29 Convey(`Test suites`, t, func() { 30 for _, tc := range []struct { 31 v string 32 exp string 33 err string 34 }{ 35 {"", "0.0.0.0", ""}, 36 {"1.2.3.4", "1.2.3.4", ""}, 37 {"1.2.3.4:1337", "1.2.3.4", ""}, 38 {"1::1", "1::1", ""}, 39 {"[1::1]", "1::1", ""}, 40 {"[1::1]:1337", "1::1", ""}, 41 {"[abc:abc:abc:abc:abc:abc:abc:abc]:80", "abc:abc:abc:abc:abc:abc:abc:abc", ""}, 42 {"[1.2.3.4]:1337", "1.2.3.4", ""}, 43 {"[1::1:1337", "", "missing closing brace"}, 44 {"[1.2.3.4", "", "missing closing brace"}, 45 {"1.3.4:1337", "", "don't know how to parse"}, 46 {"1:3:5:9:1337", "", "don't know how to parse"}, 47 } { 48 if tc.err == "" { 49 Convey(fmt.Sprintf(`Successfully parses %q into %q.`, tc.v, tc.exp), func() { 50 ip, err := parseRemoteIP(tc.v) 51 So(err, ShouldBeNil) 52 So(ip, ShouldResemble, net.ParseIP(tc.exp)) 53 }) 54 } else { 55 Convey(fmt.Sprintf(`Fails to parse %q with %q.`, tc.v, tc.err), func() { 56 _, err := parseRemoteIP(tc.v) 57 So(err, ShouldErrLike, tc.err) 58 }) 59 } 60 } 61 }) 62 }