lang: core: net: Improve formatting of the mac functions
This commit is contained in:
@@ -81,13 +81,13 @@ func MacFmt(ctx context.Context, input []types.Value) (types.Value, error) {
|
|||||||
if len(mac) != len("00:00:00:00:00:00") {
|
if len(mac) != len("00:00:00:00:00:00") {
|
||||||
return nil, fmt.Errorf("invalid MAC address length: %s", mac)
|
return nil, fmt.Errorf("invalid MAC address length: %s", mac)
|
||||||
}
|
}
|
||||||
_, err := net.ParseMAC(mac)
|
hw, err := net.ParseMAC(mac)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.StrValue{
|
return &types.StrValue{
|
||||||
V: strings.Replace(mac, "-", ":", -1),
|
V: hw.String(), // makes it lowercase and everything
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,12 +100,12 @@ func OldMacFmt(ctx context.Context, input []types.Value) (types.Value, error) {
|
|||||||
if len(mac) != len("00:00:00:00:00:00") {
|
if len(mac) != len("00:00:00:00:00:00") {
|
||||||
return nil, fmt.Errorf("invalid MAC address length: %s", mac)
|
return nil, fmt.Errorf("invalid MAC address length: %s", mac)
|
||||||
}
|
}
|
||||||
_, err := net.ParseMAC(mac)
|
hw, err := net.ParseMAC(mac)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.StrValue{
|
return &types.StrValue{
|
||||||
V: strings.Replace(mac, ":", "-", -1),
|
V: strings.Replace(hw.String(), ":", "-", -1),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ func TestMacFmt(t *testing.T) {
|
|||||||
out string
|
out string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"Valid mac with hyphens", "01-23-45-67-89-AB", "01:23:45:67:89:AB", false},
|
{"Valid mac with hyphens", "01-23-45-67-89-AB", "01:23:45:67:89:ab", false},
|
||||||
{"Valid mac with colons", "01:23:45:67:89:AB", "01:23:45:67:89:AB", false},
|
{"Valid mac with colons", "01:23:45:67:89:AB", "01:23:45:67:89:ab", false},
|
||||||
{"Incorrect mac length with colons", "01:23:45:67:89:AB:01:23:45:67:89:AB", "01:23:45:67:89:AB:01:23:45:67:89:AB", true},
|
{"Incorrect mac length with colons", "01:23:45:67:89:AB:01:23:45:67:89:AB", "01:23:45:67:89:AB:01:23:45:67:89:AB", true},
|
||||||
{"Invalid mac", "", "", true},
|
{"Invalid mac", "", "", true},
|
||||||
}
|
}
|
||||||
@@ -73,8 +73,9 @@ func TestOldMacFmt(t *testing.T) {
|
|||||||
out string
|
out string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"Valid mac with hyphens", "01:23:45:67:89:AB", "01-23-45-67-89-AB", false},
|
{"Valid mac with hyphens", "01:23:45:67:89:AB", "01-23-45-67-89-ab", false},
|
||||||
{"Valid mac with colons", "01-23-45-67-89-AB", "01-23-45-67-89-AB", false},
|
{"Valid lowercase mac with hyphens", "01:23:45:67:89:ab", "01-23-45-67-89-ab", false},
|
||||||
|
{"Valid mac with colons", "01-23-45-67-89-AB", "01-23-45-67-89-ab", false},
|
||||||
{"Incorrect mac length with hyphens", "01-23-45-67-89-AB-01-23-45-67-89-AB", "01-23-45-67-89-AB-01-23-45-67-89-AB", true},
|
{"Incorrect mac length with hyphens", "01-23-45-67-89-AB-01-23-45-67-89-AB", "01-23-45-67-89-AB-01-23-45-67-89-AB", true},
|
||||||
{"Invalid mac", "", "", true},
|
{"Invalid mac", "", "", true},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user