From 9bd127f661c7066ed4c7cb2f0921a0e5338e1ccc Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sun, 15 Nov 2026 00:03:58 -0700 Subject: [PATCH] Pin how the server-id: line is matched, and a non-UTF-8 install ID file Co-Authored-By: Claude Opus 5.5 --- .../stephenson/trace/TraceClientTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/software/stephenson/trace/TraceClientTest.java b/src/test/java/software/stephenson/trace/TraceClientTest.java index 9f0a961..243231e 100644 --- a/src/test/java/software/stephenson/trace/TraceClientTest.java +++ b/src/test/java/software/stephenson/trace/TraceClientTest.java @@ -1779,6 +1779,21 @@ void installId_anExistingServerIdIsUsedAndAnUnusableOneIsIgnored() { assertEquals(Collections.singletonMap("server-id", "x"), inTags.tags); } + @Test + void installId_theServerIdLineNeedsNoSpaceAroundItsColonAndIsCaseSensitive() { + // Unlike a tags: entry, where "a:b" is a string and not a pair, the + // server-id: line takes its value straight after the colon, and + // allows space before the colon. + assertEquals("abc", TraceClient.parseServerWideConfig(Arrays.asList("server-id:abc")).serverId); + assertEquals("abc", TraceClient.parseServerWideConfig(Arrays.asList("server-id : abc")).serverId); + assertEquals("abc", TraceClient.parseServerWideConfig(Arrays.asList("server-id:\tabc\t")).serverId); + + // The name itself is matched exactly. + assertNull(TraceClient.parseServerWideConfig(Arrays.asList("Server-Id: abc")).serverId); + assertNull(TraceClient.parseServerWideConfig(Arrays.asList("server_id: abc")).serverId); + assertNull(TraceClient.parseServerWideConfig(Arrays.asList("server-ids: abc")).serverId); + } + @Test void installId_unwritableConfigFallsBackToAnInMemoryIdWithoutThrowing(@TempDir Path scratch) throws Exception { // Arrange: a "plugins directory" that is a regular file, so nothing @@ -2144,6 +2159,34 @@ void installIdFromFile_leavesAnUnreadableFileAlone(@TempDir Path scratch) throws } } + @Test + void installIdFile_aFileThatIsNotUtf8IsLeftAloneAndAnInMemoryIdIsUsed(@TempDir Path scratch) throws Exception { + // Arrange + // An ID line below a comment saved in ISO-8859-1: the 0xE9 byte is + // not UTF-8, so the whole file fails to read, valid line and all. + Path file = scratch.resolve("trace-install-id"); + byte[] content = "# café\nmy-own-id\n".getBytes(StandardCharsets.ISO_8859_1); + Files.write(file, content); + RecordingHandler log = new RecordingHandler(); + Logger logger = Logger.getLogger("TraceClientTest.installIdFileNotUtf8"); + logger.setLevel(Level.ALL); + logger.addHandler(log); + + // Act + TraceClient client = assertDoesNotThrow(() -> TraceClient.builder(baseUrl(), "MyCli", "1.2.3").key("k") + .installIdFile(file.toFile()).logger(logger).build()); + client.close(); + + // Assert + assertTrue(client.isEnabled()); + assertTrue(client.installId().matches(UUID_PATTERN), client.installId()); + assertArrayEquals(content, Files.readAllBytes(file), "a file that cannot be read is never overwritten"); + assertNotEquals(client.installId(), TraceClient.installIdFromFile(file.toFile()), "in memory: a new one each process"); + assertTrue(log.await(1, TimeUnit.SECONDS), "the fallback should be mentioned at FINE"); + assertEquals(Level.FINE, log.records.get(0).getLevel()); + assertTrue(log.records.get(0).getMessage().contains("could not read install ID file"), log.records.get(0).getMessage()); + } + @Test void installIdFromFile_neverThrowsForABadPath() { for (File file : new File[] {null, new File(""), new File(" "), new File("bad\u0000name")}) {