Skip to content

Commit 98a9109

Browse files
committed
tests: add integration test for skvs on secret manager
Signed-off-by: chenchanglew <lewchenchang@gmail.com>
1 parent 43d9238 commit 98a9109

File tree

5 files changed

+261
-1
lines changed

5 files changed

+261
-1
lines changed

integration/go_chaincode/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
TOP = ../..
44
include $(TOP)/build.mk
55

6-
GO_TEST_DIRS=auction kv_test
6+
GO_TEST_DIRS=auction kv_test skvs
77

88
deps: ercc images
99

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright IBM Corp. All Rights Reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
TOP = ../../..
6+
include $(TOP)/build.mk
7+
8+
CHAINCODE_PATH=$(FPC_PATH)/samples/chaincode/secret-keeper-go
9+
10+
all: build test clean
11+
12+
test:
13+
FABRIC_LOGGING_SPEC=fpc=debug:grpc=error:comm.grpc=error:gossip=warning:info go test -v -failfast .
14+
15+
build:
16+
make -C $(CHAINCODE_PATH) ECC_MAIN_FILES=$(CHAINCODE_PATH)/cmd/skvs/main.go with_go env docker
17+
cp $(CHAINCODE_PATH)/mrenclave .
18+
19+
clean:
20+
rm -rf cmd
21+
rm mrenclave
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
"encoding/json"
11+
"fmt"
12+
13+
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/fpc"
14+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
15+
"github.com/pkg/errors"
16+
)
17+
18+
type Client struct {
19+
CID string
20+
Function string
21+
Args []string
22+
}
23+
24+
type ClientView struct {
25+
*Client
26+
}
27+
28+
func (c *ClientView) Call(context view.Context) (interface{}, error) {
29+
fmt.Printf("Call FPC (CID='%s') with f='%s' and Args='%v'\n", c.CID, c.Function, c.Args)
30+
_, err := fpc.GetDefaultChannel(context).Chaincode(c.CID).Invoke(c.Function, fpc.StringsToArgs(c.Args)...).Call()
31+
if err != nil {
32+
return nil, errors.Wrapf(err, "error invoking %s", c.Function)
33+
}
34+
35+
return nil, nil
36+
}
37+
38+
type ClientViewFactory struct{}
39+
40+
func (c *ClientViewFactory) NewView(in []byte) (view.View, error) {
41+
f := &ClientView{Client: &Client{}}
42+
if err := json.Unmarshal(in, f.Client); err != nil {
43+
return nil, err
44+
}
45+
return f, nil
46+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"fmt"
11+
"os"
12+
"strings"
13+
14+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/api"
15+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric"
16+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/topology"
17+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"
18+
fabric2 "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/sdk"
19+
"github.com/hyperledger/fabric-private-chaincode/integration/go_chaincode/utils"
20+
"github.com/pkg/errors"
21+
)
22+
23+
const (
24+
ChaincodeName = "secret-keeper-go"
25+
ChaincodeImageName = "fpc/secret-keeper-go"
26+
ChaincodeImageTag = "latest"
27+
)
28+
29+
func Topology() []api.Topology {
30+
// make ECC_MAIN_FILES=cmd/skvs/main.go with_go env docker
31+
chaincodeImageName := fmt.Sprintf("%s:%s", ChaincodeImageName, ChaincodeImageTag)
32+
var fpcOptions []func(chaincode *topology.ChannelChaincode)
33+
34+
if strings.ToUpper(os.Getenv("SGX_MODE")) == "HW" {
35+
chaincodeImageName = fmt.Sprintf("%s-hw:%s", ChaincodeImageName, ChaincodeImageTag)
36+
fpcOptions = append(fpcOptions, topology.WithSGXMode("HW"))
37+
38+
mrenclave, err := utils.ReadMrenclaveFromFile("mrenclave")
39+
if err != nil {
40+
panic(errors.Wrapf(err, "cannot get mrenclave"))
41+
}
42+
fpcOptions = append(fpcOptions, topology.WithMREnclave(mrenclave))
43+
44+
sgxDevicePath, err := utils.DetectSgxDevicePath()
45+
if err != nil {
46+
panic(errors.Wrapf(err, "SGX HW mode set but now sgx device found"))
47+
}
48+
fpcOptions = append(fpcOptions, topology.WithSGXDevicesPaths(sgxDevicePath))
49+
}
50+
51+
fabricTopology := fabric.NewDefaultTopology()
52+
fabricTopology.AddOrganizationsByName("Org1")
53+
fabricTopology.AddFPC(ChaincodeName, chaincodeImageName, fpcOptions...)
54+
fabricTopology.SetLogging("fpc=debug:grpc=error:comm.grpc=error:gossip=warning:info", "")
55+
fscTopology := fsc.NewTopology()
56+
57+
// client Alice
58+
clientNodeAlice := fscTopology.AddNodeByName("alice")
59+
clientNodeAlice.AddOptions(fabric.WithOrganization("Org1"))
60+
clientNodeAlice.RegisterViewFactory("invoke", &ClientViewFactory{})
61+
62+
// client Bob
63+
// clientNodeBob := fscTopology.AddNodeByName("bob")
64+
// clientNodeBob.AddOptions(fabric.WithOrganization("Org2"))
65+
// clientNodeBob.RegisterViewFactory("invoke", &ClientViewFactory{})
66+
67+
// Add Fabric SDK to FSC Nodes
68+
fscTopology.AddSDK(&fabric2.SDK{})
69+
70+
return []api.Topology{fabricTopology, fscTopology}
71+
}

0 commit comments

Comments
 (0)