github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/grpc_test/grpc_test.go (about) 1 // Copyright (c) 2022 zhaochun 2 // gmgo is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 /* 10 grpc_test 是对`gitee.com/zhaochuninhefei/gmgo/grpc`的测试包 11 */ 12 13 package grpc_test 14 15 import ( 16 "fmt" 17 "io/ioutil" 18 "log" 19 "net" 20 "testing" 21 "time" 22 23 "gitee.com/zhaochuninhefei/zcgolog/zclog" 24 "github.com/hxx258456/ccgo/gmtls" 25 "github.com/hxx258456/ccgo/grpc" 26 "github.com/hxx258456/ccgo/grpc/credentials" 27 "github.com/hxx258456/ccgo/grpc/grpc_test/echo" 28 "github.com/hxx258456/ccgo/net/context" 29 "github.com/hxx258456/ccgo/x509" 30 ) 31 32 const ( 33 port = ":50051" 34 address = "localhost:50051" 35 ca = "testdata/ca.cert" 36 signCert = "testdata/sign.cert" 37 signKey = "testdata/sign.key" 38 userCert = "testdata/user.cert" 39 userKey = "testdata/user.key" 40 ) 41 42 var end chan bool 43 44 func Test_credentials(t *testing.T) { 45 zcgologConfig := &zclog.Config{ 46 LogLevelGlobal: zclog.LOG_LEVEL_DEBUG, 47 } 48 zclog.InitLogger(zcgologConfig) 49 end = make(chan bool, 64) 50 go serverRun() 51 time.Sleep(1000000) 52 go clientRun() 53 <-end 54 } 55 56 func serverRun() { 57 signCert, err := gmtls.LoadX509KeyPair(signCert, signKey) 58 if err != nil { 59 log.Fatal(err) 60 } 61 62 certPool := x509.NewCertPool() 63 cacert, err := ioutil.ReadFile(ca) 64 if err != nil { 65 log.Fatal(err) 66 } 67 certPool.AppendCertsFromPEM(cacert) 68 lis, err := net.Listen("tcp", port) 69 if err != nil { 70 log.Fatalf("fail to listen: %v", err) 71 } 72 creds := credentials.NewTLS(&gmtls.Config{ 73 ClientAuth: gmtls.RequireAndVerifyClientCert, 74 Certificates: []gmtls.Certificate{signCert}, 75 ClientCAs: certPool, 76 }) 77 s := grpc.NewServer(grpc.Creds(creds)) 78 echo.RegisterEchoServer(s, &server{}) 79 err = s.Serve(lis) 80 if err != nil { 81 log.Fatalf("Serve: %v", err) 82 } 83 } 84 85 func clientRun() { 86 cert, err := gmtls.LoadX509KeyPair(userCert, userKey) 87 if err != nil { 88 log.Fatal(err) 89 } 90 certPool := x509.NewCertPool() 91 cacert, err := ioutil.ReadFile(ca) 92 if err != nil { 93 log.Fatal(err) 94 } 95 certPool.AppendCertsFromPEM(cacert) 96 creds := credentials.NewTLS(&gmtls.Config{ 97 ServerName: "server.test.com", 98 Certificates: []gmtls.Certificate{cert}, 99 RootCAs: certPool, 100 ClientAuth: gmtls.RequireAndVerifyClientCert, 101 }) 102 conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds)) 103 if err != nil { 104 log.Fatalf("cannot to connect: %v", err) 105 } 106 defer func(conn *grpc.ClientConn) { 107 _ = conn.Close() 108 }(conn) 109 c := echo.NewEchoClient(conn) 110 echoInClient(c) 111 end <- true 112 } 113 114 // 客户端echo处理 115 func echoInClient(c echo.EchoClient) { 116 msgClient := "hello, this is client." 117 fmt.Printf("客户端发出消息: %s\n", msgClient) 118 r, err := c.Echo(context.Background(), &echo.EchoRequest{Req: msgClient}) 119 if err != nil { 120 log.Fatalf("failed to echo: %v", err) 121 } 122 msgServer := r.Result 123 fmt.Printf("客户端收到消息: %s\n", msgServer) 124 } 125 126 type server struct{} 127 128 // Echo 服务端echo处理 129 // 130 //goland:noinspection GoUnusedParameter 131 func (s *server) Echo(ctx context.Context, req *echo.EchoRequest) (*echo.EchoResponse, error) { 132 msgClient := req.Req 133 fmt.Printf("服务端接收到消息: %s\n", msgClient) 134 msgServer := "hello,this is server." 135 fmt.Printf("服务端返回消息: %s\n", msgServer) 136 return &echo.EchoResponse{Result: msgServer}, nil 137 }