diff --git a/smx509/equal_test.go b/smx509/equal_test.go new file mode 100644 index 0000000..70d4319 --- /dev/null +++ b/smx509/equal_test.go @@ -0,0 +1,39 @@ +package smx509 + +import ( + "crypto" + "crypto/rand" + "testing" + + "github.com/emmansun/gmsm/sm2" +) + +func TestEqual(t *testing.T) { + private, _ := sm2.GenerateKey(rand.Reader) + public := &private.PublicKey + + if !public.Equal(public) { + t.Errorf("public key is not equal to itself: %q", public) + } + if !public.Equal(crypto.Signer(private).Public()) { + t.Errorf("private.Public() is not Equal to public: %q", public) + } + if !private.Equal(private) { + t.Errorf("private key is not equal to itself: %q", private) + } + + enc, err := MarshalPKCS8PrivateKey(private) + if err != nil { + t.Fatal(err) + } + decoded, err := ParsePKCS8PrivateKey(enc) + if err != nil { + t.Fatal(err) + } + if !public.Equal(decoded.(crypto.Signer).Public()) { + t.Errorf("public key is not equal to itself after decoding: %v", public) + } + if !private.Equal(decoded) { + t.Errorf("private key is not equal to itself after decoding: %v", private) + } +}