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