Skip to content

Commit

Permalink
fix a bug in VLANS.UnmarshalText
Browse files Browse the repository at this point in the history
  • Loading branch information
hujun-open committed Aug 10, 2024
1 parent 8b44fdd commit 8fb0010
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
3 changes: 3 additions & 0 deletions etherconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func (vlans *VLANs) UnmarshalText(text []byte) error {
flist := strings.Split(inputs, sep)
r := new(VLANs)
for _, v := range flist {
if v == "" {
continue
}
n, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("%v is not valid number", v)
Expand Down
90 changes: 89 additions & 1 deletion etherconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,13 +787,64 @@ func TestRUDPConn(t *testing.T) {
type testVLANsCase struct {
v etherconn.VLANs
vs string
umvs string
newIDs []uint16
newv etherconn.VLANs
shouldFail bool
}

func TestVLANs(t *testing.T) {
testCaseList := []testVLANsCase{
{
v: etherconn.VLANs{
&etherconn.VLAN{
ID: 300,
EtherType: 0x8100,
},
},
vs: "|300",
newIDs: []uint16{111},
newv: etherconn.VLANs{
&etherconn.VLAN{
ID: 111,
EtherType: 0x8100,
},
},
},
{
v: etherconn.VLANs{
&etherconn.VLAN{
ID: 300,
EtherType: 0x8100,
},
},
vs: "|300",
umvs: ".300",
newIDs: []uint16{111},
newv: etherconn.VLANs{
&etherconn.VLAN{
ID: 111,
EtherType: 0x8100,
},
},
},
{
v: etherconn.VLANs{
&etherconn.VLAN{
ID: 300,
EtherType: 0x8100,
},
},
vs: "|300",
umvs: "300",
newIDs: []uint16{111},
newv: etherconn.VLANs{
&etherconn.VLAN{
ID: 111,
EtherType: 0x8100,
},
},
},
{
v: etherconn.VLANs{
&etherconn.VLAN{
Expand All @@ -806,6 +857,32 @@ func TestVLANs(t *testing.T) {
},
},
vs: "|100|200",
umvs: "100.200",
newIDs: []uint16{111, 222},
newv: etherconn.VLANs{
&etherconn.VLAN{
ID: 111,
EtherType: 0x8100,
},
&etherconn.VLAN{
ID: 222,
EtherType: 0x8200,
},
},
},
{
v: etherconn.VLANs{
&etherconn.VLAN{
ID: 100,
EtherType: 0x8100,
},
&etherconn.VLAN{
ID: 200,
EtherType: 0x8200,
},
},
vs: "|100|200",
umvs: "100|200",
newIDs: []uint16{111, 222},
newv: etherconn.VLANs{
&etherconn.VLAN{
Expand All @@ -818,7 +895,6 @@ func TestVLANs(t *testing.T) {
},
},
},

{
v: etherconn.VLANs{
&etherconn.VLAN{
Expand Down Expand Up @@ -849,6 +925,18 @@ func TestVLANs(t *testing.T) {
if c.v.String() != c.vs {
return fmt.Errorf("c.v string %v is different from expected %v", c.v.String(), c.vs)
}
ustr := c.vs
if c.umvs != "" {
ustr = c.umvs
}
newvlan := new(etherconn.VLANs)
if err := newvlan.UnmarshalText([]byte(ustr)); err != nil {
return fmt.Errorf("failed to unmarshal %v, %w", c.vs, err)
}
if newvlan.String() != c.v.String() {
return fmt.Errorf("unmarshaled vlan %v is different from expected %v", newvlan.String(), c.v.String())
}

err := c.v.SetIDs(c.newIDs)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require (
github.com/cilium/ebpf v0.4.0 // indirect
github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0 // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)

replace github.com/hujun-open/etherconn => ../
8 changes: 4 additions & 4 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -42,8 +42,8 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/hujun-open/myaddr v0.0.0-20200628224706-46a60dd3e36b
github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0
github.com/vishvananda/netlink v1.1.0
golang.org/x/net v0.17.0
golang.org/x/sys v0.13.0
golang.org/x/net v0.7.0
golang.org/x/sys v0.5.0
)

require (
Expand Down

0 comments on commit 8fb0010

Please sign in to comment.