Skip to content

Commit 1488506

Browse files
committed
Add tests for gettxoutsetinfo, CLevelDBBatch, CLevelDBIterator
Thanks @dexX7.
1 parent 0fdf8c8 commit 1488506

File tree

3 files changed

+140
-8
lines changed

3 files changed

+140
-8
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
buildDir = BUILDDIR
3939
os.environ["BITCOIND"] = buildDir + '/src/bitcoind' + EXEEXT
4040
os.environ["BITCOINCLI"] = buildDir + '/src/bitcoin-cli' + EXEEXT
41-
41+
4242
#Disable Windows tests by default
4343
if EXEEXT == ".exe" and "-win" not in opts:
4444
print "Win tests currently disabled. Use -win option to enable"
@@ -67,6 +67,7 @@
6767
'reindex.py',
6868
'decodescript.py',
6969
'p2p-fullblocktest.py',
70+
'blockchain.py',
7071
]
7172
testScriptsExt = [
7273
'bipdersig-p2p.py',
@@ -98,20 +99,20 @@
9899
rpcTestDir = buildDir + '/qa/rpc-tests/'
99100
#Run Tests
100101
for i in range(len(testScripts)):
101-
if (len(opts) == 0 or (len(opts) == 1 and "-win" in opts ) or '-extended' in opts
102+
if (len(opts) == 0 or (len(opts) == 1 and "-win" in opts ) or '-extended' in opts
102103
or testScripts[i] in opts or re.sub(".py$", "", testScripts[i]) in opts ):
103-
print "Running testscript " + testScripts[i] + "..."
104-
subprocess.call(rpcTestDir + testScripts[i] + " --srcdir " + buildDir + '/src ' + passOn,shell=True)
104+
print "Running testscript " + testScripts[i] + "..."
105+
subprocess.call(rpcTestDir + testScripts[i] + " --srcdir " + buildDir + '/src ' + passOn,shell=True)
105106
#exit if help is called so we print just one set of instructions
106107
p = re.compile(" -h| --help")
107108
if p.match(passOn):
108109
sys.exit(0)
109110

110111
#Run Extended Tests
111112
for i in range(len(testScriptsExt)):
112-
if ('-extended' in opts or testScriptsExt[i] in opts
113+
if ('-extended' in opts or testScriptsExt[i] in opts
113114
or re.sub(".py$", "", testScriptsExt[i]) in opts):
114-
print "Running 2nd level testscript " + testScriptsExt[i] + "..."
115-
subprocess.call(rpcTestDir + testScriptsExt[i] + " --srcdir " + buildDir + '/src ' + passOn,shell=True)
115+
print "Running 2nd level testscript " + testScriptsExt[i] + "..."
116+
subprocess.call(rpcTestDir + testScriptsExt[i] + " --srcdir " + buildDir + '/src ' + passOn,shell=True)
116117
else:
117118
print "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"

qa/rpc-tests/blockchain.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2014 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# Test RPC calls related to blockchain state.
8+
#
9+
10+
import decimal
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import (
14+
initialize_chain,
15+
assert_equal,
16+
start_nodes,
17+
connect_nodes_bi,
18+
)
19+
20+
class BlockchainTest(BitcoinTestFramework):
21+
"""
22+
Test blockchain-related RPC calls:
23+
24+
- gettxoutsetinfo
25+
26+
"""
27+
28+
def setup_chain(self):
29+
print("Initializing test directory " + self.options.tmpdir)
30+
initialize_chain(self.options.tmpdir)
31+
32+
def setup_network(self, split=False):
33+
self.nodes = start_nodes(2, self.options.tmpdir)
34+
connect_nodes_bi(self.nodes, 0, 1)
35+
self.is_network_split = False
36+
self.sync_all()
37+
38+
def run_test(self):
39+
node = self.nodes[0]
40+
res = node.gettxoutsetinfo()
41+
42+
assert_equal(res[u'total_amount'], decimal.Decimal('8725.00000000'))
43+
assert_equal(res[u'transactions'], 200)
44+
assert_equal(res[u'height'], 200)
45+
assert_equal(res[u'txouts'], 200)
46+
assert_equal(res[u'bytes_serialized'], 13000),
47+
assert_equal(len(res[u'bestblock']), 64)
48+
assert_equal(len(res[u'hash_serialized']), 64)
49+
50+
51+
if __name__ == '__main__':
52+
BlockchainTest().main()

src/test/leveldbwrapper_tests.cpp

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,86 @@ BOOST_AUTO_TEST_CASE(leveldbwrapper)
4646
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
4747
}
4848
}
49-
49+
50+
// Test batch operations
51+
BOOST_AUTO_TEST_CASE(leveldbwrapper_batch)
52+
{
53+
// Perform tests both obfuscated and non-obfuscated.
54+
for (int i = 0; i < 2; i++) {
55+
bool obfuscate = (bool)i;
56+
path ph = temp_directory_path() / unique_path();
57+
CLevelDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
58+
59+
char key = 'i';
60+
uint256 in = GetRandHash();
61+
char key2 = 'j';
62+
uint256 in2 = GetRandHash();
63+
char key3 = 'k';
64+
uint256 in3 = GetRandHash();
65+
66+
uint256 res;
67+
CLevelDBBatch batch(dbw.GetObfuscateKey());
68+
69+
batch.Write(key, in);
70+
batch.Write(key2, in2);
71+
batch.Write(key3, in3);
72+
73+
// Remove key3 before it's even been written
74+
batch.Erase(key3);
75+
76+
dbw.WriteBatch(batch);
77+
78+
BOOST_CHECK(dbw.Read(key, res));
79+
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
80+
BOOST_CHECK(dbw.Read(key2, res));
81+
BOOST_CHECK_EQUAL(res.ToString(), in2.ToString());
82+
83+
// key3 never should've been written
84+
BOOST_CHECK(dbw.Read(key3, res) == false);
85+
}
86+
}
87+
88+
BOOST_AUTO_TEST_CASE(leveldbwrapper_iterator)
89+
{
90+
// Perform tests both obfuscated and non-obfuscated.
91+
for (int i = 0; i < 2; i++) {
92+
bool obfuscate = (bool)i;
93+
path ph = temp_directory_path() / unique_path();
94+
CLevelDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
95+
96+
// The two keys are intentionally chosen for ordering
97+
char key = 'j';
98+
uint256 in = GetRandHash();
99+
BOOST_CHECK(dbw.Write(key, in));
100+
char key2 = 'k';
101+
uint256 in2 = GetRandHash();
102+
BOOST_CHECK(dbw.Write(key2, in2));
103+
104+
boost::scoped_ptr<CLevelDBIterator> it(const_cast<CLevelDBWrapper*>(&dbw)->NewIterator());
105+
106+
// Be sure to seek past the obfuscation key (if it exists)
107+
it->Seek(key);
108+
109+
char key_res;
110+
uint256 val_res;
111+
112+
it->GetKey(key_res);
113+
it->GetValue(val_res);
114+
BOOST_CHECK_EQUAL(key_res, key);
115+
BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
116+
117+
it->Next();
118+
119+
it->GetKey(key_res);
120+
it->GetValue(val_res);
121+
BOOST_CHECK_EQUAL(key_res, key2);
122+
BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
123+
124+
it->Next();
125+
BOOST_CHECK_EQUAL(it->Valid(), false);
126+
}
127+
}
128+
50129
// Test that we do not obfuscation if there is existing data.
51130
BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
52131
{

0 commit comments

Comments
 (0)