|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 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 peer_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "crypto/x509" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "io/ioutil" |
| 25 | + "path/filepath" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "google.golang.org/grpc/credentials" |
| 30 | + |
| 31 | + "golang.org/x/net/context" |
| 32 | + "google.golang.org/grpc" |
| 33 | + |
| 34 | + "github.com/hyperledger/fabric/core/comm" |
| 35 | + testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc" |
| 36 | + "github.com/hyperledger/fabric/core/peer" |
| 37 | + "github.com/stretchr/testify/assert" |
| 38 | +) |
| 39 | + |
| 40 | +// default timeout for grpc connections |
| 41 | +var timeout = time.Second * 1 |
| 42 | + |
| 43 | +// test server to be registered with the GRPCServer |
| 44 | +type testServiceServer struct{} |
| 45 | + |
| 46 | +func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
| 47 | + return new(testpb.Empty), nil |
| 48 | +} |
| 49 | + |
| 50 | +// createCertPool creates an x509.CertPool from an array of PEM-encoded certificates |
| 51 | +func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) { |
| 52 | + |
| 53 | + certPool := x509.NewCertPool() |
| 54 | + for _, rootCA := range rootCAs { |
| 55 | + if !certPool.AppendCertsFromPEM(rootCA) { |
| 56 | + return nil, errors.New("Failed to load root certificates") |
| 57 | + } |
| 58 | + } |
| 59 | + return certPool, nil |
| 60 | +} |
| 61 | + |
| 62 | +// helper function to invoke the EmptyCall againt the test service |
| 63 | +func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) { |
| 64 | + |
| 65 | + //add DialOptions |
| 66 | + dialOptions = append(dialOptions, grpc.WithBlock()) |
| 67 | + dialOptions = append(dialOptions, grpc.WithTimeout(timeout)) |
| 68 | + //create GRPC client conn |
| 69 | + clientConn, err := grpc.Dial(address, dialOptions...) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + defer clientConn.Close() |
| 74 | + |
| 75 | + //create GRPC client |
| 76 | + client := testpb.NewTestServiceClient(clientConn) |
| 77 | + |
| 78 | + ctx := context.Background() |
| 79 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 80 | + defer cancel() |
| 81 | + |
| 82 | + //invoke service |
| 83 | + empty, err := client.EmptyCall(ctx, new(testpb.Empty)) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + return empty, nil |
| 89 | +} |
| 90 | + |
| 91 | +func TestCreatePeerServer(t *testing.T) { |
| 92 | + |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + // load test certs from testdata |
| 96 | + org1CA, err := ioutil.ReadFile(filepath.Join("testdata", "Org1-cert.pem")) |
| 97 | + org1Server1Key, err := ioutil.ReadFile(filepath.Join("testdata", "Org1-server1-key.pem")) |
| 98 | + org1Server1Cert, err := ioutil.ReadFile(filepath.Join("testdata", "Org1-server1-cert.pem")) |
| 99 | + org2CA, err := ioutil.ReadFile(filepath.Join("testdata", "Org2-cert.pem")) |
| 100 | + org2Server1Key, err := ioutil.ReadFile(filepath.Join("testdata", "Org2-server1-key.pem")) |
| 101 | + org2Server1Cert, err := ioutil.ReadFile(filepath.Join("testdata", "Org2-server1-cert.pem")) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to load test certificates: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + org1CertPool, err := createCertPool([][]byte{org1CA}) |
| 108 | + org2CertPool, err := createCertPool([][]byte{org2CA}) |
| 109 | + |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("Failed to load root certificates into pool: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + org1Creds := credentials.NewClientTLSFromCert(org1CertPool, "") |
| 115 | + org2Creds := credentials.NewClientTLSFromCert(org2CertPool, "") |
| 116 | + |
| 117 | + // use server cert as client cert |
| 118 | + org2ClientCert, err := tls.X509KeyPair(org2Server1Cert, org2Server1Key) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to load client certificate: %v", err) |
| 121 | + } |
| 122 | + org1Org2Creds := credentials.NewTLS(&tls.Config{ |
| 123 | + Certificates: []tls.Certificate{org2ClientCert}, |
| 124 | + RootCAs: org1CertPool, |
| 125 | + }) |
| 126 | + |
| 127 | + // basic function tests |
| 128 | + var tests = []struct { |
| 129 | + name string |
| 130 | + listenAddress string |
| 131 | + secureConfig comm.SecureServerConfig |
| 132 | + expectError bool |
| 133 | + goodOptions []grpc.DialOption |
| 134 | + badOptions []grpc.DialOption |
| 135 | + }{ |
| 136 | + { |
| 137 | + name: "NoTLS", |
| 138 | + listenAddress: fmt.Sprintf("localhost:%d", 4050), |
| 139 | + secureConfig: comm.SecureServerConfig{ |
| 140 | + UseTLS: false, |
| 141 | + }, |
| 142 | + expectError: false, |
| 143 | + goodOptions: []grpc.DialOption{grpc.WithInsecure()}, |
| 144 | + badOptions: []grpc.DialOption{grpc.WithTransportCredentials(org1Creds)}, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "BadAddress", |
| 148 | + listenAddress: "badaddress", |
| 149 | + secureConfig: comm.SecureServerConfig{ |
| 150 | + UseTLS: false, |
| 151 | + }, |
| 152 | + expectError: true, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "ServerTLSOrg1", |
| 156 | + listenAddress: fmt.Sprintf("localhost:%d", 4051), |
| 157 | + secureConfig: comm.SecureServerConfig{ |
| 158 | + UseTLS: true, |
| 159 | + ServerCertificate: org1Server1Cert, |
| 160 | + ServerKey: org1Server1Key, |
| 161 | + ServerRootCAs: [][]byte{org1CA}, |
| 162 | + }, |
| 163 | + expectError: false, |
| 164 | + goodOptions: []grpc.DialOption{grpc.WithTransportCredentials(org1Creds)}, |
| 165 | + badOptions: []grpc.DialOption{grpc.WithTransportCredentials(org2Creds)}, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "MutualTLSOrg1Org2", |
| 169 | + listenAddress: fmt.Sprintf("localhost:%d", 4052), |
| 170 | + secureConfig: comm.SecureServerConfig{ |
| 171 | + UseTLS: true, |
| 172 | + ServerCertificate: org1Server1Cert, |
| 173 | + ServerKey: org1Server1Key, |
| 174 | + ServerRootCAs: [][]byte{org1CA}, |
| 175 | + ClientRootCAs: [][]byte{org1CA, org2CA}, |
| 176 | + RequireClientCert: true, |
| 177 | + }, |
| 178 | + expectError: false, |
| 179 | + goodOptions: []grpc.DialOption{grpc.WithTransportCredentials(org1Org2Creds)}, |
| 180 | + badOptions: []grpc.DialOption{grpc.WithTransportCredentials(org1Creds)}, |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + for _, test := range tests { |
| 185 | + test := test |
| 186 | + t.Run(test.name, func(t *testing.T) { |
| 187 | + t.Parallel() |
| 188 | + t.Logf("Running test %s ...", test.name) |
| 189 | + |
| 190 | + _, err := peer.CreatePeerServer(test.listenAddress, test.secureConfig) |
| 191 | + // check to see whether to not we expect an error |
| 192 | + // we don't check the exact error because the comm package covers these cases |
| 193 | + if test.expectError { |
| 194 | + assert.Error(t, err, "CreatePeerServer should have returned an error") |
| 195 | + } else { |
| 196 | + assert.NoError(t, err, "CreatePeerServer should not have returned an error") |
| 197 | + // get the server from peer |
| 198 | + peerServer := peer.GetPeerServer() |
| 199 | + assert.NotNil(t, peerServer, "GetPeerServer should not return a nil value") |
| 200 | + // register a GRPC test service |
| 201 | + testpb.RegisterTestServiceServer(peerServer.Server(), &testServiceServer{}) |
| 202 | + go peerServer.Start() |
| 203 | + defer peerServer.Stop() |
| 204 | + |
| 205 | + //invoke the EmptyCall service with good options |
| 206 | + _, err = invokeEmptyCall(test.listenAddress, test.goodOptions) |
| 207 | + assert.NoError(t, err, "Failed to invoke the EmptyCall service") |
| 208 | + //invoke the EmptyCall service with bad options |
| 209 | + _, err = invokeEmptyCall(test.listenAddress, test.badOptions) |
| 210 | + assert.Error(t, err, "Expected error using bad dial options") |
| 211 | + |
| 212 | + } |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments