github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/common/ccpackage/ccpackage_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package ccpackage 8 9 import ( 10 "fmt" 11 "os" 12 "testing" 13 14 "github.com/golang/protobuf/proto" 15 "github.com/hyperledger/fabric-protos-go/common" 16 mspprotos "github.com/hyperledger/fabric-protos-go/msp" 17 "github.com/hyperledger/fabric-protos-go/peer" 18 "github.com/hyperledger/fabric/bccsp/sw" 19 "github.com/hyperledger/fabric/common/cauthdsl" 20 "github.com/hyperledger/fabric/msp" 21 mspmgmt "github.com/hyperledger/fabric/msp/mgmt" 22 msptesttools "github.com/hyperledger/fabric/msp/mgmt/testtools" 23 "github.com/hyperledger/fabric/protoutil" 24 ) 25 26 func ownerCreateCCDepSpec(codepackage []byte, sigpolicy *common.SignaturePolicyEnvelope, owner msp.SigningIdentity) (*common.Envelope, error) { 27 cds := &peer.ChaincodeDeploymentSpec{CodePackage: codepackage} 28 return OwnerCreateSignedCCDepSpec(cds, sigpolicy, owner) 29 } 30 31 // create an instantiation policy with just the local msp admin 32 func createInstantiationPolicy(mspid string, role mspprotos.MSPRole_MSPRoleType) *common.SignaturePolicyEnvelope { 33 principals := []*mspprotos.MSPPrincipal{{ 34 PrincipalClassification: mspprotos.MSPPrincipal_ROLE, 35 Principal: protoutil.MarshalOrPanic(&mspprotos.MSPRole{Role: role, MspIdentifier: mspid})}} 36 sigspolicy := []*common.SignaturePolicy{cauthdsl.SignedBy(int32(0))} 37 38 // create the policy: it requires exactly 1 signature from any of the principals 39 p := &common.SignaturePolicyEnvelope{ 40 Version: 0, 41 Rule: cauthdsl.NOutOf(1, sigspolicy), 42 Identities: principals, 43 } 44 45 return p 46 } 47 48 func TestOwnerCreateSignedCCDepSpec(t *testing.T) { 49 mspid, _ := localmsp.GetIdentifier() 50 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 51 env, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, signer) 52 if err != nil || env == nil { 53 t.Fatalf("error owner creating package %s", err) 54 return 55 } 56 } 57 58 func TestAddSignature(t *testing.T) { 59 mspid, _ := localmsp.GetIdentifier() 60 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 61 env, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, signer) 62 if err != nil || env == nil { 63 t.Fatalf("error owner creating package %s", err) 64 return 65 } 66 //add one more with the same signer (we don't have another signer to test with) 67 env, err = SignExistingPackage(env, signer) 68 if err != nil || env == nil { 69 t.Fatalf("error signing existing package %s", err) 70 return 71 } 72 //...and sign aother for luck 73 env, err = SignExistingPackage(env, signer) 74 if err != nil || env == nil { 75 t.Fatalf("error signing existing package %s", err) 76 return 77 } 78 79 p := &common.Payload{} 80 if err = proto.Unmarshal(env.Payload, p); err != nil { 81 t.Fatalf("fatal error unmarshal payload") 82 return 83 } 84 85 sigdepspec := &peer.SignedChaincodeDeploymentSpec{} 86 if err = proto.Unmarshal(p.Data, sigdepspec); err != nil || sigdepspec == nil { 87 t.Fatalf("fatal error unmarshal sigdepspec") 88 return 89 } 90 91 if len(sigdepspec.OwnerEndorsements) != 3 { 92 t.Fatalf("invalid number of endorsements %d", len(sigdepspec.OwnerEndorsements)) 93 return 94 } 95 } 96 97 func TestMissingSigaturePolicy(t *testing.T) { 98 env, err := ownerCreateCCDepSpec([]byte("codepackage"), nil, signer) 99 if err == nil || env != nil { 100 t.Fatalf("expected error on missing signature policy") 101 return 102 } 103 } 104 105 func TestCreateSignedCCDepSpecForInstall(t *testing.T) { 106 mspid, _ := localmsp.GetIdentifier() 107 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 108 env1, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, nil) 109 if err != nil || env1 == nil { 110 t.Fatalf("error owner creating package %s", err) 111 return 112 } 113 114 env2, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, nil) 115 if err != nil || env2 == nil { 116 t.Fatalf("error owner creating package %s", err) 117 return 118 } 119 120 pack := []*common.Envelope{env1, env2} 121 env, err := CreateSignedCCDepSpecForInstall(pack) 122 if err != nil || env == nil { 123 t.Fatalf("error creating install package %s", err) 124 return 125 } 126 127 p := &common.Payload{} 128 if err = proto.Unmarshal(env.Payload, p); err != nil { 129 t.Fatalf("fatal error unmarshal payload") 130 return 131 } 132 133 cip2 := &peer.SignedChaincodeDeploymentSpec{} 134 if err = proto.Unmarshal(p.Data, cip2); err != nil { 135 t.Fatalf("fatal error unmarshal cip") 136 return 137 } 138 139 p = &common.Payload{} 140 if err = proto.Unmarshal(env1.Payload, p); err != nil { 141 t.Fatalf("fatal error unmarshal payload") 142 return 143 } 144 145 cip1 := &peer.SignedChaincodeDeploymentSpec{} 146 if err = proto.Unmarshal(p.Data, cip1); err != nil { 147 t.Fatalf("fatal error unmarshal cip") 148 return 149 } 150 151 if err = ValidateCip(cip1, cip2); err != nil { 152 t.Fatalf("fatal error validating cip1 (%v) against cip2(%v)", cip1, cip2) 153 return 154 } 155 } 156 157 func TestCreateSignedCCDepSpecForInstallWithEndorsements(t *testing.T) { 158 mspid, _ := localmsp.GetIdentifier() 159 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 160 env1, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, signer) 161 if err != nil || env1 == nil { 162 t.Fatalf("error owner creating package %s", err) 163 return 164 } 165 166 env2, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, signer) 167 if err != nil || env2 == nil { 168 t.Fatalf("error owner creating package %s", err) 169 return 170 } 171 172 pack := []*common.Envelope{env1, env2} 173 env, err := CreateSignedCCDepSpecForInstall(pack) 174 if err != nil || env == nil { 175 t.Fatalf("error creating install package %s", err) 176 return 177 } 178 179 p := &common.Payload{} 180 if err = proto.Unmarshal(env.Payload, p); err != nil { 181 t.Fatalf("fatal error unmarshal payload") 182 return 183 } 184 185 cip2 := &peer.SignedChaincodeDeploymentSpec{} 186 if err = proto.Unmarshal(p.Data, cip2); err != nil { 187 t.Fatalf("fatal error unmarshal cip") 188 return 189 } 190 191 if len(cip2.OwnerEndorsements) != 2 { 192 t.Fatalf("invalid number of endorsements %d", len(cip2.OwnerEndorsements)) 193 return 194 } 195 196 p = &common.Payload{} 197 if err = proto.Unmarshal(env1.Payload, p); err != nil { 198 t.Fatalf("fatal error unmarshal payload") 199 return 200 } 201 202 cip1 := &peer.SignedChaincodeDeploymentSpec{} 203 if err = proto.Unmarshal(p.Data, cip1); err != nil { 204 t.Fatalf("fatal error unmarshal cip") 205 return 206 } 207 208 if len(cip1.OwnerEndorsements) != 1 { 209 t.Fatalf("invalid number of endorsements %d", len(cip1.OwnerEndorsements)) 210 return 211 } 212 } 213 214 func TestMismatchedCodePackages(t *testing.T) { 215 mspid, _ := localmsp.GetIdentifier() 216 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 217 env1, err := ownerCreateCCDepSpec([]byte("codepackage1"), sigpolicy, nil) 218 if err != nil || env1 == nil { 219 t.Fatalf("error owner creating package %s", err) 220 return 221 } 222 223 env2, err := ownerCreateCCDepSpec([]byte("codepackage2"), sigpolicy, nil) 224 if err != nil || env2 == nil { 225 t.Fatalf("error owner creating package %s", err) 226 return 227 } 228 pack := []*common.Envelope{env1, env2} 229 env, err := CreateSignedCCDepSpecForInstall(pack) 230 if err == nil || env != nil { 231 t.Fatalf("expected error creating install from mismatched code package but succeeded") 232 return 233 } 234 } 235 236 func TestMismatchedEndorsements(t *testing.T) { 237 mspid, _ := localmsp.GetIdentifier() 238 sigpolicy := createInstantiationPolicy(mspid, mspprotos.MSPRole_ADMIN) 239 env1, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, signer) 240 if err != nil || env1 == nil { 241 t.Fatalf("error owner creating package %s", err) 242 return 243 } 244 245 env2, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy, nil) 246 if err != nil || env2 == nil { 247 t.Fatalf("error owner creating package %s", err) 248 return 249 } 250 pack := []*common.Envelope{env1, env2} 251 env, err := CreateSignedCCDepSpecForInstall(pack) 252 if err == nil || env != nil { 253 t.Fatalf("expected error creating install from mismatched endorsed package but succeeded") 254 return 255 } 256 } 257 258 func TestMismatchedSigPolicy(t *testing.T) { 259 sigpolicy1 := createInstantiationPolicy("mspid1", mspprotos.MSPRole_ADMIN) 260 env1, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy1, signer) 261 if err != nil || env1 == nil { 262 t.Fatalf("error owner creating package %s", err) 263 return 264 } 265 266 sigpolicy2 := createInstantiationPolicy("mspid2", mspprotos.MSPRole_ADMIN) 267 env2, err := ownerCreateCCDepSpec([]byte("codepackage"), sigpolicy2, signer) 268 if err != nil || env2 == nil { 269 t.Fatalf("error owner creating package %s", err) 270 return 271 } 272 pack := []*common.Envelope{env1, env2} 273 env, err := CreateSignedCCDepSpecForInstall(pack) 274 if err == nil || env != nil { 275 t.Fatalf("expected error creating install from mismatched signature policies but succeeded") 276 return 277 } 278 } 279 280 var localmsp msp.MSP 281 var signer msp.SigningIdentity 282 var signerSerialized []byte 283 284 func TestMain(m *testing.M) { 285 // setup the MSP manager so that we can sign/verify 286 err := msptesttools.LoadMSPSetupForTesting() 287 if err != nil { 288 os.Exit(-1) 289 fmt.Printf("Could not initialize msp") 290 return 291 } 292 293 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 294 if err != nil { 295 fmt.Printf("Initialize cryptoProvider bccsp failed: %s", cryptoProvider) 296 os.Exit(-1) 297 return 298 } 299 localmsp = mspmgmt.GetLocalMSP(cryptoProvider) 300 if localmsp == nil { 301 os.Exit(-1) 302 fmt.Printf("Could not get msp") 303 return 304 } 305 signer, err = localmsp.GetDefaultSigningIdentity() 306 if err != nil { 307 os.Exit(-1) 308 fmt.Printf("Could not get signer") 309 return 310 } 311 312 signerSerialized, err = signer.Serialize() 313 if err != nil { 314 os.Exit(-1) 315 fmt.Printf("Could not serialize identity") 316 return 317 } 318 319 os.Exit(m.Run()) 320 }