github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/hba/hba_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package hba 12 13 import ( 14 "fmt" 15 "path/filepath" 16 "strings" 17 "testing" 18 19 "github.com/cockroachdb/datadriven" 20 "github.com/kr/pretty" 21 ) 22 23 func TestParse(t *testing.T) { 24 datadriven.RunTest(t, filepath.Join("testdata", "parse"), 25 func(t *testing.T, td *datadriven.TestData) string { 26 switch td.Cmd { 27 case "multiline": 28 conf, err := Parse(td.Input) 29 if err != nil { 30 return fmt.Sprintf("error: %v\n", err) 31 } 32 var out strings.Builder 33 fmt.Fprintf(&out, "# String render check:\n%s", conf) 34 fmt.Fprintf(&out, "# Detail:\n%# v", pretty.Formatter(conf)) 35 return out.String() 36 37 case "line": 38 tokens, err := tokenize(td.Input) 39 if err != nil { 40 td.Fatalf(t, "%v", err) 41 } 42 if len(tokens.lines) != 1 { 43 td.Fatalf(t, "line parse only valid with one line of input") 44 } 45 prefix := "" // For debugging, use prefix := pretty.Sprint(tokens.lines[0]) + "\n" 46 entry, err := parseHbaLine(tokens.lines[0]) 47 if err != nil { 48 return prefix + fmt.Sprintf("error: %v\n", err) 49 } 50 return prefix + entry.String() 51 52 default: 53 return fmt.Sprintf("unknown directive: %s", td.Cmd) 54 } 55 }) 56 } 57 58 func TestParseAndNormalizeAuthConfig(t *testing.T) { 59 datadriven.RunTest(t, filepath.Join("testdata", "normalization"), 60 func(t *testing.T, td *datadriven.TestData) string { 61 switch td.Cmd { 62 case "hba": 63 conf, err := ParseAndNormalize(td.Input) 64 if err != nil { 65 return fmt.Sprintf("error: %v\n", err) 66 } 67 return conf.String() 68 default: 69 t.Fatalf("unknown directive: %s", td.Cmd) 70 } 71 return "" 72 }) 73 } 74 75 func TestMatchConnType(t *testing.T) { 76 testCases := []struct { 77 conf, conn ConnType 78 match bool 79 }{ 80 {ConnLocal, ConnHostSSL, false}, 81 {ConnLocal, ConnHostNoSSL, false}, 82 {ConnLocal, ConnLocal, true}, 83 {ConnHostAny, ConnLocal, false}, 84 {ConnHostAny, ConnHostSSL, true}, 85 {ConnHostAny, ConnHostNoSSL, true}, 86 {ConnHostSSL, ConnLocal, false}, 87 {ConnHostSSL, ConnHostSSL, true}, 88 {ConnHostSSL, ConnHostNoSSL, false}, 89 {ConnHostNoSSL, ConnLocal, false}, 90 {ConnHostNoSSL, ConnHostSSL, false}, 91 {ConnHostNoSSL, ConnHostNoSSL, true}, 92 } 93 for _, tc := range testCases { 94 entry := Entry{ConnType: tc.conf} 95 if m := entry.ConnTypeMatches(tc.conn); m != tc.match { 96 t.Errorf("%s vs %s: expected %v, got %v", tc.conf, tc.conn, tc.match, m) 97 } 98 } 99 } 100 101 // TODO(mjibson): these are untested outside ccl +gss builds. 102 var _ = Entry.GetOption 103 var _ = Entry.GetOptions