Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Unified Diff: go/src/infra/crimson/server/crimsondb/crimsondb_test.go

Issue 2105213002: Created MacAddrStringToHexString and reverse (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@crimson-add-host
Patch Set: More tests to get coverage up Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/crimson/server/crimsondb/crimsondb.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/crimson/server/crimsondb/crimsondb_test.go
diff --git a/go/src/infra/crimson/server/crimsondb/crimsondb_test.go b/go/src/infra/crimson/server/crimsondb/crimsondb_test.go
index c32f194dcdf798a2586969f80e41badceee0de0a..c42998724750171b3fb63656ae640d499a1e48fa 100644
--- a/go/src/infra/crimson/server/crimsondb/crimsondb_test.go
+++ b/go/src/infra/crimson/server/crimsondb/crimsondb_test.go
@@ -16,25 +16,92 @@ import (
"infra/crimson/server/sqlmock"
)
+func TestMacAddrStringToHexString(t *testing.T) {
+ t.Parallel()
+ Convey("MacAddrStringToHexString works", t, func() {
+ Convey("on 00:00:00:00:00:00", func() {
+ addr, err := MacAddrStringToHexString("00:00:00:00:00:00")
+ So(err, ShouldBeNil)
+ So(addr, ShouldEqual, "0x000000000000")
+ })
+ Convey("on 01:23:45:67:89:ab", func() {
+ addr, err := MacAddrStringToHexString("01:23:45:67:89:ab")
+ So(err, ShouldBeNil)
+ So(addr, ShouldEqual, "0x0123456789ab")
+ })
+ })
+
+ Convey("MacAddrStringToHexString returns an error", t, func() {
+ Convey("on empty string", func() {
+ _, err := MacAddrStringToHexString("")
+ So(err, ShouldNotBeNil)
+ })
+ Convey("on 000000000000", func() {
+ _, err := MacAddrStringToHexString("000000000000")
+ So(err, ShouldNotBeNil)
+ })
+ Convey("on 'deadmeat'", func() {
+ _, err := MacAddrStringToHexString("deadmeat")
+ So(err, ShouldNotBeNil)
+ })
+ })
+}
+
+func TestHexStringToHardwareAddr(t *testing.T) {
+ t.Parallel()
+
+ Convey("HexStringToHardwareAddr works", t, func() {
+ Convey("on 0x000000000000", func() {
+ hw, err := HexStringToHardwareAddr("0x000000000000")
+ So(err, ShouldBeNil)
+ So(hw.String(), ShouldEqual, "00:00:00:00:00:00")
+ })
+ Convey("on 0x0123456789ab", func() {
+ hw, err := HexStringToHardwareAddr("0x0123456789ab")
+ So(err, ShouldBeNil)
+ So(hw.String(), ShouldEqual, "01:23:45:67:89:ab")
+ })
+ })
+
+ Convey("HexStringToHardwareAddr returns an error", t, func() {
+ Convey("on empty string", func() {
+ _, err := HexStringToHardwareAddr("")
+ So(err, ShouldNotBeNil)
+ })
+ Convey("on 000000000000", func() {
+ _, err := HexStringToHardwareAddr("000000000000")
+ So(err, ShouldNotBeNil)
+ })
+ Convey("on '0xdeaddeadmeat'", func() {
+ _, err := HexStringToHardwareAddr("0xdeaddeadmeat")
+ So(err, ShouldNotBeNil)
+ })
+ Convey("on 'Aa000000000000'", func() {
+ _, err := HexStringToHardwareAddr("Aa000000000000")
+ So(err, ShouldNotBeNil)
+ })
+ })
+}
+
func TestIPStringToHexString(t *testing.T) {
t.Parallel()
- Convey("TestHexStringToIPString works", t, func() {
+ Convey("IPStringToHexString works", t, func() {
Convey("on 192.168.0.1", func() {
hexString, err := IPStringToHexString("192.168.0.1")
expected := "0xc0a80001"
So(hexString, ShouldEqual, expected)
- So(err, ShouldEqual, nil)
+ So(err, ShouldBeNil)
})
Convey("on 0.0.0.0", func() {
hexString, err := IPStringToHexString("0.0.0.0")
expected := "0x00000000"
So(hexString, ShouldEqual, expected)
- So(err, ShouldEqual, nil)
+ So(err, ShouldBeNil)
})
})
- Convey("TestHexStringToIPString returns an error", t, func() {
+ Convey("IPStringToHexString returns an error", t, func() {
Convey("on empty string", func() {
hexString, err := IPStringToHexString("")
So(hexString, ShouldEqual, "")
@@ -64,23 +131,35 @@ func TestIPStringToHexString(t *testing.T) {
func TestHexStringToIPString(t *testing.T) {
t.Parallel()
- Convey("TestHexStringToIPString works", t, func() {
+ Convey("HexStringToIP works", t, func() {
Convey("on 0x00000000", func() {
- ipString := HexStringToIP("0x00000000").String()
+ ip, err := HexStringToIP("0x00000000")
+ So(err, ShouldBeNil)
expected := "0.0.0.0"
- So(ipString, ShouldEqual, expected)
+ So(ip.String(), ShouldEqual, expected)
})
Convey("on 0xc0a80001", func() {
- ipString := HexStringToIP("0xc0a80001").String()
+ ip, err := HexStringToIP("0xc0a80001")
+ So(err, ShouldBeNil)
expected := "192.168.0.1"
- So(ipString, ShouldEqual, expected)
+ So(ip.String(), ShouldEqual, expected)
})
Convey("on 0XC0A80002", func() {
- ipString := HexStringToIP("0XC0A80002").String()
+ ip, err := HexStringToIP("0XC0A80002")
+ So(err, ShouldBeNil)
expected := "192.168.0.2"
- So(ipString, ShouldEqual, expected)
+ So(ip.String(), ShouldEqual, expected)
})
})
+ Convey("HexStringToIP returns an error", t, func() {
+ Convey("on empty string", func() {
+ ip, err := HexStringToIP("0XC0A80002")
+ So(err, ShouldBeNil)
+ expected := "192.168.0.2"
+ So(ip.String(), ShouldEqual, expected)
+ })
+ })
+
}
func TestIPStringToHexAndBack(t *testing.T) {
@@ -90,23 +169,29 @@ func TestIPStringToHexAndBack(t *testing.T) {
t, func() {
Convey("on 135.45.1.84", func() {
ip1 := "135.45.1.84"
- value, _ := IPStringToHexString(ip1)
- ip2 := HexStringToIP(value).String()
- So(ip1, ShouldEqual, ip2)
+ value, err := IPStringToHexString(ip1)
+ So(err, ShouldBeNil)
+ ip2, err := HexStringToIP(value)
+ So(err, ShouldBeNil)
+ So(ip1, ShouldEqual, ip2.String())
})
Convey("on 1.2.3.4", func() {
ip1 := "1.2.3.4"
- value, _ := IPStringToHexString(ip1)
- ip2 := HexStringToIP(value).String()
- So(ip1, ShouldEqual, ip2)
+ value, err := IPStringToHexString(ip1)
+ So(err, ShouldBeNil)
+ ip2, err := HexStringToIP(value)
+ So(err, ShouldBeNil)
+ So(ip1, ShouldEqual, ip2.String())
})
Convey("on 255.255.255.255", func() {
ip1 := "255.255.255.255"
- value, _ := IPStringToHexString(ip1)
- ip2 := HexStringToIP(value).String()
- So(ip1, ShouldEqual, ip2)
+ value, err := IPStringToHexString(ip1)
+ So(err, ShouldBeNil)
+ ip2, err := HexStringToIP(value)
+ So(err, ShouldBeNil)
+ So(ip1, ShouldEqual, ip2.String())
})
})
}
« no previous file with comments | « go/src/infra/crimson/server/crimsondb/crimsondb.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698