github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/sw/keyimport_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sw 18 19 import ( 20 "crypto/rand" 21 "crypto/rsa" 22 "crypto/x509" 23 "errors" 24 "reflect" 25 "testing" 26 27 mocks2 "github.com/hyperledger/fabric/bccsp/mocks" 28 "github.com/hyperledger/fabric/bccsp/sw/mocks" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestKeyImport(t *testing.T) { 33 t.Parallel() 34 35 expectedRaw := []byte{1, 2, 3} 36 expectedOpts := &mocks2.KeyDerivOpts{EphemeralValue: true} 37 expectetValue := &mocks2.MockKey{BytesValue: []byte{1, 2, 3, 4, 5}} 38 expectedErr := errors.New("Expected Error") 39 40 keyImporters := make(map[reflect.Type]KeyImporter) 41 keyImporters[reflect.TypeOf(&mocks2.KeyDerivOpts{})] = &mocks.KeyImporter{ 42 RawArg: expectedRaw, 43 OptsArg: expectedOpts, 44 Value: expectetValue, 45 Err: expectedErr, 46 } 47 csp := CSP{KeyImporters: keyImporters} 48 value, err := csp.KeyImport(expectedRaw, expectedOpts) 49 assert.Nil(t, value) 50 assert.Contains(t, err.Error(), expectedErr.Error()) 51 52 keyImporters = make(map[reflect.Type]KeyImporter) 53 keyImporters[reflect.TypeOf(&mocks2.KeyDerivOpts{})] = &mocks.KeyImporter{ 54 RawArg: expectedRaw, 55 OptsArg: expectedOpts, 56 Value: expectetValue, 57 Err: nil, 58 } 59 csp = CSP{KeyImporters: keyImporters} 60 value, err = csp.KeyImport(expectedRaw, expectedOpts) 61 assert.Equal(t, expectetValue, value) 62 assert.Nil(t, err) 63 } 64 65 func TestAES256ImportKeyOptsKeyImporter(t *testing.T) { 66 t.Parallel() 67 68 ki := aes256ImportKeyOptsKeyImporter{} 69 70 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 71 assert.Error(t, err) 72 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 73 74 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 75 assert.Error(t, err) 76 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 77 78 _, err = ki.KeyImport([]byte(nil), &mocks2.KeyImportOpts{}) 79 assert.Error(t, err) 80 assert.Contains(t, err.Error(), "Invalid raw material. It must not be nil.") 81 82 _, err = ki.KeyImport([]byte{0}, &mocks2.KeyImportOpts{}) 83 assert.Error(t, err) 84 assert.Contains(t, err.Error(), "Invalid Key Length [") 85 } 86 87 func TestHMACImportKeyOptsKeyImporter(t *testing.T) { 88 t.Parallel() 89 90 ki := hmacImportKeyOptsKeyImporter{} 91 92 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 93 assert.Error(t, err) 94 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 95 96 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 97 assert.Error(t, err) 98 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 99 100 _, err = ki.KeyImport([]byte(nil), &mocks2.KeyImportOpts{}) 101 assert.Error(t, err) 102 assert.Contains(t, err.Error(), "Invalid raw material. It must not be nil.") 103 } 104 105 func TestECDSAPKIXPublicKeyImportOptsKeyImporter(t *testing.T) { 106 t.Parallel() 107 108 ki := ecdsaPKIXPublicKeyImportOptsKeyImporter{} 109 110 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 111 assert.Error(t, err) 112 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 113 114 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 115 assert.Error(t, err) 116 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 117 118 _, err = ki.KeyImport([]byte(nil), &mocks2.KeyImportOpts{}) 119 assert.Error(t, err) 120 assert.Contains(t, err.Error(), "Invalid raw. It must not be nil.") 121 122 _, err = ki.KeyImport([]byte{0}, &mocks2.KeyImportOpts{}) 123 assert.Error(t, err) 124 assert.Contains(t, err.Error(), "Failed converting PKIX to ECDSA public key [") 125 126 k, err := rsa.GenerateKey(rand.Reader, 512) 127 assert.NoError(t, err) 128 raw, err := x509.MarshalPKIXPublicKey(&k.PublicKey) 129 assert.NoError(t, err) 130 _, err = ki.KeyImport(raw, &mocks2.KeyImportOpts{}) 131 assert.Error(t, err) 132 assert.Contains(t, err.Error(), "Failed casting to ECDSA public key. Invalid raw material.") 133 } 134 135 func TestECDSAPrivateKeyImportOptsKeyImporter(t *testing.T) { 136 t.Parallel() 137 138 ki := ecdsaPrivateKeyImportOptsKeyImporter{} 139 140 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 141 assert.Error(t, err) 142 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 143 144 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 145 assert.Error(t, err) 146 assert.Contains(t, err.Error(), "Invalid raw material. Expected byte array.") 147 148 _, err = ki.KeyImport([]byte(nil), &mocks2.KeyImportOpts{}) 149 assert.Error(t, err) 150 assert.Contains(t, err.Error(), "Invalid raw. It must not be nil.") 151 152 _, err = ki.KeyImport([]byte{0}, &mocks2.KeyImportOpts{}) 153 assert.Error(t, err) 154 assert.Contains(t, err.Error(), "Failed converting PKIX to ECDSA public key") 155 156 k, err := rsa.GenerateKey(rand.Reader, 512) 157 assert.NoError(t, err) 158 raw := x509.MarshalPKCS1PrivateKey(k) 159 _, err = ki.KeyImport(raw, &mocks2.KeyImportOpts{}) 160 assert.Error(t, err) 161 assert.Contains(t, err.Error(), "Failed casting to ECDSA private key. Invalid raw material.") 162 } 163 164 func TestECDSAGoPublicKeyImportOptsKeyImporter(t *testing.T) { 165 t.Parallel() 166 167 ki := ecdsaGoPublicKeyImportOptsKeyImporter{} 168 169 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 170 assert.Error(t, err) 171 assert.Contains(t, err.Error(), "Invalid raw material. Expected *ecdsa.PublicKey.") 172 173 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 174 assert.Error(t, err) 175 assert.Contains(t, err.Error(), "Invalid raw material. Expected *ecdsa.PublicKey.") 176 } 177 178 func TestX509PublicKeyImportOptsKeyImporter(t *testing.T) { 179 t.Parallel() 180 181 ki := x509PublicKeyImportOptsKeyImporter{} 182 183 _, err := ki.KeyImport("Hello World", &mocks2.KeyImportOpts{}) 184 assert.Error(t, err) 185 assert.Contains(t, err.Error(), "Invalid raw material. Expected *x509.Certificate.") 186 187 _, err = ki.KeyImport(nil, &mocks2.KeyImportOpts{}) 188 assert.Error(t, err) 189 assert.Contains(t, err.Error(), "Invalid raw material. Expected *x509.Certificate.") 190 191 cert := &x509.Certificate{} 192 cert.PublicKey = "Hello world" 193 _, err = ki.KeyImport(cert, &mocks2.KeyImportOpts{}) 194 assert.Error(t, err) 195 assert.Contains(t, err.Error(), "Certificate's public key type not recognized. Supported keys: [ECDSA]") 196 }