github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/common/policydsl/policy_test.go (about) 1 package policydsl 2 3 import ( 4 "github.com/spf13/viper" 5 "testing" 6 ) 7 8 func TestPolicyByJavaYaml(t *testing.T) { 9 a := "1-of" 10 t.Log(noofPattern.FindStringSubmatch(a)) 11 newViper := viper.New() 12 newViper.AddConfigPath("test") 13 newViper.SetConfigName("endorserpolicy") 14 err := newViper.ReadInConfig() 15 t.Log(*newViper) 16 if err != nil { 17 t.Error(err) 18 return 19 } 20 t.Log("policy", newViper.GetStringMap("policy"), "\nidentities", newViper.GetStringMap("identities")) 21 identities, _, err := parseIdentitiesForJava(newViper.GetStringMap("identities")) 22 if err != nil { 23 t.Error(err) 24 return 25 } 26 27 t.Log("identities:", identities) 28 29 signaturePolicy, err := parsePolicyForJava(identities, newViper.GetStringMap("policy")) 30 if err != nil { 31 t.Error(err) 32 return 33 } 34 35 t.Log("policy:", signaturePolicy) 36 } 37 38 const endorserPolicy = `{"identities":{"org1":{"role":{"name":"member","mspId":"org1MSP"}},"org2":{"role":{"name":"member","mspId":"org2MSP"}}},"policy":{"1-of":[{"signed-by":"org1"},{"signed-by":"org2"}]}}` 39 40 func TestPolicyByJava(t *testing.T) { 41 for i := 0; i < 20; i++ { 42 policyParseJava, err := PolicyParseJava(endorserPolicy) 43 if err != nil { 44 t.Error(err) 45 return 46 } 47 t.Log("endorser policy:", policyParseJava) 48 } 49 50 } 51 52 const endorserPolicyGo = `OR('org1MSP.member','org2MSP.member')` 53 54 func TestPolicyByGo(t *testing.T) { 55 policyEnvelope, err := FromString(endorserPolicyGo) 56 if err != nil { 57 t.Error(err) 58 return 59 } 60 t.Log("endorser policy:", policyEnvelope) 61 } 62 63 const endorserPolicyNode = `{"identities":[{"role":{"name":"member","mspId":"peerOrg1"}},{"role":{"name":"member","mspId":"peerOrg2"}}],"policy":{"2-of":[{"signed-by":0},{"1-of":[{"signed-by":0},{"signed-by":1}]}]}}` 64 65 func TestPolicyByNode(t *testing.T) { 66 for i := 0; i < 20; i++ { 67 policyParseNode, err := PolicyParseNode(endorserPolicyNode) 68 if err != nil { 69 t.Error(err) 70 return 71 } 72 t.Log("endorser policy:", policyParseNode) 73 } 74 } 75 76 func TestSortMap(t *testing.T) { 77 sorted := map[string]interface{}{ 78 "org2": 1, 79 "org1": 2, 80 "org4": 1, 81 "org3": 2, 82 } 83 84 t.Log(sortMapKeys(sorted)) 85 86 }