Skip to content

Commit 8fb0010

Browse files
committed
fix a bug in VLANS.UnmarshalText
1 parent 8b44fdd commit 8fb0010

File tree

5 files changed

+100
-9
lines changed

5 files changed

+100
-9
lines changed

etherconn.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ func (vlans *VLANs) UnmarshalText(text []byte) error {
182182
flist := strings.Split(inputs, sep)
183183
r := new(VLANs)
184184
for _, v := range flist {
185+
if v == "" {
186+
continue
187+
}
185188
n, err := strconv.Atoi(v)
186189
if err != nil {
187190
return fmt.Errorf("%v is not valid number", v)

etherconn_test.go

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,64 @@ func TestRUDPConn(t *testing.T) {
787787
type testVLANsCase struct {
788788
v etherconn.VLANs
789789
vs string
790+
umvs string
790791
newIDs []uint16
791792
newv etherconn.VLANs
792793
shouldFail bool
793794
}
794795

795796
func TestVLANs(t *testing.T) {
796797
testCaseList := []testVLANsCase{
798+
{
799+
v: etherconn.VLANs{
800+
&etherconn.VLAN{
801+
ID: 300,
802+
EtherType: 0x8100,
803+
},
804+
},
805+
vs: "|300",
806+
newIDs: []uint16{111},
807+
newv: etherconn.VLANs{
808+
&etherconn.VLAN{
809+
ID: 111,
810+
EtherType: 0x8100,
811+
},
812+
},
813+
},
814+
{
815+
v: etherconn.VLANs{
816+
&etherconn.VLAN{
817+
ID: 300,
818+
EtherType: 0x8100,
819+
},
820+
},
821+
vs: "|300",
822+
umvs: ".300",
823+
newIDs: []uint16{111},
824+
newv: etherconn.VLANs{
825+
&etherconn.VLAN{
826+
ID: 111,
827+
EtherType: 0x8100,
828+
},
829+
},
830+
},
831+
{
832+
v: etherconn.VLANs{
833+
&etherconn.VLAN{
834+
ID: 300,
835+
EtherType: 0x8100,
836+
},
837+
},
838+
vs: "|300",
839+
umvs: "300",
840+
newIDs: []uint16{111},
841+
newv: etherconn.VLANs{
842+
&etherconn.VLAN{
843+
ID: 111,
844+
EtherType: 0x8100,
845+
},
846+
},
847+
},
797848
{
798849
v: etherconn.VLANs{
799850
&etherconn.VLAN{
@@ -806,6 +857,32 @@ func TestVLANs(t *testing.T) {
806857
},
807858
},
808859
vs: "|100|200",
860+
umvs: "100.200",
861+
newIDs: []uint16{111, 222},
862+
newv: etherconn.VLANs{
863+
&etherconn.VLAN{
864+
ID: 111,
865+
EtherType: 0x8100,
866+
},
867+
&etherconn.VLAN{
868+
ID: 222,
869+
EtherType: 0x8200,
870+
},
871+
},
872+
},
873+
{
874+
v: etherconn.VLANs{
875+
&etherconn.VLAN{
876+
ID: 100,
877+
EtherType: 0x8100,
878+
},
879+
&etherconn.VLAN{
880+
ID: 200,
881+
EtherType: 0x8200,
882+
},
883+
},
884+
vs: "|100|200",
885+
umvs: "100|200",
809886
newIDs: []uint16{111, 222},
810887
newv: etherconn.VLANs{
811888
&etherconn.VLAN{
@@ -818,7 +895,6 @@ func TestVLANs(t *testing.T) {
818895
},
819896
},
820897
},
821-
822898
{
823899
v: etherconn.VLANs{
824900
&etherconn.VLAN{
@@ -849,6 +925,18 @@ func TestVLANs(t *testing.T) {
849925
if c.v.String() != c.vs {
850926
return fmt.Errorf("c.v string %v is different from expected %v", c.v.String(), c.vs)
851927
}
928+
ustr := c.vs
929+
if c.umvs != "" {
930+
ustr = c.umvs
931+
}
932+
newvlan := new(etherconn.VLANs)
933+
if err := newvlan.UnmarshalText([]byte(ustr)); err != nil {
934+
return fmt.Errorf("failed to unmarshal %v, %w", c.vs, err)
935+
}
936+
if newvlan.String() != c.v.String() {
937+
return fmt.Errorf("unmarshaled vlan %v is different from expected %v", newvlan.String(), c.v.String())
938+
}
939+
852940
err := c.v.SetIDs(c.newIDs)
853941
if err != nil {
854942
return err

example/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ require (
1515
github.com/cilium/ebpf v0.4.0 // indirect
1616
github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0 // indirect
1717
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
18-
golang.org/x/net v0.17.0 // indirect
19-
golang.org/x/sys v0.13.0 // indirect
18+
golang.org/x/net v0.7.0 // indirect
19+
golang.org/x/sys v0.5.0 // indirect
2020
)
2121

2222
replace github.com/hujun-open/etherconn => ../

example/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
3232
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
3333
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3434
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
35-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
36-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
35+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
36+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
3737
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3838
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3939
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -42,8 +42,8 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7w
4242
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4343
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4444
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
46-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
45+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
46+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4747
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4848
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
4949
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ require (
99
github.com/hujun-open/myaddr v0.0.0-20200628224706-46a60dd3e36b
1010
github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0
1111
github.com/vishvananda/netlink v1.1.0
12-
golang.org/x/net v0.17.0
13-
golang.org/x/sys v0.13.0
12+
golang.org/x/net v0.7.0
13+
golang.org/x/sys v0.5.0
1414
)
1515

1616
require (

0 commit comments

Comments
 (0)