garydgregory commented on code in PR #627:
URL: https://github.com/apache/commons-compress/pull/627#discussion_r1894650358
##########
src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java:
##########
@@ -214,4 +220,57 @@ public void testFileNameChinesePercentEncoded() throws
IOException {
testFileName("??????.xml", EXPECTED_FILE_NAME);
}
+
+ /**
+ * Tests the gzip header CRC.
+ *
+ * @throws IOException When the test has issues with the underlying file
system or unexpected gzip operations.
+ */
+ @Test
+ public void testHcrc() throws IOException, DecoderException {
+ final GzipParameters parameters = new GzipParameters();
+ parameters.setHcrc(true);
+ parameters.setModificationTime(0x66554433); // avoid changing time
+ parameters.setFileName("AAAA");
+ parameters.setComment("ZZZZ");
+ parameters.setOS(OS.UNIX);
+ final ExtraField extra = new ExtraField();
+ extra.addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1));
+ parameters.setExtraField(extra);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (GzipCompressorOutputStream gos = new
GzipCompressorOutputStream(baos, parameters)) {
+ // no thing to write for this test.
+ }
+ byte[] result = baos.toByteArray();
+ byte[] expected = Hex.decodeHex("1f8b" // id1 id2
+ + "08" //cm
+ + "1e" //flg(FEXTRA|FNAME|FCOMMENT|FHCRC)
+ + "33445566" // mtime little endian
+ + "00" + "03" // xfl os
+ + "0800" + "4242" + "0400" + "43434343" //xlen sfid sflen
"CCCC"
+ + "4141414100" // "AAAA" with \0
+ + "5a5a5a5a00" // "ZZZZ" with \0
+ + "d842" //crc32 = 839242d8
+ + "0300" // empty deflate stream
+ + "00000000" // crs32
+ + "00000000" // isize
+ );
+ assertArrayEquals(expected, result);
+ try (GZIPInputStream gis = new GZIPInputStream(new
ByteArrayInputStream(result))) {
+ //if it does not fail, the hcrc is good.
+ }
+ try (GzipCompressorInputStream gis = new GzipCompressorInputStream(new
ByteArrayInputStream(result))) {
+ GzipParameters md = gis.getMetaData();
+ assertTrue(md.hasHcrc());
+ assertEquals(0x66554433, md.getModificationTime());
+ assertEquals(1, md.getExtraField().size());
+ SubField sf = md.getExtraField().iterator().next();
+ assertEquals("BB", sf.getId());
+ assertEquals("CCCC", new String(sf.getPayload(),
StandardCharsets.ISO_8859_1));
+ assertEquals("AAAA", md.getFileName());
+ assertEquals("ZZZZ", md.getComment());
+ assertEquals(OS.UNIX, md.getOS());
Review Comment:
Since the stream now computes the header CRC, add an assertion that it is
correct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]