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