isle/go/nebula/signing_key_test.go

53 lines
1.1 KiB
Go

package nebula
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestSigningKeysJSON(t *testing.T) {
pub, priv := GenerateSigningPair()
t.Run("SigningPublicKey", func(t *testing.T) {
pubJSON, err := json.Marshal(pub)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(string(pubJSON), `"S0`) {
t.Fatalf("pub key didn't marshal with prefix: %q", pubJSON)
}
var pub2 SigningPublicKey
if err := json.Unmarshal(pubJSON, &pub2); err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte(pub), []byte(pub2)) {
t.Fatalf("json unmarshaling got different result: %q", pub2)
}
})
t.Run("SigningPrivateKey", func(t *testing.T) {
privJSON, err := json.Marshal(priv)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(string(privJSON), `"s0`) {
t.Fatalf("priv key didn't marshal with prefix: %q", privJSON)
}
var priv2 SigningPrivateKey
if err := json.Unmarshal(privJSON, &priv2); err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte(priv), []byte(priv2)) {
t.Fatalf("json unmarshaling got different result: %q", priv2)
}
})
}