11package  pgconn_test
22
33import  (
4- 	"bufio" 
54	"bytes" 
65	"compress/gzip" 
76	"context" 
@@ -54,71 +53,56 @@ func TestConnect(t *testing.T) {
5453	}
5554}
5655
57- // TestConnectTLS is separate from other connect tests because it has an additional test to ensure it really is a secure 
58- // connection. 
59- func  TestConnectTLS (t  * testing.T ) {
60- 	t .Parallel ()
61- 
62- 	connString  :=  os .Getenv ("PGX_TEST_TLS_CONN_STRING" )
63- 	if  connString  ==  ""  {
64- 		t .Skipf ("Skipping due to missing environment variable %v" , "PGX_TEST_TLS_CONN_STRING" )
65- 	}
66- 
67- 	var  conn  * pgconn.PgConn 
68- 	var  err  error 
69- 
70- 	isSslPasswrodEmpty  :=  strings .HasSuffix (connString , "sslpassword=" )
71- 
72- 	if  isSslPasswrodEmpty  {
73- 		config , err  :=  pgconn .ParseConfigWithSslPasswordCallback (connString , GetSslPassword )
74- 		require .Nil (t , err )
75- 
76- 		conn , err  =  pgconn .ConnectConfig (context .Background (), config )
77- 		require .NoError (t , err )
78- 	} else  {
79- 		conn , err  =  pgconn .Connect (context .Background (), connString )
80- 		require .NoError (t , err )
81- 	}
82- 
83- 	if  _ , ok  :=  conn .Conn ().(* tls.Conn ); ! ok  {
84- 		t .Error ("not a TLS connection" )
56+ func  TestConnectWithOption (t  * testing.T ) {
57+ 	tests  :=  []struct  {
58+ 		name  string 
59+ 		env   string 
60+ 	}{
61+ 		{"Unix socket" , "PGX_TEST_UNIX_SOCKET_CONN_STRING" },
62+ 		{"TCP" , "PGX_TEST_TCP_CONN_STRING" },
63+ 		{"Plain password" , "PGX_TEST_PLAIN_PASSWORD_CONN_STRING" },
64+ 		{"MD5 password" , "PGX_TEST_MD5_PASSWORD_CONN_STRING" },
65+ 		{"SCRAM password" , "PGX_TEST_SCRAM_PASSWORD_CONN_STRING" },
8566	}
8667
87- 	closeConn (t , conn )
88- }
68+ 	for  _ , tt  :=  range  tests  {
69+ 		tt  :=  tt 
70+ 		t .Run (tt .name , func (t  * testing.T ) {
71+ 			connString  :=  os .Getenv (tt .env )
72+ 			if  connString  ==  ""  {
73+ 				t .Skipf ("Skipping due to missing environment variable %v" , tt .env )
74+ 			}
75+             var  sslOptions  pgconn.ParseConfigOptions 
76+ 	        sslOptions .GetSSLPassword  =  GetSSLPassword 
77+ 			conn , err  :=  pgconn .ConnectWithOptions (context .Background (), connString , sslOptions )
78+ 			require .NoError (t , err )
8979
90- func  GetSslPassword () string  {
91- 	readFile , err  :=  os .Open ("data.txt" )
92- 	if  err  !=  nil  {
93- 		fmt .Println (err )
94- 	}
95- 	fileScanner  :=  bufio .NewScanner (readFile )
96- 	fileScanner .Split (bufio .ScanLines )
97- 	for  fileScanner .Scan () {
98- 		line  :=  fileScanner .Text ()
99- 		if  strings .HasPrefix (line , "sslpassword=" ) {
100- 			index  :=  len ("sslpassword=" )
101- 			line  :=  line [index :]
102- 			return  line 
103- 		}
80+ 			closeConn (t , conn )
81+ 		})
10482	}
105- 	return  "" 
10683}
10784
108- func  TestConnectTLSCallback (t  * testing.T ) {
85+ // TestConnectTLS is separate from other connect tests because it has an additional test to ensure it really is a secure 
86+ // connection. 
87+ func  TestConnectTLS (t  * testing.T ) {
10988	t .Parallel ()
11089
11190	connString  :=  os .Getenv ("PGX_TEST_TLS_CONN_STRING" )
11291	if  connString  ==  ""  {
11392		t .Skipf ("Skipping due to missing environment variable %v" , "PGX_TEST_TLS_CONN_STRING" )
11493	}
11594
116- 	config , err  :=  pgconn .ParseConfigWithSslPasswordCallback (connString , GetSslPassword )
95+ 	var  conn  * pgconn.PgConn 
96+ 	var  err  error 
97+ 
98+ 	var  sslOptions  pgconn.ParseConfigOptions 
99+ 	sslOptions .GetSSLPassword  =  GetSSLPassword 
100+     config , err  :=  pgconn .ParseConfigWithOptions (connString , sslOptions )
117101	require .Nil (t , err )
118102
119- 	 conn , err  : =  pgconn .ConnectConfig (context .Background (), config )
103+      conn , err  =  pgconn .ConnectConfig (context .Background (), config )
120104	require .NoError (t , err )
121- 
105+ 	 
122106	if  _ , ok  :=  conn .Conn ().(* tls.Conn ); ! ok  {
123107		t .Error ("not a TLS connection" )
124108	}
@@ -2180,3 +2164,8 @@ func Example() {
21802164	// 3 
21812165	// SELECT 3 
21822166}
2167+ 
2168+ func  GetSSLPassword (ctx  context.Context ) string  {
2169+ 	connString  :=  os .Getenv ("PGX_SSL_PASSWORD" )
2170+     return  connString 
2171+ }
0 commit comments