aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCJentzsch <jentzsch.software@gmail.com>2015-03-02 21:34:45 +0800
committerGav Wood <i@gavwood.com>2015-03-06 19:19:45 +0800
commit5236bdfd455f32ef139ab40c9f7424c4ef19f95e (patch)
tree2b690a89fa26224d6d4e763e4e37454d6087868e
parent83479e96e97869e53013ded64872fdf26d1e3ac3 (diff)
downloaddexon-solidity-5236bdfd455f32ef139ab40c9f7424c4ef19f95e.tar.gz
dexon-solidity-5236bdfd455f32ef139ab40c9f7424c4ef19f95e.tar.zst
dexon-solidity-5236bdfd455f32ef139ab40c9f7424c4ef19f95e.zip
add secure trie tests
-rw-r--r--trie.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/trie.cpp b/trie.cpp
index bdb18865..8a259821 100644
--- a/trie.cpp
+++ b/trie.cpp
@@ -196,6 +196,48 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder)
}
}
+BOOST_AUTO_TEST_CASE(trie_test_anyorder_secureTrie)
+{
+ string testPath = test::getTestPath();
+
+ testPath += "/TrieTests";
+
+ cnote << "Testing Trie...";
+ js::mValue v;
+ string s = asString(contents(testPath + "/trieanyorder_secureTrie.json"));
+ BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?");
+ js::read_string(s, v);
+ for (auto& i: v.get_obj())
+ {
+ cnote << i.first;
+ js::mObject& o = i.second.get_obj();
+ vector<pair<string, string>> ss;
+ for (auto i: o["in"].get_obj())
+ {
+ ss.push_back(make_pair(i.first, i.second.get_str()));
+ if (!ss.back().first.find("0x"))
+ ss.back().first = asString(fromHex(ss.back().first.substr(2)));
+ if (!ss.back().second.find("0x"))
+ ss.back().second = asString(fromHex(ss.back().second.substr(2)));
+ }
+ for (unsigned j = 0; j < min(1000u, dev::test::fac((unsigned)ss.size())); ++j)
+ {
+ next_permutation(ss.begin(), ss.end());
+ MemoryDB m;
+ SecureGenericTrieDB<MemoryDB> t(&m);
+ t.init();
+ BOOST_REQUIRE(t.check(true));
+ for (auto const& k: ss)
+ {
+ t.insert(k.first, k.second);
+ BOOST_REQUIRE(t.check(true));
+ }
+ BOOST_REQUIRE(!o["root"].is_null());
+ BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray()));
+ }
+ }
+}
+
BOOST_AUTO_TEST_CASE(trie_tests_ordered)
{
string testPath = test::getTestPath();
@@ -277,6 +319,69 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered)
}
}
+BOOST_AUTO_TEST_CASE(trie_tests_ordered_secureTrie)
+{
+ string testPath = test::getTestPath();
+
+ testPath += "/TrieTests";
+
+ cnote << "Testing Trie...";
+ js::mValue v;
+ string s = asString(contents(testPath + "/trietest_secureTrie.json"));
+ BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?");
+ js::read_string(s, v);
+
+ for (auto& i: v.get_obj())
+ {
+ cnote << i.first;
+ js::mObject& o = i.second.get_obj();
+ vector<pair<string, string>> ss;
+ vector<string> keysToBeDeleted;
+ for (auto& i: o["in"].get_array())
+ {
+ vector<string> values;
+ for (auto& s: i.get_array())
+ {
+ if (s.type() == json_spirit::str_type)
+ values.push_back(s.get_str());
+ else if (s.type() == json_spirit::null_type)
+ {
+ // mark entry for deletion
+ values.push_back("");
+ if (!values[0].find("0x"))
+ values[0] = asString(fromHex(values[0].substr(2)));
+ keysToBeDeleted.push_back(values[0]);
+ }
+ else
+ BOOST_FAIL("Bad type (expected string)");
+ }
+
+ BOOST_REQUIRE(values.size() == 2);
+ ss.push_back(make_pair(values[0], values[1]));
+ if (!ss.back().first.find("0x"))
+ ss.back().first = asString(fromHex(ss.back().first.substr(2)));
+ if (!ss.back().second.find("0x"))
+ ss.back().second = asString(fromHex(ss.back().second.substr(2)));
+ }
+
+ MemoryDB m;
+ SecureGenericTrieDB<MemoryDB> t(&m);
+ t.init();
+ BOOST_REQUIRE(t.check(true));
+ for (auto const& k: ss)
+ {
+ if (find(keysToBeDeleted.begin(), keysToBeDeleted.end(), k.first) != keysToBeDeleted.end() && k.second.empty())
+ t.remove(k.first);
+ else
+ t.insert(k.first, k.second);
+ BOOST_REQUIRE(t.check(true));
+ }
+
+ BOOST_REQUIRE(!o["root"].is_null());
+ BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray()));
+ }
+}
+
inline h256 stringMapHash256(StringMap const& _s)
{
return hash256(_s);