Bitcoin Core 28.99.0
P2P Digital Currency
torcontrol_tests.cpp
Go to the documentation of this file.
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
6#include <boost/test/unit_test.hpp>
7
8#include <map>
9#include <string>
10#include <utility>
11
12
13std::pair<std::string, std::string> SplitTorReplyLine(const std::string& s);
14std::map<std::string, std::string> ParseTorReplyMapping(const std::string& s);
15
16
17BOOST_AUTO_TEST_SUITE(torcontrol_tests)
18
19static void CheckSplitTorReplyLine(std::string input, std::string command, std::string args)
20{
21 auto ret = SplitTorReplyLine(input);
23 BOOST_CHECK_EQUAL(ret.second, args);
24}
25
26BOOST_AUTO_TEST_CASE(util_SplitTorReplyLine)
27{
28 // Data we should receive during normal usage
30 "PROTOCOLINFO PIVERSION",
31 "PROTOCOLINFO", "PIVERSION");
33 "AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"",
34 "AUTH", "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"");
36 "AUTH METHODS=NULL",
37 "AUTH", "METHODS=NULL");
39 "AUTH METHODS=HASHEDPASSWORD",
40 "AUTH", "METHODS=HASHEDPASSWORD");
42 "VERSION Tor=\"0.2.9.8 (git-a0df013ea241b026)\"",
43 "VERSION", "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"");
45 "AUTHCHALLENGE SERVERHASH=aaaa SERVERNONCE=bbbb",
46 "AUTHCHALLENGE", "SERVERHASH=aaaa SERVERNONCE=bbbb");
47
48 // Other valid inputs
49 CheckSplitTorReplyLine("COMMAND", "COMMAND", "");
50 CheckSplitTorReplyLine("COMMAND SOME ARGS", "COMMAND", "SOME ARGS");
51
52 // These inputs are valid because PROTOCOLINFO accepts an OtherLine that is
53 // just an OptArguments, which enables multiple spaces to be present
54 // between the command and arguments.
55 CheckSplitTorReplyLine("COMMAND ARGS", "COMMAND", " ARGS");
56 CheckSplitTorReplyLine("COMMAND EVEN+more ARGS", "COMMAND", " EVEN+more ARGS");
57}
58
59static void CheckParseTorReplyMapping(std::string input, std::map<std::string,std::string> expected)
60{
61 auto ret = ParseTorReplyMapping(input);
62 BOOST_CHECK_EQUAL(ret.size(), expected.size());
63 auto r_it = ret.begin();
64 auto e_it = expected.begin();
65 while (r_it != ret.end() && e_it != expected.end()) {
66 BOOST_CHECK_EQUAL(r_it->first, e_it->first);
67 BOOST_CHECK_EQUAL(r_it->second, e_it->second);
68 r_it++;
69 e_it++;
70 }
71}
72
73BOOST_AUTO_TEST_CASE(util_ParseTorReplyMapping)
74{
75 // Data we should receive during normal usage
77 "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"", {
78 {"METHODS", "COOKIE,SAFECOOKIE"},
79 {"COOKIEFILE", "/home/x/.tor/control_auth_cookie"},
80 });
82 "METHODS=NULL", {
83 {"METHODS", "NULL"},
84 });
86 "METHODS=HASHEDPASSWORD", {
87 {"METHODS", "HASHEDPASSWORD"},
88 });
90 "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"", {
91 {"Tor", "0.2.9.8 (git-a0df013ea241b026)"},
92 });
94 "SERVERHASH=aaaa SERVERNONCE=bbbb", {
95 {"SERVERHASH", "aaaa"},
96 {"SERVERNONCE", "bbbb"},
97 });
99 "ServiceID=exampleonion1234", {
100 {"ServiceID", "exampleonion1234"},
101 });
103 "PrivateKey=RSA1024:BLOB", {
104 {"PrivateKey", "RSA1024:BLOB"},
105 });
107 "ClientAuth=bob:BLOB", {
108 {"ClientAuth", "bob:BLOB"},
109 });
110
111 // Other valid inputs
113 "Foo=Bar=Baz Spam=Eggs", {
114 {"Foo", "Bar=Baz"},
115 {"Spam", "Eggs"},
116 });
118 "Foo=\"Bar=Baz\"", {
119 {"Foo", "Bar=Baz"},
120 });
122 "Foo=\"Bar Baz\"", {
123 {"Foo", "Bar Baz"},
124 });
125
126 // Escapes
128 "Foo=\"Bar\\ Baz\"", {
129 {"Foo", "Bar Baz"},
130 });
132 "Foo=\"Bar\\Baz\"", {
133 {"Foo", "BarBaz"},
134 });
136 "Foo=\"Bar\\@Baz\"", {
137 {"Foo", "Bar@Baz"},
138 });
140 "Foo=\"Bar\\\"Baz\" Spam=\"\\\"Eggs\\\"\"", {
141 {"Foo", "Bar\"Baz"},
142 {"Spam", "\"Eggs\""},
143 });
145 "Foo=\"Bar\\\\Baz\"", {
146 {"Foo", "Bar\\Baz"},
147 });
148
149 // C escapes
151 "Foo=\"Bar\\nBaz\\t\" Spam=\"\\rEggs\" Octals=\"\\1a\\11\\17\\18\\81\\377\\378\\400\\2222\" Final=Check", {
152 {"Foo", "Bar\nBaz\t"},
153 {"Spam", "\rEggs"},
154 {"Octals", "\1a\11\17\1" "881\377\37" "8\40" "0\222" "2"},
155 {"Final", "Check"},
156 });
158 "Valid=Mapping Escaped=\"Escape\\\\\"", {
159 {"Valid", "Mapping"},
160 {"Escaped", "Escape\\"},
161 });
163 "Valid=Mapping Bare=\"Escape\\\"", {});
165 "OneOctal=\"OneEnd\\1\" TwoOctal=\"TwoEnd\\11\"", {
166 {"OneOctal", "OneEnd\1"},
167 {"TwoOctal", "TwoEnd\11"},
168 });
169
170 // Special handling for null case
171 // (needed because string comparison reads the null as end-of-string)
172 auto ret = ParseTorReplyMapping("Null=\"\\0\"");
173 BOOST_CHECK_EQUAL(ret.size(), 1U);
174 auto r_it = ret.begin();
175 BOOST_CHECK_EQUAL(r_it->first, "Null");
176 BOOST_CHECK_EQUAL(r_it->second.size(), 1U);
177 BOOST_CHECK_EQUAL(r_it->second[0], '\0');
178
179 // A more complex valid grammar. PROTOCOLINFO accepts a VersionLine that
180 // takes a key=value pair followed by an OptArguments, making this valid.
181 // Because an OptArguments contains no semantic data, there is no point in
182 // parsing it.
184 "SOME=args,here MORE optional=arguments here", {
185 {"SOME", "args,here"},
186 });
187
188 // Inputs that are effectively invalid under the target grammar.
189 // PROTOCOLINFO accepts an OtherLine that is just an OptArguments, which
190 // would make these inputs valid. However,
191 // - This parser is never used in that situation, because the
192 // SplitTorReplyLine parser enables OtherLine to be skipped.
193 // - Even if these were valid, an OptArguments contains no semantic data,
194 // so there is no point in parsing it.
195 CheckParseTorReplyMapping("ARGS", {});
196 CheckParseTorReplyMapping("MORE ARGS", {});
197 CheckParseTorReplyMapping("MORE ARGS", {});
198 CheckParseTorReplyMapping("EVEN more=ARGS", {});
199 CheckParseTorReplyMapping("EVEN+more ARGS", {});
200}
201
int ret
const auto command
ArgsManager & args
Definition: bitcoind.cpp:277
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
std::pair< std::string, std::string > SplitTorReplyLine(const std::string &s)
Definition: torcontrol.cpp:209
std::map< std::string, std::string > ParseTorReplyMapping(const std::string &s)
Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'.
Definition: torcontrol.cpp:228
BOOST_AUTO_TEST_CASE(util_SplitTorReplyLine)
static void CheckSplitTorReplyLine(std::string input, std::string command, std::string args)
static void CheckParseTorReplyMapping(std::string input, std::map< std::string, std::string > expected)