github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/idemix/handlers/nymsigner_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package handlers_test
     7  
     8  import (
     9  	"errors"
    10  
    11  	"github.com/hyperledger/fabric/bccsp"
    12  	"github.com/hyperledger/fabric/bccsp/idemix/handlers"
    13  	"github.com/hyperledger/fabric/bccsp/idemix/handlers/mock"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Nym Signature", func() {
    19  
    20  	Describe("when creating a signature", func() {
    21  
    22  		var (
    23  			NymSigner           *handlers.NymSigner
    24  			fakeSignatureScheme *mock.NymSignatureScheme
    25  			nymSK               bccsp.Key
    26  		)
    27  
    28  		BeforeEach(func() {
    29  			fakeSignatureScheme = &mock.NymSignatureScheme{}
    30  			NymSigner = &handlers.NymSigner{NymSignatureScheme: fakeSignatureScheme}
    31  
    32  			var err error
    33  			sk := &mock.Big{}
    34  			sk.BytesReturns([]byte{1, 2, 3, 4}, nil)
    35  			nymSK, err = handlers.NewNymSecretKey(sk, nil, false)
    36  			Expect(err).NotTo(HaveOccurred())
    37  		})
    38  
    39  		Context("and the underlying cryptographic algorithm succeed", func() {
    40  			var (
    41  				fakeSignature []byte
    42  			)
    43  			BeforeEach(func() {
    44  				fakeSignature = []byte("fake signature")
    45  				fakeSignatureScheme.SignReturns(fakeSignature, nil)
    46  			})
    47  
    48  			It("returns no error and a signature", func() {
    49  				signature, err := NymSigner.Sign(
    50  					handlers.NewUserSecretKey(nil, false),
    51  					[]byte("a digest"),
    52  					&bccsp.IdemixNymSignerOpts{
    53  						Nym:      nymSK,
    54  						IssuerPK: handlers.NewIssuerPublicKey(nil),
    55  					},
    56  				)
    57  				Expect(err).NotTo(HaveOccurred())
    58  				Expect(signature).To(BeEquivalentTo(fakeSignature))
    59  
    60  			})
    61  		})
    62  
    63  		Context("and the underlying cryptographic algorithm fails", func() {
    64  			BeforeEach(func() {
    65  				fakeSignatureScheme.SignReturns(nil, errors.New("sign error"))
    66  			})
    67  
    68  			It("returns an error", func() {
    69  				signature, err := NymSigner.Sign(
    70  					handlers.NewUserSecretKey(nil, false),
    71  					[]byte("a digest"),
    72  					&bccsp.IdemixNymSignerOpts{
    73  						Nym:      nymSK,
    74  						IssuerPK: handlers.NewIssuerPublicKey(nil),
    75  					},
    76  				)
    77  				Expect(err).To(MatchError("sign error"))
    78  				Expect(signature).To(BeNil())
    79  			})
    80  		})
    81  
    82  		Context("and the parameters are not well formed", func() {
    83  
    84  			Context("and the user secret key is nil", func() {
    85  				It("returns error", func() {
    86  					signature, err := NymSigner.Sign(
    87  						nil,
    88  						[]byte("a digest"),
    89  						&bccsp.IdemixNymSignerOpts{
    90  							Nym:      nymSK,
    91  							IssuerPK: handlers.NewIssuerPublicKey(nil),
    92  						},
    93  					)
    94  					Expect(err).To(MatchError("invalid key, expected *userSecretKey"))
    95  					Expect(signature).To(BeNil())
    96  				})
    97  			})
    98  
    99  			Context("and the user secret key is not of type *userSecretKey", func() {
   100  				It("returns error", func() {
   101  					signature, err := NymSigner.Sign(
   102  						handlers.NewIssuerPublicKey(nil),
   103  						[]byte("a digest"),
   104  						&bccsp.IdemixNymSignerOpts{
   105  							Nym:      nymSK,
   106  							IssuerPK: handlers.NewIssuerPublicKey(nil),
   107  						},
   108  					)
   109  					Expect(err).To(MatchError("invalid key, expected *userSecretKey"))
   110  					Expect(signature).To(BeNil())
   111  				})
   112  			})
   113  
   114  			Context("and the option is nil", func() {
   115  				It("returns error", func() {
   116  					signature, err := NymSigner.Sign(
   117  						handlers.NewUserSecretKey(nil, false),
   118  						[]byte("a digest"),
   119  						nil,
   120  					)
   121  					Expect(err).To(MatchError("invalid options, expected *IdemixNymSignerOpts"))
   122  					Expect(signature).To(BeNil())
   123  				})
   124  			})
   125  
   126  			Context("and the option is not of type *IdemixNymSignerOpts", func() {
   127  				It("returns error", func() {
   128  					signature, err := NymSigner.Sign(
   129  						handlers.NewUserSecretKey(nil, false),
   130  						[]byte("a digest"),
   131  						&bccsp.IdemixCRISignerOpts{},
   132  					)
   133  					Expect(err).To(MatchError("invalid options, expected *IdemixNymSignerOpts"))
   134  					Expect(signature).To(BeNil())
   135  				})
   136  			})
   137  
   138  			Context("and the nym is nil", func() {
   139  				It("returns error", func() {
   140  					signature, err := NymSigner.Sign(
   141  						handlers.NewUserSecretKey(nil, false),
   142  						[]byte("a digest"),
   143  						&bccsp.IdemixNymSignerOpts{
   144  							IssuerPK: handlers.NewIssuerPublicKey(nil),
   145  						},
   146  					)
   147  					Expect(err).To(MatchError("invalid options, missing nym key"))
   148  					Expect(signature).To(BeNil())
   149  				})
   150  			})
   151  
   152  			Context("and the nym is not of type *nymSecretKey", func() {
   153  				It("returns error", func() {
   154  					signature, err := NymSigner.Sign(
   155  						handlers.NewUserSecretKey(nil, false),
   156  						[]byte("a digest"),
   157  						&bccsp.IdemixNymSignerOpts{
   158  							Nym:      handlers.NewIssuerPublicKey(nil),
   159  							IssuerPK: handlers.NewIssuerPublicKey(nil),
   160  						},
   161  					)
   162  					Expect(err).To(MatchError("invalid nym key, expected *nymSecretKey"))
   163  					Expect(signature).To(BeNil())
   164  				})
   165  			})
   166  
   167  			Context("and the IssuerPk is nil", func() {
   168  				It("returns error", func() {
   169  					signature, err := NymSigner.Sign(
   170  						handlers.NewUserSecretKey(nil, false),
   171  						[]byte("a digest"),
   172  						&bccsp.IdemixNymSignerOpts{
   173  							Nym: nymSK,
   174  						},
   175  					)
   176  					Expect(err).To(MatchError("invalid options, missing issuer public key"))
   177  					Expect(signature).To(BeNil())
   178  				})
   179  			})
   180  
   181  			Context("and the IssuerPk is not of type *issuerPublicKey", func() {
   182  				It("returns error", func() {
   183  					signature, err := NymSigner.Sign(
   184  						handlers.NewUserSecretKey(nil, false),
   185  						[]byte("a digest"),
   186  						&bccsp.IdemixNymSignerOpts{
   187  							Nym:      nymSK,
   188  							IssuerPK: handlers.NewUserSecretKey(nil, false),
   189  						},
   190  					)
   191  					Expect(err).To(MatchError("invalid issuer public key, expected *issuerPublicKey"))
   192  					Expect(signature).To(BeNil())
   193  				})
   194  			})
   195  		})
   196  	})
   197  
   198  	Describe("when verifying a signature", func() {
   199  
   200  		var (
   201  			NymVerifier         *handlers.NymVerifier
   202  			fakeSignatureScheme *mock.NymSignatureScheme
   203  		)
   204  
   205  		BeforeEach(func() {
   206  			fakeSignatureScheme = &mock.NymSignatureScheme{}
   207  			NymVerifier = &handlers.NymVerifier{NymSignatureScheme: fakeSignatureScheme}
   208  		})
   209  
   210  		Context("and the underlying cryptographic algorithm succeed", func() {
   211  			BeforeEach(func() {
   212  				fakeSignatureScheme.VerifyReturns(nil)
   213  			})
   214  
   215  			It("returns no error and valid signature", func() {
   216  				valid, err := NymVerifier.Verify(
   217  					handlers.NewNymPublicKey(nil),
   218  					[]byte("a signature"),
   219  					[]byte("a digest"),
   220  					&bccsp.IdemixNymSignerOpts{
   221  						IssuerPK: handlers.NewIssuerPublicKey(nil),
   222  					},
   223  				)
   224  				Expect(err).NotTo(HaveOccurred())
   225  				Expect(valid).To(BeTrue())
   226  			})
   227  		})
   228  
   229  		Context("and the underlying cryptographic algorithm fails", func() {
   230  			BeforeEach(func() {
   231  				fakeSignatureScheme.VerifyReturns(errors.New("verify error"))
   232  			})
   233  
   234  			It("returns an error", func() {
   235  				valid, err := NymVerifier.Verify(
   236  					handlers.NewNymPublicKey(nil),
   237  					[]byte("a signature"),
   238  					[]byte("a digest"),
   239  					&bccsp.IdemixNymSignerOpts{
   240  						IssuerPK: handlers.NewIssuerPublicKey(nil),
   241  					},
   242  				)
   243  				Expect(err).To(MatchError("verify error"))
   244  				Expect(valid).To(BeFalse())
   245  			})
   246  		})
   247  
   248  		Context("and the parameters are not well formed", func() {
   249  
   250  			Context("and the nym public key is nil", func() {
   251  				It("returns error", func() {
   252  					valid, err := NymVerifier.Verify(
   253  						nil,
   254  						[]byte("fake signature"),
   255  						nil,
   256  						&bccsp.IdemixNymSignerOpts{IssuerPK: handlers.NewIssuerPublicKey(nil)},
   257  					)
   258  					Expect(err).To(MatchError("invalid key, expected *nymPublicKey"))
   259  					Expect(valid).To(BeFalse())
   260  				})
   261  			})
   262  
   263  			Context("and the nym public key is not of type *nymPublicKey", func() {
   264  				It("returns error", func() {
   265  					valid, err := NymVerifier.Verify(
   266  						handlers.NewUserSecretKey(nil, false),
   267  						[]byte("fake signature"),
   268  						nil,
   269  						&bccsp.IdemixNymSignerOpts{IssuerPK: handlers.NewIssuerPublicKey(nil)},
   270  					)
   271  					Expect(err).To(MatchError("invalid key, expected *nymPublicKey"))
   272  					Expect(valid).To(BeFalse())
   273  				})
   274  			})
   275  
   276  			Context("and the signature is empty", func() {
   277  				It("returns error", func() {
   278  					valid, err := NymVerifier.Verify(
   279  						handlers.NewNymPublicKey(nil),
   280  						nil,
   281  						[]byte("a digest"),
   282  						&bccsp.IdemixNymSignerOpts{IssuerPK: handlers.NewIssuerPublicKey(nil)},
   283  					)
   284  					Expect(err).To(MatchError("invalid signature, it must not be empty"))
   285  					Expect(valid).To(BeFalse())
   286  				})
   287  			})
   288  
   289  			Context("and the option is empty", func() {
   290  				It("returns error", func() {
   291  					valid, err := NymVerifier.Verify(
   292  						handlers.NewNymPublicKey(nil),
   293  						[]byte("a signature"),
   294  						[]byte("a digest"),
   295  						nil,
   296  					)
   297  					Expect(err).To(MatchError("invalid options, expected *IdemixNymSignerOpts"))
   298  					Expect(valid).To(BeFalse())
   299  				})
   300  			})
   301  
   302  			Context("and the option is not of type *IdemixNymSignerOpts", func() {
   303  				It("returns error", func() {
   304  					valid, err := NymVerifier.Verify(
   305  						handlers.NewNymPublicKey(nil),
   306  						[]byte("a signature"),
   307  						[]byte("a digest"),
   308  						&bccsp.IdemixCredentialRequestSignerOpts{},
   309  					)
   310  					Expect(err).To(MatchError("invalid options, expected *IdemixNymSignerOpts"))
   311  					Expect(valid).To(BeFalse())
   312  				})
   313  			})
   314  
   315  			Context("and the option's issuer public key is empty", func() {
   316  				It("returns error", func() {
   317  					valid, err := NymVerifier.Verify(
   318  						handlers.NewNymPublicKey(nil),
   319  						[]byte("fake signature"),
   320  						nil,
   321  						&bccsp.IdemixNymSignerOpts{},
   322  					)
   323  					Expect(err).To(MatchError("invalid options, missing issuer public key"))
   324  					Expect(valid).To(BeFalse())
   325  				})
   326  			})
   327  
   328  			Context("and the option's issuer public key is not of type *issuerPublicKey", func() {
   329  				It("returns error", func() {
   330  					valid, err := NymVerifier.Verify(
   331  						handlers.NewNymPublicKey(nil),
   332  						[]byte("fake signature"),
   333  						nil,
   334  						&bccsp.IdemixNymSignerOpts{
   335  							IssuerPK: handlers.NewNymPublicKey(nil),
   336  						},
   337  					)
   338  					Expect(err).To(MatchError("invalid issuer public key, expected *issuerPublicKey"))
   339  					Expect(valid).To(BeFalse())
   340  				})
   341  			})
   342  		})
   343  	})
   344  })