add gongxianghui_auth code

parent 77fa04d6
This diff is collapsed.
package main package main
import ( import (
"bytes"
"crypto/x509"
"encoding/json" "encoding/json"
"encoding/pem"
"fmt" "fmt"
"github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer" pb "github.com/hyperledger/fabric/protos/peer"
...@@ -55,7 +58,8 @@ func (t *GXHCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response { ...@@ -55,7 +58,8 @@ func (t *GXHCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
} }
return shim.Success([]byte(res)) return shim.Success([]byte(res))
case "cert":
testCertificate(stub)
default: default:
return shim.Error(fmt.Sprintf("Unsupported function %s", functionName)) return shim.Error(fmt.Sprintf("Unsupported function %s", functionName))
...@@ -84,18 +88,18 @@ func put(args []string, stub shim.ChaincodeStubInterface) (string, error) { ...@@ -84,18 +88,18 @@ func put(args []string, stub shim.ChaincodeStubInterface) (string, error) {
json.Unmarshal([]byte(result),&schemaMap) json.Unmarshal([]byte(result),&schemaMap)
if err := json.Unmarshal([]byte(args[1]), &Result); err != nil { if err := json.Unmarshal([]byte(args[1]), &Result); err != nil {
return "", fmt.Errorf("parameters Unmarshal fail,args string not json string, err:%s", err) return "", fmt.Errorf("parameters Unmarshal fail,args string not json array, err:%s", err)
} }
for k, mapres := range Result{ for k, mapres := range Result{
Id ,ok:= mapres["id"] Id ,ok:= mapres["id"]
if !ok{ if !ok{
return "", fmt.Errorf("The id field must exist ") return "", fmt.Errorf("The id field must exist ")
} }
if result,err := stub.GetState(args[0]+Id.(string));err!=nil{ //if result,err := stub.GetState(args[0]+Id.(string));err!=nil{
return "", fmt.Errorf("get %s data happen err: ", args[0]+Id.(string)) // return "", fmt.Errorf("get %s data happen err: ", args[0]+Id.(string))
}else if result!=nil{ //}else if result!=nil{
return "", fmt.Errorf("%s data already exist", args[0]+Id.(string)) // return "", fmt.Errorf("%s data already exist", args[0]+Id.(string))
} //}
if !SchemaCheck(schemaMap,mapres){ if !SchemaCheck(schemaMap,mapres){
return "", fmt.Errorf("SchemaCheck fail,args index %d string doesn't match %s format", k,args[0]) return "", fmt.Errorf("SchemaCheck fail,args index %d string doesn't match %s format", k,args[0])
} }
...@@ -278,6 +282,7 @@ func (this *Schema) put(args []string, stub shim.ChaincodeStubInterface) (string ...@@ -278,6 +282,7 @@ func (this *Schema) put(args []string, stub shim.ChaincodeStubInterface) (string
/* /*
if update Schema 那么我们没有办法比较旧有的一个模板, if update Schema 那么我们没有办法比较旧有的一个模板,
Schema 的 update,不支持部分修改,因为存在增加和删除某些字段的情况。
TODO,增加版本管理 TODO,增加版本管理
*/ */
func (this *Schema) update(args []string, stub shim.ChaincodeStubInterface) (string, error) { func (this *Schema) update(args []string, stub shim.ChaincodeStubInterface) (string, error) {
...@@ -318,6 +323,36 @@ func (this *Schema) get(args string, stub shim.ChaincodeStubInterface) (string, ...@@ -318,6 +323,36 @@ func (this *Schema) get(args string, stub shim.ChaincodeStubInterface) (string,
return string(result), nil return string(result), nil
} }
func testCertificate(stub shim.ChaincodeStubInterface) pb.Response{
creatorByte,_:= stub.GetCreator()
certStart := bytes.IndexAny(creatorByte, "-----BEGIN")
if certStart == -1 {
fmt.Errorf("No certificate found")
}
certText := creatorByte[certStart:]
bl, _ := pem.Decode(certText)
if bl == nil {
fmt.Errorf("Could not decode the PEM structure")
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
fmt.Errorf("ParseCertificate failed")
}
fmt.Println("cert %v ",cert)
result,err := json.Marshal(cert)
if err!=nil{
fmt.Println("Marshal err:",err)
}
fmt.Println("cert marshal:",string(result))
fmt.Println("cert Subject %v ",cert.Subject)
uname:=cert.Subject.CommonName
fmt.Println("Name:"+uname)
return shim.Success([]byte("Called testCertificate "+uname))
}
func main() { func main() {
err := shim.Start(&GXHCC{}) err := shim.Start(&GXHCC{})
if err != nil { if err != nil {
......
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"testing"
)
func TestGXHCC(t *testing.T) {
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
response:= stub.MockInit("init",nil)
fmt.Println(string(response.Payload))
input := `{"aname":"alibusi","aname":"alibus","atype":"type","alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"","aader":"aader","amob":"1234567890"}}`
responseByPut := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",responseByPut.Status,responseByPut.Message,string(responseByPut.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s \n",responseByget.Status,responseByget.Message,string(responseByget.Payload))
return
input2 := `{"id":"1","aname":"aaa","atype":"aaa","alinkman":"cc","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"aaa","aader":"aader","amob":"1234567890"}}`
responseByPut2 := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(input2)})
fmt.Printf("Invoke22 status %d,message %s and payload %s\n",responseByPut2.Status,responseByPut2.Message,string(responseByPut2.Payload))
responseByget2 := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke22 status %d,message %s and payload %s",responseByget2.Status,responseByget2.Message,string(responseByget2.Payload))
}
func TestGXHSchema(t *testing.T){
input := `{"id":"1","aname":"alibusi","aname":"alibus","atype":"type","alinkman":"","aader":"aader",
"amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"",
"aader":"aader","amob":"1234567890"}}`
inputarrary := `[{"id":"1","aname":"alibusi","atype":"type","alinkman":"",
"aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890"}},{"id":"2","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890"}}]`
update := `{"alinkman":"张三","id":"1","backup":{}}`
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
response:= stub.MockInit("init",nil)
fmt.Println("MockInit Payload: ",string(response.Payload))
fmt.Println()
fmt.Println("========================================invoke schema put============================================")
fmt.Println()
fmt.Println()
responseByPutschema := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("put"),[]byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
responseByGet := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("get"),[]byte("alibusi")})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByGet.Status,responseByGet.Message,string(responseByGet.Payload))
fmt.Println()
fmt.Println("========================================invoke data put============================================")
fmt.Println()
fmt.Println()
responseByPut := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(inputarrary)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByPut.Status,responseByPut.Message,string(responseByPut.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget.Status,responseByget.Message,string(responseByget.Payload))
fmt.Println()
fmt.Println("========================================invoke data update============================================")
fmt.Println()
fmt.Println()
responseByUpdate := stub.MockInvoke("invoke1",[][]byte{[]byte("update"), []byte("alibusi"),[]byte(update)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByUpdate.Status,responseByUpdate.Message,string(responseByUpdate.Payload))
responseByget2 := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget2.Status,responseByget2.Message,string(responseByget2.Payload))
//inputfail := `{"id":"2","atype":"type","alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"","aader":"aader","amob":"1234567890"}}`
//fmt.Println()
//fmt.Println("========================================fail input data============================================")
//
//
//responseByPutfail := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(inputfail)})
//fmt.Printf("Invoke status %d,message %s and payload %s\n",
// responseByPutfail.Status,responseByPutfail.Message,string(responseByPutfail.Payload))
//responseBygetfail := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
//fmt.Printf("Invoke status %d,message %s and payload %s \n",
// responseBygetfail.Status,responseBygetfail.Message,string(responseBygetfail.Payload))
}
func TestGXH(t *testing.T){
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
stub.MockInit("init",nil)
stub.MockInvoke("invoke1",[][]byte{[]byte("cert")})
}
\ No newline at end of file
This diff is collapsed.
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"testing"
)
var schema = `{"Parameters":{"Alinkman":{"Value":"Alinkman","Read":"","Write":""},"Amob":{"Value":"Amob","Read":"","Write":""},
"Aname":{"Value":"Aname","Read":"","Write":""},"Atype":{"Value":"Atype","Read":"","Write":""},"Backup":{"Value":[{"Value":"1",
"Read":"","Write":""},{"Value":"Aname","Read":"","Write":""},{"Value":"Atype","Read":"","Write":""},{"Value":"Alinkman","Read":"","Write":""}],
"Read":"","Write":""},"id":{"Value":"1","Read":"","Write":""}},"Read":"","Write":"","ParametersRead":true,"ParametersWrite":true}`
func TestGXHCC(t *testing.T) {
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
response:= stub.MockInit("init",nil)
fmt.Println(string(response.Payload))
input := `{"aname":"alibusi","aname":"alibus","atype":"type","alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"","aader":"aader","amob":"1234567890"}}`
responseByPut := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",responseByPut.Status,responseByPut.Message,string(responseByPut.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s \n",responseByget.Status,responseByget.Message,string(responseByget.Payload))
return
input2 := `{"id":"1","aname":"aaa","atype":"aaa","alinkman":"cc","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"aaa","aader":"aader","amob":"1234567890"}}`
responseByPut2 := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(input2)})
fmt.Printf("Invoke22 status %d,message %s and payload %s\n",responseByPut2.Status,responseByPut2.Message,string(responseByPut2.Payload))
responseByget2 := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke22 status %d,message %s and payload %s",responseByget2.Status,responseByget2.Message,string(responseByget2.Payload))
}
func TestGXHSchema(t *testing.T){
input := `{"id":"1","aname":"alibusi","aname":"alibus","atype":"type","alinkman":"","aader":"aader",
"amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"",
"aader":"aader","amob":"1234567890"}}`
inputarrary := `[{"id":"1","aname":"alibusi","atype":"type","alinkman":"",
"aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890"}},{"id":"2","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type",
"alinkman":"","aader":"aader","amob":"1234567890"}}]`
update := `{"alinkman":"张三","id":"1","backup":{}}`
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
response:= stub.MockInit("init",nil)
fmt.Println("MockInit Payload: ",string(response.Payload))
fmt.Println()
fmt.Println("========================================invoke schema put============================================")
fmt.Println()
fmt.Println()
responseByPutschema := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("put"),[]byte("alibusi"),[]byte(input)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
responseByGet := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("get"),[]byte("alibusi")})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByGet.Status,responseByGet.Message,string(responseByGet.Payload))
fmt.Println()
fmt.Println("========================================invoke data put============================================")
fmt.Println()
fmt.Println()
responseByPut := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(inputarrary)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByPut.Status,responseByPut.Message,string(responseByPut.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget.Status,responseByget.Message,string(responseByget.Payload))
fmt.Println()
fmt.Println("========================================invoke data update============================================")
fmt.Println()
fmt.Println()
responseByUpdate := stub.MockInvoke("invoke1",[][]byte{[]byte("update"), []byte("alibusi"),[]byte(update)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByUpdate.Status,responseByUpdate.Message,string(responseByUpdate.Payload))
responseByget2 := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget2.Status,responseByget2.Message,string(responseByget2.Payload))
//inputfail := `{"id":"2","atype":"type","alinkman":"","aader":"aader","amob":"1234567890","backup":{"id":"1","aname":"alibusi","atype":"type","alinkman":"","aader":"aader","amob":"1234567890"}}`
//fmt.Println()
//fmt.Println("========================================fail input data============================================")
//
//
//responseByPutfail := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(inputfail)})
//fmt.Printf("Invoke status %d,message %s and payload %s\n",
// responseByPutfail.Status,responseByPutfail.Message,string(responseByPutfail.Payload))
//responseBygetfail := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
//fmt.Printf("Invoke status %d,message %s and payload %s \n",
// responseBygetfail.Status,responseBygetfail.Message,string(responseBygetfail.Payload))
}
/*
以下部分是增加权限内容的测试
*/
var (
authInput = `{"Parameters":{"alinkman":{"Value":"Alinkman","Read":["user"],"Write":["admin"]},"amob":{"Value":"Amob","Read":[],"Write":[]},"aname":
{"Value":"Aname","Read":["admin"],"Write":["admin"]},"atype":{"Value":"Atype","Read":["admin"],"Write":["admin"]},"backup":{"Value":{"aname":
{"Value":"Aname","Read":["admin"],"Write":["admin"]},"atype":{"Value":"Atype","Read":["admin"],"Write":["admin"]}},"Read":["admin"],"Write":["admin"]},
"id":{"Value":"1","Read":[],"Write":[]}},"Read":[],"Write":["user"]}
`
inputUpdate = `{"id":"1","aname":"alibusi","atype":"newtype","alinkman":"newalinkman",
"amob":"new1234567890","backup":{"aname":"alibusi","atype":"type"}}`
inputarrary = `[{"id":"1","aname":"alibusi","atype":"type","alinkman":"alinkman",
"amob":"1234567890","backup":{"aname":"alibusi","atype":"type"}},{"id":"2","aname":"alibusi","atype":"type","alinkman":"alinkman",
"amob":"1234567890","backup":{"aname":"alibusi","atype":"type"}}]`
)
func TestGXHSchemaAuth(t *testing.T){
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
stub.MockInit("init",nil)
fmt.Println()
fmt.Println("========================================invoke schema put============================================")
fmt.Println()
fmt.Println()
responseByPutschema := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("put"),[]byte("alibusi"),[]byte(authInput)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("get"),[]byte("alibusi")})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget.Status,responseByget.Message,string(responseByget.Payload))
}
func TestGXHDataPutAuth(t *testing.T){
cc := new(GXHCC)
stub := shim.NewMockStub("GXHCC",cc)
stub.MockInit("init",nil)
fmt.Println()
fmt.Println("========================================invoke schema put============================================")
fmt.Println()
fmt.Println()
responseByPutschema := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("put"),[]byte("alibusi"),[]byte(authInput)})
fmt.Printf("Invoke status %d,message %s and payload %s\n",
responseByPutschema.Status,responseByPutschema.Message,string(responseByPutschema.Payload))
responseByget := stub.MockInvoke("invoke1",[][]byte{[]byte("schema"), []byte("get"),[]byte("alibusi")})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget.Status,responseByget.Message,string(responseByget.Payload))
fmt.Println()
fmt.Println("========================================invoke data put============================================")
fmt.Println()
fmt.Println()
responseByPut := stub.MockInvoke("invoke1",[][]byte{[]byte("put"), []byte("alibusi"),[]byte(inputarrary)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByPut.Status,responseByPut.Message,string(responseByPut.Payload))
responseByget = stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget.Status,responseByget.Message,string(responseByget.Payload))
fmt.Println()
fmt.Println("========================================invoke data update============================================")
fmt.Println()
fmt.Println()
responseByUpdate := stub.MockInvoke("invoke1",[][]byte{[]byte("update"), []byte("alibusi"),[]byte(inputUpdate)})
fmt.Printf("Invoke status %d,message %s ,and payload %s\n",
responseByUpdate.Status,responseByUpdate.Message,string(responseByUpdate.Payload))
responseByget2 := stub.MockInvoke("invoke1",[][]byte{[]byte("get"), []byte("alibusi"),[]byte(`{"id":"1"}`)})
fmt.Printf("Invoke status %d,message %s ,and payload %s \n",
responseByget2.Status,responseByget2.Message,string(responseByget2.Payload))
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment