From ab2d19a4ac0fd77bf615c4ac98a86cdcd7121db2 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:05:13 +0100 Subject: [PATCH] chore(test): compare certificate content, not mtime, in the TLS reuse tests (#7549) The regeneration and reuse tests compared the certificate file's modification time 100 ms apart. On a filesystem that records times to the second, a regenerated certificate usually keeps the same time. They now compare the certificate content, as the spoof-flag test already does, and no longer sleep. --- .../TlsConfigSourceCertificateReuseTest.java | 46 +++++-------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java index fa5e2f63ec..8101eaea94 100644 --- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java +++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java @@ -75,10 +75,7 @@ class TlsConfigSourceCertificateReuseTest { // Read initial metadata String initialMetadata = Files.readString(metadataFile); - long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); - - // Wait a bit to ensure timestamp difference - Thread.sleep(100); + String initialCert = Files.readString(certFile); // Change hostname configuration System.setProperty("floci.hostname", "host2"); @@ -91,12 +88,11 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(metadataFile), "Metadata should still exist"); String newMetadata = Files.readString(metadataFile); - long newModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); assertNotEquals(initialMetadata, newMetadata, "Metadata should be updated with new hostname"); - assertTrue(newModifiedTime > initialModifiedTime, - "Certificate should be regenerated (newer timestamp)"); + assertNotEquals(initialCert, Files.readString(certFile), + "Certificate should be regenerated (content changed)"); assertTrue(newMetadata.contains("host2"), "New metadata should contain 'host2'"); } @@ -123,15 +119,12 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(certFile), "Initial certificate should be generated"); assertTrue(Files.exists(metadataFile), "Initial metadata should be generated"); - long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); + String initialCert = Files.readString(certFile); // Delete metadata file to simulate old version Files.delete(metadataFile); assertFalse(Files.exists(metadataFile), "Metadata should be deleted"); - // Wait a bit to ensure timestamp difference - Thread.sleep(100); - // Act: Create new TlsConfigSource - should regenerate certificate new TlsConfigSource(); @@ -139,8 +132,7 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(certFile), "Certificate should still exist"); assertTrue(Files.exists(metadataFile), "Metadata should be regenerated"); - long newModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); - assertTrue(newModifiedTime > initialModifiedTime, + assertNotEquals(initialCert, Files.readString(certFile), "Certificate should be regenerated when metadata is missing"); } @@ -166,12 +158,9 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(certFile), "Initial certificate should be generated"); assertTrue(Files.exists(metadataFile), "Initial metadata should be generated"); - long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); + String initialCert = Files.readString(certFile); String initialMetadata = Files.readString(metadataFile); - // Wait a bit to ensure timestamp difference would be visible if regenerated - Thread.sleep(100); - // Act: Create new TlsConfigSource with same configuration - should reuse certificate new TlsConfigSource(); @@ -179,11 +168,10 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(certFile), "Certificate should still exist"); assertTrue(Files.exists(metadataFile), "Metadata should still exist"); - long newModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); String newMetadata = Files.readString(metadataFile); - assertEquals(initialModifiedTime, newModifiedTime, - "Certificate should be reused (same timestamp)"); + assertEquals(initialCert, Files.readString(certFile), + "Certificate should be reused (content unchanged)"); assertEquals(initialMetadata, newMetadata, "Metadata should be unchanged"); } @@ -235,12 +223,8 @@ class TlsConfigSourceCertificateReuseTest { new TlsConfigSource(); Path certFile = tempDir.resolve("tls").resolve("floci-server.crt"); assertTrue(Files.exists(certFile), "Initial certificate should be generated"); - long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); String initialCert = Files.readString(certFile); - // Ensure a regeneration would be detectable via timestamp. - Thread.sleep(100); - // Second boot with BouncyCastle NOT registered — mirrors the real ConfigSource-before-CDI // ordering. The constructor must register it and REUSE the existing cert (not regenerate). boolean wasRegistered = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null; @@ -253,10 +237,8 @@ class TlsConfigSourceCertificateReuseTest { } } - assertEquals(initialModifiedTime, Files.getLastModifiedTime(certFile).toMillis(), - "Certificate should be reused (same timestamp) even when BouncyCastle was not pre-registered"); assertEquals(initialCert, Files.readString(certFile), - "Certificate content should be unchanged (reused, not regenerated)"); + "Certificate should be reused even when BouncyCastle was not pre-registered"); } /** @@ -321,10 +303,7 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(metadataFile), "Initial metadata should be generated"); String initialMetadata = Files.readString(metadataFile); - long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); - - // Wait a bit to ensure timestamp difference - Thread.sleep(100); + String initialCert = Files.readString(certFile); // Change base URL hostname System.setProperty("floci.base-url", "https://host2:4566"); @@ -337,12 +316,11 @@ class TlsConfigSourceCertificateReuseTest { assertTrue(Files.exists(metadataFile), "Metadata should still exist"); String newMetadata = Files.readString(metadataFile); - long newModifiedTime = Files.getLastModifiedTime(certFile).toMillis(); assertNotEquals(initialMetadata, newMetadata, "Metadata should be updated with new hostname"); - assertTrue(newModifiedTime > initialModifiedTime, - "Certificate should be regenerated (newer timestamp)"); + assertNotEquals(initialCert, Files.readString(certFile), + "Certificate should be regenerated (content changed)"); assertTrue(newMetadata.contains("host2"), "New metadata should contain 'host2'"); }