-
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 Hash test coverage
Using the approach discussed in FAB-3465, this change-sets refactors the way hashing 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 Hash is now at more than 85% Change-Id: Ib9c5b45da54241d959c09f7b0351f3163be0c6ef Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
- Loading branch information
Showing
8 changed files
with
240 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
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 ( | ||
"hash" | ||
|
||
"github.com/hyperledger/fabric/bccsp" | ||
) | ||
|
||
type hasher struct { | ||
hash func() hash.Hash | ||
} | ||
|
||
func (c *hasher) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) { | ||
h := c.hash() | ||
h.Write(msg) | ||
return h.Sum(nil), nil | ||
} | ||
|
||
func (c *hasher) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) { | ||
return c.hash(), nil | ||
} |
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,84 @@ | ||
/* | ||
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 ( | ||
"crypto/sha256" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
mocks2 "github.com/hyperledger/fabric/bccsp/mocks" | ||
"github.com/hyperledger/fabric/bccsp/sw/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHash(t *testing.T) { | ||
expectetMsg := []byte{1, 2, 3, 4} | ||
expectedOpts := &mocks2.HashOpts{} | ||
expectetValue := []byte{1, 2, 3, 4, 5} | ||
expectedErr := errors.New("no error") | ||
|
||
hashers := make(map[reflect.Type]Hasher) | ||
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{ | ||
MsgArg: expectetMsg, | ||
OptsArg: expectedOpts, | ||
Value: expectetValue, | ||
Err: expectedErr, | ||
} | ||
|
||
csp := impl{hashers: hashers} | ||
|
||
value, err := csp.Hash(expectetMsg, expectedOpts) | ||
assert.Equal(t, expectetValue, value) | ||
assert.Equal(t, expectedErr, err) | ||
} | ||
|
||
func TestGetHash(t *testing.T) { | ||
expectedOpts := &mocks2.HashOpts{} | ||
expectetValue := sha256.New() | ||
expectedErr := errors.New("no error") | ||
|
||
hashers := make(map[reflect.Type]Hasher) | ||
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{ | ||
OptsArg: expectedOpts, | ||
ValueHash: expectetValue, | ||
Err: expectedErr, | ||
} | ||
|
||
csp := impl{hashers: hashers} | ||
|
||
value, err := csp.GetHash(expectedOpts) | ||
assert.Equal(t, expectetValue, value) | ||
assert.Equal(t, expectedErr, err) | ||
} | ||
|
||
func TestHasher(t *testing.T) { | ||
hasher := &hasher{hash: sha256.New} | ||
|
||
msg := []byte("Hello World") | ||
out, err := hasher.Hash(msg, nil) | ||
assert.NoError(t, err) | ||
h := sha256.New() | ||
h.Write(msg) | ||
out2 := h.Sum(nil) | ||
assert.Equal(t, out, out2) | ||
|
||
hf, err := hasher.GetHash(nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, hf, sha256.New()) | ||
} |
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
Oops, something went wrong.