github.com/binyushen/fabric@v2.1.1+incompatible/core/operations/operations_suite_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package operations_test
     8  
     9  import (
    10  	"crypto/tls"
    11  	"crypto/x509"
    12  	"io/ioutil"
    13  	"net/http"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/hyperledger/fabric-lib-go/healthz"
    18  	"github.com/hyperledger/fabric/common/crypto/tlsgen"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  func TestOperations(t *testing.T) {
    24  	RegisterFailHandler(Fail)
    25  	RunSpecs(t, "Operations Suite")
    26  }
    27  
    28  func generateCertificates(tempDir string) {
    29  	serverCA, err := tlsgen.NewCA()
    30  	Expect(err).NotTo(HaveOccurred())
    31  	err = ioutil.WriteFile(filepath.Join(tempDir, "server-ca.pem"), serverCA.CertBytes(), 0640)
    32  	Expect(err).NotTo(HaveOccurred())
    33  	serverKeyPair, err := serverCA.NewServerCertKeyPair("127.0.0.1")
    34  	Expect(err).NotTo(HaveOccurred())
    35  	err = ioutil.WriteFile(filepath.Join(tempDir, "server-cert.pem"), serverKeyPair.Cert, 0640)
    36  	Expect(err).NotTo(HaveOccurred())
    37  	err = ioutil.WriteFile(filepath.Join(tempDir, "server-key.pem"), serverKeyPair.Key, 0640)
    38  	Expect(err).NotTo(HaveOccurred())
    39  
    40  	clientCA, err := tlsgen.NewCA()
    41  	Expect(err).NotTo(HaveOccurred())
    42  	err = ioutil.WriteFile(filepath.Join(tempDir, "client-ca.pem"), clientCA.CertBytes(), 0640)
    43  	Expect(err).NotTo(HaveOccurred())
    44  	clientKeyPair, err := clientCA.NewClientCertKeyPair()
    45  	Expect(err).NotTo(HaveOccurred())
    46  	err = ioutil.WriteFile(filepath.Join(tempDir, "client-cert.pem"), clientKeyPair.Cert, 0640)
    47  	Expect(err).NotTo(HaveOccurred())
    48  	err = ioutil.WriteFile(filepath.Join(tempDir, "client-key.pem"), clientKeyPair.Key, 0640)
    49  	Expect(err).NotTo(HaveOccurred())
    50  }
    51  
    52  func newHTTPClient(tlsDir string, withClientCert bool) *http.Client {
    53  	clientCertPool := x509.NewCertPool()
    54  	caCert, err := ioutil.ReadFile(filepath.Join(tlsDir, "server-ca.pem"))
    55  	Expect(err).NotTo(HaveOccurred())
    56  	clientCertPool.AppendCertsFromPEM(caCert)
    57  
    58  	tlsClientConfig := &tls.Config{
    59  		RootCAs: clientCertPool,
    60  	}
    61  	if withClientCert {
    62  		clientCert, err := tls.LoadX509KeyPair(
    63  			filepath.Join(tlsDir, "client-cert.pem"),
    64  			filepath.Join(tlsDir, "client-key.pem"),
    65  		)
    66  		Expect(err).NotTo(HaveOccurred())
    67  		tlsClientConfig.Certificates = []tls.Certificate{clientCert}
    68  	}
    69  
    70  	return &http.Client{
    71  		Transport: &http.Transport{
    72  			TLSClientConfig: tlsClientConfig,
    73  		},
    74  	}
    75  }
    76  
    77  //go:generate counterfeiter -o fakes/healthchecker.go -fake-name HealthChecker . healthChecker
    78  type healthChecker interface {
    79  	healthz.HealthChecker
    80  }