MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / TestPostgresClientConninfo

Function TestPostgresClientConninfo

cmd/pad/postgres_client_test.go:17–59  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestPostgresClientConninfo(t *testing.T) {
18 for _, tc := range []struct {
19 name, input, want, password string
20 hasPassword, invalid bool
21 }{
22 {name: "uri", input: "postgres://u:secret@host:5544/db?sslmode=require", want: "postgres://u@host:5544/db?sslmode=require", password: "secret", hasPassword: true},
23 {name: "escaped password", input: "postgresql://u:p%40ss%3A%27%5C@host/db", want: "postgresql://u@host/db", password: "p@ss:'\\", hasPassword: true},
24 {name: "query wins", input: "postgres://u:old@host/db?password=first&password=last&sslmode=disable", want: "postgres://u@host/db?sslmode=disable", password: "last", hasPassword: true},
25 {name: "empty userinfo password uses defaults", input: "postgres://u:@host/db", want: "postgres://u:@host/db"},
26 {name: "literal plus", input: "postgres://u@host/db?password=p+ass&application_name=a+b", want: "postgres://u@host/db?application_name=a+b", password: "p+ass", hasPassword: true},
27 {name: "multiple hosts", input: "postgresql://u:p@host1:5544,host2:5545/db?target_session_attrs=read-write", want: "postgresql://u@host1:5544,host2:5545/db?target_session_attrs=read-write", password: "p", hasPassword: true},
28 {name: "no password", input: "postgresql://u@host/db?application_name=backup", want: "postgresql://u@host/db?application_name=backup"},
29 {name: "keyword", input: "host=localhost port=5544 user=u password='p\\'a\\\\ss' dbname='data base' sslmode=disable", want: "host=localhost port=5544 user=u dbname='data base' sslmode=disable", password: "p'a\\ss", hasPassword: true},
30 {name: "escaped space", input: `dbname=db password=p\ ass user=u`, want: "dbname=db user=u", password: "p ass", hasPassword: true},
31 {name: "duplicate", input: "password=first dbname=db password=last", want: "dbname=db", password: "last", hasPassword: true},
32 {name: "embedded equals", input: "dbname=db password=a=b", want: "dbname=db", password: "a=b", hasPassword: true},
33 {name: "service", input: "service=myservice application_name=backup", want: "service=myservice application_name=backup"},
34 {name: "bare", input: "my_database", want: "my_database"},
35 {name: "unterminated", input: "password='secret", invalid: true},
36 {name: "trailing escape", input: "password=secret\\", invalid: true},
37 {name: "missing equals", input: "host localhost password=secret", invalid: true},
38 {name: "bad uri", input: "postgres://u:secret%zz@host/db", invalid: true},
39 {name: "bad query", input: "postgres://u:secret@host/db?password=%zz", invalid: true},
40 {name: "inline ssl passphrase", input: "postgres://u@host/db?sslpassword=secret", invalid: true},
41 {name: "service password precedence", input: "service=myservice password=secret", invalid: true},
42 } {
43 t.Run(tc.name, func(t *testing.T) {
44 dsn, password, err := postgresClientConninfo(tc.input)
45 if tc.invalid {
46 if err == nil || strings.Contains(err.Error(), "secret") {
47 t.Fatalf("expected a redacted error, got %v", err)
48 }
49 return
50 }
51 if err != nil || dsn != tc.want || (password != nil) != tc.hasPassword {
52 t.Fatalf("dsn=%q, password present=%v, err=%v", dsn, password != nil, err)
53 }
54 if password != nil && *password != tc.password {
55 t.Fatal("password decoded incorrectly")
56 }
57 })
58 }
59}
60
61func TestPostgresClientPasswordHandoff(t *testing.T) {
62 t.Setenv("PGPASSWORD", "inherited")

Callers

nothing calls this directly

Calls 5

postgresClientConninfoFunction · 0.85
FatalfMethod · 0.80
FatalMethod · 0.80
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected