|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package kv |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-smart-client/integration" |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestFlow(t *testing.T) { |
| 18 | + |
| 19 | + // setup fabric network |
| 20 | + ii, err := integration.Generate(23000, false, Topology()...) |
| 21 | + assert.NoError(t, err) |
| 22 | + ii.Start() |
| 23 | + defer ii.Stop() |
| 24 | + |
| 25 | + // 1. Initialize Secret Keeper: |
| 26 | + // ./fpcclient invoke InitSecretKeeper |
| 27 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 28 | + CID: ChaincodeName, |
| 29 | + Function: "InitSecretKeeper", |
| 30 | + Args: []string{}, |
| 31 | + })) |
| 32 | + assert.NoError(t, err) |
| 33 | + |
| 34 | + // 2. Reveal the secret as Alice: |
| 35 | + // ./fpcclient query RevealSecret Alice |
| 36 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 37 | + CID: ChaincodeName, |
| 38 | + Function: "RevealSecret", |
| 39 | + Args: []string{"Alice"}, |
| 40 | + })) |
| 41 | + assert.NoError(t, err) |
| 42 | + |
| 43 | + // 3. Change the secret as Bob: |
| 44 | + // ./fpcclient invoke LockSecret Bob NewSecret |
| 45 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 46 | + CID: ChaincodeName, |
| 47 | + Function: "LockSecret", |
| 48 | + Args: []string{"Bob", "NewSecret"}, |
| 49 | + })) |
| 50 | + assert.NoError(t, err) |
| 51 | + |
| 52 | + // 4. Attempt to reveal the secret as Alice (now updated): |
| 53 | + // ./fpcclient query revealSecret Alice |
| 54 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 55 | + CID: ChaincodeName, |
| 56 | + Function: "RevealSecret", |
| 57 | + Args: []string{"Alice"}, |
| 58 | + })) |
| 59 | + assert.NoError(t, err) |
| 60 | + |
| 61 | + // 5. Remove Bob's access as Alice: |
| 62 | + // ./fpcclient invoke removeUser Alice Bob |
| 63 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 64 | + CID: ChaincodeName, |
| 65 | + Function: "RemoveUser", |
| 66 | + Args: []string{"Alice", "Bob"}, |
| 67 | + })) |
| 68 | + assert.NoError(t, err) |
| 69 | + |
| 70 | + // 6. Attempt to reveal the secret as Bob (should fail): |
| 71 | + // ./fpcclient query revealSecret Bob // (will failed) |
| 72 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 73 | + CID: ChaincodeName, |
| 74 | + Function: "RevealSecret", |
| 75 | + Args: []string{"Bob"}, |
| 76 | + })) |
| 77 | + assert.Error(t, err) |
| 78 | + |
| 79 | + // 7. Re-add Bob to the authorization list as Alice: |
| 80 | + // ./fpcclient invoke addUser Alice Bob |
| 81 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 82 | + CID: ChaincodeName, |
| 83 | + Function: "AddUser", |
| 84 | + Args: []string{"Alice", "Bob"}, |
| 85 | + })) |
| 86 | + assert.NoError(t, err) |
| 87 | + |
| 88 | + // 8. Bob can now reveal the secret successfully: |
| 89 | + // ./fpcclient query revealSecret Bob // (will success) |
| 90 | + _, err = ii.Client("alice").CallView("invoke", common.JSONMarshall(&Client{ |
| 91 | + CID: ChaincodeName, |
| 92 | + Function: "RevealSecret", |
| 93 | + Args: []string{"Bob"}, |
| 94 | + })) |
| 95 | + assert.NoError(t, err) |
| 96 | + |
| 97 | + /* |
| 98 | + TODO: |
| 99 | + instead of using Alice/Bob as a parameter during invoke/query |
| 100 | + we use the signer identity |
| 101 | + NEED to change secret-keeper.go implementation. |
| 102 | +
|
| 103 | + Before: |
| 104 | + cd $FPC_PATH/samples/application/simple-cli-go |
| 105 | + make |
| 106 | + source $FPC_PATH/samples/chaincode/secret-keeper-go/details.env |
| 107 | + $FPC_PATH/samples/deployment/fabric-smart-client/the-simple-testing-network/env.sh Org1 |
| 108 | + source Org1.env |
| 109 | + After: |
| 110 | + cd $FPC_PATH/samples/application/simple-cli-go |
| 111 | + make |
| 112 | + source $FPC_PATH/samples/chaincode/secret-keeper-go/details.env |
| 113 | + $FPC_PATH/samples/deployment/fabric-smart-client/the-simple-testing-network/env.sh Org1 |
| 114 | + source Org1.env |
| 115 | +
|
| 116 | + (Another Terminal) |
| 117 | + $FPC_PATH/samples/deployment/fabric-smart-client/the-simple-testing-network/env.sh Org2 |
| 118 | + source Org2.env |
| 119 | +
|
| 120 | + make ECC_MAIN_FILES=cmd/skvs/main.go with_go env docker |
| 121 | + */ |
| 122 | +} |
0 commit comments