github.com/zorawar87/trillian@v1.2.1/client/rpcflags/rpcflags_test.go (about) 1 // Copyright 2018 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rpcflags 16 17 import ( 18 "context" 19 "encoding/pem" 20 "flag" 21 "os" 22 "testing" 23 24 "github.com/google/trillian" 25 "github.com/google/trillian/testonly/integration" 26 "github.com/google/trillian/testonly/setup" 27 "github.com/google/trillian/util/flagsaver" 28 "google.golang.org/grpc" 29 "google.golang.org/grpc/credentials" 30 ) 31 32 func TestNewClientDialOptionsFromFlagsWithTLSCertFileNotSet(t *testing.T) { 33 // Set up Trillian servers 34 const numSequencers = 2 35 serverOpts := []grpc.ServerOption{} 36 clientOpts := []grpc.DialOption{grpc.WithInsecure()} 37 logEnv, err := integration.NewLogEnvWithGRPCOptions(context.Background(), numSequencers, serverOpts, clientOpts) 38 if err != nil { 39 t.Fatal(err) 40 } 41 defer logEnv.Close() 42 43 dialOpts, err := NewClientDialOptionsFromFlags() 44 if err != nil { 45 t.Errorf("Got an unexpected error: %v", err) 46 } 47 48 // Check that the returned dial options can be used to connect and make 49 // requests against the Trillian services created above. 50 conn, err := grpc.Dial(logEnv.Address, dialOpts...) 51 if err != nil { 52 t.Errorf("failed to dial %v: %v", logEnv.Address, err) 53 } 54 defer conn.Close() 55 56 adminClient := trillian.NewTrillianAdminClient(conn) 57 if _, err = adminClient.ListTrees(context.Background(), &trillian.ListTreesRequest{}); err != nil { 58 t.Errorf("failed to request trees from the Admin Server: %v", err) 59 } 60 } 61 62 func TestNewClientDialOptionsFromFlagsWithTLSCertFileMissing(t *testing.T) { 63 defer flagsaver.Save().Restore() 64 if err := flag.Set("tls_cert_file", "/a/missing/file"); err != nil { 65 t.Errorf("Failed to set flag: %v", err) 66 } 67 68 dialOpts, err := NewClientDialOptionsFromFlags() 69 if err == nil { 70 t.Errorf("Expected to get an error due to the file not being found") 71 } 72 73 if _, ok := err.(*os.PathError); !ok { 74 t.Errorf("Expected to get an os.PathError due to the file not being found, instead got: %v", err) 75 } 76 77 if dialOpts != nil { 78 t.Errorf("Expected returned dialOpts to be nil, instead got: %v", dialOpts) 79 } 80 } 81 82 func TestNewClientDialOptionsFromFlagsWithTLSCertFileSet(t *testing.T) { 83 // Create new TLS certificates for the test services, and write the Client 84 // certificate to a file (so we can refer to it using the flag). 85 crtFile, cleanupCrtFile := setup.TempFile(t, "test.crt.") 86 defer cleanupCrtFile() 87 88 tlsCert := setup.NewTLSCertificate(t) 89 90 // Certificate file and client dial options. 91 err := pem.Encode(crtFile, &pem.Block{Type: "CERTIFICATE", Bytes: tlsCert.Certificate[0]}) 92 if err != nil { 93 t.Fatalf("Failed to encode the test TLS certificate %v", err) 94 } 95 clientCreds, err := credentials.NewClientTLSFromFile(crtFile.Name(), "") 96 if err != nil { 97 t.Fatalf("Failed to get credentials: %v", err) 98 } 99 100 // Set up Trillian servers (with TLS enabled) 101 const numSequencers = 0 // we don't actually need any sequencers. 102 serverCreds := credentials.NewServerTLSFromCert(&tlsCert) 103 serverOpts := []grpc.ServerOption{grpc.Creds(serverCreds)} 104 clientOpts := []grpc.DialOption{grpc.WithTransportCredentials(clientCreds)} 105 logEnv, err := integration.NewLogEnvWithGRPCOptions(context.Background(), numSequencers, serverOpts, clientOpts) 106 if err != nil { 107 t.Fatal(err) 108 } 109 defer logEnv.Close() 110 111 // Set up the flag. 112 defer flagsaver.Save().Restore() 113 err = flag.Set("tls_cert_file", crtFile.Name()) 114 if err != nil { 115 t.Errorf("Failed to set -tls_cert_file flag: %v", err) 116 } 117 118 dialOpts, err := NewClientDialOptionsFromFlags() 119 if err != nil { 120 t.Errorf("Got an unexpected error: %v", err) 121 } 122 123 // Check that the returned dial options can be used to connect and make 124 // requests against the Trillian services created above. 125 conn, err := grpc.Dial(logEnv.Address, dialOpts...) 126 if err != nil { 127 t.Errorf("failed to dial %v: %v", logEnv.Address, err) 128 } 129 defer conn.Close() 130 131 adminClient := trillian.NewTrillianAdminClient(conn) 132 if _, err := adminClient.ListTrees(context.Background(), &trillian.ListTreesRequest{}); err != nil { 133 t.Errorf("failed to request trees from the Admin Server: %v", err) 134 } 135 }