github.com/emmansun/gmsm@v0.29.1/smx509/equal_test.go (about)

     1  package smx509
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/rand"
     6  	"testing"
     7  
     8  	"github.com/emmansun/gmsm/sm2"
     9  )
    10  
    11  func TestEqual(t *testing.T) {
    12  	private, _ := sm2.GenerateKey(rand.Reader)
    13  	public := &private.PublicKey
    14  
    15  	if !public.Equal(public) {
    16  		t.Errorf("public key is not equal to itself: %q", public)
    17  	}
    18  	if !public.Equal(crypto.Signer(private).Public()) {
    19  		t.Errorf("private.Public() is not Equal to public: %q", public)
    20  	}
    21  	if !private.Equal(private) {
    22  		t.Errorf("private key is not equal to itself: %q", private.PrivateKey)
    23  	}
    24  
    25  	enc, err := MarshalPKCS8PrivateKey(private)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	decoded, err := ParsePKCS8PrivateKey(enc)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if !public.Equal(decoded.(crypto.Signer).Public()) {
    34  		t.Errorf("public key is not equal to itself after decoding: %v", public)
    35  	}
    36  	if !private.Equal(decoded) {
    37  		t.Errorf("private key is not equal to itself after decoding: %v", private)
    38  	}
    39  }