[tests] fixup dbcrash interaction with add_nodes()
[bitcoinplatinum.git] / src / test / torcontrol_tests.cpp
blobb7affaacde585dae3c395359ecc30364ea88a40f
1 // Copyright (c) 2017 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 //
5 #include "test/test_bitcoin.h"
6 #include "torcontrol.cpp"
8 #include <boost/test/unit_test.hpp>
11 BOOST_FIXTURE_TEST_SUITE(torcontrol_tests, BasicTestingSetup)
13 void CheckSplitTorReplyLine(std::string input, std::string command, std::string args)
15 BOOST_TEST_MESSAGE(std::string("CheckSplitTorReplyLine(") + input + ")");
16 auto ret = SplitTorReplyLine(input);
17 BOOST_CHECK_EQUAL(ret.first, command);
18 BOOST_CHECK_EQUAL(ret.second, args);
21 BOOST_AUTO_TEST_CASE(util_SplitTorReplyLine)
23 // Data we should receive during normal usage
24 CheckSplitTorReplyLine(
25 "PROTOCOLINFO PIVERSION",
26 "PROTOCOLINFO", "PIVERSION");
27 CheckSplitTorReplyLine(
28 "AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"",
29 "AUTH", "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"");
30 CheckSplitTorReplyLine(
31 "AUTH METHODS=NULL",
32 "AUTH", "METHODS=NULL");
33 CheckSplitTorReplyLine(
34 "AUTH METHODS=HASHEDPASSWORD",
35 "AUTH", "METHODS=HASHEDPASSWORD");
36 CheckSplitTorReplyLine(
37 "VERSION Tor=\"0.2.9.8 (git-a0df013ea241b026)\"",
38 "VERSION", "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"");
39 CheckSplitTorReplyLine(
40 "AUTHCHALLENGE SERVERHASH=aaaa SERVERNONCE=bbbb",
41 "AUTHCHALLENGE", "SERVERHASH=aaaa SERVERNONCE=bbbb");
43 // Other valid inputs
44 CheckSplitTorReplyLine("COMMAND", "COMMAND", "");
45 CheckSplitTorReplyLine("COMMAND SOME ARGS", "COMMAND", "SOME ARGS");
47 // These inputs are valid because PROTOCOLINFO accepts an OtherLine that is
48 // just an OptArguments, which enables multiple spaces to be present
49 // between the command and arguments.
50 CheckSplitTorReplyLine("COMMAND ARGS", "COMMAND", " ARGS");
51 CheckSplitTorReplyLine("COMMAND EVEN+more ARGS", "COMMAND", " EVEN+more ARGS");
54 void CheckParseTorReplyMapping(std::string input, std::map<std::string,std::string> expected)
56 BOOST_TEST_MESSAGE(std::string("CheckParseTorReplyMapping(") + input + ")");
57 auto ret = ParseTorReplyMapping(input);
58 BOOST_CHECK_EQUAL(ret.size(), expected.size());
59 auto r_it = ret.begin();
60 auto e_it = expected.begin();
61 while (r_it != ret.end() && e_it != expected.end()) {
62 BOOST_CHECK_EQUAL(r_it->first, e_it->first);
63 BOOST_CHECK_EQUAL(r_it->second, e_it->second);
64 r_it++;
65 e_it++;
69 BOOST_AUTO_TEST_CASE(util_ParseTorReplyMapping)
71 // Data we should receive during normal usage
72 CheckParseTorReplyMapping(
73 "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"", {
74 {"METHODS", "COOKIE,SAFECOOKIE"},
75 {"COOKIEFILE", "/home/x/.tor/control_auth_cookie"},
76 });
77 CheckParseTorReplyMapping(
78 "METHODS=NULL", {
79 {"METHODS", "NULL"},
80 });
81 CheckParseTorReplyMapping(
82 "METHODS=HASHEDPASSWORD", {
83 {"METHODS", "HASHEDPASSWORD"},
84 });
85 CheckParseTorReplyMapping(
86 "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"", {
87 {"Tor", "0.2.9.8 (git-a0df013ea241b026)"},
88 });
89 CheckParseTorReplyMapping(
90 "SERVERHASH=aaaa SERVERNONCE=bbbb", {
91 {"SERVERHASH", "aaaa"},
92 {"SERVERNONCE", "bbbb"},
93 });
94 CheckParseTorReplyMapping(
95 "ServiceID=exampleonion1234", {
96 {"ServiceID", "exampleonion1234"},
97 });
98 CheckParseTorReplyMapping(
99 "PrivateKey=RSA1024:BLOB", {
100 {"PrivateKey", "RSA1024:BLOB"},
102 CheckParseTorReplyMapping(
103 "ClientAuth=bob:BLOB", {
104 {"ClientAuth", "bob:BLOB"},
107 // Other valid inputs
108 CheckParseTorReplyMapping(
109 "Foo=Bar=Baz Spam=Eggs", {
110 {"Foo", "Bar=Baz"},
111 {"Spam", "Eggs"},
113 CheckParseTorReplyMapping(
114 "Foo=\"Bar=Baz\"", {
115 {"Foo", "Bar=Baz"},
117 CheckParseTorReplyMapping(
118 "Foo=\"Bar Baz\"", {
119 {"Foo", "Bar Baz"},
122 // Escapes
123 CheckParseTorReplyMapping(
124 "Foo=\"Bar\\ Baz\"", {
125 {"Foo", "Bar Baz"},
127 CheckParseTorReplyMapping(
128 "Foo=\"Bar\\Baz\"", {
129 {"Foo", "BarBaz"},
131 CheckParseTorReplyMapping(
132 "Foo=\"Bar\\@Baz\"", {
133 {"Foo", "Bar@Baz"},
135 CheckParseTorReplyMapping(
136 "Foo=\"Bar\\\"Baz\" Spam=\"\\\"Eggs\\\"\"", {
137 {"Foo", "Bar\"Baz"},
138 {"Spam", "\"Eggs\""},
140 CheckParseTorReplyMapping(
141 "Foo=\"Bar\\\\Baz\"", {
142 {"Foo", "Bar\\Baz"},
145 // C escapes
146 CheckParseTorReplyMapping(
147 "Foo=\"Bar\\nBaz\\t\" Spam=\"\\rEggs\" Octals=\"\\1a\\11\\17\\18\\81\\377\\378\\400\\2222\" Final=Check", {
148 {"Foo", "Bar\nBaz\t"},
149 {"Spam", "\rEggs"},
150 {"Octals", "\1a\11\17\1" "881\377\37" "8\40" "0\222" "2"},
151 {"Final", "Check"},
153 CheckParseTorReplyMapping(
154 "Valid=Mapping Escaped=\"Escape\\\\\"", {
155 {"Valid", "Mapping"},
156 {"Escaped", "Escape\\"},
158 CheckParseTorReplyMapping(
159 "Valid=Mapping Bare=\"Escape\\\"", {});
160 CheckParseTorReplyMapping(
161 "OneOctal=\"OneEnd\\1\" TwoOctal=\"TwoEnd\\11\"", {
162 {"OneOctal", "OneEnd\1"},
163 {"TwoOctal", "TwoEnd\11"},
166 // Special handling for null case
167 // (needed because string comparison reads the null as end-of-string)
168 BOOST_TEST_MESSAGE(std::string("CheckParseTorReplyMapping(Null=\"\\0\")"));
169 auto ret = ParseTorReplyMapping("Null=\"\\0\"");
170 BOOST_CHECK_EQUAL(ret.size(), 1);
171 auto r_it = ret.begin();
172 BOOST_CHECK_EQUAL(r_it->first, "Null");
173 BOOST_CHECK_EQUAL(r_it->second.size(), 1);
174 BOOST_CHECK_EQUAL(r_it->second[0], '\0');
176 // A more complex valid grammar. PROTOCOLINFO accepts a VersionLine that
177 // takes a key=value pair followed by an OptArguments, making this valid.
178 // Because an OptArguments contains no semantic data, there is no point in
179 // parsing it.
180 CheckParseTorReplyMapping(
181 "SOME=args,here MORE optional=arguments here", {
182 {"SOME", "args,here"},
185 // Inputs that are effectively invalid under the target grammar.
186 // PROTOCOLINFO accepts an OtherLine that is just an OptArguments, which
187 // would make these inputs valid. However,
188 // - This parser is never used in that situation, because the
189 // SplitTorReplyLine parser enables OtherLine to be skipped.
190 // - Even if these were valid, an OptArguments contains no semantic data,
191 // so there is no point in parsing it.
192 CheckParseTorReplyMapping("ARGS", {});
193 CheckParseTorReplyMapping("MORE ARGS", {});
194 CheckParseTorReplyMapping("MORE ARGS", {});
195 CheckParseTorReplyMapping("EVEN more=ARGS", {});
196 CheckParseTorReplyMapping("EVEN+more ARGS", {});
199 BOOST_AUTO_TEST_SUITE_END()