github.com/Hyperledger-TWGC/tjfoc-gm@v1.4.0/gmtls/gmcredentials/credentials_test.go (about)

     1  package gmcredentials
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"net"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/Hyperledger-TWGC/tjfoc-gm/gmtls"
    12  	"github.com/Hyperledger-TWGC/tjfoc-gm/gmtls/gmcredentials/echo"
    13  	"github.com/Hyperledger-TWGC/tjfoc-gm/x509"
    14  	"golang.org/x/net/context"
    15  	"google.golang.org/grpc"
    16  )
    17  
    18  const (
    19  	port    = ":50051"
    20  	address = "localhost:50051"
    21  )
    22  
    23  var end chan bool
    24  
    25  type server struct{}
    26  
    27  func (s *server) Echo(ctx context.Context, req *echo.EchoRequest) (*echo.EchoResponse, error) {
    28  	return &echo.EchoResponse{Result: req.Req}, nil
    29  }
    30  
    31  const ca = "testdata/ca.cert"
    32  const signCert = "testdata/sign.cert"
    33  const signKey = "testdata/sign.key"
    34  const encryptCert = "testdata/encrypt.cert"
    35  const encryptKey = "testdata/encrypt.key"
    36  
    37  const userCert = "testdata/user.cert"
    38  const userKey = "testdata/user.key"
    39  
    40  func serverRun() {
    41  	signCert, err := gmtls.LoadX509KeyPair(signCert, signKey)
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	encryptCert, err := gmtls.LoadX509KeyPair(encryptCert, encryptKey)
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	certPool := x509.NewCertPool()
    51  	cacert, err := ioutil.ReadFile(ca)
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  	certPool.AppendCertsFromPEM(cacert)
    56  	lis, err := net.Listen("tcp", port)
    57  	if err != nil {
    58  		log.Fatalf("fail to listen: %v", err)
    59  	}
    60  	creds := NewTLS(&gmtls.Config{
    61  		GMSupport:    &gmtls.GMSupport{},
    62  		ClientAuth:   gmtls.RequireAndVerifyClientCert,
    63  		Certificates: []gmtls.Certificate{signCert, encryptCert},
    64  		ClientCAs:    certPool,
    65  	})
    66  	s := grpc.NewServer(grpc.Creds(creds))
    67  	echo.RegisterEchoServer(s, &server{})
    68  	err = s.Serve(lis)
    69  	if err != nil {
    70  		log.Fatalf("Serve: %v", err)
    71  	}
    72  }
    73  
    74  func clientRun() {
    75  	cert, err := gmtls.LoadX509KeyPair(userCert, userKey)
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	certPool := x509.NewCertPool()
    80  	cacert, err := ioutil.ReadFile(ca)
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	certPool.AppendCertsFromPEM(cacert)
    85  	creds := NewTLS(&gmtls.Config{
    86  		GMSupport:    &gmtls.GMSupport{},
    87  		ServerName:   "test.example.com",
    88  		Certificates: []gmtls.Certificate{cert},
    89  		RootCAs:      certPool,
    90  		ClientAuth:   gmtls.RequireAndVerifyClientCert,
    91  	})
    92  	conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds))
    93  	if err != nil {
    94  		log.Fatalf("cannot to connect: %v", err)
    95  	}
    96  	defer conn.Close()
    97  	c := echo.NewEchoClient(conn)
    98  	echoTest(c)
    99  	end <- true
   100  }
   101  
   102  func echoTest(c echo.EchoClient) {
   103  	r, err := c.Echo(context.Background(), &echo.EchoRequest{Req: "hello"})
   104  	if err != nil {
   105  		log.Fatalf("failed to echo: %v", err)
   106  	}
   107  	fmt.Printf("%s\n", r.Result)
   108  }
   109  
   110  func Test(t *testing.T) {
   111  	end = make(chan bool, 64)
   112  	go serverRun()
   113  	time.Sleep(1000000)
   114  	go clientRun()
   115  	<-end
   116  }