From 52864050455783a2e506c1cb3adc7e751de8697f Mon Sep 17 00:00:00 2001 From: Damien Diederen Date: Sat, 6 Mar 2021 19:15:28 +0100 Subject: [PATCH] ZOOKEEPER-4232: Ensure that ZOOKEEPER-367 test data fails to parse --- .../zookeeper/test/InvalidSnapshotTest.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java index 0437555bff3..7fe69f0a682 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java @@ -21,6 +21,7 @@ import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; import java.io.File; +import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.zookeeper.PortAssignment; import org.apache.zookeeper.ZKTestCase; @@ -75,7 +76,25 @@ public void testSnapshotFormatterWithNull() throws Exception { String[] args = {snapfile.getCanonicalFile().toString()}; SnapshotFormatter.main(args); } - + + /** + * Verify the SnapshotFormatter fails as expected on corrupted snapshot. + */ + @Test + public void testSnapshotFormatterWithInvalidSnap() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + // Broken snapshot introduced by ZOOKEEPER-367, and used to + // demonstrate recovery in testSnapshot below. + File snapfile = new File(new File(snapDir, "version-2"), "snapshot.83f"); + String[] args = {snapfile.getCanonicalFile().toString()}; + try { + SnapshotFormatter.main(args); + Assert.fail("Snapshot '" + snapfile + "' unexpectedly parsed without error."); + } catch (IOException e) { + Assert.assertTrue(e.getMessage().contains("Unreasonable length = 977468229")); + } + } + /** * test the snapshot * @throws Exception an exception could be expected