@@ -18,22 +18,18 @@ public class AuthenticationTests : MultiplexingTestBase
1818 [ NonParallelizable ] // Sets environment variable
1919 public async Task Connect_UserNameFromEnvironment_Succeeds ( )
2020 {
21- var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
22- using var _ = SetEnvironmentVariable ( "PGUSER" , builder . Username ) ;
23- builder . Username = null ;
24- using var __ = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
25- using var ___ = await OpenConnectionAsync ( connectionString ) ;
21+ using var _ = SetEnvironmentVariable ( "PGUSER" , new NpgsqlConnectionStringBuilder ( ConnectionString ) . Username ) ;
22+ await using var dataSource = CreateDataSource ( csb => csb . Username = null ) ;
23+ await using var __ = await dataSource . OpenConnectionAsync ( ) ;
2624 }
2725
2826 [ Test ]
2927 [ NonParallelizable ] // Sets environment variable
3028 public async Task Connect_PasswordFromEnvironment_Succeeds ( )
3129 {
32- var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
33- using var _ = SetEnvironmentVariable ( "PGPASSWORD" , builder . Password ) ;
34- builder . Password = null ;
35- using var __ = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
36- using var ___ = await OpenConnectionAsync ( connectionString ) ;
30+ using var _ = SetEnvironmentVariable ( "PGPASSWORD" , new NpgsqlConnectionStringBuilder ( ConnectionString ) . Password ) ;
31+ await using var dataSource = CreateDataSource ( csb => csb . Passfile = null ) ;
32+ await using var __ = await dataSource . OpenConnectionAsync ( ) ;
3733 }
3834
3935 [ Test ]
@@ -142,18 +138,17 @@ public async Task Use_pgpass_from_connection_string()
142138 {
143139 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
144140 var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
145-
146- var password = builder . Password ;
147- builder . Password = null ;
148-
149141 var passFile = Path . GetTempFileName ( ) ;
150- File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
151- builder . Passfile = passFile ;
142+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ builder . Password } ") ;
152143
153144 try
154145 {
155- using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
156- using var conn = await OpenConnectionAsync ( connectionString ) ;
146+ await using var dataSource = CreateDataSource ( csb =>
147+ {
148+ csb . Passfile = null ;
149+ csb . Passfile = passFile ;
150+ } ) ;
151+ await using var conn = await dataSource . OpenConnectionAsync ( ) ;
157152 }
158153 finally
159154 {
@@ -167,18 +162,14 @@ public async Task Use_pgpass_from_environment_variable()
167162 {
168163 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
169164 var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
170-
171- var password = builder . Password ;
172- builder . Password = null ;
173-
174165 var passFile = Path . GetTempFileName ( ) ;
175- File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
166+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ builder . Password } ") ;
176167 using var passFileVariable = SetEnvironmentVariable ( "PGPASSFILE" , passFile ) ;
177168
178169 try
179170 {
180- using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
181- using var conn = await OpenConnectionAsync ( connectionString ) ;
171+ await using var dataSource = CreateDataSource ( csb => csb . Password = null ) ;
172+ await using var conn = await dataSource . OpenConnectionAsync ( ) ;
182173 }
183174 finally
184175 {
@@ -191,10 +182,6 @@ public async Task Use_pgpass_from_environment_variable()
191182 public async Task Use_pgpass_from_homedir ( )
192183 {
193184 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
194- var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
195-
196- var password = builder . Password ;
197- builder . Password = null ;
198185
199186 string ? dirToDelete = null ;
200187 string passFile ;
@@ -222,9 +209,10 @@ public async Task Use_pgpass_from_homedir()
222209
223210 try
224211 {
225- File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
226- using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
227- using var conn = await OpenConnectionAsync ( connectionString ) ;
212+ var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
213+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ builder . Password } ") ;
214+ await using var dataSource = CreateDataSource ( csb => csb . Passfile = null ) ;
215+ await using var conn = await dataSource . OpenConnectionAsync ( ) ;
228216 }
229217 finally
230218 {
@@ -243,8 +231,8 @@ public async Task Use_pgpass_from_homedir()
243231 public void Password_source_precedence ( )
244232 {
245233 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
246- var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
247234
235+ var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
248236 var password = builder . Password ;
249237 var passwordBad = password + "_bad" ;
250238
@@ -257,51 +245,66 @@ public void Password_source_precedence()
257245 File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
258246 File . WriteAllText ( passFileBad , $ "*:*:*:{ builder . Username } :{ passwordBad } ") ;
259247
260- using ( var passFileVariable = SetEnvironmentVariable ( "PGPASSFILE" , passFileBad ) )
248+ using ( SetEnvironmentVariable ( "PGPASSFILE" , passFileBad ) )
261249 {
262250 // Password from the connection string goes first
263- using ( var passwordVariable = SetEnvironmentVariable ( "PGPASSWORD" , passwordBad ) )
264- Assert . That ( OpenConnection ( password , passFileBad ) , Throws . Nothing ) ;
251+ using ( SetEnvironmentVariable ( "PGPASSWORD" , passwordBad ) )
252+ {
253+ using var dataSource1 = CreateDataSource ( csb =>
254+ {
255+ csb . Password = password ;
256+ csb . Passfile = passFileBad ;
257+ } ) ;
258+
259+ Assert . That ( ( ) => dataSource1 . OpenConnection ( ) , Throws . Nothing ) ;
260+ }
265261
266262 // Password from the environment variable goes second
267- using ( var passwordVariable = SetEnvironmentVariable ( "PGPASSWORD" , password ) )
268- Assert . That ( OpenConnection ( password : null , passFileBad ) , Throws . Nothing ) ;
263+ using ( SetEnvironmentVariable ( "PGPASSWORD" , password ) )
264+ {
265+ using var dataSource2 = CreateDataSource ( csb =>
266+ {
267+ csb . Password = null ;
268+ csb . Passfile = passFileBad ;
269+ } ) ;
270+
271+ Assert . That ( ( ) => dataSource2 . OpenConnection ( ) , Throws . Nothing ) ;
272+ }
269273
270274 // Passfile from the connection string goes third
271- Assert . That ( OpenConnection ( password : null , passFile : passFile ) , Throws . Nothing ) ;
275+ using var dataSource3 = CreateDataSource ( csb =>
276+ {
277+ csb . Password = null ;
278+ csb . Passfile = passFile ;
279+ } ) ;
280+
281+ Assert . That ( ( ) => dataSource3 . OpenConnection ( ) , Throws . Nothing ) ;
272282 }
273283
274284 // Passfile from the environment variable goes fourth
275- using ( var passFileVariable = SetEnvironmentVariable ( "PGPASSFILE" , passFile ) )
276- Assert . That ( OpenConnection ( password : null , passFile : null ) , Throws . Nothing ) ;
277-
278- Func < ValueTask > OpenConnection ( string ? password , string ? passFile ) => async ( ) =>
285+ using ( SetEnvironmentVariable ( "PGPASSFILE" , passFile ) )
279286 {
280- builder . Password = password ;
281- builder . Passfile = passFile ;
282- builder . ApplicationName = $ "{ nameof ( Password_source_precedence ) } :{ Guid . NewGuid ( ) } ";
287+ using var dataSource4 = CreateDataSource ( csb =>
288+ {
289+ csb . Password = null ;
290+ csb . Passfile = null ;
291+ } ) ;
283292
284- using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
285- using var connection = await OpenConnectionAsync ( connectionString ) ;
286- } ;
293+ Assert . That ( ( ) => dataSource4 . OpenConnection ( ) , Throws . Nothing ) ;
294+ }
287295 }
288296
289297 [ Test , Description ( "Connects with a bad password to ensure the proper error is thrown" ) ]
290298 public void Authentication_failure ( )
291299 {
292- var builder = new NpgsqlConnectionStringBuilder ( ConnectionString )
293- {
294- Password = "bad"
295- } ;
296- using ( CreateTempPool ( builder , out var connectionString ) )
297- using ( var conn = new NpgsqlConnection ( connectionString ) )
298- {
299- Assert . That ( ( ) => conn . OpenAsync ( ) , Throws . Exception
300- . TypeOf < PostgresException > ( )
301- . With . Property ( nameof ( PostgresException . SqlState ) ) . StartsWith ( "28" )
302- ) ;
303- Assert . That ( conn . FullState , Is . EqualTo ( ConnectionState . Closed ) ) ;
304- }
300+ using var dataSource = CreateDataSource ( csb => csb . Password = "bad" ) ;
301+ using var conn = dataSource . CreateConnection ( ) ;
302+
303+ Assert . That ( ( ) => conn . OpenAsync ( ) , Throws . Exception
304+ . TypeOf < PostgresException > ( )
305+ . With . Property ( nameof ( PostgresException . SqlState ) ) . StartsWith ( "28" )
306+ ) ;
307+ Assert . That ( conn . FullState , Is . EqualTo ( ConnectionState . Closed ) ) ;
305308 }
306309
307310 [ Test , Description ( "Simulates a timeout during the authentication phase" ) ]
@@ -310,13 +313,13 @@ public async Task Timeout_during_authentication()
310313 {
311314 var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) { Timeout = 1 } ;
312315 await using var postmasterMock = new PgPostmasterMock ( builder . ConnectionString ) ;
313- using var _ = CreateTempPool ( postmasterMock . ConnectionString , out var connectionString ) ;
314-
315- var __ = postmasterMock . AcceptServer ( ) ;
316+ _ = postmasterMock . AcceptServer ( ) ;
316317
317318 // The server will accept a connection from the client, but will not respond to the client's authentication
318319 // request. This should trigger a timeout
319- Assert . That ( async ( ) => await OpenConnectionAsync ( connectionString ) ,
320+ await using var dataSource = CreateDataSource ( postmasterMock . ConnectionString ) ;
321+ await using var connection = dataSource . CreateConnection ( ) ;
322+ Assert . That ( async ( ) => await connection . OpenAsync ( ) ,
320323 Throws . Exception . TypeOf < NpgsqlException > ( )
321324 . With . InnerException . TypeOf < TimeoutException > ( ) ) ;
322325 }
@@ -344,7 +347,7 @@ public async Task AuthenticateIntegratedSecurity()
344347 Username = null ,
345348 Password = null
346349 } ) ;
347- await using var c = await dataSource . OpenConnectionAsync ( ) ;
350+ await using var c = await dataSource . OpenConnectionAsync ( ) ;
348351 Assert . That ( c . State , Is . EqualTo ( ConnectionState . Open ) ) ;
349352 }
350353
0 commit comments