github.com/hyperledger-labs/bdls@v2.1.1+incompatible/core/chaincode/extcc/extcc_handler_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package extcc_test
     8  
     9  import (
    10  	"net"
    11  	"time"
    12  
    13  	"github.com/hyperledger/fabric/core/chaincode/extcc"
    14  	"github.com/hyperledger/fabric/core/chaincode/extcc/mock"
    15  	"github.com/hyperledger/fabric/core/container/ccintf"
    16  	"github.com/hyperledger/fabric/internal/pkg/comm"
    17  
    18  	"google.golang.org/grpc"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("Extcc", func() {
    25  	var (
    26  		i        *extcc.ExternalChaincodeRuntime
    27  		shandler *mock.StreamHandler
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		shandler = &mock.StreamHandler{}
    32  		i = &extcc.ExternalChaincodeRuntime{}
    33  	})
    34  
    35  	Context("Run", func() {
    36  		When("chaincode is running", func() {
    37  			var (
    38  				cclist net.Listener
    39  				ccserv *grpc.Server
    40  			)
    41  			BeforeEach(func() {
    42  				var err error
    43  				cclist, err = net.Listen("tcp", "127.0.0.1:0")
    44  				Expect(err).To(BeNil())
    45  				Expect(cclist).To(Not(BeNil()))
    46  				ccserv = grpc.NewServer([]grpc.ServerOption{}...)
    47  				go ccserv.Serve(cclist)
    48  			})
    49  
    50  			AfterEach(func() {
    51  				if ccserv != nil {
    52  					ccserv.Stop()
    53  				}
    54  				if cclist != nil {
    55  					cclist.Close()
    56  				}
    57  			})
    58  
    59  			It("runs to completion", func() {
    60  				ccinfo := &ccintf.ChaincodeServerInfo{
    61  					Address: cclist.Addr().String(),
    62  					ClientConfig: comm.ClientConfig{
    63  						KaOpts:  comm.DefaultKeepaliveOptions,
    64  						Timeout: 10 * time.Second,
    65  					},
    66  				}
    67  				err := i.Stream("ccid", ccinfo, shandler)
    68  				Expect(err).To(BeNil())
    69  				Expect(shandler.HandleChaincodeStreamCallCount()).To(Equal(1))
    70  
    71  				streamArg := shandler.HandleChaincodeStreamArgsForCall(0)
    72  				Expect(streamArg).To(Not(BeNil()))
    73  			})
    74  		})
    75  		Context("chaincode info incorrect", func() {
    76  			var (
    77  				ccinfo *ccintf.ChaincodeServerInfo
    78  			)
    79  			BeforeEach(func() {
    80  				ccinfo = &ccintf.ChaincodeServerInfo{
    81  					Address: "ccaddress:12345",
    82  					ClientConfig: comm.ClientConfig{
    83  						SecOpts: comm.SecureOptions{
    84  							UseTLS:            true,
    85  							RequireClientCert: true,
    86  							Certificate:       []byte("fake-cert"),
    87  							Key:               []byte("fake-key"),
    88  							ServerRootCAs:     [][]byte{[]byte("fake-root-cert")},
    89  						},
    90  						Timeout: 10 * time.Second,
    91  					},
    92  				}
    93  			})
    94  			When("address is bad", func() {
    95  				BeforeEach(func() {
    96  					ccinfo.ClientConfig.SecOpts.UseTLS = false
    97  					ccinfo.Address = "<badaddress>"
    98  				})
    99  				It("returns an error", func() {
   100  					err := i.Stream("ccid", ccinfo, shandler)
   101  					Expect(err).To(MatchError(ContainSubstring("error creating grpc connection to <badaddress>")))
   102  				})
   103  			})
   104  			When("unspecified client spec", func() {
   105  				BeforeEach(func() {
   106  					ccinfo.ClientConfig.SecOpts.Key = nil
   107  				})
   108  				It("returns an error", func() {
   109  					err := i.Stream("ccid", ccinfo, shandler)
   110  					Expect(err).To(MatchError(ContainSubstring("both Key and Certificate are required when using mutual TLS")))
   111  				})
   112  			})
   113  		})
   114  	})
   115  })