-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(net/gipv4): add unit tests (#4052)
- Loading branch information
Showing
6 changed files
with
251 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |