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

Side by Side 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, 5 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 unified diff | Download patch
« no previous file with comments | « go/src/infra/crimson/server/crimsondb/crimsondb.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package crimsondb 5 package crimsondb
6 6
7 import ( 7 import (
8 "database/sql/driver" 8 "database/sql/driver"
9 "testing" 9 "testing"
10 10
11 "golang.org/x/net/context" 11 "golang.org/x/net/context"
12 12
13 . "github.com/smartystreets/goconvey/convey" 13 . "github.com/smartystreets/goconvey/convey"
14 14
15 crimson "infra/crimson/proto" 15 crimson "infra/crimson/proto"
16 "infra/crimson/server/sqlmock" 16 "infra/crimson/server/sqlmock"
17 ) 17 )
18 18
19 func TestMacAddrStringToHexString(t *testing.T) {
20 t.Parallel()
21 Convey("MacAddrStringToHexString works", t, func() {
22 Convey("on 00:00:00:00:00:00", func() {
23 addr, err := MacAddrStringToHexString("00:00:00:00:00:00 ")
24 So(err, ShouldBeNil)
25 So(addr, ShouldEqual, "0x000000000000")
26 })
27 Convey("on 01:23:45:67:89:ab", func() {
28 addr, err := MacAddrStringToHexString("01:23:45:67:89:ab ")
29 So(err, ShouldBeNil)
30 So(addr, ShouldEqual, "0x0123456789ab")
31 })
32 })
33
34 Convey("MacAddrStringToHexString returns an error", t, func() {
35 Convey("on empty string", func() {
36 _, err := MacAddrStringToHexString("")
37 So(err, ShouldNotBeNil)
38 })
39 Convey("on 000000000000", func() {
40 _, err := MacAddrStringToHexString("000000000000")
41 So(err, ShouldNotBeNil)
42 })
43 Convey("on 'deadmeat'", func() {
44 _, err := MacAddrStringToHexString("deadmeat")
45 So(err, ShouldNotBeNil)
46 })
47 })
48 }
49
50 func TestHexStringToHardwareAddr(t *testing.T) {
51 t.Parallel()
52
53 Convey("HexStringToHardwareAddr works", t, func() {
54 Convey("on 0x000000000000", func() {
55 hw, err := HexStringToHardwareAddr("0x000000000000")
56 So(err, ShouldBeNil)
57 So(hw.String(), ShouldEqual, "00:00:00:00:00:00")
58 })
59 Convey("on 0x0123456789ab", func() {
60 hw, err := HexStringToHardwareAddr("0x0123456789ab")
61 So(err, ShouldBeNil)
62 So(hw.String(), ShouldEqual, "01:23:45:67:89:ab")
63 })
64 })
65
66 Convey("HexStringToHardwareAddr returns an error", t, func() {
67 Convey("on empty string", func() {
68 _, err := HexStringToHardwareAddr("")
69 So(err, ShouldNotBeNil)
70 })
71 Convey("on 000000000000", func() {
72 _, err := HexStringToHardwareAddr("000000000000")
73 So(err, ShouldNotBeNil)
74 })
75 Convey("on '0xdeaddeadmeat'", func() {
76 _, err := HexStringToHardwareAddr("0xdeaddeadmeat")
77 So(err, ShouldNotBeNil)
78 })
79 Convey("on 'Aa000000000000'", func() {
80 _, err := HexStringToHardwareAddr("Aa000000000000")
81 So(err, ShouldNotBeNil)
82 })
83 })
84 }
85
19 func TestIPStringToHexString(t *testing.T) { 86 func TestIPStringToHexString(t *testing.T) {
20 t.Parallel() 87 t.Parallel()
21 » Convey("TestHexStringToIPString works", t, func() { 88 » Convey("IPStringToHexString works", t, func() {
22 Convey("on 192.168.0.1", func() { 89 Convey("on 192.168.0.1", func() {
23 hexString, err := IPStringToHexString("192.168.0.1") 90 hexString, err := IPStringToHexString("192.168.0.1")
24 expected := "0xc0a80001" 91 expected := "0xc0a80001"
25 So(hexString, ShouldEqual, expected) 92 So(hexString, ShouldEqual, expected)
26 » » » So(err, ShouldEqual, nil) 93 » » » So(err, ShouldBeNil)
27 }) 94 })
28 95
29 Convey("on 0.0.0.0", func() { 96 Convey("on 0.0.0.0", func() {
30 hexString, err := IPStringToHexString("0.0.0.0") 97 hexString, err := IPStringToHexString("0.0.0.0")
31 expected := "0x00000000" 98 expected := "0x00000000"
32 So(hexString, ShouldEqual, expected) 99 So(hexString, ShouldEqual, expected)
33 » » » So(err, ShouldEqual, nil) 100 » » » So(err, ShouldBeNil)
34 }) 101 })
35 }) 102 })
36 103
37 » Convey("TestHexStringToIPString returns an error", t, func() { 104 » Convey("IPStringToHexString returns an error", t, func() {
38 Convey("on empty string", func() { 105 Convey("on empty string", func() {
39 hexString, err := IPStringToHexString("") 106 hexString, err := IPStringToHexString("")
40 So(hexString, ShouldEqual, "") 107 So(hexString, ShouldEqual, "")
41 So(err, ShouldNotEqual, nil) 108 So(err, ShouldNotEqual, nil)
42 }) 109 })
43 110
44 Convey("on non-numerical string with dots", func() { 111 Convey("on non-numerical string with dots", func() {
45 hexString, err := IPStringToHexString("ah.ah.ah.ah") 112 hexString, err := IPStringToHexString("ah.ah.ah.ah")
46 So(hexString, ShouldEqual, "") 113 So(hexString, ShouldEqual, "")
47 So(err, ShouldNotEqual, nil) 114 So(err, ShouldNotEqual, nil)
48 }) 115 })
49 116
50 Convey("on non-numerical string", func() { 117 Convey("on non-numerical string", func() {
51 hexString, err := IPStringToHexString("aaaaaah") 118 hexString, err := IPStringToHexString("aaaaaah")
52 So(hexString, ShouldEqual, "") 119 So(hexString, ShouldEqual, "")
53 So(err, ShouldNotEqual, nil) 120 So(err, ShouldNotEqual, nil)
54 }) 121 })
55 122
56 Convey("on '128.26.4'", func() { 123 Convey("on '128.26.4'", func() {
57 hexString, err := IPStringToHexString("128.26.4") 124 hexString, err := IPStringToHexString("128.26.4")
58 So(hexString, ShouldEqual, "") 125 So(hexString, ShouldEqual, "")
59 So(err, ShouldNotEqual, nil) 126 So(err, ShouldNotEqual, nil)
60 }) 127 })
61 }) 128 })
62 129
63 } 130 }
64 131
65 func TestHexStringToIPString(t *testing.T) { 132 func TestHexStringToIPString(t *testing.T) {
66 t.Parallel() 133 t.Parallel()
67 » Convey("TestHexStringToIPString works", t, func() { 134 » Convey("HexStringToIP works", t, func() {
68 Convey("on 0x00000000", func() { 135 Convey("on 0x00000000", func() {
69 » » » ipString := HexStringToIP("0x00000000").String() 136 » » » ip, err := HexStringToIP("0x00000000")
137 » » » So(err, ShouldBeNil)
70 expected := "0.0.0.0" 138 expected := "0.0.0.0"
71 » » » So(ipString, ShouldEqual, expected) 139 » » » So(ip.String(), ShouldEqual, expected)
72 }) 140 })
73 Convey("on 0xc0a80001", func() { 141 Convey("on 0xc0a80001", func() {
74 » » » ipString := HexStringToIP("0xc0a80001").String() 142 » » » ip, err := HexStringToIP("0xc0a80001")
143 » » » So(err, ShouldBeNil)
75 expected := "192.168.0.1" 144 expected := "192.168.0.1"
76 » » » So(ipString, ShouldEqual, expected) 145 » » » So(ip.String(), ShouldEqual, expected)
77 }) 146 })
78 Convey("on 0XC0A80002", func() { 147 Convey("on 0XC0A80002", func() {
79 » » » ipString := HexStringToIP("0XC0A80002").String() 148 » » » ip, err := HexStringToIP("0XC0A80002")
149 » » » So(err, ShouldBeNil)
80 expected := "192.168.0.2" 150 expected := "192.168.0.2"
81 » » » So(ipString, ShouldEqual, expected) 151 » » » So(ip.String(), ShouldEqual, expected)
82 }) 152 })
83 }) 153 })
154 Convey("HexStringToIP returns an error", t, func() {
155 Convey("on empty string", func() {
156 ip, err := HexStringToIP("0XC0A80002")
157 So(err, ShouldBeNil)
158 expected := "192.168.0.2"
159 So(ip.String(), ShouldEqual, expected)
160 })
161 })
162
84 } 163 }
85 164
86 func TestIPStringToHexAndBack(t *testing.T) { 165 func TestIPStringToHexAndBack(t *testing.T) {
87 t.Parallel() 166 t.Parallel()
88 // Check that function which are supposed to be exact inverse actually a re. 167 // Check that function which are supposed to be exact inverse actually a re.
89 Convey("HexStringToIP and IPStringToHexString are inverse of each other" , 168 Convey("HexStringToIP and IPStringToHexString are inverse of each other" ,
90 t, func() { 169 t, func() {
91 Convey("on 135.45.1.84", func() { 170 Convey("on 135.45.1.84", func() {
92 ip1 := "135.45.1.84" 171 ip1 := "135.45.1.84"
93 » » » » value, _ := IPStringToHexString(ip1) 172 » » » » value, err := IPStringToHexString(ip1)
94 » » » » ip2 := HexStringToIP(value).String() 173 » » » » So(err, ShouldBeNil)
95 » » » » So(ip1, ShouldEqual, ip2) 174 » » » » ip2, err := HexStringToIP(value)
175 » » » » So(err, ShouldBeNil)
176 » » » » So(ip1, ShouldEqual, ip2.String())
96 }) 177 })
97 178
98 Convey("on 1.2.3.4", func() { 179 Convey("on 1.2.3.4", func() {
99 ip1 := "1.2.3.4" 180 ip1 := "1.2.3.4"
100 » » » » value, _ := IPStringToHexString(ip1) 181 » » » » value, err := IPStringToHexString(ip1)
101 » » » » ip2 := HexStringToIP(value).String() 182 » » » » So(err, ShouldBeNil)
102 » » » » So(ip1, ShouldEqual, ip2) 183 » » » » ip2, err := HexStringToIP(value)
184 » » » » So(err, ShouldBeNil)
185 » » » » So(ip1, ShouldEqual, ip2.String())
103 }) 186 })
104 187
105 Convey("on 255.255.255.255", func() { 188 Convey("on 255.255.255.255", func() {
106 ip1 := "255.255.255.255" 189 ip1 := "255.255.255.255"
107 » » » » value, _ := IPStringToHexString(ip1) 190 » » » » value, err := IPStringToHexString(ip1)
108 » » » » ip2 := HexStringToIP(value).String() 191 » » » » So(err, ShouldBeNil)
109 » » » » So(ip1, ShouldEqual, ip2) 192 » » » » ip2, err := HexStringToIP(value)
193 » » » » So(err, ShouldBeNil)
194 » » » » So(ip1, ShouldEqual, ip2.String())
110 }) 195 })
111 }) 196 })
112 } 197 }
113 198
114 func TestSelectIPRange(t *testing.T) { 199 func TestSelectIPRange(t *testing.T) {
115 t.Parallel() 200 t.Parallel()
116 ctx := context.Background() 201 ctx := context.Background()
117 db, conn := sqlmock.NewMockDB() 202 db, conn := sqlmock.NewMockDB()
118 ctx = UseDB(ctx, db) 203 ctx = UseDB(ctx, db)
119 204
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 []driver.Value{ 389 []driver.Value{
305 "site0", 390 "site0",
306 "0x01020314", 391 "0x01020314",
307 "0x01020304"}) 392 "0x01020304"})
308 393
309 query, err = conn.PopOldestQuery() 394 query, err = conn.PopOldestQuery()
310 So(err, ShouldNotBeNil) 395 So(err, ShouldNotBeNil)
311 }) 396 })
312 }) 397 })
313 } 398 }
OLDNEW
« 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