|
31 | 31 |
|
32 | 32 | package com.google.auth.oauth2; |
33 | 33 |
|
34 | | -import static com.google.auth.Credentials.GOOGLE_DEFAULT_UNIVERSE; |
35 | | -import static com.google.auth.oauth2.MockExternalAccountCredentialsTransport.SERVICE_ACCOUNT_IMPERSONATION_URL; |
36 | | -import static com.google.auth.oauth2.OAuth2Utils.JSON_FACTORY; |
37 | 34 | import static org.junit.Assert.*; |
38 | 35 |
|
39 | | -import com.google.api.client.http.HttpTransport; |
40 | | -import com.google.api.client.json.GenericJson; |
41 | | -import com.google.api.client.util.Clock; |
42 | | -import com.google.auth.TestUtils; |
43 | | -import com.google.auth.http.HttpTransportFactory; |
44 | | -import java.io.ByteArrayInputStream; |
45 | | -import java.io.File; |
46 | | -import java.io.IOException; |
47 | | -import java.io.InputStream; |
48 | | -import java.nio.charset.StandardCharsets; |
49 | | -import java.util.Arrays; |
| 36 | +import com.google.auth.oauth2.IdentityPoolCredentialSource.IdentityPoolCredentialSourceType; |
50 | 37 | import java.util.HashMap; |
51 | | -import java.util.List; |
52 | 38 | import java.util.Map; |
53 | | -import javax.annotation.Nullable; |
54 | 39 | import org.junit.Test; |
55 | 40 | import org.junit.runner.RunWith; |
56 | 41 | import org.junit.runners.JUnit4; |
57 | 42 |
|
58 | | -import com.google.auth.oauth2.IdentityPoolCredentialSource.IdentityPoolCredentialSourceType; |
59 | | - |
60 | 43 | /** Tests for {@link IdentityPoolCredentialSource}. */ |
61 | 44 | @RunWith(JUnit4.class) |
62 | 45 | public class IdentityPoolCredentialsSourceTest { |
63 | 46 |
|
64 | 47 | @Test |
65 | | - public void constructor_certificateConfig(){ |
| 48 | + public void constructor_certificateConfig() { |
66 | 49 | Map<String, Object> certificateMap = new HashMap<>(); |
67 | 50 | certificateMap.put("certificate_config_location", "/path/to/certificate"); |
68 | 51 |
|
69 | 52 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
70 | 53 | credentialSourceMap.put("certificate", certificateMap); |
71 | 54 |
|
72 | | - IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap); |
73 | | - assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
| 55 | + IdentityPoolCredentialSource credentialSource = |
| 56 | + new IdentityPoolCredentialSource(credentialSourceMap); |
| 57 | + assertEquals( |
| 58 | + IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
74 | 59 | assertNotNull(credentialSource.certificateConfig); |
75 | 60 | assertFalse(credentialSource.certificateConfig.useDefaultCertificateConfig()); |
76 | | - assertEquals("/path/to/certificate", credentialSource.certificateConfig.getCertificateConfigLocation()); |
| 61 | + assertEquals( |
| 62 | + "/path/to/certificate", credentialSource.certificateConfig.getCertificateConfigLocation()); |
77 | 63 | } |
78 | 64 |
|
79 | 65 | @Test |
80 | | - public void constructor_certificateConfig_useDefault(){ |
| 66 | + public void constructor_certificateConfig_useDefault() { |
81 | 67 | Map<String, Object> certificateMap = new HashMap<>(); |
82 | 68 | certificateMap.put("use_default_certificate_config", true); |
83 | 69 |
|
84 | 70 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
85 | 71 | credentialSourceMap.put("certificate", certificateMap); |
86 | 72 |
|
87 | | - IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap); |
88 | | - assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
| 73 | + IdentityPoolCredentialSource credentialSource = |
| 74 | + new IdentityPoolCredentialSource(credentialSourceMap); |
| 75 | + assertEquals( |
| 76 | + IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
89 | 77 | assertNotNull(credentialSource.certificateConfig); |
90 | 78 | assertTrue(credentialSource.certificateConfig.useDefaultCertificateConfig()); |
91 | 79 | } |
92 | 80 |
|
93 | 81 | @Test |
94 | | - public void constructor_certificateConfig_missingRequiredFields_throws(){ |
| 82 | + public void constructor_certificateConfig_missingRequiredFields_throws() { |
95 | 83 | Map<String, Object> certificateMap = new HashMap<>(); |
96 | | - //Missing both use_default_certificate_config and certificate_config_location |
| 84 | + // Missing both use_default_certificate_config and certificate_config_location. |
97 | 85 | certificateMap.put("trust_chain_path", "path/to/trust/chain"); |
98 | 86 |
|
99 | 87 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
100 | 88 | credentialSourceMap.put("certificate", certificateMap); |
101 | 89 |
|
102 | | - IllegalArgumentException exception = assertThrows( |
103 | | - IllegalArgumentException.class, |
104 | | - () -> new IdentityPoolCredentialSource(credentialSourceMap) |
105 | | - ); |
106 | | - assertTrue(exception.getMessage().contains("must either specify a certificate_config_location or use_default_certificate_config should be true")); |
| 90 | + IllegalArgumentException exception = |
| 91 | + assertThrows( |
| 92 | + IllegalArgumentException.class, |
| 93 | + () -> new IdentityPoolCredentialSource(credentialSourceMap)); |
| 94 | + assertEquals( |
| 95 | + "Invalid 'certificate' configuration in credential source: Must specify either 'certificate_config_location' or set 'use_default_certificate_config' to true.", |
| 96 | + exception.getMessage()); |
107 | 97 | } |
108 | 98 |
|
109 | 99 | @Test |
110 | | - public void constructor_certificateConfig_bothFieldsSet_throws(){ |
| 100 | + public void constructor_certificateConfig_bothFieldsSet_throws() { |
111 | 101 | Map<String, Object> certificateMap = new HashMap<>(); |
112 | 102 | certificateMap.put("use_default_certificate_config", true); |
113 | 103 | certificateMap.put("certificate_config_location", "/path/to/certificate"); |
114 | 104 |
|
115 | 105 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
116 | 106 | credentialSourceMap.put("certificate", certificateMap); |
117 | 107 |
|
118 | | - IllegalArgumentException exception = assertThrows( |
119 | | - IllegalArgumentException.class, |
120 | | - () -> new IdentityPoolCredentialSource(credentialSourceMap) |
121 | | - ); |
122 | | - assertTrue(exception.getMessage().contains("cannot specify both a certificate_config_location and use_default_certificate_config=true")); |
| 108 | + IllegalArgumentException exception = |
| 109 | + assertThrows( |
| 110 | + IllegalArgumentException.class, |
| 111 | + () -> new IdentityPoolCredentialSource(credentialSourceMap)); |
| 112 | + |
| 113 | + assertEquals( |
| 114 | + "Invalid 'certificate' configuration in credential source: Cannot specify both 'certificate_config_location' and set 'use_default_certificate_config' to true.", |
| 115 | + exception.getMessage()); |
123 | 116 | } |
124 | 117 |
|
125 | 118 | @Test |
126 | | - public void constructor_certificateConfig_trustChainPath(){ |
| 119 | + public void constructor_certificateConfig_trustChainPath() { |
127 | 120 | Map<String, Object> certificateMap = new HashMap<>(); |
128 | 121 | certificateMap.put("use_default_certificate_config", true); |
129 | 122 | certificateMap.put("trust_chain_path", "path/to/trust/chain"); |
130 | 123 |
|
131 | 124 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
132 | 125 | credentialSourceMap.put("certificate", certificateMap); |
133 | 126 |
|
134 | | - IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap); |
135 | | - assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
| 127 | + IdentityPoolCredentialSource credentialSource = |
| 128 | + new IdentityPoolCredentialSource(credentialSourceMap); |
| 129 | + assertEquals( |
| 130 | + IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType); |
136 | 131 | assertNotNull(credentialSource.certificateConfig); |
137 | 132 | assertEquals("path/to/trust/chain", credentialSource.certificateConfig.getTrustChainPath()); |
138 | 133 | } |
139 | 134 |
|
140 | | - |
141 | 135 | @Test |
142 | | - public void constructor_certificateConfig_invalidType_throws(){ |
| 136 | + public void constructor_certificateConfig_invalidType_throws() { |
143 | 137 | Map<String, Object> certificateMap = new HashMap<>(); |
144 | 138 | certificateMap.put("use_default_certificate_config", "invalid-type"); |
145 | 139 |
|
146 | 140 | Map<String, Object> credentialSourceMap = new HashMap<>(); |
147 | 141 | credentialSourceMap.put("certificate", certificateMap); |
148 | 142 |
|
149 | | - IllegalArgumentException exception = assertThrows( |
150 | | - IllegalArgumentException.class, |
151 | | - () -> new IdentityPoolCredentialSource(credentialSourceMap) |
152 | | - ); |
153 | | - assertTrue(exception.getMessage().contains("Invalid type for 'use_default_certificate_config' in certificate configuration: expected Boolean")); |
154 | | - } |
| 143 | + IllegalArgumentException exception = |
| 144 | + assertThrows( |
| 145 | + IllegalArgumentException.class, |
| 146 | + () -> new IdentityPoolCredentialSource(credentialSourceMap)); |
155 | 147 |
|
| 148 | + assertEquals( |
| 149 | + "Invalid type for 'use_default_certificate_config' in certificate configuration: expected Boolean, got String.", |
| 150 | + exception.getMessage()); |
| 151 | + } |
156 | 152 | } |
0 commit comments