github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/hba/scanner_test.go (about) 1 // Copyright 2020 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 "testing" 16 17 "github.com/cockroachdb/datadriven" 18 "github.com/kr/pretty" 19 ) 20 21 func TestSpecialCharacters(t *testing.T) { 22 // We use Go test cases here instead of datadriven because the input 23 // strings would be stripped of whitespace or considered invalid by 24 // datadriven. 25 testData := []struct { 26 input string 27 expErr string 28 }{ 29 {"\"ab\tcd\"", `line 1: invalid characters in quoted string`}, 30 {"\"ab\fcd\"", `line 1: invalid characters in quoted string`}, 31 {`0 0 0 0 ` + "\f", `line 1: unsupported character: "\f"`}, 32 {`0 0 0 0 ` + "\x00", `line 1: unsupported character: "\x00"`}, 33 } 34 35 for _, tc := range testData { 36 _, err := tokenize(tc.input) 37 if err == nil || err.Error() != tc.expErr { 38 t.Errorf("expected:\n%s\ngot:\n%v", tc.expErr, err) 39 } 40 } 41 } 42 43 func TestScanner(t *testing.T) { 44 datadriven.RunTest(t, "testdata/scan", func(t *testing.T, td *datadriven.TestData) string { 45 switch td.Cmd { 46 case "token": 47 remaining, tok, trailingComma, err := nextToken(td.Input) 48 if err != nil { 49 return fmt.Sprintf("error: %v", err) 50 } 51 return fmt.Sprintf("%# v %v %q", pretty.Formatter(tok), trailingComma, remaining) 52 53 case "field": 54 remaining, field, err := nextFieldExpand(td.Input) 55 if err != nil { 56 return fmt.Sprintf("error: %v", err) 57 } 58 return fmt.Sprintf("%+v\n%q", field, remaining) 59 60 case "file": 61 tokens, err := tokenize(td.Input) 62 if err != nil { 63 return fmt.Sprintf("error: %v", err) 64 } 65 return fmt.Sprintf("%# v", pretty.Formatter(tokens)) 66 default: 67 t.Fatalf("unknown directive: %s", td.Cmd) 68 } 69 return "" 70 }) 71 }