gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/config_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package command 8 9 import ( 10 "testing" 11 12 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib" 13 ) 14 15 func TestProcessAttributes(t *testing.T) { 16 // test cases 17 positiveAttrs := []string{ 18 "AttrList=peer,orderer,client,user", 19 "AttrListWithECertAttr=peer,orderer,client,user:ecert", 20 "AttrTrue=true", 21 "AttrTrueWithECertAttr=true:ecert", 22 "AttrFalse=false", 23 "AttrStar=*", 24 "AttrStarWithECertAttr=*:ecert", 25 } 26 negativeAttrs1 := []string{ 27 "AttrTrueWithInvalidAttr=true:invalid", 28 } 29 negativeAttrs2 := []string{ 30 "AttrTrueWithDuplicateAttrs=true:ecert:ecert", 31 } 32 33 clientCfg := lib.ClientConfig{} 34 35 err := processAttributes(positiveAttrs, &clientCfg) 36 if err != nil { 37 t.Error(err) 38 } 39 40 for _, attr := range clientCfg.ID.Attributes { 41 switch attr.Name { 42 case "AttrList": 43 if attr.Value != "peer,orderer,client,user" || attr.ECert != false { 44 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 45 attr.Name, attr.Value, attr.ECert) 46 } 47 case "AttrListWithECertAttr": 48 if attr.Value != "peer,orderer,client,user" || attr.ECert != true { 49 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 50 attr.Name, attr.Value, attr.ECert) 51 } 52 case "AttrTrue": 53 if attr.Value != "true" || attr.ECert != false { 54 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 55 attr.Name, attr.Value, attr.ECert) 56 } 57 case "AttrTrueWithECertAttr": 58 if attr.Value != "true" || attr.ECert != true { 59 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 60 attr.Name, attr.Value, attr.ECert) 61 } 62 case "AttrFalse": 63 if attr.Value != "false" || attr.ECert != false { 64 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 65 attr.Name, attr.Value, attr.ECert) 66 } 67 case "AttrStar": 68 if attr.Value != "*" || attr.ECert != false { 69 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 70 attr.Name, attr.Value, attr.ECert) 71 } 72 case "AttrStarWithECertAttr": 73 if attr.Value != "*" || attr.ECert != true { 74 t.Fatalf("Attr conversion of '%s' failed (value='%s', ecert='%v')", 75 attr.Name, attr.Value, attr.ECert) 76 } 77 default: 78 t.Fatal("Unknown test case") 79 } 80 } 81 82 err = processAttributes(negativeAttrs1, &clientCfg) 83 if err == nil { 84 t.Fatal("Negative test case 1 should have failed") 85 } 86 87 err = processAttributes(negativeAttrs2, &clientCfg) 88 if err == nil { 89 t.Fatal("Negative test case 2 should have failed") 90 } 91 }