提交 121598c2 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: improved detection of the wrong encryption key (or file corruption)

上级 53fd89a8
......@@ -592,6 +592,11 @@ public class DataUtils {
while (i < size) {
c = s.charAt(i++);
if (c == '\\') {
if (i == size) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT,
"Not a map: {0}", s);
}
c = s.charAt(i++);
} else if (c == '\"') {
break;
......
......@@ -10,6 +10,8 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import org.h2.mvstore.DataUtils;
import org.h2.test.TestBase;
......@@ -32,6 +34,7 @@ public class TestDataUtils extends TestBase {
testEncodeLength();
testFletcher();
testMap();
testMapRandomized();
testMaxShortVarIntVarLong();
testVarIntVarLong();
testCheckValue();
......@@ -79,6 +82,24 @@ public class TestDataUtils extends TestBase {
assertEquals("\"test\"", m.get("d"));
}
private void testMapRandomized() {
Random r = new Random(1);
String chars = "a_1,\\\":";
for (int i = 0; i < 1000; i++) {
StringBuilder buff = new StringBuilder();
for (int j = 0; j < 20; j++) {
buff.append(chars.charAt(r.nextInt(chars.length())));
}
try {
HashMap<String, String> map = DataUtils.parseMap(buff.toString());
assertFalse(map == null);
// ok
} catch (IllegalStateException e) {
// ok - but not another exception
}
}
}
private void testMaxShortVarIntVarLong() {
ByteBuffer buff = ByteBuffer.allocate(100);
DataUtils.writeVarInt(buff, DataUtils.COMPRESSED_VAR_INT_MAX);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论