github.com/hyperledger-gerrit-archive/fabric-ca@v2.0.0-alpha.0.20190916143245-4cd4192f0366+incompatible/lib/attrmgr/attrmgr_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package attrmgr_test 8 9 import ( 10 "crypto/x509" 11 "testing" 12 13 "github.com/hyperledger/fabric-ca/lib/attrmgr" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 // TestAttrs tests attributes 18 func TestAttrs(t *testing.T) { 19 mgr := attrmgr.New() 20 attrs := []attrmgr.Attribute{ 21 &Attribute{Name: "attr1", Value: "val1"}, 22 &Attribute{Name: "attr2", Value: "val2"}, 23 &Attribute{Name: "attr3", Value: "val3"}, 24 &Attribute{Name: "boolAttr", Value: "true"}, 25 } 26 reqs := []attrmgr.AttributeRequest{ 27 &AttributeRequest{Name: "attr1", Require: false}, 28 &AttributeRequest{Name: "attr2", Require: true}, 29 &AttributeRequest{Name: "boolAttr", Require: true}, 30 &AttributeRequest{Name: "noattr1", Require: false}, 31 } 32 cert := &x509.Certificate{} 33 34 // Verify that the certificate has no attributes 35 at, err := mgr.GetAttributesFromCert(cert) 36 if err != nil { 37 t.Fatalf("Failed to GetAttributesFromCert: %s", err) 38 } 39 numAttrs := len(at.Names()) 40 assert.True(t, numAttrs == 0, "expecting 0 attributes but found %d", numAttrs) 41 42 // Add attributes to certificate 43 err = mgr.ProcessAttributeRequestsForCert(reqs, attrs, cert) 44 if err != nil { 45 t.Fatalf("Failed to ProcessAttributeRequestsForCert: %s", err) 46 } 47 48 // Get attributes from the certificate and verify the count is correct 49 at, err = mgr.GetAttributesFromCert(cert) 50 if err != nil { 51 t.Fatalf("Failed to GetAttributesFromCert: %s", err) 52 } 53 numAttrs = len(at.Names()) 54 assert.True(t, numAttrs == 3, "expecting 3 attributes but found %d", numAttrs) 55 56 // Check individual attributes 57 checkAttr(t, "attr1", "val1", at) 58 checkAttr(t, "attr2", "val2", at) 59 checkAttr(t, "attr3", "", at) 60 checkAttr(t, "noattr1", "", at) 61 assert.NoError(t, at.True("boolAttr")) 62 63 // Negative test case: add required attributes which don't exist 64 reqs = []attrmgr.AttributeRequest{ 65 &AttributeRequest{Name: "noattr1", Require: true}, 66 } 67 err = mgr.ProcessAttributeRequestsForCert(reqs, attrs, cert) 68 assert.Error(t, err) 69 } 70 71 func checkAttr(t *testing.T, name, val string, attrs *attrmgr.Attributes) { 72 v, ok, err := attrs.Value(name) 73 assert.NoError(t, err) 74 if val == "" { 75 assert.False(t, attrs.Contains(name), "contains attribute '%s'", name) 76 assert.False(t, ok, "attribute '%s' was found", name) 77 } else { 78 assert.True(t, attrs.Contains(name), "does not contain attribute '%s'", name) 79 assert.True(t, ok, "attribute '%s' was not found", name) 80 assert.True(t, v == val, "incorrect value for '%s'; expected '%s' but found '%s'", name, val, v) 81 } 82 } 83 84 type Attribute struct { 85 Name, Value string 86 } 87 88 func (a *Attribute) GetName() string { 89 return a.Name 90 } 91 92 func (a *Attribute) GetValue() string { 93 return a.Value 94 } 95 96 type AttributeRequest struct { 97 Name string 98 Require bool 99 } 100 101 func (ar *AttributeRequest) GetName() string { 102 return ar.Name 103 } 104 105 func (ar *AttributeRequest) IsRequired() bool { 106 return ar.Require 107 }