Skip to content

Commit

Permalink
test(net/gipv4): add unit tests (#4052)
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git authored Dec 17, 2024
1 parent f79aef6 commit e7fdf82
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 16 deletions.
30 changes: 18 additions & 12 deletions net/gipv4/gipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@
package gipv4

import (
"fmt"
"net"
"strconv"

"github.com/gogf/gf/v2/text/gregex"
"strings"
)

// Validate checks whether given `ip` a valid IPv4 address.
func Validate(ip string) bool {
return gregex.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip)
parsed := net.ParseIP(ip)
return parsed != nil && parsed.To4() != nil
}

// ParseAddress parses `address` to its ip and port.
// Eg: 192.168.1.1:80 -> 192.168.1.1, 80
func ParseAddress(address string) (string, int) {
match, err := gregex.MatchString(`^(.+):(\d+)$`, address)
if err == nil {
i, _ := strconv.Atoi(match[2])
return match[1], i
host, port, err := net.SplitHostPort(address)
if err != nil {
return "", 0
}
portInt, err := strconv.Atoi(port)
if err != nil {
return "", 0
}
return "", 0
return host, portInt
}

// GetSegment returns the segment of given ip address.
// Eg: 192.168.2.102 -> 192.168.2
func GetSegment(ip string) string {
match, err := gregex.MatchString(`^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$`, ip)
if err != nil || len(match) < 4 {
if !Validate(ip) {
return ""
}
segments := strings.Split(ip, ".")
if len(segments) != 4 {
return ""
}
return fmt.Sprintf("%s.%s.%s", match[1], match[2], match[3])
return strings.Join(segments[:3], ".")
}
9 changes: 5 additions & 4 deletions net/gipv4/gipv4_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func GetIpArray() (ips []string, err error) {
}
for _, address := range interfaceAddr {
ipNet, isValidIpNet := address.(*net.IPNet)
if isValidIpNet && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP.String())
}
if !(isValidIpNet && !ipNet.IP.IsLoopback()) {
continue
}
if ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP.String())
}
}
return ips, nil
Expand Down
81 changes: 81 additions & 0 deletions net/gipv4/gipv4_z_unit_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gipv4_test

import (
"testing"

"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/test/gtest"
)

func TestGetIpArray(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ips, err := gipv4.GetIpArray()
t.AssertNil(err)
t.AssertGT(len(ips), 0)
for _, ip := range ips {
t.Assert(gipv4.Validate(ip), true)
}
})
}

func TestMustGetIntranetIp(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("MustGetIntranetIp() panicked: %v", r)
}
}()
ip := gipv4.MustGetIntranetIp()
t.Assert(gipv4.IsIntranet(ip), true)
})
}

func TestGetIntranetIp(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ip, err := gipv4.GetIntranetIp()
t.AssertNil(err)
t.AssertNE(ip, "")
t.Assert(gipv4.IsIntranet(ip), true)
})
}

func TestGetIntranetIpArray(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ips, err := gipv4.GetIntranetIpArray()
t.AssertNil(err)
t.AssertGT(len(ips), 0)
for _, ip := range ips {
t.Assert(gipv4.IsIntranet(ip), true)
}
})
}

func TestIsIntranet(t *testing.T) {
tests := []struct {
ip string
expected bool
}{
{"127.0.0.1", true},
{"10.0.0.1", true},
{"172.16.0.1", true},
{"172.31.255.255", true},
{"192.168.0.1", true},
{"192.168.255.255", true},
{"8.8.8.8", false},
{"172.32.0.1", false},
{"256.256.256.256", false},
}

gtest.C(t, func(t *gtest.T) {
for _, test := range tests {
result := gipv4.IsIntranet(test.ip)
t.Assert(result, test.expected)
}
})
}
30 changes: 30 additions & 0 deletions net/gipv4/gipv4_z_unit_lookup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gipv4_test

import (
"testing"

"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/test/gtest"
)

func TestGetHostByName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ip, err := gipv4.GetHostByName("localhost")
t.AssertNil(err)
t.Assert(ip, "127.0.0.1")
})
}

func TestGetHostsByName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ips, err := gipv4.GetHostsByName("localhost")
t.AssertNil(err)
t.AssertIN("127.0.0.1", ips)
})
}
36 changes: 36 additions & 0 deletions net/gipv4/gipv4_z_unit_mac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gipv4_test

import (
"testing"

"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/test/gtest"
)

func TestGetMac(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
mac, err := gipv4.GetMac()
t.AssertNil(err)
t.AssertNE(mac, "")
// MAC addresses are typically 17 characters in length
t.Assert(len(mac), 17)
})
}

func TestGetMacArray(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
macs, err := gipv4.GetMacArray()
t.AssertNil(err)
t.AssertGT(len(macs), 0)
for _, mac := range macs {
// MAC addresses are typically 17 characters in length
t.Assert(len(mac), 17)
}
})
}
81 changes: 81 additions & 0 deletions net/gipv4/gipv4_z_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gipv4_test

import (
"testing"

"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/test/gtest"
)

func TestValidate(t *testing.T) {
tests := []struct {
ip string
expected bool
}{
{"192.168.1.1", true},
{"255.255.255.255", true},
{"0.0.0.0", true},
{"256.256.256.256", false},
{"192.168.1", false},
{"abc.def.ghi.jkl", false},
{"19216811", false},
{"abcdefghijkl", false},
}

gtest.C(t, func(t *gtest.T) {
for _, test := range tests {
result := gipv4.Validate(test.ip)
t.Assert(result, test.expected)
}
})
}

func TestParseAddress(t *testing.T) {
tests := []struct {
address string
expectedIP string
expectedPort int
}{
{"192.168.1.1:80", "192.168.1.1", 80},
{"10.0.0.1:8080", "10.0.0.1", 8080},
{"127.0.0.1:65535", "127.0.0.1", 65535},
{"invalid:address", "", 0},
{"192.168.1.1", "", 0},
{"19216811", "", 0},
}

gtest.C(t, func(t *gtest.T) {
for _, test := range tests {
ip, port := gipv4.ParseAddress(test.address)
t.Assert(ip, test.expectedIP)
t.Assert(port, test.expectedPort)
}
})
}

func TestGetSegment(t *testing.T) {
tests := []struct {
ip string
expected string
}{
{"192.168.2.102", "192.168.2"},
{"10.0.0.1", "10.0.0"},
{"255.255.255.255", "255.255.255"},
{"invalid.ip.address", ""},
{"123", ""},
{"192.168.2.102.123", ""},
}

gtest.C(t, func(t *gtest.T) {
for _, test := range tests {
result := gipv4.GetSegment(test.ip)
t.Assert(result, test.expected)
}
})
}

0 comments on commit e7fdf82

Please sign in to comment.