LCOV - code coverage report
Current view: top level - src/test - torcontrol_tests.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 126 126 100.0 %
Date: 2026-06-25 07:23:51 Functions: 11 11 100.0 %

          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             : 
       6             : #include <boost/test/unit_test.hpp>
       7             : 
       8             : #include <map>
       9             : #include <string>
      10             : #include <utility>
      11             : 
      12             : 
      13             : std::pair<std::string, std::string> SplitTorReplyLine(const std::string& s);
      14             : std::map<std::string, std::string> ParseTorReplyMapping(const std::string& s);
      15             : 
      16             : 
      17         146 : BOOST_AUTO_TEST_SUITE(torcontrol_tests)
      18             : 
      19          10 : static void CheckSplitTorReplyLine(std::string input, std::string command, std::string args)
      20             : {
      21          10 :     auto ret = SplitTorReplyLine(input);
      22          10 :     BOOST_CHECK_EQUAL(ret.first, command);
      23          10 :     BOOST_CHECK_EQUAL(ret.second, args);
      24          10 : }
      25             : 
      26         148 : BOOST_AUTO_TEST_CASE(util_SplitTorReplyLine)
      27             : {
      28             :     // Data we should receive during normal usage
      29           1 :     CheckSplitTorReplyLine(
      30           1 :         "PROTOCOLINFO PIVERSION",
      31           1 :         "PROTOCOLINFO", "PIVERSION");
      32           1 :     CheckSplitTorReplyLine(
      33           1 :         "AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"",
      34           1 :         "AUTH", "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"");
      35           1 :     CheckSplitTorReplyLine(
      36           1 :         "AUTH METHODS=NULL",
      37           1 :         "AUTH", "METHODS=NULL");
      38           1 :     CheckSplitTorReplyLine(
      39           1 :         "AUTH METHODS=HASHEDPASSWORD",
      40           1 :         "AUTH", "METHODS=HASHEDPASSWORD");
      41           1 :     CheckSplitTorReplyLine(
      42           1 :         "VERSION Tor=\"0.2.9.8 (git-a0df013ea241b026)\"",
      43           1 :         "VERSION", "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"");
      44           1 :     CheckSplitTorReplyLine(
      45           1 :         "AUTHCHALLENGE SERVERHASH=aaaa SERVERNONCE=bbbb",
      46           1 :         "AUTHCHALLENGE", "SERVERHASH=aaaa SERVERNONCE=bbbb");
      47             : 
      48             :     // Other valid inputs
      49           1 :     CheckSplitTorReplyLine("COMMAND", "COMMAND", "");
      50           1 :     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           1 :     CheckSplitTorReplyLine("COMMAND  ARGS", "COMMAND", " ARGS");
      56           1 :     CheckSplitTorReplyLine("COMMAND   EVEN+more  ARGS", "COMMAND", "  EVEN+more  ARGS");
      57           1 : }
      58             : 
      59          26 : static void CheckParseTorReplyMapping(std::string input, std::map<std::string,std::string> expected)
      60             : {
      61          26 :     auto ret = ParseTorReplyMapping(input);
      62          26 :     BOOST_CHECK_EQUAL(ret.size(), expected.size());
      63          26 :     auto r_it = ret.begin();
      64          26 :     auto e_it = expected.begin();
      65          84 :     while (r_it != ret.end() && e_it != expected.end()) {
      66          29 :         BOOST_CHECK_EQUAL(r_it->first, e_it->first);
      67          29 :         BOOST_CHECK_EQUAL(r_it->second, e_it->second);
      68          29 :         r_it++;
      69          29 :         e_it++;
      70             :     }
      71          26 : }
      72             : 
      73         148 : BOOST_AUTO_TEST_CASE(util_ParseTorReplyMapping)
      74             : {
      75             :     // Data we should receive during normal usage
      76           1 :     CheckParseTorReplyMapping(
      77           1 :         "METHODS=COOKIE,SAFECOOKIE COOKIEFILE=\"/home/x/.tor/control_auth_cookie\"", {
      78           1 :             {"METHODS", "COOKIE,SAFECOOKIE"},
      79           1 :             {"COOKIEFILE", "/home/x/.tor/control_auth_cookie"},
      80             :         });
      81           1 :     CheckParseTorReplyMapping(
      82           1 :         "METHODS=NULL", {
      83           1 :             {"METHODS", "NULL"},
      84             :         });
      85           1 :     CheckParseTorReplyMapping(
      86           1 :         "METHODS=HASHEDPASSWORD", {
      87           1 :             {"METHODS", "HASHEDPASSWORD"},
      88             :         });
      89           1 :     CheckParseTorReplyMapping(
      90           1 :         "Tor=\"0.2.9.8 (git-a0df013ea241b026)\"", {
      91           1 :             {"Tor", "0.2.9.8 (git-a0df013ea241b026)"},
      92             :         });
      93           1 :     CheckParseTorReplyMapping(
      94           1 :         "SERVERHASH=aaaa SERVERNONCE=bbbb", {
      95           1 :             {"SERVERHASH", "aaaa"},
      96           1 :             {"SERVERNONCE", "bbbb"},
      97             :         });
      98           1 :     CheckParseTorReplyMapping(
      99           1 :         "ServiceID=exampleonion1234", {
     100           1 :             {"ServiceID", "exampleonion1234"},
     101             :         });
     102           1 :     CheckParseTorReplyMapping(
     103           1 :         "PrivateKey=RSA1024:BLOB", {
     104           1 :             {"PrivateKey", "RSA1024:BLOB"},
     105             :         });
     106           1 :     CheckParseTorReplyMapping(
     107           1 :         "ClientAuth=bob:BLOB", {
     108           1 :             {"ClientAuth", "bob:BLOB"},
     109             :         });
     110             : 
     111             :     // Other valid inputs
     112           1 :     CheckParseTorReplyMapping(
     113           1 :         "Foo=Bar=Baz Spam=Eggs", {
     114           1 :             {"Foo", "Bar=Baz"},
     115           1 :             {"Spam", "Eggs"},
     116             :         });
     117           1 :     CheckParseTorReplyMapping(
     118           1 :         "Foo=\"Bar=Baz\"", {
     119           1 :             {"Foo", "Bar=Baz"},
     120             :         });
     121           1 :     CheckParseTorReplyMapping(
     122           1 :         "Foo=\"Bar Baz\"", {
     123           1 :             {"Foo", "Bar Baz"},
     124             :         });
     125             : 
     126             :     // Escapes
     127           1 :     CheckParseTorReplyMapping(
     128           1 :         "Foo=\"Bar\\ Baz\"", {
     129           1 :             {"Foo", "Bar Baz"},
     130             :         });
     131           1 :     CheckParseTorReplyMapping(
     132           1 :         "Foo=\"Bar\\Baz\"", {
     133           1 :             {"Foo", "BarBaz"},
     134             :         });
     135           1 :     CheckParseTorReplyMapping(
     136           1 :         "Foo=\"Bar\\@Baz\"", {
     137           1 :             {"Foo", "Bar@Baz"},
     138             :         });
     139           1 :     CheckParseTorReplyMapping(
     140           1 :         "Foo=\"Bar\\\"Baz\" Spam=\"\\\"Eggs\\\"\"", {
     141           1 :             {"Foo", "Bar\"Baz"},
     142           1 :             {"Spam", "\"Eggs\""},
     143             :         });
     144           1 :     CheckParseTorReplyMapping(
     145           1 :         "Foo=\"Bar\\\\Baz\"", {
     146           1 :             {"Foo", "Bar\\Baz"},
     147             :         });
     148             : 
     149             :     // C escapes
     150           1 :     CheckParseTorReplyMapping(
     151           1 :         "Foo=\"Bar\\nBaz\\t\" Spam=\"\\rEggs\" Octals=\"\\1a\\11\\17\\18\\81\\377\\378\\400\\2222\" Final=Check", {
     152           1 :             {"Foo", "Bar\nBaz\t"},
     153           1 :             {"Spam", "\rEggs"},
     154           1 :             {"Octals", "\1a\11\17\1" "881\377\37" "8\40" "0\222" "2"},
     155           1 :             {"Final", "Check"},
     156             :         });
     157           1 :     CheckParseTorReplyMapping(
     158           1 :         "Valid=Mapping Escaped=\"Escape\\\\\"", {
     159           1 :             {"Valid", "Mapping"},
     160           1 :             {"Escaped", "Escape\\"},
     161             :         });
     162           1 :     CheckParseTorReplyMapping(
     163           1 :         "Valid=Mapping Bare=\"Escape\\\"", {});
     164           1 :     CheckParseTorReplyMapping(
     165           1 :         "OneOctal=\"OneEnd\\1\" TwoOctal=\"TwoEnd\\11\"", {
     166           1 :             {"OneOctal", "OneEnd\1"},
     167           1 :             {"TwoOctal", "TwoEnd\11"},
     168             :         });
     169             : 
     170             :     // Special handling for null case
     171             :     // (needed because string comparison reads the null as end-of-string)
     172           1 :     auto ret = ParseTorReplyMapping("Null=\"\\0\"");
     173           1 :     BOOST_CHECK_EQUAL(ret.size(), 1U);
     174           1 :     auto r_it = ret.begin();
     175           1 :     BOOST_CHECK_EQUAL(r_it->first, "Null");
     176           1 :     BOOST_CHECK_EQUAL(r_it->second.size(), 1U);
     177           1 :     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.
     183           1 :     CheckParseTorReplyMapping(
     184           1 :         "SOME=args,here MORE optional=arguments  here", {
     185           1 :             {"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           1 :     CheckParseTorReplyMapping("ARGS", {});
     196           1 :     CheckParseTorReplyMapping("MORE ARGS", {});
     197           1 :     CheckParseTorReplyMapping("MORE  ARGS", {});
     198           1 :     CheckParseTorReplyMapping("EVEN more=ARGS", {});
     199           1 :     CheckParseTorReplyMapping("EVEN+more ARGS", {});
     200           1 : }
     201             : 
     202         146 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.16