diff --git a/common/helpers/bimap/tests.go b/common/helpers/bimap/tests.go new file mode 100644 index 00000000..70d491b1 --- /dev/null +++ b/common/helpers/bimap/tests.go @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2024 Free Mobile +// SPDX-License-Identifier: AGPL-3.0-only + +//go:build !release + +package bimap + +import ( + "encoding" + "fmt" + "reflect" + "testing" +) + +// TestMarshalUnmarshal is an helper to test String(), MarshalText() and +// UnmarshalText() functions for a bimap. +func (bi *Bimap[K, V]) TestMarshalUnmarshal(t *testing.T) { + once := false + for _, k := range bi.Keys() { + v, _ := bi.LoadValue(k) + if !once { + t.Logf("key = %v, value = %v", reflect.TypeOf(k), reflect.TypeOf(v)) + once = true + } + if v, ok := any(v).(string); ok { + if k, ok := any(k).(fmt.Stringer); ok { + if k.String() != v { + t.Errorf("%d.String() == %s, expected %s", k, k.String(), v) + } + } else { + t.Fatalf("key should implement Stringer") + } + if k, ok := any(k).(encoding.TextMarshaler); ok { + if m, err := k.MarshalText(); err != nil { + t.Errorf("%d.MarshalText() error:\n%+v", k, err) + } else if string(m) != v { + t.Errorf("%d.MarshalText() == %s, expected %s", k, string(m), v) + } + } else { + t.Fatalf("key should implement TextMarshaler") + } + u := k + if u2, ok := any(&u).(encoding.TextUnmarshaler); ok { + if err := u2.UnmarshalText([]byte(v)); err != nil { + t.Errorf("UnmarshalText(%q) error:\n%+v", v, err) + } else if u != k { + t.Errorf("UnmarshalText(%q) == %v, expected %v", v, u, k) + } + } else { + t.Fatalf("key should implement TextUnmarshaler") + } + } else { + t.Fatalf("value should be a string") + } + } +} diff --git a/common/kafka/config_test.go b/common/kafka/config_test.go index 493dcbc6..bc7d08b9 100644 --- a/common/kafka/config_test.go +++ b/common/kafka/config_test.go @@ -164,3 +164,7 @@ func TestTLSConfiguration(t *testing.T) { }, }) } + +func TestMarshalUnmarshal(t *testing.T) { + saslAlgorithmMap.TestMarshalUnmarshal(t) +} diff --git a/common/schema/definition.go b/common/schema/definition.go index 1867c18e..a52e9414 100644 --- a/common/schema/definition.go +++ b/common/schema/definition.go @@ -37,30 +37,30 @@ var ( errUnknownInterfaceBoundary = errors.New("unknown interface boundary") ) -// MarshalText turns a SASL algorithm to text -func (sa InterfaceBoundary) MarshalText() ([]byte, error) { - got, ok := interfaceBoundaryMap.LoadValue(sa) +// MarshalText turns an interface boundary to text +func (ib InterfaceBoundary) MarshalText() ([]byte, error) { + got, ok := interfaceBoundaryMap.LoadValue(ib) if ok { return []byte(got), nil } return nil, errUnknownInterfaceBoundary } -// String turns a SASL algorithm to string -func (sa InterfaceBoundary) String() string { - got, _ := interfaceBoundaryMap.LoadValue(sa) +// String turns an interface boundary to string +func (ib InterfaceBoundary) String() string { + got, _ := interfaceBoundaryMap.LoadValue(ib) return got } -// UnmarshalText provides a SASL algorithm from text -func (sa *InterfaceBoundary) UnmarshalText(input []byte) error { +// UnmarshalText provides an interface boundary from text +func (ib *InterfaceBoundary) UnmarshalText(input []byte) error { if len(input) == 0 { - *sa = InterfaceBoundaryUndefined + *ib = InterfaceBoundaryUndefined return nil } got, ok := interfaceBoundaryMap.LoadKey(string(input)) if ok { - *sa = got + *ib = got return nil } return errUnknownInterfaceBoundary diff --git a/common/schema/definition_test.go b/common/schema/definition_test.go index dbf1772c..ce007076 100644 --- a/common/schema/definition_test.go +++ b/common/schema/definition_test.go @@ -66,3 +66,8 @@ func TestDisabledGroup(t *testing.T) { t.Error("ColumnGroupNAT is disabled while it should not") } } + +func TestMarshalUnmarshal(t *testing.T) { + interfaceBoundaryMap.TestMarshalUnmarshal(t) + columnNameMap.TestMarshalUnmarshal(t) +} diff --git a/inlet/core/config_test.go b/inlet/core/config_test.go index f07d78d9..7dfa4d82 100644 --- a/inlet/core/config_test.go +++ b/inlet/core/config_test.go @@ -115,3 +115,8 @@ func TestConfigurationUnmarshallerHook(t *testing.T) { }, }) } + +func TestMarshalUnmarshal(t *testing.T) { + asnProviderMap.TestMarshalUnmarshal(t) + netProviderMap.TestMarshalUnmarshal(t) +} diff --git a/inlet/metadata/provider/snmp/config.go b/inlet/metadata/provider/snmp/config.go index d939d687..ce9f11a0 100644 --- a/inlet/metadata/provider/snmp/config.go +++ b/inlet/metadata/provider/snmp/config.go @@ -116,14 +116,14 @@ func ConfigurationUnmarshallerHook() mapstructure.DecodeHookFunc { // AuthProtocol represents a SNMPv3 authentication protocol type AuthProtocol gosnmp.SnmpV3AuthProtocol -var authProtocolMap = bimap.New(map[gosnmp.SnmpV3AuthProtocol]string{ - gosnmp.NoAuth: "", - gosnmp.MD5: "MD5", - gosnmp.SHA: "SHA", - gosnmp.SHA224: "SHA224", - gosnmp.SHA256: "SHA256", - gosnmp.SHA384: "SHA384", - gosnmp.SHA512: "SHA512", +var authProtocolMap = bimap.New(map[AuthProtocol]string{ + AuthProtocol(gosnmp.NoAuth): "", + AuthProtocol(gosnmp.MD5): "MD5", + AuthProtocol(gosnmp.SHA): "SHA", + AuthProtocol(gosnmp.SHA224): "SHA224", + AuthProtocol(gosnmp.SHA256): "SHA256", + AuthProtocol(gosnmp.SHA384): "SHA384", + AuthProtocol(gosnmp.SHA512): "SHA512", }) // UnmarshalText parses a SNMPv3 authentication protocol @@ -138,7 +138,7 @@ func (ap *AuthProtocol) UnmarshalText(text []byte) error { // String turns a SNMPv3 authentication protocol to a string func (ap AuthProtocol) String() string { - protocol, ok := authProtocolMap.LoadValue(gosnmp.SnmpV3AuthProtocol(ap)) + protocol, ok := authProtocolMap.LoadValue(ap) if !ok { return "" } @@ -153,14 +153,14 @@ func (ap AuthProtocol) MarshalText() ([]byte, error) { // PrivProtocol represents a SNMPv3 privacy protocol type PrivProtocol gosnmp.SnmpV3PrivProtocol -var privProtocolMap = bimap.New(map[gosnmp.SnmpV3PrivProtocol]string{ - gosnmp.NoPriv: "", - gosnmp.DES: "DES", - gosnmp.AES: "AES", - gosnmp.AES192: "AES192", - gosnmp.AES256: "AES256", - gosnmp.AES192C: "AES192C", - gosnmp.AES256C: "AES256C", +var privProtocolMap = bimap.New(map[PrivProtocol]string{ + PrivProtocol(gosnmp.NoPriv): "", + PrivProtocol(gosnmp.DES): "DES", + PrivProtocol(gosnmp.AES): "AES", + PrivProtocol(gosnmp.AES192): "AES192", + PrivProtocol(gosnmp.AES256): "AES256", + PrivProtocol(gosnmp.AES192C): "AES192C", + PrivProtocol(gosnmp.AES256C): "AES256C", }) // UnmarshalText parses a SNMPv3 privacy protocol @@ -175,7 +175,7 @@ func (pp *PrivProtocol) UnmarshalText(text []byte) error { // String turns a SNMPv3 privacy protocol to a string func (pp PrivProtocol) String() string { - protocol, ok := privProtocolMap.LoadValue(gosnmp.SnmpV3PrivProtocol(pp)) + protocol, ok := privProtocolMap.LoadValue(pp) if !ok { return "" } diff --git a/inlet/metadata/provider/snmp/config_test.go b/inlet/metadata/provider/snmp/config_test.go index 605ccea2..ff065aac 100644 --- a/inlet/metadata/provider/snmp/config_test.go +++ b/inlet/metadata/provider/snmp/config_test.go @@ -257,3 +257,8 @@ func TestConfigurationUnmarshallerHook(t *testing.T) { }, }) } + +func TestMarshalUnmarshal(t *testing.T) { + authProtocolMap.TestMarshalUnmarshal(t) + privProtocolMap.TestMarshalUnmarshal(t) +}