open-vault/helper/random/serializing_test.go
Michael Golowka f77bcc53c4
Move sdk/helper/random -> helper/random (#9226)
* This package is new for 1.5 so this is not a breaking change.
* This is being moved because this code was originally intended to be used
within plugins, however the design of password policies has changed such
that this is no longer needed. Thus, this code doesn't need to be in the
public SDK.
2020-06-17 14:24:38 -06:00

59 lines
1.1 KiB
Go

package random
import (
"encoding/json"
"reflect"
"testing"
)
func TestJSONMarshalling(t *testing.T) {
expected := serializableRules{
CharsetRule{
Charset: LowercaseRuneset,
MinChars: 1,
},
CharsetRule{
Charset: UppercaseRuneset,
MinChars: 1,
},
CharsetRule{
Charset: NumericRuneset,
MinChars: 1,
},
CharsetRule{
Charset: ShortSymbolRuneset,
MinChars: 1,
},
}
marshalled, err := json.Marshal(expected)
if err != nil {
t.Fatalf("no error expected, got: %s", err)
}
actual := serializableRules{}
err = json.Unmarshal(marshalled, &actual)
if err != nil {
t.Fatalf("no error expected, got: %s", err)
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Actual: %#v\nExpected: %#v", actual, expected)
}
}
func TestRunes_UnmarshalJSON(t *testing.T) {
data := []byte(`"noaw8hgfsdjlkfsj3"`)
expected := runes([]rune("noaw8hgfsdjlkfsj3"))
actual := runes{}
err := (&actual).UnmarshalJSON(data)
if err != nil {
t.Fatalf("no error expected, got: %s", err)
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Actual: %#v\nExpected: %#v", actual, expected)
}
}