github.com/adecaro/fabric-ca@v2.0.0-alpha+incompatible/lib/server/idemix/issuercredential_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package idemix_test
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	proto "github.com/golang/protobuf/proto"
    17  	. "github.com/hyperledger/fabric-ca/lib/server/idemix"
    18  	"github.com/hyperledger/fabric-ca/lib/server/idemix/mocks"
    19  	"github.com/hyperledger/fabric/idemix"
    20  	"github.com/pkg/errors"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  const (
    25  	testPublicKeyFile = "../../../testdata/IdemixPublicKey"
    26  	testSecretKeyFile = "../../../testdata/IdemixSecretKey"
    27  )
    28  
    29  func TestLoadEmptyIdemixPublicKey(t *testing.T) {
    30  	testdir, err := ioutil.TempDir(".", "issuerkeyloadTest")
    31  	if err != nil {
    32  		t.Fatalf("Failed to create temp directory: %s", err.Error())
    33  	}
    34  	pubkeyfile, err := ioutil.TempFile(testdir, "IdemixPublicKey")
    35  	if err != nil {
    36  		t.Fatalf("Failed to create temp file: %s", err.Error())
    37  	}
    38  	defer os.RemoveAll(testdir)
    39  	idemixLib := new(mocks.Lib)
    40  	ic := NewIssuerCredential(pubkeyfile.Name(), testSecretKeyFile, idemixLib)
    41  	err = ic.Load()
    42  	assert.Error(t, err, "Should have failed to load non existing issuer public key")
    43  	if err != nil {
    44  		assert.Contains(t, err.Error(), "Issuer public key file is empty")
    45  	}
    46  }
    47  
    48  func TestLoadFakeIdemixPublicKey(t *testing.T) {
    49  	testdir, err := ioutil.TempDir(".", "issuerkeyloadTest")
    50  	if err != nil {
    51  		t.Fatalf("Failed to create temp directory: %s", err.Error())
    52  	}
    53  	pubkeyfile, err := ioutil.TempFile(testdir, "IdemixPublicKey")
    54  	if err != nil {
    55  		t.Fatalf("Failed to create temp file: %s", err.Error())
    56  	}
    57  	privkeyfile, err := ioutil.TempFile(testdir, "IdemixSecretKey")
    58  	if err != nil {
    59  		t.Fatalf("Failed to create temp file: %s", err.Error())
    60  	}
    61  	defer os.RemoveAll(testdir)
    62  	_, err = pubkeyfile.WriteString("foo")
    63  	if err != nil {
    64  		t.Fatalf("Failed to write to the file %s", pubkeyfile.Name())
    65  	}
    66  	idemixLib := new(mocks.Lib)
    67  	ik := NewIssuerCredential(pubkeyfile.Name(), privkeyfile.Name(), idemixLib)
    68  	err = ik.Load()
    69  	assert.Error(t, err, "Should have failed to load non existing issuer public key")
    70  	if err != nil {
    71  		assert.Contains(t, err.Error(), "Failed to unmarshal Issuer public key bytes")
    72  	}
    73  }
    74  
    75  func TestLoadEmptyIdemixSecretKey(t *testing.T) {
    76  	testdir, err := ioutil.TempDir(".", "issuerkeyloadTest")
    77  	if err != nil {
    78  		t.Fatalf("Failed to create temp directory: %s", err.Error())
    79  	}
    80  	privkeyfile, err := ioutil.TempFile(testdir, "IdemixSecretKey")
    81  	if err != nil {
    82  		t.Fatalf("Failed to create temp file: %s", err.Error())
    83  	}
    84  	defer os.RemoveAll(testdir)
    85  	idemixLib := new(mocks.Lib)
    86  	ik := NewIssuerCredential(testPublicKeyFile, privkeyfile.Name(), idemixLib)
    87  	err = ik.Load()
    88  	assert.Error(t, err, "Should have failed to load non existing issuer secret key")
    89  	if err != nil {
    90  		assert.Contains(t, err.Error(), "Issuer secret key file is empty")
    91  	}
    92  }
    93  
    94  func TestLoadNonExistentIdemixSecretKey(t *testing.T) {
    95  	testdir, err := ioutil.TempDir(".", "issuerkeyloadTest")
    96  	if err != nil {
    97  		t.Fatalf("Failed to create temp directory: %s", err.Error())
    98  	}
    99  	defer os.RemoveAll(testdir)
   100  	idemixLib := new(mocks.Lib)
   101  	ik := NewIssuerCredential(testPublicKeyFile, filepath.Join(testdir, "IdemixSecretKey"), idemixLib)
   102  	err = ik.Load()
   103  	assert.Error(t, err, "Should have failed to load non existing issuer secret key")
   104  	if err != nil {
   105  		assert.Contains(t, err.Error(), "Failed to read Issuer secret key")
   106  	}
   107  }
   108  
   109  func TestLoad(t *testing.T) {
   110  	idemixLib := new(mocks.Lib)
   111  	ik := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   112  	err := ik.Load()
   113  	assert.NoError(t, err, "Failed to load Idemix issuer credential")
   114  
   115  	err = ik.Store()
   116  	assert.NoError(t, err, "Failed to store Idemix issuer credential")
   117  }
   118  
   119  func TestStoreNilIssuerKey(t *testing.T) {
   120  	idemixLib := new(mocks.Lib)
   121  	ik := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   122  	err := ik.Store()
   123  	assert.Error(t, err, "Should fail if store is called without setting the issuer key or loading the issuer key from disk")
   124  	if err != nil {
   125  		assert.Equal(t, err.Error(), "Issuer credential is not set")
   126  	}
   127  }
   128  
   129  func TestStoreNilIdemixPublicKey(t *testing.T) {
   130  	idemixLib := new(mocks.Lib)
   131  	ik := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   132  	ik.SetIssuerKey(&idemix.IssuerKey{})
   133  	err := ik.Store()
   134  	assert.Error(t, err, "Should fail if store is called with empty issuer public key byte array")
   135  	if err != nil {
   136  		assert.Equal(t, err.Error(), "Failed to marshal Issuer public key")
   137  	}
   138  }
   139  
   140  func TestStoreReadonlyPublicKeyFilePath(t *testing.T) {
   141  	testdir, err := ioutil.TempDir(".", "issuerpubkeytest")
   142  	if err != nil {
   143  		t.Fatalf("Failed to create temp directory: %s", err.Error())
   144  	}
   145  	defer os.RemoveAll(testdir)
   146  
   147  	pubkeyfile := path.Join(testdir, "testdata1/IdemixPublicKey")
   148  
   149  	// Valid issuer public key
   150  	validPubKeyFile := testPublicKeyFile
   151  	pubKeyBytes, err := ioutil.ReadFile(validPubKeyFile)
   152  	if err != nil {
   153  		t.Fatalf("Failed to read idemix public key file %s", validPubKeyFile)
   154  	}
   155  
   156  	pubKey := &idemix.IssuerPublicKey{}
   157  	err = proto.Unmarshal(pubKeyBytes, pubKey)
   158  	if err != nil {
   159  		t.Fatalf("Failed to unmarshal idemix public key bytes from %s", validPubKeyFile)
   160  	}
   161  	idemixLib := new(mocks.Lib)
   162  	err = os.MkdirAll(path.Dir(pubkeyfile), 4444)
   163  	if err != nil {
   164  		t.Fatalf("Failed to create read only directory: %s", err.Error())
   165  	}
   166  	ik := NewIssuerCredential(pubkeyfile, testSecretKeyFile, idemixLib)
   167  	ik.SetIssuerKey(&idemix.IssuerKey{Ipk: pubKey})
   168  	err = ik.Store()
   169  	assert.Error(t, err, "Should fail if issuer public key is being stored to readonly directory")
   170  	if err != nil {
   171  		assert.Equal(t, err.Error(), "Failed to store Issuer public key")
   172  	}
   173  }
   174  
   175  func TestStoreReadonlySecretKeyFilePath(t *testing.T) {
   176  	testdir, err := ioutil.TempDir(".", "issuerkeystoreTest")
   177  	if err != nil {
   178  		t.Fatalf("Failed to create temp directory: %s", err.Error())
   179  	}
   180  	defer os.RemoveAll(testdir)
   181  
   182  	// foo directory is non-existent
   183  	privkeyfile := filepath.Join(testdir, "foo/IdemixSecretKey")
   184  
   185  	// Valid issuer public key
   186  	pubKeyBytes, err := ioutil.ReadFile(testPublicKeyFile)
   187  	if err != nil {
   188  		t.Fatalf("Failed to read idemix public key file %s", testPublicKeyFile)
   189  	}
   190  
   191  	pubKey := &idemix.IssuerPublicKey{}
   192  	err = proto.Unmarshal(pubKeyBytes, pubKey)
   193  	if err != nil {
   194  		t.Fatalf("Failed to unmarshal idemix public key bytes from %s", testPublicKeyFile)
   195  	}
   196  	idemixLib := new(mocks.Lib)
   197  	err = os.MkdirAll(path.Dir(privkeyfile), 4444)
   198  	if err != nil {
   199  		t.Fatalf("Failed to create read only directory: %s", err.Error())
   200  	}
   201  	ik := NewIssuerCredential(testPublicKeyFile, privkeyfile, idemixLib)
   202  	ik.SetIssuerKey(&idemix.IssuerKey{Ipk: pubKey})
   203  	err = ik.Store()
   204  	assert.Error(t, err, "Should fail if issuer secret key is being stored to read-only directory")
   205  	if err != nil {
   206  		assert.Equal(t, "Failed to store Issuer secret key", err.Error())
   207  	}
   208  }
   209  
   210  func TestGetIssuerKey(t *testing.T) {
   211  	idemixLib := new(mocks.Lib)
   212  	ik := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   213  	_, err := ik.GetIssuerKey()
   214  	assert.Error(t, err, "GetIssuerKey should return an error if it is called without setting the issuer key or loading the issuer key from disk")
   215  	if err != nil {
   216  		assert.Equal(t, err.Error(), "Issuer credential is not set")
   217  	}
   218  	err = ik.Load()
   219  	if err != nil {
   220  		t.Fatalf("Load of valid issuer public and secret key should not fail: %s", err)
   221  	}
   222  	_, err = ik.GetIssuerKey()
   223  	assert.NoError(t, err, "GetIssuerKey should not return an error if the issuer key is set")
   224  }
   225  
   226  func TestNewIssuerKeyGetRandError(t *testing.T) {
   227  	idemixLib := new(mocks.Lib)
   228  	idemixLib.On("GetRand").Return(nil, errors.New("Failed to generate random number"))
   229  	ic := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   230  	_, err := ic.NewIssuerKey()
   231  	assert.Error(t, err)
   232  	assert.Contains(t, err.Error(), "Error creating new issuer key")
   233  }
   234  
   235  func TestNewIssuerKeyError(t *testing.T) {
   236  	idemixLib := new(mocks.Lib)
   237  	rnd, err := NewLib().GetRand()
   238  	if err != nil {
   239  		t.Fatalf("Failed to generate a random number: %s", err.Error())
   240  	}
   241  	idemixLib.On("GetRand").Return(rnd, nil)
   242  	idemixLib.On("NewIssuerKey", GetAttributeNames(), rnd).Return(nil, errors.New("Failed to create new issuer key"))
   243  	ic := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   244  	_, err = ic.NewIssuerKey()
   245  	assert.Error(t, err)
   246  }
   247  
   248  func TestNewIssuerKey(t *testing.T) {
   249  	idemixLib := new(mocks.Lib)
   250  	idemix := NewLib()
   251  	rnd, err := idemix.GetRand()
   252  	if err != nil {
   253  		t.Fatalf("Failed to generate a random number: %s", err.Error())
   254  	}
   255  	attrNames := GetAttributeNames()
   256  	ik, err := idemix.NewIssuerKey(attrNames, rnd)
   257  	if err != nil {
   258  		t.Fatalf("Failed to create new issuer key: %s", err.Error())
   259  	}
   260  	idemixLib.On("GetRand").Return(rnd, nil)
   261  	idemixLib.On("NewIssuerKey", attrNames, rnd).Return(ik, nil)
   262  	ic := NewIssuerCredential(testPublicKeyFile, testSecretKeyFile, idemixLib)
   263  	_, err = ic.NewIssuerKey()
   264  	assert.NoError(t, err)
   265  }