forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request: dhcpd: do not assume mac addrs of 6 bytes
Closes AdguardTeam#2828. Squashed commit of the following: commit 26c6cf8 Author: Ainar Garipov <[email protected]> Date: Tue Mar 30 17:43:53 2021 +0300 dhcpd: do not assume mac addrs of 6 bytes
- Loading branch information
Showing
7 changed files
with
204 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package aghnet | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/agherr" | ||
) | ||
|
||
// ValidateHardwareAddress returns an error if hwa is not a valid EUI-48, | ||
// EUI-64, or 20-octet InfiniBand link-layer address. | ||
func ValidateHardwareAddress(hwa net.HardwareAddr) (err error) { | ||
defer agherr.Annotate("validating hardware address %q: %w", &err, hwa) | ||
|
||
switch l := len(hwa); l { | ||
case 0: | ||
return agherr.Error("address is empty") | ||
case 6, 8, 20: | ||
return nil | ||
default: | ||
return fmt.Errorf("bad len: %d", l) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package aghnet | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateHardwareAddress(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
wantErrMsg string | ||
in net.HardwareAddr | ||
}{{ | ||
name: "success_eui_48", | ||
wantErrMsg: "", | ||
in: net.HardwareAddr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, | ||
}, { | ||
name: "success_eui_64", | ||
wantErrMsg: "", | ||
in: net.HardwareAddr{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, | ||
}, { | ||
name: "success_infiniband", | ||
wantErrMsg: "", | ||
in: net.HardwareAddr{ | ||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | ||
0x10, 0x11, 0x12, 0x13, | ||
}, | ||
}, { | ||
name: "error_nil", | ||
wantErrMsg: `validating hardware address "": address is empty`, | ||
in: nil, | ||
}, { | ||
name: "error_empty", | ||
wantErrMsg: `validating hardware address "": address is empty`, | ||
in: net.HardwareAddr{}, | ||
}, { | ||
name: "error_bad", | ||
wantErrMsg: `validating hardware address "00:01:02:03": bad len: 4`, | ||
in: net.HardwareAddr{0x00, 0x01, 0x02, 0x03}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := ValidateHardwareAddress(tc.in) | ||
if tc.wantErrMsg == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
assert.Equal(t, tc.wantErrMsg, err.Error()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.