Skip to content

Commit e4088aa

Browse files
committed
test(registry): assert pinned tar metadata directly, not just digest equality
1 parent bb0b3cf commit e4088aa

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎src/main/java/io/github/intisy/docker/registry/LayerBuilder.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ static List<String> directoriesFor(String prefix, Path source, List<Path> files)
107107
return directories;
108108
}
109109

110+
/**
111+
* @implNote the uid, gid and owner name calls are redundant with commons-compress 1.24.0's own
112+
* defaults for the {@code TarArchiveEntry(String)} constructor used above, and are kept
113+
* deliberately: the library sources {@code userName} from {@code System.getProperty("user.name")}
114+
* on another of its constructor paths, so a future constructor change here would otherwise leak
115+
* the building machine's identity into the layer and silently break cross-machine determinism.
116+
*/
110117
private static void pin(TarArchiveEntry entry, int mode, long size) {
111118
entry.setMode((entry.isDirectory() ? TarArchiveEntry.DEFAULT_DIR_MODE : 0) | mode);
112119
entry.setModTime(0L);

‎src/test/java/io/github/intisy/docker/unit/LayerBuilderTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ public void diffIdIsTheUncompressedDigestAndDigestIsTheCompressedOne(@TempDir Pa
9292
assertEquals(Files.size(layer.file()), layer.size());
9393
}
9494

95+
/**
96+
* Determinism comes from pinning every field that could otherwise vary between two builds of
97+
* the same tree. Asserting the resulting digests match is not enough on its own: tar stores
98+
* mtime at one-second granularity, so two builds in the same second agree even when nothing
99+
* is pinned at all.
100+
*/
101+
@Test
102+
void everyEntryHasItsVaryingMetadataPinned(@TempDir Path tmp) throws IOException {
103+
Layer layer = LayerBuilder.fromDirectory(
104+
launcherLikeTree(tmp), "/opt/spisor/launcher", tmp.resolve("layer.tar.gz"));
105+
106+
TarArchiveInputStream tar = openTar(layer.file());
107+
try {
108+
int entries = 0;
109+
TarArchiveEntry entry;
110+
while ((entry = tar.getNextTarEntry()) != null) {
111+
entries++;
112+
assertEquals(0L, entry.getModTime().getTime(), entry.getName() + " mtime");
113+
assertEquals(0, entry.getLongUserId(), entry.getName() + " uid");
114+
assertEquals(0, entry.getLongGroupId(), entry.getName() + " gid");
115+
assertEquals("", entry.getUserName(), entry.getName() + " user name");
116+
assertEquals("", entry.getGroupName(), entry.getName() + " group name");
117+
}
118+
assertTrue(entries > 0, "expected entries in the layer");
119+
} finally {
120+
tar.close();
121+
}
122+
}
123+
95124
/**
96125
* @implNote {@link Digests#sha256(InputStream)} does not close its argument; on Windows a
97126
* lingering open handle on the layer file blocks {@code @TempDir} cleanup after the test.

0 commit comments

Comments
 (0)