34
34
import java .nio .file .Path ;
35
35
import java .time .Duration ;
36
36
import java .time .Instant ;
37
+ import java .util .Collection ;
37
38
import java .util .Collections ;
39
+ import javax .naming .NameNotFoundException ;
38
40
import javax .net .ssl .SSLHandshakeException ;
39
41
import org .junit .After ;
40
42
import org .junit .Before ;
@@ -147,6 +149,57 @@ public void create_successfulPublicConnection() throws IOException, InterruptedE
147
149
assertThat (readLine (socket )).isEqualTo (SERVER_MESSAGE );
148
150
}
149
151
152
+ @ Test
153
+ public void create_successfulPublicConnectionWithDomainName ()
154
+ throws IOException , InterruptedException {
155
+ FakeSslServer sslServer = new FakeSslServer ();
156
+ ConnectionConfig config =
157
+ new ConnectionConfig .Builder ()
158
+ .withCloudSqlInstance ("db.example.com" )
159
+ .withIpTypes ("PRIMARY" )
160
+ .build ();
161
+
162
+ int port = sslServer .start (PUBLIC_IP );
163
+
164
+ Connector connector = newConnector (config .getConnectorConfig (), port );
165
+
166
+ Socket socket = connector .connect (config , TEST_MAX_REFRESH_MS );
167
+
168
+ assertThat (readLine (socket )).isEqualTo (SERVER_MESSAGE );
169
+ }
170
+
171
+ @ Test
172
+ public void create_throwsErrorForUnresolvedDomainName () throws IOException {
173
+ ConnectionConfig config =
174
+ new ConnectionConfig .Builder ()
175
+ .withCloudSqlInstance ("baddomain.example.com" )
176
+ .withIpTypes ("PRIMARY" )
177
+ .build ();
178
+ Connector c = newConnector (config .getConnectorConfig (), DEFAULT_SERVER_PROXY_PORT );
179
+ RuntimeException ex =
180
+ assertThrows (RuntimeException .class , () -> c .connect (config , TEST_MAX_REFRESH_MS ));
181
+
182
+ assertThat (ex )
183
+ .hasMessageThat ()
184
+ .contains ("Cloud SQL connection name is invalid: \" baddomain.example.com\" " );
185
+ }
186
+
187
+ @ Test
188
+ public void create_throwsErrorForDomainNameBadTargetValue () throws IOException {
189
+ ConnectionConfig config =
190
+ new ConnectionConfig .Builder ()
191
+ .withCloudSqlInstance ("badvalue.example.com" )
192
+ .withIpTypes ("PRIMARY" )
193
+ .build ();
194
+ Connector c = newConnector (config .getConnectorConfig (), DEFAULT_SERVER_PROXY_PORT );
195
+ RuntimeException ex =
196
+ assertThrows (RuntimeException .class , () -> c .connect (config , TEST_MAX_REFRESH_MS ));
197
+
198
+ assertThat (ex )
199
+ .hasMessageThat ()
200
+ .contains ("Cloud SQL connection name is invalid: \" badvalue.example.com\" " );
201
+ }
202
+
150
203
private boolean isWindows () {
151
204
String os = System .getProperty ("os.name" ).toLowerCase ();
152
205
return os .contains ("win" );
@@ -210,7 +263,8 @@ public void create_successfulDomainScopedConnection() throws IOException, Interr
210
263
clientKeyPair ,
211
264
10 ,
212
265
TEST_MAX_REFRESH_MS ,
213
- port );
266
+ port ,
267
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
214
268
215
269
Socket socket = c .connect (config , TEST_MAX_REFRESH_MS );
216
270
@@ -271,7 +325,8 @@ public void create_throwsException_adminApiNotEnabled() throws IOException {
271
325
clientKeyPair ,
272
326
10 ,
273
327
TEST_MAX_REFRESH_MS ,
274
- DEFAULT_SERVER_PROXY_PORT );
328
+ DEFAULT_SERVER_PROXY_PORT ,
329
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
275
330
276
331
// Use a different project to get Api Not Enabled Error.
277
332
TerminalException ex =
@@ -303,7 +358,8 @@ public void create_throwsException_adminApiReturnsNotAuthorized() throws IOExcep
303
358
clientKeyPair ,
304
359
10 ,
305
360
TEST_MAX_REFRESH_MS ,
306
- DEFAULT_SERVER_PROXY_PORT );
361
+ DEFAULT_SERVER_PROXY_PORT ,
362
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
307
363
308
364
// Use a different instance to simulate incorrect permissions.
309
365
TerminalException ex =
@@ -335,7 +391,8 @@ public void create_throwsException_badGateway() throws IOException {
335
391
clientKeyPair ,
336
392
10 ,
337
393
TEST_MAX_REFRESH_MS ,
338
- DEFAULT_SERVER_PROXY_PORT );
394
+ DEFAULT_SERVER_PROXY_PORT ,
395
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
339
396
340
397
// If the gateway is down, then this is a temporary error, not a fatal error.
341
398
RuntimeException ex =
@@ -377,7 +434,8 @@ public void create_successfulPublicConnection_withIntermittentBadGatewayErrors()
377
434
clientKeyPair ,
378
435
10 ,
379
436
TEST_MAX_REFRESH_MS ,
380
- port );
437
+ port ,
438
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
381
439
382
440
Socket socket = c .connect (config , TEST_MAX_REFRESH_MS );
383
441
@@ -410,7 +468,8 @@ public void supportsCustomCredentialFactoryWithIAM() throws InterruptedException
410
468
clientKeyPair ,
411
469
10 ,
412
470
TEST_MAX_REFRESH_MS ,
413
- port );
471
+ port ,
472
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
414
473
415
474
Socket socket = c .connect (config , TEST_MAX_REFRESH_MS );
416
475
@@ -442,7 +501,8 @@ public void supportsCustomCredentialFactoryWithNoExpirationTime()
442
501
clientKeyPair ,
443
502
10 ,
444
503
TEST_MAX_REFRESH_MS ,
445
- port );
504
+ port ,
505
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
446
506
447
507
Socket socket = c .connect (config , TEST_MAX_REFRESH_MS );
448
508
@@ -480,7 +540,8 @@ public HttpRequestInitializer create() {
480
540
clientKeyPair ,
481
541
10 ,
482
542
TEST_MAX_REFRESH_MS ,
483
- DEFAULT_SERVER_PROXY_PORT );
543
+ DEFAULT_SERVER_PROXY_PORT ,
544
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
484
545
485
546
assertThrows (RuntimeException .class , () -> c .connect (config , TEST_MAX_REFRESH_MS ));
486
547
}
@@ -497,7 +558,8 @@ private Connector newConnector(ConnectorConfig config, int port) {
497
558
clientKeyPair ,
498
559
10 ,
499
560
TEST_MAX_REFRESH_MS ,
500
- port );
561
+ port ,
562
+ new DnsInstanceConnectionNameResolver (new MockDnsResolver ()));
501
563
return connector ;
502
564
}
503
565
@@ -506,4 +568,19 @@ private String readLine(Socket socket) throws IOException {
506
568
new BufferedReader (new InputStreamReader (socket .getInputStream (), UTF_8 ));
507
569
return bufferedReader .readLine ();
508
570
}
571
+
572
+ private static class MockDnsResolver implements DnsResolver {
573
+
574
+ @ Override
575
+ public Collection <DnsSrvRecord > resolveSrv (String domainName ) throws NameNotFoundException {
576
+ if ("db.example.com" .equals (domainName )) {
577
+ return Collections .singletonList (
578
+ new DnsSrvRecord ("0 10 3307 myProject:myRegion:myInstance." ));
579
+ }
580
+ if ("badvalue.example.com" .equals (domainName )) {
581
+ return Collections .singletonList (new DnsSrvRecord ("0 10 3307 not-an-instance-name." ));
582
+ }
583
+ throw new NameNotFoundException ("Not found: " + domainName );
584
+ }
585
+ }
509
586
}
0 commit comments