From f97273bb5a7f4ead208e6761e1ba37af1e94c434 Mon Sep 17 00:00:00 2001 From: Emman Date: Fri, 28 Jan 2022 11:51:08 +0800 Subject: [PATCH] [SM2] add key equal test --- smx509/equal_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 smx509/equal_test.go 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) + } +}