github.com/extrame/fabric-ca@v2.0.0-alpha+incompatible/lib/tcert/tcert_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tcert 18 19 import ( 20 "testing" 21 22 "github.com/cloudflare/cfssl/log" 23 "github.com/hyperledger/fabric-ca/api" 24 "github.com/hyperledger/fabric-ca/util" 25 ) 26 27 func TestTCertWithoutAttribute(t *testing.T) { 28 29 log.Level = log.LevelDebug 30 31 // Get a manager 32 mgr := getMgr(t) 33 if mgr == nil { 34 return 35 } 36 37 ecert, err := LoadCert("/") 38 if err == nil { 39 t.Error("Should have failed") 40 } 41 42 ecert, err = LoadCert("../../testdata/ec.pem") 43 if err != nil { 44 t.Errorf("LoadCert unable to load ec.pem %v", err) 45 } 46 47 batchReq := &GetTCertBatchRequest{} 48 batchReq.Count = 1 49 batchReq.PreKey = "anyroot" 50 51 resp, err := mgr.GetBatch(batchReq, ecert) 52 if err != nil { 53 t.Errorf("Error from GetBatch: %s", err) 54 return 55 } 56 if len(resp.TCerts) != 1 { 57 t.Errorf("Returned incorrect number of TCerts: expecting 1 but found %d", len(resp.TCerts)) 58 } 59 60 } 61 62 func TestTCertWitAttributes(t *testing.T) { 63 64 log.Level = log.LevelDebug 65 66 // Get a manager 67 mgr := getMgr(t) 68 if mgr == nil { 69 return 70 } 71 72 ecert, err := LoadCert("../../testdata/ec.pem") 73 if err != nil { 74 return 75 } 76 var Attrs = []api.Attribute{ 77 { 78 Name: "SSN", 79 Value: "123-456-789", 80 }, 81 82 { 83 Name: "Income", 84 Value: "USD", 85 }, 86 } 87 batchReq := &GetTCertBatchRequest{} 88 batchReq.Count = 2 89 batchReq.EncryptAttrs = true 90 batchReq.Attrs = Attrs 91 batchReq.PreKey = "anotherprekey" 92 resp, err := mgr.GetBatch(batchReq, ecert) 93 if err != nil { 94 t.Errorf("Error from GetBatch: %s", err) 95 return 96 } 97 if len(resp.TCerts) != 2 { 98 t.Errorf("Returned incorrect number of certs: expecting 2 but found %d", len(resp.TCerts)) 99 } 100 101 } 102 103 func getMgr(t *testing.T) *Mgr { 104 keyFile := "../../testdata/ec-key.pem" 105 certFile := "../../testdata/ec.pem" 106 mgr, err := LoadMgr(keyFile, certFile, util.GetDefaultBCCSP()) 107 if err != nil { 108 t.Errorf("failed loading mgr: %s", err) 109 return nil 110 } 111 return mgr 112 }