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 7de4c175f47..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,7 +21,8 @@ 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; import org.apache.zookeeper.ZooKeeper; @@ -75,14 +76,37 @@ 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 */ @Test public void testSnapshot() throws Exception { - File snapDir = new File(testData, "invalidsnap"); + File origSnapDir = new File(testData, "invalidsnap"); + + // This test otherwise updates the resources directory. + File snapDir = ClientBase.createTmpDir(); + FileUtils.copyDirectory(origSnapDir, snapDir); + ZooKeeperServer zks = new ZooKeeperServer(snapDir, snapDir, 3000); SyncRequestProcessor.setSnapCount(1000); final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);