common/helpers: add a way to test Marshal/Unmarshal for bimaps

This commit is contained in:
Vincent Bernat
2024-01-22 21:53:26 +01:00
parent 374a1fce55
commit f321e8fa64
7 changed files with 103 additions and 28 deletions

View File

@@ -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")
}
}
}

View File

@@ -164,3 +164,7 @@ func TestTLSConfiguration(t *testing.T) {
},
})
}
func TestMarshalUnmarshal(t *testing.T) {
saslAlgorithmMap.TestMarshalUnmarshal(t)
}

View File

@@ -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

View File

@@ -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)
}

View File

@@ -115,3 +115,8 @@ func TestConfigurationUnmarshallerHook(t *testing.T) {
},
})
}
func TestMarshalUnmarshal(t *testing.T) {
asnProviderMap.TestMarshalUnmarshal(t)
netProviderMap.TestMarshalUnmarshal(t)
}

View File

@@ -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 ""
}

View File

@@ -257,3 +257,8 @@ func TestConfigurationUnmarshallerHook(t *testing.T) {
},
})
}
func TestMarshalUnmarshal(t *testing.T) {
authProtocolMap.TestMarshalUnmarshal(t)
privProtocolMap.TestMarshalUnmarshal(t)
}