53 lines
1.1 KiB
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), `"-----BEGIN `) {
|
||
|
t.Fatalf("pub key didn't marshal to PEM: %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), `"-----BEGIN `) {
|
||
|
t.Fatalf("priv key didn't marshal to PEM: %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)
|
||
|
}
|
||
|
})
|
||
|
}
|