|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 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 identity |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/hyperledger/fabric/gossip/api" |
| 25 | + "github.com/hyperledger/fabric/gossip/common" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +var msgCryptoService = &naiveCryptoService{} |
| 30 | + |
| 31 | +type naiveCryptoService struct { |
| 32 | +} |
| 33 | + |
| 34 | +func (*naiveCryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error { |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +// GetPKIidOfCert returns the PKI-ID of a peer's identity |
| 39 | +func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType { |
| 40 | + return common.PKIidType(peerIdentity) |
| 41 | +} |
| 42 | + |
| 43 | +// VerifyBlock returns nil if the block is properly signed, |
| 44 | +// else returns error |
| 45 | +func (*naiveCryptoService) VerifyBlock(signedBlock api.SignedBlock) error { |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// Sign signs msg with this peer's signing key and outputs |
| 50 | +// the signature if no error occurred. |
| 51 | +func (*naiveCryptoService) Sign(msg []byte) ([]byte, error) { |
| 52 | + return msg, nil |
| 53 | +} |
| 54 | + |
| 55 | +// Verify checks that signature is a valid signature of message under a peer's verification key. |
| 56 | +// If the verification succeeded, Verify returns nil meaning no error occurred. |
| 57 | +// If peerCert is nil, then the signature is verified against this peer's verification key. |
| 58 | +func (*naiveCryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error { |
| 59 | + equal := bytes.Equal(signature, message) |
| 60 | + if !equal { |
| 61 | + return fmt.Errorf("Wrong certificate") |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func TestPut(t *testing.T) { |
| 67 | + idStore := NewIdentityMapper(msgCryptoService) |
| 68 | + identity := []byte("yacovm") |
| 69 | + identity2 := []byte("not-yacovm") |
| 70 | + pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity)) |
| 71 | + pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2)) |
| 72 | + assert.NoError(t, idStore.Put(pkiID, identity)) |
| 73 | + assert.Error(t, idStore.Put(nil, identity)) |
| 74 | + assert.Error(t, idStore.Put(pkiID2, nil)) |
| 75 | + assert.Error(t, idStore.Put(pkiID2, identity)) |
| 76 | + assert.Error(t, idStore.Put(pkiID, identity2)) |
| 77 | +} |
| 78 | + |
| 79 | +func TestGet(t *testing.T) { |
| 80 | + idStore := NewIdentityMapper(msgCryptoService) |
| 81 | + identity := []byte("yacovm") |
| 82 | + identity2 := []byte("not-yacovm") |
| 83 | + pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity)) |
| 84 | + pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2)) |
| 85 | + assert.NoError(t, idStore.Put(pkiID, identity)) |
| 86 | + cert, err := idStore.Get(pkiID) |
| 87 | + assert.NoError(t, err) |
| 88 | + assert.Equal(t, api.PeerIdentityType(identity), cert) |
| 89 | + cert, err = idStore.Get(pkiID2) |
| 90 | + assert.Nil(t, cert) |
| 91 | + assert.Error(t, err) |
| 92 | +} |
| 93 | + |
| 94 | +func TestVerify(t *testing.T) { |
| 95 | + idStore := NewIdentityMapper(msgCryptoService) |
| 96 | + identity := []byte("yacovm") |
| 97 | + identity2 := []byte("not-yacovm") |
| 98 | + pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity)) |
| 99 | + pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2)) |
| 100 | + idStore.Put(pkiID, api.PeerIdentityType(identity)) |
| 101 | + signed, err := idStore.Sign([]byte("bla bla")) |
| 102 | + assert.NoError(t, err) |
| 103 | + assert.NoError(t, idStore.Verify(pkiID, signed, []byte("bla bla"))) |
| 104 | + assert.Error(t, idStore.Verify(pkiID2, signed, []byte("bla bla"))) |
| 105 | +} |
0 commit comments