LCOV - code coverage report
Current view: top level - src/test - getarg_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 296 296
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 19 19
Branches: 48.7 % 2382 1159

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-2022 The Bitcoin Core 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 <common/args.h>
       6                 :             : #include <common/settings.h>
       7                 :             : #include <logging.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <univalue.h>
      10                 :             : #include <util/strencodings.h>
      11                 :             : 
      12                 :             : #include <limits>
      13                 :             : #include <string>
      14                 :             : #include <utility>
      15                 :             : #include <vector>
      16                 :             : 
      17                 :             : #include <boost/test/unit_test.hpp>
      18                 :             : 
      19                 :             : using util::SplitString;
      20                 :             : 
      21                 :             : BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
      22                 :             : 
      23                 :          54 : void ResetArgs(ArgsManager& local_args, const std::string& strArg)
      24                 :             : {
      25                 :          54 :     std::vector<std::string> vecArg;
      26         [ +  + ]:          54 :     if (strArg.size()) {
      27         [ +  - ]:          50 :         vecArg = SplitString(strArg, ' ');
      28                 :             :     }
      29                 :             : 
      30                 :             :     // Insert dummy executable name:
      31   [ +  -  +  - ]:          54 :     vecArg.insert(vecArg.begin(), "testbitcoin");
      32                 :             : 
      33                 :             :     // Convert to char*:
      34                 :          54 :     std::vector<const char*> vecChar;
      35         [ +  - ]:          54 :     vecChar.reserve(vecArg.size());
      36         [ +  + ]:         172 :     for (const std::string& s : vecArg)
      37         [ +  - ]:         118 :         vecChar.push_back(s.c_str());
      38                 :             : 
      39         [ +  - ]:          54 :     std::string error;
      40   [ +  -  +  -  :         108 :     BOOST_CHECK(local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
                   +  - ]
      41                 :          54 : }
      42                 :             : 
      43                 :           8 : void SetupArgs(ArgsManager& local_args, const std::vector<std::pair<std::string, unsigned int>>& args)
      44                 :             : {
      45         [ +  + ]:          23 :     for (const auto& arg : args) {
      46         [ +  - ]:          30 :         local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
      47                 :             :     }
      48                 :           8 : }
      49                 :             : 
      50                 :             : // Test behavior of GetArg functions when string, integer, and boolean types
      51                 :             : // are specified in the settings.json file. GetArg functions are convenience
      52                 :             : // functions. The GetSetting method can always be used instead of GetArg
      53                 :             : // methods to retrieve original values, and there's not always an objective
      54                 :             : // answer to what GetArg behavior is best in every case. This test makes sure
      55                 :             : // there's test coverage for whatever the current behavior is, so it's not
      56                 :             : // broken or changed unintentionally.
      57   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(setting_args)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      58                 :             : {
      59                 :           1 :     ArgsManager args;
      60   [ +  -  +  -  :           3 :     SetupArgs(args, {{"-foo", ArgsManager::ALLOW_ANY}});
             +  +  -  - ]
      61                 :             : 
      62                 :          14 :     auto set_foo = [&](const common::SettingsValue& value) {
      63   [ +  -  +  -  :          23 :       args.LockSettings([&](common::Settings& settings) {
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      64   [ +  -  +  - ]:          13 :         settings.rw_settings["foo"] = value;
      65                 :          13 :       });
      66                 :          13 :     };
      67                 :             : 
      68         [ +  - ]:           2 :     set_foo("str");
      69   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"str\"");
          +  -  +  -  +  
                      - ]
      70   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "str");
          +  -  +  -  +  
                      - ]
      71   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
             +  -  +  - ]
      72   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
             +  -  +  - ]
      73   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
             +  -  +  - ]
      74                 :             : 
      75         [ +  - ]:           2 :     set_foo("99");
      76   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"99\"");
          +  -  +  -  +  
                      - ]
      77   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
          +  -  +  -  +  
                      - ]
      78   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
             +  -  +  - ]
      79   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
             +  -  +  - ]
      80   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
             +  -  +  - ]
      81                 :             : 
      82         [ +  - ]:           2 :     set_foo("3.25");
      83   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"3.25\"");
             +  -  +  - ]
      84   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
          +  -  +  -  +  
                      - ]
      85   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 3);
             +  -  +  - ]
      86   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
             +  -  +  - ]
      87   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
             +  -  +  - ]
      88                 :             : 
      89         [ +  - ]:           2 :     set_foo("0");
      90   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"0\"");
          +  -  +  -  +  
                      - ]
      91   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
          +  -  +  -  +  
                      - ]
      92   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
             +  -  +  - ]
      93   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
             +  -  +  - ]
      94   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
             +  -  +  - ]
      95                 :             : 
      96         [ +  - ]:           2 :     set_foo("");
      97   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"\"");
          +  -  +  -  +  
                      - ]
      98   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "");
          +  -  +  -  +  
                      - ]
      99   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
             +  -  +  - ]
     100   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
             +  -  +  - ]
     101   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
             +  -  +  - ]
     102                 :             : 
     103         [ +  - ]:           2 :     set_foo(99);
     104   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99");
          +  -  +  -  +  
                      - ]
     105   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
          +  -  +  -  +  
                      - ]
     106   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
             +  -  +  - ]
     107   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     108   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     109                 :             : 
     110         [ +  - ]:           2 :     set_foo(3.25);
     111   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25");
          +  -  +  -  +  
                      - ]
     112   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
          +  -  +  -  +  
                      - ]
     113   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     114   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     115   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     116                 :             : 
     117         [ +  - ]:           2 :     set_foo(0);
     118   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0");
          +  -  +  -  +  
                      - ]
     119   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
          +  -  +  -  +  
                      - ]
     120   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
             +  -  +  - ]
     121   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     122   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     123                 :             : 
     124         [ +  - ]:           2 :     set_foo(true);
     125   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "true");
          +  -  +  -  +  
                      - ]
     126   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "1");
          +  -  +  -  +  
                      - ]
     127   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 1);
             +  -  +  - ]
     128   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
             +  -  +  - ]
     129   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
             +  -  +  - ]
     130                 :             : 
     131         [ +  - ]:           2 :     set_foo(false);
     132   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "false");
          +  -  +  -  +  
                      - ]
     133   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
          +  -  +  -  +  
                      - ]
     134   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
             +  -  +  - ]
     135   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
             +  -  +  - ]
     136   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
             +  -  +  - ]
     137                 :             : 
     138         [ +  - ]:           2 :     set_foo(UniValue::VOBJ);
     139   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "{}");
          +  -  +  -  +  
                      - ]
     140   [ +  -  +  -  :           4 :     BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
          +  -  -  +  -  
          -  -  -  -  +  
             +  -  +  - ]
     141   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     142   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     143   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     144                 :             : 
     145         [ +  - ]:           2 :     set_foo(UniValue::VARR);
     146   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "[]");
          +  -  +  -  +  
                      - ]
     147   [ +  -  +  -  :           4 :     BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
          +  -  -  +  -  
          -  -  -  -  +  
             +  -  +  - ]
     148   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     149   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     150   [ +  -  +  -  :           3 :     BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
          -  +  -  -  -  
          -  -  +  +  -  
                   +  - ]
     151                 :             : 
     152         [ +  - ]:           2 :     set_foo(UniValue::VNULL);
     153   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "null");
          +  -  +  -  +  
                      - ]
     154   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "default");
          +  -  +  -  +  
                      - ]
     155   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 100);
             +  -  +  - ]
     156   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
             +  -  +  - ]
     157   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
             +  -  +  - ]
     158         [ +  - ]:           2 : }
     159                 :             : 
     160   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(boolarg)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     161                 :             : {
     162                 :           1 :     ArgsManager local_args;
     163                 :             : 
     164                 :           1 :     const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
     165   [ +  -  +  +  :           2 :     SetupArgs(local_args, {foo});
                   -  - ]
     166   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo");
     167   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     168   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     169                 :             : 
     170   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-fo", false));
          +  -  +  -  +  
                      - ]
     171   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-fo", true));
          +  -  +  -  +  
                      - ]
     172                 :             : 
     173   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-fooo", false));
          +  -  +  -  +  
                      - ]
     174   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-fooo", true));
          +  -  +  -  +  
                      - ]
     175                 :             : 
     176   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=0");
     177   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     178   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     179                 :             : 
     180   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=1");
     181   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     182   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
             +  -  +  - ]
     183                 :             : 
     184                 :             :     // New 0.6 feature: auto-map -nosomething to !-something:
     185   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo");
     186   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     187   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     188                 :             : 
     189   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo=1");
     190   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     191   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     192                 :             : 
     193   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo -nofoo"); // -nofoo should win
     194   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     195   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     196                 :             : 
     197   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=1 -nofoo=1"); // -nofoo should win
     198   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     199   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     200                 :             : 
     201   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=0 -nofoo=0"); // -nofoo=0 should win
     202   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     203   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     204                 :             : 
     205                 :             :     // New 0.6 feature: treat -- same as -:
     206   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "--foo=1");
     207   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     208   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     209                 :             : 
     210   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "--nofoo=1");
     211   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     212   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
             +  -  +  - ]
     213   [ +  -  +  - ]:           2 : }
     214                 :             : 
     215   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(stringarg)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     216                 :             : {
     217                 :           1 :     ArgsManager local_args;
     218                 :             : 
     219                 :           1 :     const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
     220                 :           1 :     const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
     221   [ +  -  +  +  :           3 :     SetupArgs(local_args, {foo, bar});
                   -  - ]
     222   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "");
     223   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
          +  -  +  -  +  
                      - ]
     224   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
          +  -  +  -  +  
                      - ]
     225                 :             : 
     226   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo -bar");
     227   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
          +  -  +  -  +  
                      - ]
     228   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
          +  -  +  -  +  
                      - ]
     229                 :             : 
     230   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=");
     231   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
          +  -  +  -  +  
                      - ]
     232   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
          +  -  +  -  +  
                      - ]
     233                 :             : 
     234   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=11");
     235   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "11");
          +  -  +  -  +  
                      - ]
     236   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "11");
          +  -  +  -  +  
                      - ]
     237                 :             : 
     238   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=eleven");
     239   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "eleven");
          +  -  +  -  +  
                      - ]
     240   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
          +  -  +  -  +  
                      - ]
     241   [ +  -  +  -  :           2 : }
             +  -  -  - ]
     242                 :             : 
     243   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(intarg)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     244                 :             : {
     245                 :           1 :     ArgsManager local_args;
     246                 :             : 
     247                 :           1 :     const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
     248                 :           1 :     const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
     249   [ +  -  +  +  :           3 :     SetupArgs(local_args, {foo, bar});
                   -  - ]
     250   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "");
     251   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 11);
             +  -  +  - ]
     252   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 0);
             +  -  +  - ]
     253                 :             : 
     254   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo -bar");
     255   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 0);
             +  -  +  - ]
     256   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
             +  -  +  - ]
     257                 :             : 
     258                 :             :     // Check under-/overflow behavior.
     259   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=-9223372036854775809 -bar=9223372036854775808");
     260   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
             +  -  +  - ]
     261   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
             +  -  +  - ]
     262                 :             : 
     263   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=11 -bar=12");
     264   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 11);
             +  -  +  - ]
     265   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 12);
             +  -  +  - ]
     266                 :             : 
     267   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo=NaN -bar=NotANumber");
     268   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 1), 0);
             +  -  +  - ]
     269   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
             +  -  +  - ]
     270   [ +  -  +  -  :           2 : }
             +  -  -  - ]
     271                 :             : 
     272   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(patharg)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     273                 :             : {
     274                 :           1 :     ArgsManager local_args;
     275                 :             : 
     276                 :           1 :     const auto dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
     277   [ +  -  +  +  :           2 :     SetupArgs(local_args, {dir});
                   -  - ]
     278   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "");
     279   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), fs::path{});
             +  -  +  - ]
     280                 :             : 
     281         [ +  - ]:           1 :     const fs::path root_path{"/"};
     282   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/");
     283   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
             +  -  +  - ]
     284                 :             : 
     285   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/.");
     286   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
             +  -  +  - ]
     287                 :             : 
     288   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/./");
     289   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
             +  -  +  - ]
     290                 :             : 
     291   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/.//");
     292   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
             +  -  +  - ]
     293                 :             : 
     294                 :             : #ifdef WIN32
     295                 :             :     const fs::path win_root_path{"C:\\"};
     296                 :             :     ResetArgs(local_args, "-dir=C:\\");
     297                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     298                 :             : 
     299                 :             :     ResetArgs(local_args, "-dir=C:/");
     300                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     301                 :             : 
     302                 :             :     ResetArgs(local_args, "-dir=C:\\\\");
     303                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     304                 :             : 
     305                 :             :     ResetArgs(local_args, "-dir=C:\\.");
     306                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     307                 :             : 
     308                 :             :     ResetArgs(local_args, "-dir=C:\\.\\");
     309                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     310                 :             : 
     311                 :             :     ResetArgs(local_args, "-dir=C:\\.\\\\");
     312                 :             :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
     313                 :             : #endif
     314                 :             : 
     315         [ +  - ]:           1 :     const fs::path absolute_path{"/home/user/.bitcoin"};
     316   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin");
     317   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     318                 :             : 
     319   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/root/../home/user/.bitcoin");
     320   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     321                 :             : 
     322   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/./user/.bitcoin");
     323   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     324                 :             : 
     325   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin/");
     326   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     327                 :             : 
     328   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin//");
     329   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     330                 :             : 
     331   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin/.");
     332   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     333                 :             : 
     334   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin/./");
     335   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     336                 :             : 
     337   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=/home/user/.bitcoin/.//");
     338   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
             +  -  +  - ]
     339                 :             : 
     340         [ +  - ]:           1 :     const fs::path relative_path{"user/.bitcoin"};
     341   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin");
     342   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     343                 :             : 
     344   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=somewhere/../user/.bitcoin");
     345   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     346                 :             : 
     347   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/./.bitcoin");
     348   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     349                 :             : 
     350   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin/");
     351   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     352                 :             : 
     353   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin//");
     354   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     355                 :             : 
     356   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin/.");
     357   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     358                 :             : 
     359   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin/./");
     360   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     361                 :             : 
     362   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=user/.bitcoin/.//");
     363   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
             +  -  +  - ]
     364                 :             : 
     365                 :             :     // Check negated and default argument handling. Specifying an empty argument
     366                 :             :     // is the same as not specifying the argument. This is convenient for
     367                 :             :     // scripting so later command line arguments can override earlier command
     368                 :             :     // line arguments or bitcoin.conf values. Currently the -dir= case cannot be
     369                 :             :     // distinguished from -dir case with no assignment, but #16545 would add the
     370                 :             :     // ability to distinguish these in the future (and treat the no-assign case
     371                 :             :     // like an imperative command or an error).
     372   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "");
     373   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
          +  -  +  -  +  
                -  +  - ]
     374   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=override");
     375   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
          +  -  +  -  +  
                -  +  - ]
     376   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir=");
     377   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
          +  -  +  -  +  
                -  +  - ]
     378   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-dir");
     379   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
          +  -  +  -  +  
                -  +  - ]
     380   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nodir");
     381   [ +  -  +  -  :           4 :     BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
          +  -  +  -  +  
                -  +  - ]
     382   [ +  -  +  - ]:           4 : }
     383                 :             : 
     384   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(doubledash)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     385                 :             : {
     386                 :           1 :     ArgsManager local_args;
     387                 :             : 
     388                 :           1 :     const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
     389                 :           1 :     const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
     390   [ +  -  +  +  :           3 :     SetupArgs(local_args, {foo, bar});
                   -  - ]
     391   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "--foo");
     392   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetBoolArg("-foo", false), true);
             +  -  +  - ]
     393                 :             : 
     394   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "--foo=verbose --bar=1");
     395   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "verbose");
          +  -  +  -  +  
                      - ]
     396   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), 1);
             +  -  +  - ]
     397   [ +  -  +  -  :           2 : }
             +  -  -  - ]
     398                 :             : 
     399   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(boolargno)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     400                 :             : {
     401                 :           1 :     ArgsManager local_args;
     402                 :             : 
     403                 :           1 :     const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
     404                 :           1 :     const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
     405   [ +  -  +  +  :           3 :     SetupArgs(local_args, {foo, bar});
                   -  - ]
     406   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo");
     407   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     408   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     409                 :             : 
     410   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo=1");
     411   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     412   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     413                 :             : 
     414   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo=0");
     415   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     416   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     417                 :             : 
     418   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-foo --nofoo"); // --nofoo should win
     419   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     420   [ +  -  +  -  :           2 :     BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
          +  -  +  -  +  
                      - ]
     421                 :             : 
     422   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-nofoo -foo"); // foo always wins:
     423   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", true));
          +  -  +  -  +  
                      - ]
     424   [ +  -  +  -  :           2 :     BOOST_CHECK(local_args.GetBoolArg("-foo", false));
             +  -  +  - ]
     425   [ +  -  +  -  :           2 : }
             +  -  -  - ]
     426                 :             : 
     427   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(logargs)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     428                 :             : {
     429                 :           1 :     ArgsManager local_args;
     430                 :             : 
     431                 :           1 :     const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
     432                 :           1 :     const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
     433                 :           1 :     const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
     434                 :           1 :     const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
     435   [ +  -  +  +  :           5 :     SetupArgs(local_args, {okaylog_bool, okaylog_negbool, okaylog, dontlog});
                   -  - ]
     436   [ +  -  +  - ]:           1 :     ResetArgs(local_args, "-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private42");
     437                 :             : 
     438                 :             :     // Everything logged to debug.log will also append to str
     439         [ +  - ]:           1 :     std::string str;
     440   [ +  -  +  - ]:           1 :     auto print_connection = LogInstance().PushBackCallback(
     441         [ +  - ]:           5 :         [&str](const std::string& s) {
     442                 :           4 :             str += s;
     443                 :             :         });
     444                 :             : 
     445                 :             :     // Log the arguments
     446         [ +  - ]:           1 :     local_args.LogArgs();
     447                 :             : 
     448   [ +  -  +  - ]:           1 :     LogInstance().DeleteCallback(print_connection);
     449                 :             :     // Check that what should appear does, and what shouldn't doesn't.
     450   [ +  -  +  -  :           2 :     BOOST_CHECK(str.find("Command-line arg: okaylog-bool=\"\"") != std::string::npos);
                   +  - ]
     451   [ +  -  +  -  :           2 :     BOOST_CHECK(str.find("Command-line arg: okaylog-negbool=false") != std::string::npos);
                   +  - ]
     452   [ +  -  +  -  :           2 :     BOOST_CHECK(str.find("Command-line arg: okaylog=\"public\"") != std::string::npos);
                   +  - ]
     453   [ +  -  +  -  :           2 :     BOOST_CHECK(str.find("dontlog=****") != std::string::npos);
                   +  - ]
     454   [ +  -  +  - ]:           2 :     BOOST_CHECK(str.find("private42") == std::string::npos);
     455   [ +  -  +  -  :           2 : }
          +  -  +  -  +  
                -  -  - ]
     456                 :             : 
     457                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1