Himanshu-g81 commented on code in PR #2231:
URL: https://github.com/apache/phoenix/pull/2231#discussion_r2215805528


##########
phoenix-core/src/test/java/org/apache/phoenix/replication/log/LogFileFormatTest.java:
##########
@@ -406,6 +411,68 @@ public void testLogFileCorruptionPartialTrailer() throws 
IOException {
         assertEquals("Records read count mismatch", totalRecords, 
readerContext.getRecordsRead());
     }
 
+    @Test(expected=InvalidLogHeaderException.class)
+    public void testFailIfMissingHeader() throws IOException {
+        // Zero length file
+        byte[] data = new byte[0];
+        LogFileTestUtil.SeekableByteArrayInputStream input =
+            new LogFileTestUtil.SeekableByteArrayInputStream(data);
+        readerContext.setFileSize(data.length);
+        readerContext.setValidateTrailer(false);
+        reader.init(readerContext, input);
+        fail("Expected InvalidLogHeaderException for zero length file");
+    }
+
+    @Test(expected=InvalidLogHeaderException.class)
+    public void testFailIfInvalidHeader() throws IOException {
+        initLogFileWriter();
+        writer.close(); // Writes valid trailer
+        byte[] data = writerBaos.toByteArray();
+        LogFileTestUtil.SeekableByteArrayInputStream input =
+            new LogFileTestUtil.SeekableByteArrayInputStream(data);
+        readerContext.setFileSize(data.length);
+        readerContext.setValidateTrailer(true);
+        data[0] = (byte) 'X'; // Corrupt the first magic byte
+        reader.init(readerContext, input);
+        fail("Expected InvalidLogHeaderException for file with corrupted 
header magic");
+    }
+
+    @Test(expected=InvalidLogTrailerException.class)
+    public void testFailIfMissingTrailer() throws IOException {
+        initLogFileWriter();
+        writeBlock(writer, "B1", 0, 5);
+        // Don't close the writer, simulate missing trailer
+        byte[] data = writerBaos.toByteArray();
+        // Re-initialize reader with truncated data and trailer validation 
enabled
+        LogFileTestUtil.SeekableByteArrayInputStream input =
+            new LogFileTestUtil.SeekableByteArrayInputStream(data);
+        readerContext.setFileSize(data.length);
+        // Enable trailer validation
+        readerContext.setValidateTrailer(true);
+        reader.init(readerContext, input);
+        fail("Expected InvalidLogTrailerException when trailer is missing");
+    }
+
+    @Test(expected=InvalidLogTrailerException.class)
+    public void testFailIfInvalidTrailer() throws IOException {
+        initLogFileWriter();
+        writeBlock(writer, "B1", 0, 5);
+        writer.close(); // Writes valid trailer
+        byte[] data = writerBaos.toByteArray();
+        // Corrupt the trailer by changing the magic bytes
+        int trailerStartOffset = data.length - 
LogFileTrailer.FIXED_TRAILER_SIZE;
+        int magicOffset = trailerStartOffset + 
LogFileTrailer.FIXED_TRAILER_SIZE
+            - LogFileHeader.MAGIC.length;
+        data[magicOffset] = (byte) 'X'; // Corrupt the first magic byte
+        // Re-initialize reader with corrupted trailer and trailer validation 
enabled
+        LogFileTestUtil.SeekableByteArrayInputStream input =
+            new LogFileTestUtil.SeekableByteArrayInputStream(data);
+        readerContext.setFileSize(data.length);
+        readerContext.setValidateTrailer(true);
+        reader.init(readerContext, input);

Review Comment:
   nit: Should we validate the exception message also, i.e. Short Magic / Bad 
Magic / Unsupported version, etc?



-- 
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: issues-unsubscr...@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to