Skip to content

Commit 91ec285

Browse files
committed
Add unit tests for IdentityPoolCredentialsSource.
1 parent 7cd3529 commit 91ec285

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

oauth2_http/java/com/google/auth/oauth2/IdentityPoolCredentialSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private CertificateConfig getCertificateConfig(Map<String, Object> credentialSou
121121
}
122122
return (String) value;
123123
}
124+
124125
/**
125126
* Represents the configuration options for X.509-based workload credentials (mTLS). It specifies
126127
* how to locate and use the client certificate, private key, and optional trust chain for mutual
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package com.google.auth.oauth2;
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+
import static org.junit.Assert.*;
38+
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;
50+
import java.util.HashMap;
51+
import java.util.List;
52+
import java.util.Map;
53+
import javax.annotation.Nullable;
54+
import org.junit.Test;
55+
import org.junit.runner.RunWith;
56+
import org.junit.runners.JUnit4;
57+
58+
import com.google.auth.oauth2.IdentityPoolCredentialSource.IdentityPoolCredentialSourceType;
59+
60+
/** Tests for {@link IdentityPoolCredentialSource}. */
61+
@RunWith(JUnit4.class)
62+
public class IdentityPoolCredentialsSourceTest {
63+
64+
@Test
65+
public void constructor_certificateConfig(){
66+
Map<String, Object> certificateMap = new HashMap<>();
67+
certificateMap.put("certificate_config_location", "/path/to/certificate");
68+
69+
Map<String, Object> credentialSourceMap = new HashMap<>();
70+
credentialSourceMap.put("certificate", certificateMap);
71+
72+
IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap);
73+
assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType);
74+
assertNotNull(credentialSource.certificateConfig);
75+
assertFalse(credentialSource.certificateConfig.useDefaultCertificateConfig());
76+
assertEquals("/path/to/certificate", credentialSource.certificateConfig.getCertificateConfigLocation());
77+
}
78+
79+
@Test
80+
public void constructor_certificateConfig_useDefault(){
81+
Map<String, Object> certificateMap = new HashMap<>();
82+
certificateMap.put("use_default_certificate_config", true);
83+
84+
Map<String, Object> credentialSourceMap = new HashMap<>();
85+
credentialSourceMap.put("certificate", certificateMap);
86+
87+
IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap);
88+
assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType);
89+
assertNotNull(credentialSource.certificateConfig);
90+
assertTrue(credentialSource.certificateConfig.useDefaultCertificateConfig());
91+
}
92+
93+
@Test
94+
public void constructor_certificateConfig_missingRequiredFields_throws(){
95+
Map<String, Object> certificateMap = new HashMap<>();
96+
//Missing both use_default_certificate_config and certificate_config_location
97+
certificateMap.put("trust_chain_path", "path/to/trust/chain");
98+
99+
Map<String, Object> credentialSourceMap = new HashMap<>();
100+
credentialSourceMap.put("certificate", certificateMap);
101+
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"));
107+
}
108+
109+
@Test
110+
public void constructor_certificateConfig_bothFieldsSet_throws(){
111+
Map<String, Object> certificateMap = new HashMap<>();
112+
certificateMap.put("use_default_certificate_config", true);
113+
certificateMap.put("certificate_config_location", "/path/to/certificate");
114+
115+
Map<String, Object> credentialSourceMap = new HashMap<>();
116+
credentialSourceMap.put("certificate", certificateMap);
117+
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"));
123+
}
124+
125+
@Test
126+
public void constructor_certificateConfig_trustChainPath(){
127+
Map<String, Object> certificateMap = new HashMap<>();
128+
certificateMap.put("use_default_certificate_config", true);
129+
certificateMap.put("trust_chain_path", "path/to/trust/chain");
130+
131+
Map<String, Object> credentialSourceMap = new HashMap<>();
132+
credentialSourceMap.put("certificate", certificateMap);
133+
134+
IdentityPoolCredentialSource credentialSource = new IdentityPoolCredentialSource(credentialSourceMap);
135+
assertEquals(IdentityPoolCredentialSourceType.CERTIFICATE, credentialSource.credentialSourceType);
136+
assertNotNull(credentialSource.certificateConfig);
137+
assertEquals("path/to/trust/chain", credentialSource.certificateConfig.getTrustChainPath());
138+
}
139+
140+
141+
@Test
142+
public void constructor_certificateConfig_invalidType_throws(){
143+
Map<String, Object> certificateMap = new HashMap<>();
144+
certificateMap.put("use_default_certificate_config", "invalid-type");
145+
146+
Map<String, Object> credentialSourceMap = new HashMap<>();
147+
credentialSourceMap.put("certificate", certificateMap);
148+
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+
}
155+
156+
}

0 commit comments

Comments
 (0)