|
| 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 localmsp |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/hyperledger/fabric/core/crypto/primitives" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/hyperledger/fabric/core/peer/msp" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMain(m *testing.M) { |
| 29 | + // 1. Determine MSP configuration |
| 30 | + var mspMgrConfigDir string |
| 31 | + var alternativeCfgPath = os.Getenv("ORDERER_CFG_PATH") |
| 32 | + if alternativeCfgPath != "" { |
| 33 | + mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/" |
| 34 | + } else if _, err := os.Stat("./msp/sampleconfig/"); err == nil { |
| 35 | + mspMgrConfigDir = "./msp/sampleconfig/" |
| 36 | + } else { |
| 37 | + mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" |
| 38 | + } |
| 39 | + |
| 40 | + if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir); err != nil { |
| 41 | + os.Exit(-1) |
| 42 | + } |
| 43 | + |
| 44 | + os.Exit(m.Run()) |
| 45 | +} |
| 46 | + |
| 47 | +func TestNewSigner(t *testing.T) { |
| 48 | + signer := NewSigner() |
| 49 | + assert.NotNil(t, signer, "Signer must be differentr from nil.") |
| 50 | +} |
| 51 | + |
| 52 | +func TestMspSigner_NewSignatureHeader(t *testing.T) { |
| 53 | + signer := NewSigner() |
| 54 | + |
| 55 | + sh, err := signer.NewSignatureHeader() |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Failed creting signature header [%s]", err) |
| 58 | + } |
| 59 | + |
| 60 | + assert.NotNil(t, sh, "SignatureHeader must be different from nil") |
| 61 | + assert.Len(t, sh.Nonce, primitives.NonceSize, "SignatureHeader.Nonce must be of length %d", primitives.NonceSize) |
| 62 | + |
| 63 | + mspIdentity, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity() |
| 64 | + assert.NoError(t, err, "Failed getting default MSP Identity") |
| 65 | + publicIdentity := mspIdentity.GetPublicVersion() |
| 66 | + assert.NotNil(t, publicIdentity, "Failed getting default public identity. It must be different from nil.") |
| 67 | + publicIdentityRaw, err := publicIdentity.Serialize() |
| 68 | + assert.NoError(t, err, "Failed serializing default public identity") |
| 69 | + assert.Equal(t, publicIdentityRaw, sh.Creator, "Creator must be local default signer identity") |
| 70 | +} |
| 71 | + |
| 72 | +func TestMspSigner_Sign(t *testing.T) { |
| 73 | + signer := NewSigner() |
| 74 | + |
| 75 | + msg := []byte("Hello World") |
| 76 | + sigma, err := signer.Sign(msg) |
| 77 | + assert.NoError(t, err, "FAiled generating signature") |
| 78 | + |
| 79 | + // Verify signature |
| 80 | + mspIdentity, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity() |
| 81 | + assert.NoError(t, err, "Failed getting default MSP Identity") |
| 82 | + err = mspIdentity.Verify(msg, sigma) |
| 83 | + assert.NoError(t, err, "Failed verifiing signature") |
| 84 | +} |
0 commit comments