1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | func TestInvokeValidation(t *testing.T) { fmt.Println("Entering TestInvokeValidation") attributes := make(map[string][]byte) attributes["username"] = []byte("vojha24") attributes["role"] = []byte("client") stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } _, err := stub.MockInvoke("t123", "CreateLoanApplication", []string{loanApplicationID, loanApplication}) if err == nil { t.Fatalf("Expected unauthorized user error to be returned") } } |
1 --- FAIL: TestInvokeValidation (0.00s) 2 sample_chaincode_test.go:158 Expected unauthorized user error to be returned 3 FAIL 4 exit status 1 |
1 2 3 4 | func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("Entering Invoke") return nil, errors.New("unauthorized user") } |
1 Entering TestInvokeValidation 2 2017/03/06 23:22:27 MockStub( mockStub &{} ) 3 Entering Invoke 4 PASS |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | func TestInvokeValidation2(t *testing.T) { fmt.Println("Entering TestInvokeValidation") attributes := make(map[string][]byte) attributes["username"] = []byte("vojha24") attributes["role"] = []byte("Bank_Admin") stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } _, err := stub.MockInvoke("t123", "CreateLoanApplication", []string{loanApplicationID, loanApplication}) if err != nil { t.Fatalf("Expected CreateLoanApplication to be invoked") } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("Entering Invoke") ubytes, _ := stub.ReadCertAttribute("username") rbytes, _ := stub.ReadCertAttribute("role") username := string(ubytes) role := string(rbytes) if role != "Bank_Admin" { return nil, errors.New("caller with " + username + " and role " + role + " does not have access to invoke CreateLoanApplication") } return nil, nil } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | func TestInvokeFunctionValidation(t *testing.T) { fmt.Println("Entering TestInvokeFunctionValidation") attributes := make(map[string][]byte) attributes["username"] = []byte("vojha24") attributes["role"] = []byte("Bank_Admin") stub := shim.NewCustomMockStub("mockStub", new(SampleChaincode), attributes) if stub == nil { t.Fatalf("MockStub creation failed") } _, err := stub.MockInvoke("t123", "InvalidFunctionName", []string{}) if err == nil { t.Fatalf("Expected invalid function name error") } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |