github.com/keleustes/helm@v3.0.0-beta.3+incompatible/internal/tlsutil/tlsutil_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 tlsutil 18 19 import ( 20 "crypto/tls" 21 "path/filepath" 22 "testing" 23 ) 24 25 const tlsTestDir = "../../testdata" 26 27 const ( 28 testCaCertFile = "ca.pem" 29 testCertFile = "crt.pem" 30 testKeyFile = "key.pem" 31 ) 32 33 func TestClientConfig(t *testing.T) { 34 opts := Options{ 35 CaCertFile: testfile(t, testCaCertFile), 36 CertFile: testfile(t, testCertFile), 37 KeyFile: testfile(t, testKeyFile), 38 InsecureSkipVerify: false, 39 } 40 41 cfg, err := ClientConfig(opts) 42 if err != nil { 43 t.Fatalf("error building tls client config: %v", err) 44 } 45 46 if got := len(cfg.Certificates); got != 1 { 47 t.Fatalf("expecting 1 client certificates, got %d", got) 48 } 49 if cfg.InsecureSkipVerify { 50 t.Fatalf("insecure skip verify mistmatch, expecting false") 51 } 52 if cfg.RootCAs == nil { 53 t.Fatalf("mismatch tls RootCAs, expecting non-nil") 54 } 55 } 56 57 func TestServerConfig(t *testing.T) { 58 opts := Options{ 59 CaCertFile: testfile(t, testCaCertFile), 60 CertFile: testfile(t, testCertFile), 61 KeyFile: testfile(t, testKeyFile), 62 ClientAuth: tls.RequireAndVerifyClientCert, 63 } 64 65 cfg, err := ServerConfig(opts) 66 if err != nil { 67 t.Fatalf("error building tls server config: %v", err) 68 } 69 if got := cfg.MinVersion; got != tls.VersionTLS12 { 70 t.Errorf("expecting TLS version 1.2, got %d", got) 71 } 72 if got := cfg.ClientCAs; got == nil { 73 t.Errorf("expecting non-nil CA pool") 74 } 75 } 76 77 func testfile(t *testing.T, file string) (path string) { 78 var err error 79 if path, err = filepath.Abs(filepath.Join(tlsTestDir, file)); err != nil { 80 t.Fatalf("error getting absolute path to test file %q: %v", file, err) 81 } 82 return path 83 }