Skip to content

Commit 9ce9349

Browse files
committed
Fixes:
- Removeed JSON tags and use unexported fields (ofPort, macAddress) following L62-66 pattern - Removed redundant format checks in callback (UnmarshalText handles all validation) - Fixed string slicing to search full string for closeParen and addrIdx - Consolidated error messages to generic 'invalid port mapping format'
1 parent e61e803 commit 9ce9349

File tree

2 files changed

+71
-84
lines changed

2 files changed

+71
-84
lines changed

ovs/openflow.go

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ type flowDirective struct {
7474

7575
// PortMapping contains the ofport number and MAC address for an interface.
7676
type PortMapping struct {
77-
// OfPort specifies the OpenFlow port number.
78-
OfPort int `json:"ofPort"`
77+
// ofPort specifies the OpenFlow port number.
78+
ofPort int
7979

80-
// MACAddress specifies the MAC address of the interface.
81-
MACAddress string `json:"macAddress"`
80+
// macAddress specifies the MAC address of the interface.
81+
macAddress string
8282
}
8383

8484
// UnmarshalText unmarshals a PortMapping from textual form as output by
@@ -93,15 +93,14 @@ func (p *PortMapping) UnmarshalText(b []byte) error {
9393
// Find the opening parenthesis
9494
openParen := strings.IndexByte(s, '(')
9595
if openParen == -1 {
96-
return fmt.Errorf("invalid port mapping format: missing opening parenthesis")
96+
return fmt.Errorf("invalid port mapping format")
9797
}
9898

99-
// Find the closing parenthesis
100-
closeParen := strings.IndexByte(s[openParen:], ')')
101-
if closeParen == -1 {
102-
return fmt.Errorf("invalid port mapping format: missing closing parenthesis")
99+
// Find the closing parenthesis in the full string
100+
closeParen := strings.IndexByte(s, ')')
101+
if closeParen == -1 || closeParen <= openParen {
102+
return fmt.Errorf("invalid port mapping format")
103103
}
104-
closeParen += openParen
105104

106105
// Extract ofport number (before opening parenthesis)
107106
ofportStr := strings.TrimSpace(s[:openParen])
@@ -115,13 +114,13 @@ func (p *PortMapping) UnmarshalText(b []byte) error {
115114
return fmt.Errorf("ofport %d out of valid range [0, 65535]", ofport)
116115
}
117116

118-
// Find "addr:" after the closing parenthesis
117+
// Find "addr:" after the closing parenthesis in the full string
119118
addrPrefix := ": addr:"
120-
addrIdx := strings.Index(s[closeParen:], addrPrefix)
121-
if addrIdx == -1 {
122-
return fmt.Errorf("invalid port mapping format: missing addr prefix")
119+
addrIdx := strings.Index(s, addrPrefix)
120+
if addrIdx == -1 || addrIdx <= closeParen {
121+
return fmt.Errorf("invalid port mapping format")
123122
}
124-
addrIdx += closeParen + len(addrPrefix)
123+
addrIdx += len(addrPrefix)
125124

126125
// Extract MAC address (after "addr:")
127126
macAddress := strings.TrimSpace(s[addrIdx:])
@@ -131,9 +130,9 @@ func (p *PortMapping) UnmarshalText(b []byte) error {
131130
return fmt.Errorf("invalid MAC address %q: %w", macAddress, err)
132131
}
133132

134-
p.OfPort = ofport
133+
p.ofPort = ofport
135134
// Note: interface name is not stored in PortMapping, it's used as map key
136-
p.MACAddress = macAddress
135+
p.macAddress = macAddress
137136

138137
return nil
139138
}
@@ -422,31 +421,19 @@ func (o *OpenFlowService) DumpPortMappings(bridge string) (map[string]*PortMappi
422421

423422
mappings := make(map[string]*PortMapping)
424423
err = parseEachPort(out, showPrefix, func(line []byte) error {
425-
// Check if line matches port format: " 7(interface1): addr:..."
426-
if !bytes.Contains(line, []byte("(")) || !bytes.Contains(line, []byte("): addr:")) {
427-
// Skip lines that don't match the port format
428-
return nil
429-
}
430-
431-
// Extract interface name from the line for map key (do this once)
432-
openParen := bytes.IndexByte(line, '(')
433-
if openParen == -1 {
434-
return nil
435-
}
436-
closeParen := bytes.IndexByte(line[openParen:], ')')
437-
if closeParen == -1 {
438-
return nil
439-
}
440-
closeParen += openParen
441-
interfaceName := string(line[openParen+1 : closeParen])
442-
443-
// Parse the PortMapping
424+
// Parse the PortMapping - UnmarshalText will validate format
444425
pm := new(PortMapping)
445426
if err := pm.UnmarshalText(line); err != nil {
446427
// Skip malformed lines
447428
return nil
448429
}
449430

431+
// Extract interface name from the validated line for map key
432+
s := string(line)
433+
openParen := strings.IndexByte(s, '(')
434+
closeParen := strings.IndexByte(s, ')')
435+
interfaceName := s[openParen+1 : closeParen]
436+
450437
mappings[interfaceName] = pm
451438
return nil
452439
})

ovs/openflow_test.go

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,28 +1419,28 @@ func mustVerifyFlowBundle(t *testing.T, stdin io.Reader, flows []*Flow, matchFlo
14191419
func TestClientOpenFlowDumpPortMappingsOK(t *testing.T) {
14201420
want := map[string]*PortMapping{
14211421
"interface1": {
1422-
OfPort: 7,
1423-
MACAddress: "fe:4f:76:09:88:2b",
1422+
ofPort: 7,
1423+
macAddress: "fe:4f:76:09:88:2b",
14241424
},
14251425
"interface2": {
1426-
OfPort: 8,
1427-
MACAddress: "fe:be:7b:0d:53:d8",
1426+
ofPort: 8,
1427+
macAddress: "fe:be:7b:0d:53:d8",
14281428
},
14291429
"interface3": {
1430-
OfPort: 9,
1431-
MACAddress: "fe:b6:4c:d5:40:79",
1430+
ofPort: 9,
1431+
macAddress: "fe:b6:4c:d5:40:79",
14321432
},
14331433
"interface4": {
1434-
OfPort: 20,
1435-
MACAddress: "fe:cf:a6:90:30:29",
1434+
ofPort: 20,
1435+
macAddress: "fe:cf:a6:90:30:29",
14361436
},
14371437
"LOCAL": {
1438-
OfPort: 65534,
1439-
MACAddress: "fe:74:0f:80:cf:9a",
1438+
ofPort: 65534,
1439+
macAddress: "fe:74:0f:80:cf:9a",
14401440
},
14411441
"eth0": {
1442-
OfPort: 1,
1443-
MACAddress: "aa:bb:cc:dd:ee:ff",
1442+
ofPort: 1,
1443+
macAddress: "aa:bb:cc:dd:ee:ff",
14441444
},
14451445
}
14461446

@@ -1511,14 +1511,14 @@ actions: output enqueue set_vlan_vid set_vlan_pcp strip_vlan mod_dl_src mod_dl_d
15111511
t.Fatalf("missing mapping for interface %q", name)
15121512
}
15131513

1514-
if wantMapping.OfPort != gotMapping.OfPort {
1515-
t.Fatalf("unexpected OfPort for %q:\n- want: %d\n- got: %d",
1516-
name, wantMapping.OfPort, gotMapping.OfPort)
1514+
if wantMapping.ofPort != gotMapping.ofPort {
1515+
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1516+
name, wantMapping.ofPort, gotMapping.ofPort)
15171517
}
15181518

1519-
if wantMapping.MACAddress != gotMapping.MACAddress {
1520-
t.Fatalf("unexpected MACAddress for %q:\n- want: %q\n- got: %q",
1521-
name, wantMapping.MACAddress, gotMapping.MACAddress)
1519+
if wantMapping.macAddress != gotMapping.macAddress {
1520+
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1521+
name, wantMapping.macAddress, gotMapping.macAddress)
15221522
}
15231523
}
15241524

@@ -1548,12 +1548,12 @@ func TestClientOpenFlowDumpPortMappingsEmptyOutput(t *testing.T) {
15481548
func TestClientOpenFlowDumpPortMappingsAllInterfaces(t *testing.T) {
15491549
want := map[string]*PortMapping{
15501550
"eth0": {
1551-
OfPort: 1,
1552-
MACAddress: "aa:bb:cc:dd:ee:ff",
1551+
ofPort: 1,
1552+
macAddress: "aa:bb:cc:dd:ee:ff",
15531553
},
15541554
"eth1": {
1555-
OfPort: 2,
1556-
MACAddress: "11:22:33:44:55:66",
1555+
ofPort: 2,
1556+
macAddress: "11:22:33:44:55:66",
15571557
},
15581558
}
15591559

@@ -1582,14 +1582,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
15821582
t.Fatalf("missing mapping for interface %q", name)
15831583
}
15841584

1585-
if wantMapping.OfPort != gotMapping.OfPort {
1586-
t.Fatalf("unexpected OfPort for %q:\n- want: %d\n- got: %d",
1587-
name, wantMapping.OfPort, gotMapping.OfPort)
1585+
if wantMapping.ofPort != gotMapping.ofPort {
1586+
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1587+
name, wantMapping.ofPort, gotMapping.ofPort)
15881588
}
15891589

1590-
if wantMapping.MACAddress != gotMapping.MACAddress {
1591-
t.Fatalf("unexpected MACAddress for %q:\n- want: %q\n- got: %q",
1592-
name, wantMapping.MACAddress, gotMapping.MACAddress)
1590+
if wantMapping.macAddress != gotMapping.macAddress {
1591+
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1592+
name, wantMapping.macAddress, gotMapping.macAddress)
15931593
}
15941594
}
15951595
}
@@ -1605,8 +1605,8 @@ func TestClientOpenFlowDumpPortMappingsForInterface(t *testing.T) {
16051605
name: "LOCAL interface",
16061606
interfaceName: "LOCAL",
16071607
want: &PortMapping{
1608-
OfPort: 65534,
1609-
MACAddress: "fe:74:0f:80:cf:9a",
1608+
ofPort: 65534,
1609+
macAddress: "fe:74:0f:80:cf:9a",
16101610
},
16111611
output: `
16121612
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1617,8 +1617,8 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
16171617
name: "interface1",
16181618
interfaceName: "interface1",
16191619
want: &PortMapping{
1620-
OfPort: 7,
1621-
MACAddress: "fe:4f:76:09:88:2b",
1620+
ofPort: 7,
1621+
macAddress: "fe:4f:76:09:88:2b",
16221622
},
16231623
output: `
16241624
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1645,14 +1645,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
16451645
t.Fatalf("missing mapping for interface %q", tt.interfaceName)
16461646
}
16471647

1648-
if tt.want.OfPort != gotMapping.OfPort {
1649-
t.Fatalf("unexpected OfPort for %q:\n- want: %d\n- got: %d",
1650-
tt.interfaceName, tt.want.OfPort, gotMapping.OfPort)
1648+
if tt.want.ofPort != gotMapping.ofPort {
1649+
t.Fatalf("unexpected ofPort for %q:\n- want: %d\n- got: %d",
1650+
tt.interfaceName, tt.want.ofPort, gotMapping.ofPort)
16511651
}
16521652

1653-
if tt.want.MACAddress != gotMapping.MACAddress {
1654-
t.Fatalf("unexpected MACAddress for %q:\n- want: %q\n- got: %q",
1655-
tt.interfaceName, tt.want.MACAddress, gotMapping.MACAddress)
1653+
if tt.want.macAddress != gotMapping.macAddress {
1654+
t.Fatalf("unexpected macAddress for %q:\n- want: %q\n- got: %q",
1655+
tt.interfaceName, tt.want.macAddress, gotMapping.macAddress)
16561656
}
16571657
})
16581658
}
@@ -1689,8 +1689,8 @@ func TestClientOpenFlowDumpPortMapping(t *testing.T) {
16891689
name: "successful retrieval",
16901690
interfaceName: "interface1",
16911691
want: &PortMapping{
1692-
OfPort: 7,
1693-
MACAddress: "fe:4f:76:09:88:2b",
1692+
ofPort: 7,
1693+
macAddress: "fe:4f:76:09:88:2b",
16941694
},
16951695
output: `
16961696
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1713,8 +1713,8 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
17131713
name: "LOCAL interface",
17141714
interfaceName: "LOCAL",
17151715
want: &PortMapping{
1716-
OfPort: 65534,
1717-
MACAddress: "fe:74:0f:80:cf:9a",
1716+
ofPort: 65534,
1717+
macAddress: "fe:74:0f:80:cf:9a",
17181718
},
17191719
output: `
17201720
OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
@@ -1747,14 +1747,14 @@ OFPT_FEATURES_REPLY (xid=0x2): dpid:0000000000000001
17471747
t.Fatalf("unexpected error: %v", err)
17481748
}
17491749

1750-
if tt.want.OfPort != got.OfPort {
1751-
t.Fatalf("unexpected OfPort:\n- want: %d\n- got: %d",
1752-
tt.want.OfPort, got.OfPort)
1750+
if tt.want.ofPort != got.ofPort {
1751+
t.Fatalf("unexpected ofPort:\n- want: %d\n- got: %d",
1752+
tt.want.ofPort, got.ofPort)
17531753
}
17541754

1755-
if tt.want.MACAddress != got.MACAddress {
1756-
t.Fatalf("unexpected MACAddress:\n- want: %q\n- got: %q",
1757-
tt.want.MACAddress, got.MACAddress)
1755+
if tt.want.macAddress != got.macAddress {
1756+
t.Fatalf("unexpected macAddress:\n- want: %q\n- got: %q",
1757+
tt.want.macAddress, got.macAddress)
17581758
}
17591759
})
17601760
}

0 commit comments

Comments
 (0)