2222import java .util .Collection ;
2323import java .util .Collections ;
2424import java .util .List ;
25+ import java .util .concurrent .atomic .AtomicLong ;
2526import org .junit .After ;
2627import org .junit .Before ;
2728import org .junit .Test ;
@@ -39,49 +40,20 @@ public class AgentIdentityUtilsTest {
3940 private static final String INVALID_SPIFFE_FORMAT =
4041 "spiffe://agents.global.org-INVALID.system.id.goog/path" ;
4142
42- // A minimal, valid self-signed X.509 certificate (PEM format) for testing loading.
43- // Generated for testing purposes.
44- private static final String TEST_CERT_PEM =
45- "-----BEGIN CERTIFICATE-----\n "
46- + "MIIDWTCCAkGgAwIBAgIUX5/1aT1uuxgj1+F7Q/r+5Q9y4JQwDQYJKoZIhvcNAQEL\n "
47- + "BQAwHTEbMBkGA1UEAwwSdGVzdC5leGFtcGxlLmNvbTAeFw0yNDAxMDEwMDAwMDBa\n "
48- + "Fw0zNDAxMDEwMDAwMDBaMB0xGzAZBgNVBAMMEnRlc3QuZXhhbXBsZS5jb20wggEi\n "
49- + "MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDV/8Q/5+8+X9Y+5+6+7+8+9+0+\n "
50- + "A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/\n "
51- + "g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B\n "
52- + "/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/g/h\n "
53- + "/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B/C/\n "
54- + "D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/g/h/i/\n "
55- + "j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B/C/D/E\n "
56- + "AgMBAAGjUzBRMB0GA1UdDgQWBBS/1/2/3/4/5/6/7/8/9/+A/B/C/DAfBgNVHSME\n "
57- + "GDAWgBS/1/2/3/4/5/6/7/8/9/+A/B/C/DAPBgNVHRMBAf8EBTADAQH/MA0GCSqG\n "
58- + "SIb3DQEBCwUAA4IBAQDV/8Q/5+8+X9Y+5+6+7+8+9+0+A/B/C/D/E/F/G/H/I/J/\n "
59- + "K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/\n "
60- + "q/r/s/t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B/C/D/E/F/G/H/I/J/K/L\n "
61- + "/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r\n "
62- + "/s/t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B/C/D/E/F/G/H/I/J/K/L/M/\n "
63- + "N/O/P/Q/R/S/T/U/V/W/X/Y/Z/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/\n "
64- + "t/u/v/w/x/y/z/0/1/2/3/4/5/6/7/8/9/+A/B/C/D/E\n "
65- + "-----END CERTIFICATE-----" ;
66-
6743 private TestEnvironmentProvider envProvider ;
6844 private Path tempDir ;
6945
7046 @ Before
7147 public void setUp () throws IOException {
7248 envProvider = new TestEnvironmentProvider ();
73- // Inject our test environment reader
7449 AgentIdentityUtils .setEnvReader (envProvider ::getEnv );
7550 tempDir = Files .createTempDirectory ("agent_identity_test" );
7651 }
7752
7853 @ After
7954 public void tearDown () throws IOException {
80- // Reset polling constants to defaults after each test to avoid side effects
81- AgentIdentityUtils .TOTAL_TIMEOUT_MS = 30000 ;
82- AgentIdentityUtils .FAST_POLL_INTERVAL_MS = 100 ;
83- AgentIdentityUtils .FAST_POLL_DURATION_MS = 5000 ;
84- AgentIdentityUtils .SLOW_POLL_INTERVAL_MS = 500 ;
55+ // Reset the time service to default after each test
56+ AgentIdentityUtils .resetTimeService ();
8557
8658 // Clean up temp files
8759 if (tempDir != null ) {
@@ -176,8 +148,8 @@ public void getAgentIdentityCertificate_noConfigEnvVar_returnsNull() throws IOEx
176148 @ Test
177149 public void getAgentIdentityCertificate_happyPath_loadsCertificate () throws IOException {
178150 // Setup: Get the absolute path of the test resource.
179- URL certUrl = getClass ().getClassLoader ().getResource ("agent_cert .pem" );
180- assertNotNull ("Test resource agent_cert .pem not found" , certUrl );
151+ URL certUrl = getClass ().getClassLoader ().getResource ("x509_leaf_certificate .pem" );
152+ assertNotNull ("Test resource x509_leaf_certificate .pem not found" , certUrl );
181153 String certPath = new File (certUrl .getFile ()).getAbsolutePath ();
182154
183155 // Create config file pointing to the cert.
@@ -206,7 +178,7 @@ public void getAgentIdentityCertificate_happyPath_loadsCertificate() throws IOEx
206178
207179 // Verify
208180 assertNotNull (cert );
209- // Basic verification that it loaded OUR cert (checking issuer from agent_cert.pem)
181+ // Basic verification that it loaded OUR cert
210182 assertTrue (cert .getIssuerDN ().getName ().contains ("unit-tests" ));
211183 }
212184
@@ -217,11 +189,9 @@ public void getAgentIdentityCertificate_timeout_throwsIOException() {
217189 "GOOGLE_API_CERTIFICATE_CONFIG" ,
218190 tempDir .resolve ("missing.json" ).toAbsolutePath ().toString ());
219191
220- // Reduce timeout to make test fast (e.g., 100ms total)
221- AgentIdentityUtils .TOTAL_TIMEOUT_MS = 100 ;
222- AgentIdentityUtils .FAST_POLL_INTERVAL_MS = 10 ;
223- AgentIdentityUtils .SLOW_POLL_INTERVAL_MS = 10 ;
224- AgentIdentityUtils .FAST_POLL_DURATION_MS = 50 ;
192+ // Use a fake time service that advances time rapidly when sleep is called.
193+ // This allows the 30s timeout loop to complete instantly in test execution time.
194+ AgentIdentityUtils .setTimeService (new FakeTimeService ());
225195
226196 // Execute & Verify
227197 IOException e =
@@ -231,6 +201,22 @@ public void getAgentIdentityCertificate_timeout_throwsIOException() {
231201 .contains ("Certificate config or certificate file not found after multiple retries" ));
232202 }
233203
204+ // Fake time service that advances time when sleep is requested.
205+ private static class FakeTimeService implements AgentIdentityUtils .TimeService {
206+ private final AtomicLong currentTime = new AtomicLong (0 );
207+
208+ @ Override
209+ public long currentTimeMillis () {
210+ return currentTime .get ();
211+ }
212+
213+ @ Override
214+ public void sleep (long millis ) throws InterruptedException {
215+ // Instead of actually sleeping, just advance the fake clock.
216+ currentTime .addAndGet (millis );
217+ }
218+ }
219+
234220 // A helper class to mock System.getenv for testing purposes within this file.
235221 private static class TestEnvironmentProvider {
236222 private final java .util .Map <String , String > env = new java .util .HashMap <>();
0 commit comments