github.com/XiaoMi/Gaea@v1.2.5/util/ip_test.go (about) 1 // Copyright 2016 The kingshard Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // 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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 package util 15 16 import ( 17 "fmt" 18 "net" 19 "testing" 20 ) 21 22 func TestParseIP(t *testing.T) { 23 ip := net.ParseIP("abcdefg") 24 fmt.Println(ip) 25 fmt.Println(ip.String()) 26 } 27 28 func TestCreateIPInfoIPSuccess(t *testing.T) { 29 addr := "127.0.0.1" 30 info, err := ParseIPInfo(addr) 31 if err != nil { 32 t.FailNow() 33 } 34 if info.isIPNet { 35 t.FailNow() 36 } 37 if addr != info.info { 38 t.FailNow() 39 } 40 if addr != info.ip.String() { 41 t.FailNow() 42 } 43 } 44 45 func TestCreateIPInfoIPError(t *testing.T) { 46 addr := "127.255.256.1" 47 if _, err := ParseIPInfo(addr); err == nil { 48 t.FailNow() 49 } 50 } 51 52 func TestCreateIPInfoIPError2(t *testing.T) { 53 addr := "abcdefg" 54 if _, err := ParseIPInfo(addr); err == nil { 55 t.FailNow() 56 } 57 } 58 59 func TestCreateIPInfoIPNetSuccess(t *testing.T) { 60 addr := "192.168.122.1/24" 61 netAddr := "192.168.122.0/24" 62 info, err := ParseIPInfo(addr) 63 if err != nil { 64 t.FailNow() 65 } 66 if !info.isIPNet { 67 t.FailNow() 68 } 69 if addr != info.info { 70 t.FailNow() 71 } 72 if netAddr != info.ipNet.String() { 73 t.FailNow() 74 } 75 } 76 77 func TestCreateIPInfoIPNetError(t *testing.T) { 78 addr := "192.168.122.1/" 79 if _, err := ParseIPInfo(addr); err == nil { 80 t.FailNow() 81 } 82 } 83 84 func TestCreateIPInfoIPNetError2(t *testing.T) { 85 addr := "192.168.122.1/35" 86 if _, err := ParseIPInfo(addr); err == nil { 87 t.FailNow() 88 } 89 }