|
40 | 40 | import static org.junit.Assert.assertThrows; |
41 | 41 | import static org.junit.Assert.assertTrue; |
42 | 42 | import static org.junit.Assert.fail; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.when; |
43 | 45 |
|
44 | 46 | import com.google.api.client.http.HttpStatusCodes; |
45 | 47 | import com.google.api.client.json.GenericJson; |
@@ -940,6 +942,32 @@ public void sign_sameAs_nonGDU() { |
940 | 942 | assertArrayEquals(expectedSignature, targetCredentials.sign(expectedSignature)); |
941 | 943 | } |
942 | 944 |
|
| 945 | + @Test |
| 946 | + public void sign_universeDomainException() throws IOException { |
| 947 | + // Currently, no credentials allowed as source credentials throws exception for |
| 948 | + // getUniverseDomain(), mock this behavior for test only. ServiceAccountCredentials |
| 949 | + // should not throw for getUniverseDomain() calls. |
| 950 | + ServiceAccountCredentials sourceCredentialsMock = mock(ServiceAccountCredentials.class); |
| 951 | + when(sourceCredentialsMock.getUniverseDomain()).thenThrow(IOException.class); |
| 952 | + |
| 953 | + MockIAMCredentialsServiceTransportFactory transportFactory = |
| 954 | + new MockIAMCredentialsServiceTransportFactory(); |
| 955 | + ImpersonatedCredentials targetCredentials = |
| 956 | + ImpersonatedCredentials.create( |
| 957 | + sourceCredentialsMock, |
| 958 | + IMPERSONATED_CLIENT_EMAIL, |
| 959 | + null, |
| 960 | + IMMUTABLE_SCOPES_LIST, |
| 961 | + VALID_LIFETIME, |
| 962 | + transportFactory); |
| 963 | + |
| 964 | + byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD}; |
| 965 | + |
| 966 | + IllegalStateException exception = |
| 967 | + assertThrows(IllegalStateException.class, () -> targetCredentials.sign(expectedSignature)); |
| 968 | + assertEquals("Failed to sign: Error obtaining universe domain", exception.getMessage()); |
| 969 | + } |
| 970 | + |
943 | 971 | @Test |
944 | 972 | public void idTokenWithAudience_sameAs() throws IOException { |
945 | 973 | mockTransportFactory.getTransport().setTargetPrincipal(IMPERSONATED_CLIENT_EMAIL); |
|
0 commit comments