Ejemplo n.º 1
0
func TestExample04_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex05", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("222"), []byte("b"), []byte("333")})
	stub.MockPeerChaincode(example02Url, stubEx2)

	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})

	// a + b = 222 + 333 = 555
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "222")
	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "333")

	// update A-=10 and B+=10
	checkInvoke(t, stubEx2, [][]byte{[]byte("invoke"), []byte("a"), []byte("b"), []byte("10")})

	// a + b = 212 + 343 = 555
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(example02Url), []byte("sumStoreName")})
	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "555") // example05 doesn't return JSON?
	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("a")}, "212")
	checkQuery(t, stubEx2, [][]byte{[]byte("query"), []byte("b")}, "343")
}
Ejemplo n.º 2
0
func TestExample04_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex05", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
	checkInit(t, stubEx2, []string{"a", "222", "b", "333"})
	stub.MockPeerChaincode(example02Url, stubEx2)

	checkInit(t, stub, []string{"sumStoreName", "0"})

	// a + b = 222 + 333 = 555
	checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
	checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
	checkQuery(t, stubEx2, []string{"a"}, "222")
	checkQuery(t, stubEx2, []string{"b"}, "333")

	// update A-=10 and B+=10
	checkInvoke(t, stubEx2, []string{"a", "b", "10"})

	// a + b = 212 + 343 = 555
	checkInvoke(t, stub, []string{example02Url, "sumStoreName"})
	checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "555") // example05 doesn't return JSON?
	checkQuery(t, stubEx2, []string{"a"}, "212")
	checkQuery(t, stubEx2, []string{"b"}, "343")
}
Ejemplo n.º 3
0
func TestExample04_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex04", scc)

	chaincodeToInvoke := "ex02"

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub(chaincodeToInvoke, ccEx2)
	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
	stub.MockPeerChaincode(chaincodeToInvoke, stubEx2)

	// Init A=567 B=678
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})

	// Invoke A->B for 10 via Example04's chaincode
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "101")
	checkQuery(t, stubEx2, "b", "232")

	// Invoke A->B for 10 via Example04's chaincode
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "91")
	checkQuery(t, stubEx2, "b", "242")
}
Ejemplo n.º 4
0
func TestExample04_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex05", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
	checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
	stub.MockPeerChaincode(example02Url, stubEx2)

	checkInit(t, stub, []string{"sumStoreName", "0"})

	// a + b = 111 + 222 = 333
	checkQuery(t, stub, []string{example02Url, "sumStoreName"}, "333") // example05 doesn't return JSON?
}
Ejemplo n.º 5
0
func TestExample04_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex05", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
	checkInit(t, stubEx2, [][]byte{[]byte("init"), []byte("a"), []byte("111"), []byte("b"), []byte("222")})
	stub.MockPeerChaincode(example02Url, stubEx2)

	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("0")})

	// a + b = 111 + 222 = 333
	checkQuery(t, stub, [][]byte{[]byte("query"), []byte(example02Url), []byte("sumStoreName")}, "333") // example05 doesn't return JSON?
}
Ejemplo n.º 6
0
//TestUpgradeNonExistChaincode tests upgrade non exist chaincode
func TestUpgradeNonExistChaincode(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var b []byte
	if b, err = proto.Marshal(cds); err != nil || b == nil {
		t.Fatalf("Marshal DeploymentSpec failed")
	}

	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.Fatalf("Deploy chaincode error: %v", err)
	}

	newCds, err := constructDeploymentSpec("example03", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var newb []byte
	if newb, err = proto.Marshal(newCds); err != nil || newb == nil {
		t.Fatalf("Marshal DeploymentSpec failed")
	}

	args = [][]byte{[]byte(UPGRADE), []byte("test"), newb}
	_, err = stub.MockInvoke("1", args)
	if _, ok := err.(NotFoundErr); !ok {
		t.FailNow()
	}
}
Ejemplo n.º 7
0
//TestUpgrade tests the upgrade function
func TestUpgrade(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var b []byte
	if b, err = proto.Marshal(cds); err != nil || b == nil {
		t.Fatalf("Marshal DeploymentSpec failed")
	}

	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.Fatalf("Deploy chaincode error: %v", err)
	}

	newCds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var newb []byte
	if newb, err = proto.Marshal(newCds); err != nil || newb == nil {
		t.Fatalf("Marshal DeploymentSpec failed")
	}

	args = [][]byte{[]byte(UPGRADE), []byte("test"), newb}
	version, err := stub.MockInvoke("1", args)
	if err != nil {
		t.Fatalf("Upgrade chaincode error: %v", err)
	}

	expectVer := "1"
	newVer := string(version)
	if newVer != expectVer {
		t.Fatalf("Upgrade chaincode version error, expected %s, got %s", expectVer, newVer)
	}
}
func TestInit(t *testing.T) {
	v := new(ValidatorOneValidSignature)
	stub := shim.NewMockStub("validatoronevalidsignature", v)

	if _, err := stub.MockInit("1", nil); err != nil {
		t.Fatalf("vscc init failed with %v", err)
	}
}
Ejemplo n.º 9
0
func TestInit(t *testing.T) {
	e := new(PeerConfiger)
	stub := shim.NewMockStub("PeerConfiger", e)

	if _, err := stub.MockInit("1", nil); err != nil {
		fmt.Println("Init failed", err)
		t.FailNow()
	}
}
Ejemplo n.º 10
0
func TestExample03_Init(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex03", scc)

	// Init A=123 B=234
	checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123")})

	checkState(t, stub, "A", "123")
}
Ejemplo n.º 11
0
func TestExample04_Init(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex04", scc)

	// Init A=123 B=234
	checkInit(t, stub, []string{"Event", "123"})

	checkState(t, stub, "Event", "123")
}
Ejemplo n.º 12
0
func TestExample04_Init(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex05", scc)

	// Init A=123 B=234
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("sumStoreName"), []byte("432")})

	checkState(t, stub, "sumStoreName", "432")
}
Ejemplo n.º 13
0
func TestExample04_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex04", scc)

	// Init A=345 B=456
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})

	// Query A
	checkQuery(t, stub, "Event", eventResponse)
}
Ejemplo n.º 14
0
func TestExample03_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex03", scc)

	// Init A=345 B=456
	checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345")})

	// Query A
	checkQuery(t, scc, stub, [][]byte{[]byte("query"), []byte("A"), []byte("345")})
}
Ejemplo n.º 15
0
func TestExample03_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex03", scc)

	// Init A=345 B=456
	checkInit(t, scc, stub, []string{"A", "345"})

	// Query A
	checkQuery(t, scc, stub, []string{"A", "345"}, "345")
}
Ejemplo n.º 16
0
func TestExample02_Init(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=123 B=234
	checkInit(t, stub, []string{"A", "123", "B", "234"})

	checkState(t, stub, "A", "123")
	checkState(t, stub, "B", "234")
}
Ejemplo n.º 17
0
func Test_Init(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=123 B=234
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")}, []byte("OK"))

	checkState(t, stub, "A", "123")
	checkState(t, stub, "B", "234")
}
Ejemplo n.º 18
0
//TestInvalidCodeDeploy tests the deploy function with invalid code package
func TestInvalidCodeDeploy(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	baddepspec := []byte("bad deploy spec")
	args := [][]byte{[]byte(DEPLOY), []byte("test"), baddepspec}
	_, err := stub.MockInvoke("1", args)
	if _, ok := err.(InvalidDeploymentSpecErr); !ok {
		t.FailNow()
	}
}
Ejemplo n.º 19
0
func TestExample02_Query(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=345 B=456
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345"), []byte("B"), []byte("456")})

	// Query A
	checkQuery(t, stub, "A", "345")

	// Query B
	checkQuery(t, stub, "B", "456")
}
Ejemplo n.º 20
0
func Test_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=567 B=678
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")}, []byte("OK"))

	// Invoke A->B for 123
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")}, []byte("{444,801}"))

	// Invoke B->A for 234
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")}, []byte("{567,678}"))
}
Ejemplo n.º 21
0
func TestInvokeJoinChainMissingParams(t *testing.T) {
	viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
	defer os.RemoveAll("/var/hyperledger/test/")

	e := new(PeerConfiger)
	stub := shim.NewMockStub("PeerConfiger", e)

	// Failed path: Not enough parameters
	args := [][]byte{[]byte("JoinChain")}
	if _, err := stub.MockInvoke("1", args); err == nil {
		t.Fatalf("cscc invoke JoinChain should have failed with invalid number of args: %v", args)
	}
}
Ejemplo n.º 22
0
func TestInvokeJoinChainWrongParams(t *testing.T) {
	viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
	defer os.RemoveAll("/var/hyperledger/test/")

	e := new(PeerConfiger)
	stub := shim.NewMockStub("PeerConfiger", e)

	// Failed path: wrong parameter type
	args := [][]byte{[]byte("JoinChain"), []byte("action")}
	if _, err := stub.MockInvoke("1", args); err == nil {
		fmt.Println("Invoke", args, "failed", err)
		t.Fatalf("cscc invoke JoinChain should have failed with null genesis block.  args: %v", args)
	}
}
Ejemplo n.º 23
0
func TestExample04_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex04", scc)

	ccEx2 := new(ex02.SimpleChaincode)
	stubEx2 := shim.NewMockStub("ex02", ccEx2)
	checkInit(t, stubEx2, []string{"a", "111", "b", "222"})
	stub.MockPeerChaincode(scc.GetChaincodeToCall(), stubEx2)

	// Init A=567 B=678
	checkInit(t, stub, []string{"Event", "1"})

	// Invoke A->B for 10 via Example04's chaincode
	checkInvoke(t, stub, []string{"Event", "1"})
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "101")
	checkQuery(t, stubEx2, "b", "232")

	// Invoke A->B for 10 via Example04's chaincode
	checkInvoke(t, stub, []string{"Event", "1"})
	checkQuery(t, stub, "Event", eventResponse)
	checkQuery(t, stubEx2, "a", "91")
	checkQuery(t, stubEx2, "b", "242")
}
Ejemplo n.º 24
0
//TestDeploy tests the deploy function (stops short of actually running the chaincode)
func TestDeploy(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var b []byte
	if b, err = proto.Marshal(cds); err != nil || b == nil {
		t.FailNow()
	}

	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.FailNow()
	}
}
Ejemplo n.º 25
0
func TestExample02_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=567 B=678
	checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")})

	// Invoke A->B for 123
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")})
	checkQuery(t, stub, "A", "444")
	checkQuery(t, stub, "B", "801")

	// Invoke B->A for 234
	checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")})
	checkQuery(t, stub, "A", "678")
	checkQuery(t, stub, "B", "567")
	checkQuery(t, stub, "A", "678")
	checkQuery(t, stub, "B", "567")
}
Ejemplo n.º 26
0
func TestExample02_Invoke(t *testing.T) {
	scc := new(SimpleChaincode)
	stub := shim.NewMockStub("ex02", scc)

	// Init A=567 B=678
	checkInit(t, stub, []string{"A", "567", "B", "678"})

	// Invoke A->B for 123
	checkInvoke(t, stub, []string{"A", "B", "123"})
	checkQuery(t, stub, "A", "444")
	checkQuery(t, stub, "B", "801")

	// Invoke B->A for 234
	checkInvoke(t, stub, []string{"B", "A", "234"})
	checkQuery(t, stub, "A", "678")
	checkQuery(t, stub, "B", "567")
	checkState(t, stub, "A", "678")
	checkState(t, stub, "B", "567")
}
Ejemplo n.º 27
0
//TestInvalidChaincodeName tests the deploy function with invalid chaincode name
func TestInvalidChaincodeName(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})

	//change name to empty
	cds.ChaincodeSpec.ChaincodeID.Name = ""

	var b []byte
	if b, err = proto.Marshal(cds); err != nil || b == nil {
		t.FailNow()
	}

	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
	_, err = stub.MockInvoke("1", args)
	if _, ok := err.(InvalidChaincodeNameErr); !ok {
		t.FailNow()
	}
}
Ejemplo n.º 28
0
func TestInvokeUpdateConfigBlock(t *testing.T) {
	e := new(PeerConfiger)
	stub := shim.NewMockStub("PeerConfiger", e)

	// Failed path: Not enough parameters
	args := [][]byte{[]byte("UpdateConfigBlock")}
	if _, err := stub.MockInvoke("1", args); err == nil {
		t.Fatalf("cscc invoke UpdateConfigBlock should have failed with invalid number of args: %v", args)
	}

	// Failed path: wrong parameter type
	args = [][]byte{[]byte("UpdateConfigBlock"), []byte("action")}
	if _, err := stub.MockInvoke("1", args); err == nil {
		fmt.Println("Invoke", args, "failed", err)
		t.Fatalf("cscc invoke UpdateConfigBlock should have failed with null genesis block - args: %v", args)
	}

	// Successful path for JoinChain
	blockBytes := mockConfigBlock()
	if blockBytes == nil {
		t.Fatalf("cscc invoke UpdateConfigBlock failed because invalid block")
	}
	args = [][]byte{[]byte("UpdateConfigBlock"), blockBytes}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.Fatalf("cscc invoke UpdateConfigBlock failed with: %v", err)
	}

	// Query the configuration block
	//chainID := []byte{143, 222, 22, 192, 73, 145, 76, 110, 167, 154, 118, 66, 132, 204, 113, 168}
	chainID, err := getChainID(blockBytes)
	if err != nil {
		t.Fatalf("cscc invoke UpdateConfigBlock failed with: %v", err)
	}
	args = [][]byte{[]byte("GetConfigBlock"), []byte(chainID)}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.Fatalf("cscc invoke GetConfigBlock failed with: %v", err)
	}

}
Ejemplo n.º 29
0
//TestRetryFailedDeploy tests re-deploying after a failure
func TestRetryFailedDeploy(t *testing.T) {
	initialize()

	scc := new(LifeCycleSysCC)
	stub := shim.NewMockStub("lccc", scc)

	//deploy 02
	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
	var b []byte
	if b, err = proto.Marshal(cds); err != nil || b == nil {
		t.FailNow()
	}

	//send invalid chain name name that should fail
	args := [][]byte{[]byte(DEPLOY), []byte(""), b}
	if _, err = stub.MockInvoke("1", args); err == nil {
		//expected error but got success
		t.FailNow()
	}

	if _, ok := err.(InvalidChainNameErr); !ok {
		//expected invalid chain name
		t.FailNow()
	}

	//deploy correctly now
	args = [][]byte{[]byte(DEPLOY), []byte("test"), b}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.FailNow()
	}

	//get the deploymentspec
	args = [][]byte{[]byte(GETDEPSPEC), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeID.Name)}
	if depspec, err := stub.MockInvoke("1", args); err != nil || depspec == nil {
		t.FailNow()
	}
}
func TestInvoke(t *testing.T) {
	v := new(ValidatorOneValidSignature)
	stub := shim.NewMockStub("validatoronevalidsignature", v)

	// Failed path: Invalid arguments
	args := [][]byte{[]byte("dv")}
	if _, err := stub.MockInvoke("1", args); err == nil {
		t.Fatalf("vscc invoke should have failed")
		return
	}

	args = [][]byte{[]byte("dv"), []byte("tx")}
	args[1] = nil
	if _, err := stub.MockInvoke("1", args); err == nil {
		t.Fatalf("vscc invoke should have failed")
		return
	}

	tx, err := createTx()
	if err != nil {
		t.Fatalf("createTx returned err %s", err)
		return
	}

	envBytes, err := utils.GetBytesEnvelope(tx)
	if err != nil {
		t.Fatalf("GetBytesEnvelope returned err %s", err)
		return
	}

	args = [][]byte{[]byte("dv"), envBytes}
	if _, err := stub.MockInvoke("1", args); err != nil {
		t.Fatalf("vscc invoke returned err %s", err)
		return
	}
}