-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-3441] bccsp/sw AES test coverage
Using the approach discussed in FAB-3465, this change-sets refactors the way AES encryption and decryption is done at bccsp/sw. Essentially, the switch has been replaced by a map. The approach decouples the testing of the bccsp interface implementation from the cryptographic algorithms. Test-coverage of AES is now at more than 90% Change-Id: I828d882b727321a3ffa52d2aa0a73e7dd075e92c Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
- Loading branch information
Showing
7 changed files
with
251 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sw | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
mocks2 "github.com/hyperledger/fabric/bccsp/mocks" | ||
"github.com/hyperledger/fabric/bccsp/sw/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncrypt(t *testing.T) { | ||
expectedKey := &mocks2.MockKey{} | ||
expectedPlaintext := []byte{1, 2, 3, 4} | ||
expectedOpts := &mocks2.EncrypterOpts{} | ||
expectedCiphertext := []byte{0, 1, 2, 3, 4} | ||
expectedErr := errors.New("no error") | ||
|
||
encryptors := make(map[reflect.Type]Encryptor) | ||
encryptors[reflect.TypeOf(&mocks2.MockKey{})] = &mocks.Encryptor{ | ||
KeyArg: expectedKey, | ||
PlaintextArg: expectedPlaintext, | ||
OptsArg: expectedOpts, | ||
EncValue: expectedCiphertext, | ||
EncErr: expectedErr, | ||
} | ||
|
||
csp := impl{encryptors: encryptors} | ||
|
||
ct, err := csp.Encrypt(expectedKey, expectedPlaintext, expectedOpts) | ||
assert.Equal(t, expectedCiphertext, ct) | ||
assert.Equal(t, expectedErr, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sw | ||
|
||
import "github.com/hyperledger/fabric/bccsp" | ||
|
||
// Encryptor is a BCCSP-like interface that provides encryption algorithms | ||
type Encryptor interface { | ||
|
||
// Encrypt encrypts plaintext using key k. | ||
// The opts argument should be appropriate for the algorithm used. | ||
Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) | ||
} | ||
|
||
// Decryptor is a BCCSP-like interface that provides decryption algorithms | ||
type Decryptor interface { | ||
|
||
// Decrypt decrypts ciphertext using key k. | ||
// The opts argument should be appropriate for the algorithm used. | ||
Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package mocks | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
|
||
"github.com/hyperledger/fabric/bccsp" | ||
) | ||
|
||
type Encryptor struct { | ||
KeyArg bccsp.Key | ||
PlaintextArg []byte | ||
OptsArg bccsp.EncrypterOpts | ||
|
||
EncValue []byte | ||
EncErr error | ||
} | ||
|
||
func (e *Encryptor) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) { | ||
if !reflect.DeepEqual(e.KeyArg, k) { | ||
return nil, errors.New("invalid key") | ||
} | ||
if !reflect.DeepEqual(e.PlaintextArg, plaintext) { | ||
return nil, errors.New("invalid plaintext") | ||
} | ||
if !reflect.DeepEqual(e.OptsArg, opts) { | ||
return nil, errors.New("invalid opts") | ||
} | ||
|
||
return e.EncValue, e.EncErr | ||
} |