Skip to content

Commit b82c47f

Browse files
shanhuhai5739iyangsj
authored andcommitted
Tweak bfe_util/ipdict (bfenetworks#286)
1 parent cb9183e commit b82c47f

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

bfe_util/ipdict/ip_loc_table.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ipdict
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"net"
2122
"sort"
@@ -99,7 +100,7 @@ func (t *IpLocationTable) Add(startIP, endIP net.IP, location string) error {
99100
}
100101

101102
if t.offset >= t.maxSize {
102-
return fmt.Errorf("Add():caused by table is full")
103+
return errors.New("Add():caused by table is full")
103104
}
104105

105106
startIP16 := startIP.To16()

bfe_util/ipdict/ipdict_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package ipdict
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"net"
2120
"testing"
@@ -30,13 +29,13 @@ func checkEqual(src, dst ipPairs) bool {
3029
return false
3130
}
3231
for i := 0; i < len(src); i++ {
33-
if !bytes.Equal(src[i].startIP, dst[i].startIP) {
32+
if !src[i].startIP.Equal(dst[i].startIP) {
3433
fmt.Printf("checkEqual(): start element [%d] and [%d] are not equal!\n",
3534
src[i].startIP, dst[i].startIP)
3635
return false
3736
}
3837

39-
if !bytes.Equal(src[i].endIP, dst[i].endIP) {
38+
if !src[i].endIP.Equal(dst[i].endIP) {
4039
fmt.Printf("checkEqual(): end element [%d] and [%d] are not equal!\n",
4140
src[i].endIP, dst[i].endIP)
4241
return false
@@ -156,11 +155,11 @@ func TestSwap_Case0(t *testing.T) {
156155

157156
p.Swap(0, 1)
158157

159-
if !bytes.Equal(ip1, p[1].startIP) || !bytes.Equal(ip1, p[1].endIP) {
158+
if !ip1.Equal(p[1].startIP) || !ip1.Equal(p[1].endIP) {
160159
t.Errorf("Swap(): %s and %s swap failed!", ipStr1, ipStr2)
161160
}
162161

163-
if !bytes.Equal(ip2, p[0].startIP) || !bytes.Equal(ip2, p[0].endIP) {
162+
if !ip2.Equal(p[0].startIP) || !ip2.Equal(p[0].endIP) {
164163
t.Errorf("Swap(): %s and %s swap failed!", ipStr1, ipStr2)
165164
}
166165

@@ -177,11 +176,11 @@ func TestSwap_Case0(t *testing.T) {
177176

178177
p2.Swap(0, 1)
179178

180-
if !bytes.Equal(ip1, p2[1].startIP) || !bytes.Equal(ip1, p2[1].endIP) {
179+
if !ip1.Equal(p2[1].startIP) || !ip1.Equal(p2[1].endIP) {
181180
t.Errorf("Swap(): %s and %s swap failed!", ipStr1, ipStr2)
182181
}
183182

184-
if !bytes.Equal(ip2, p2[0].startIP) || !bytes.Equal(ip2, p2[0].endIP) {
183+
if !ip2.Equal(p2[0].startIP) || !ip2.Equal(p2[0].endIP) {
185184
t.Errorf("Swap(): %s and %s swap failed!", ipStr1, ipStr2)
186185
}
187186
}

bfe_util/ipdict/txt_load/txt_info.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package txt_load
1616

1717
import (
1818
"bufio"
19-
"bytes"
2019
"encoding/json"
2120
"fmt"
2221
"net"
@@ -132,7 +131,7 @@ func getActualFileInfo(path string) (*MetaInfo, error) {
132131
}
133132

134133
// insert start ip and end ip into dict
135-
if bytes.Equal(startIP, endIP) {
134+
if startIP.Equal(endIP) {
136135
singleIPCounter += 1
137136
} else {
138137
pairIPCounter += 1

bfe_util/ipdict/txt_load/txt_load_test.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package txt_load
1616

1717
import (
18-
"bytes"
1918
"net"
2019
"testing"
2120
)
@@ -32,39 +31,39 @@ func TestCheckSplit_Case0(t *testing.T) {
3231

3332
line = "1.1.1.1 2.2.2.2"
3433
startIP, endIP, err = checkSplit(line, " ")
35-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
36-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
34+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
35+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
3736
err != nil {
3837
t.Error("TestCheckSplit():", err)
3938
}
4039

4140
line = "1.1.1.1"
4241
startIP, endIP, err = checkSplit(line, " ")
43-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
44-
!bytes.Equal(endIP, net.ParseIP("1.1.1.1")) ||
42+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
43+
!endIP.Equal(net.ParseIP("1.1.1.1")) ||
4544
err != nil {
4645
t.Error("TestCheckSplit():", err)
4746
}
4847

4948
line = "1.1.1.1 2.2.2.2"
5049
startIP, endIP, err = checkSplit(line, " ")
51-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
52-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
50+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
51+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
5352
err != nil {
5453
t.Error("TestCheckSplit():", err)
5554
}
5655

5756
line = "1.1.1.1 \t\t 2.2.2.2"
5857
startIP, endIP, err = checkSplit(line, " ")
59-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
60-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
58+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
59+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
6160
err != nil {
6261
t.Error("TestCheckSplit():", err)
6362
}
6463
line = "1::1 \t\t 1::FFFF"
6564
startIP, endIP, err = checkSplit(line, " ")
66-
if !bytes.Equal(startIP, net.ParseIP("1::1")) ||
67-
!bytes.Equal(endIP, net.ParseIP("1::FFFF")) ||
65+
if !startIP.Equal(net.ParseIP("1::1")) ||
66+
!endIP.Equal(net.ParseIP("1::FFFF")) ||
6867
err != nil {
6968
t.Error("TestCheckSplit():", err)
7069
}
@@ -113,47 +112,47 @@ func TestCheckLine_Case0(t *testing.T) {
113112

114113
line = "1.1.1.1 2.2.2.2"
115114
startIP, endIP, err = checkLine(line)
116-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
117-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
115+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
116+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
118117
err != nil {
119118
t.Error("TestCheckLine():", err)
120119
}
121120

122121
line = "1.1.1.1"
123122
startIP, endIP, err = checkLine(line)
124-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
125-
!bytes.Equal(endIP, net.ParseIP("1.1.1.1")) ||
123+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
124+
!endIP.Equal(net.ParseIP("1.1.1.1")) ||
126125
err != nil {
127126
t.Error("TestCheckLine():", err)
128127
}
129128

130129
line = "1.1.1.1 2.2.2.2"
131130
startIP, endIP, err = checkLine(line)
132-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
133-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
131+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
132+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
134133
err != nil {
135134
t.Error("TestCheckLine():", err)
136135
}
137136

138137
line = "1.1.1.1 \t\t 2.2.2.2"
139138
startIP, endIP, err = checkLine(line)
140-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
141-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
139+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
140+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
142141
err != nil {
143142
t.Error("TestCheckLine():", err)
144143
}
145144

146145
line = "1.1.1.1\t\t \t\t2.2.2.2"
147146
startIP, endIP, err = checkLine(line)
148-
if !bytes.Equal(startIP, net.ParseIP("1.1.1.1")) ||
149-
!bytes.Equal(endIP, net.ParseIP("2.2.2.2")) ||
147+
if !startIP.Equal(net.ParseIP("1.1.1.1")) ||
148+
!endIP.Equal(net.ParseIP("2.2.2.2")) ||
150149
err != nil {
151150
t.Error("TestCheckLine():", err)
152151
}
153152
line = "1::1\t\t \t\t1::FFFF"
154153
startIP, endIP, err = checkLine(line)
155-
if !bytes.Equal(startIP, net.ParseIP("1::1")) ||
156-
!bytes.Equal(endIP, net.ParseIP("1::FFFF")) ||
154+
if !startIP.Equal(net.ParseIP("1::1")) ||
155+
!endIP.Equal(net.ParseIP("1::FFFF")) ||
157156
err != nil {
158157
t.Error("TestCheckLine():", err)
159158
}

0 commit comments

Comments
 (0)