• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.94
/src/cli/utils.cpp
1
/*
2
* (C) 2009,2010,2014,2015 Jack Lloyd
3
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9

10
#include <botan/version.h>
11
#include <botan/internal/target_info.h>
12
#include <iomanip>
13
#include <sstream>
14

15
#if defined(BOTAN_HAS_CPUID)
16
   #include <botan/internal/cpuid.h>
17
#endif
18

19
#if defined(BOTAN_HAS_HTTP_UTIL)
20
   #include <botan/internal/http_util.h>
21
#endif
22

23
#if defined(BOTAN_HAS_UUID)
24
   #include <botan/uuid.h>
25
#endif
26

27
#if defined(BOTAN_HAS_OS_UTILS)
28
   #include <botan/internal/os_utils.h>
29
#endif
30

31
namespace Botan_CLI {
32

33
namespace {
34

35
class Print_Help final : public Command {
36
   public:
37
      Print_Help() : Command("help") {}
4✔
38

39
      std::string help_text() const override {
1✔
40
         std::map<std::string, std::vector<std::unique_ptr<Command>>> grouped_commands;
1✔
41

42
         auto reg_commands = Command::registered_cmds();
1✔
43
         for(const auto& cmd_name : reg_commands) {
74✔
44
            auto cmd = Command::get_cmd(cmd_name);
73✔
45
            if(cmd) {
73✔
46
               grouped_commands[cmd->group()].push_back(std::move(cmd));
73✔
47
            }
48
         }
73✔
49

50
         const std::map<std::string, std::string> groups_description{{"codec", "Encoders/Decoders"},
1✔
51
                                                                     {"compression", "Compression"},
52
                                                                     {
53
                                                                        "crypto",
54
                                                                        "Encryption",
55
                                                                     },
56
                                                                     {"fec", "Forward Error Correction"},
57
                                                                     {"hash", "Hash Functions"},
58
                                                                     {"hmac", "HMAC"},
59
                                                                     {"info", "Informational"},
60
                                                                     {"misc", "Miscellaneous"},
61
                                                                     {"numtheory", "Number Theory"},
62
                                                                     {"passhash", "Password Hashing"},
63
                                                                     {"psk", "PSK Database"},
64
                                                                     {"pubkey", "Public Key Cryptography"},
65
                                                                     {"testing", "Testing"},
66
                                                                     {"tls", "TLS"},
67
                                                                     {"tss", "Secret Sharing"},
68
                                                                     {"x509", "X.509"}};
17✔
69

70
         std::ostringstream oss;
1✔
71

72
         oss << "Usage: botan <cmd> <cmd-options>\n";
1✔
73
         oss << "All commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed=\n\n";
1✔
74
         oss << "Available commands:\n\n";
1✔
75

76
         for(const auto& commands : grouped_commands) {
19✔
77
            const std::string group = commands.first;
18✔
78
            if(group.empty()) {
18✔
79
               // ???
80
               continue;
1✔
81
            }
82

83
            auto descr = groups_description.find(group);
17✔
84
            if(descr != groups_description.end()) {
17✔
85
               oss << descr->second;
16✔
86
            } else {
87
               oss << group;
1✔
88
            }
89
            oss << ":\n";
17✔
90
            for(const auto& cmd : commands.second) {
89✔
91
               oss << "   " << std::setw(16) << std::left << cmd->cmd_name() << "   " << cmd->description() << "\n";
288✔
92
            }
93
            oss << "\n";
17✔
94
         }
18✔
95

96
         return oss.str();
1✔
97
      }
2✔
98

99
      std::string group() const override { return ""; }
1✔
100

101
      std::string description() const override { return "Prints a help string"; }
×
102

103
      void go() override {
1✔
104
         this->set_return_code(1);
1✔
105
         output() << help_text();
2✔
106
      }
1✔
107
};
108

109
BOTAN_REGISTER_COMMAND("help", Print_Help);
2✔
110

111
class Has_Command final : public Command {
112
   public:
113
      Has_Command() : Command("has_command cmd") {}
22✔
114

115
      std::string group() const override { return "info"; }
1✔
116

117
      std::string description() const override { return "Test if a command is available"; }
1✔
118

119
      void go() override {
10✔
120
         const std::string cmd = get_arg("cmd");
10✔
121

122
         bool exists = false;
10✔
123
         for(const auto& registered_cmd : Command::registered_cmds()) {
531✔
124
            if(cmd == registered_cmd) {
531✔
125
               exists = true;
126
               break;
127
            }
128
         }
10✔
129

130
         if(verbose()) {
10✔
131
            output() << "Command '" << cmd << "' is " << (exists ? "" : "not ") << "available\n";
×
132
         }
133

134
         if(!exists) {
10✔
135
            this->set_return_code(1);
×
136
         }
137
      }
10✔
138
};
139

140
BOTAN_REGISTER_COMMAND("has_command", Has_Command);
11✔
141

142
class Config_Info final : public Command {
143
   public:
144
      Config_Info() : Command("config info_type") {}
10✔
145

146
      std::string help_text() const override {
×
147
         return "Usage: config info_type\n"
×
148
                "   prefix: Print install prefix\n"
149
                "   cflags: Print include params\n"
150
                "   ldflags: Print linker params\n"
151
                "   libs: Print libraries\n";
×
152
      }
153

154
      std::string group() const override { return "info"; }
1✔
155

156
      std::string description() const override { return "Print the used prefix, cflags, ldflags or libs"; }
1✔
157

158
      void go() override {
4✔
159
         const std::string arg = get_arg("info_type");
4✔
160

161
         if(arg == "prefix") {
4✔
162
            output() << BOTAN_INSTALL_PREFIX << "\n";
1✔
163
         } else if(arg == "cflags") {
3✔
164
            output() << "-I" << BOTAN_INSTALL_PREFIX << "/" << BOTAN_INSTALL_HEADER_DIR << "\n";
1✔
165
         } else if(arg == "ldflags") {
2✔
166
            if(*BOTAN_LINK_FLAGS != 0) {
1✔
167
               output() << BOTAN_LINK_FLAGS << ' ';
1✔
168
            }
169
            output() << "-L" << BOTAN_INSTALL_LIB_DIR << "\n";
1✔
170
         } else if(arg == "libs") {
1✔
171
            output() << "-lbotan-" << Botan::version_major() << " " << BOTAN_LIB_LINK << "\n";
1✔
172
         } else {
173
            throw CLI_Usage_Error("Unknown option to botan config " + arg);
×
174
         }
175
      }
4✔
176
};
177

178
BOTAN_REGISTER_COMMAND("config", Config_Info);
5✔
179

180
class Version_Info final : public Command {
181
   public:
182
      Version_Info() : Command("version --full") {}
6✔
183

184
      std::string group() const override { return "info"; }
1✔
185

186
      std::string description() const override { return "Print version info"; }
1✔
187

188
      void go() override {
2✔
189
         if(flag_set("full")) {
2✔
190
            output() << Botan::version_string() << "\n";
3✔
191
         } else {
192
            output() << Botan::short_version_string() << "\n";
3✔
193
         }
194
      }
2✔
195
};
196

197
BOTAN_REGISTER_COMMAND("version", Version_Info);
3✔
198

199
#if defined(BOTAN_HAS_CPUID)
200

201
class Print_Cpuid final : public Command {
202
   public:
203
      Print_Cpuid() : Command("cpuid") {}
32✔
204

205
      std::string group() const override { return "info"; }
1✔
206

207
      std::string description() const override {
1✔
208
         return "List available processor flags (aes_ni, SIMD extensions, ...)";
1✔
209
      }
210

211
      void go() override { output() << "CPUID flags: " << Botan::CPUID::to_string() << "\n"; }
45✔
212
};
213

214
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
16✔
215

216
#endif
217

218
#if defined(BOTAN_HAS_OS_UTILS)
219

220
class Cycle_Counter final : public Command {
221
   public:
222
      Cycle_Counter() : Command("cpu_clock --test-duration=500") {}
4✔
223

224
      std::string group() const override { return "info"; }
1✔
225

226
      std::string description() const override { return "Estimate the speed of the CPU cycle counter"; }
1✔
227

228
      void go() override {
1✔
229
         if(Botan::OS::get_cpu_cycle_counter() == 0) {
1✔
230
            output() << "No CPU cycle counter on this machine\n";
×
231
            return;
×
232
         }
233

234
         const uint64_t test_duration_ns = get_arg_sz("test-duration") * 1000000;
1✔
235

236
         if(test_duration_ns == 0) {
1✔
237
            output() << "Invalid test duration\n";
×
238
            return;
×
239
         }
240

241
         const uint64_t cc_start = Botan::OS::get_cpu_cycle_counter();
1✔
242
         const uint64_t ns_start = Botan::OS::get_system_timestamp_ns();
1✔
243

244
         uint64_t cc_end = 0;
1✔
245
         uint64_t ns_end = ns_start;
1✔
246

247
         while((ns_end - ns_start) < test_duration_ns) {
10,047,530✔
248
            ns_end = Botan::OS::get_system_timestamp_ns();
10,047,528✔
249
            cc_end = Botan::OS::get_cpu_cycle_counter();
10,047,528✔
250
         }
251

252
         if(cc_end <= cc_start) {
1✔
253
            output() << "Cycle counter seems to have wrapped, try again\n";
×
254
            return;
×
255
         }
256

257
         if(ns_end <= ns_start) {
1✔
258
            output() << "System clock seems to have wrapped (?!?)\n";
×
259
            return;
×
260
         }
261

262
         const uint64_t ns_duration = ns_end - ns_start;
1✔
263
         const uint64_t cc_duration = cc_end - cc_start;
1✔
264

265
         const double ratio = static_cast<double>(cc_duration) / ns_duration;
1✔
266

267
         if(ratio >= 1.0) {
1✔
268
            // GHz
269
            output() << "Estimated CPU clock " << std::setprecision(2) << ratio << " GHz\n";
1✔
270
         } else {
271
            // MHz
272
            output() << "Estimated CPU clock " << static_cast<size_t>(ratio * 1000) << " MHz\n";
×
273
         }
274
      }
275
};
276

277
BOTAN_REGISTER_COMMAND("cpu_clock", Cycle_Counter);
2✔
278

279
#endif
280

281
#if defined(BOTAN_HAS_UUID)
282

283
class Print_UUID final : public Command {
284
   public:
285
      Print_UUID() : Command("uuid") {}
6✔
286

287
      std::string group() const override { return "misc"; }
1✔
288

289
      std::string description() const override { return "Print a random UUID"; }
1✔
290

291
      void go() override {
2✔
292
         const Botan::UUID uuid(rng());
2✔
293
         output() << uuid.to_string() << "\n";
6✔
294
      }
2✔
295
};
296

297
BOTAN_REGISTER_COMMAND("uuid", Print_UUID);
3✔
298

299
#endif
300

301
#if defined(BOTAN_HAS_HTTP_UTIL)
302

303
class HTTP_Get final : public Command {
304
   public:
305
      HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {}
2✔
306

307
      std::string group() const override { return "misc"; }
1✔
308

309
      std::string description() const override { return "Retrieve resource from the passed http/https url"; }
1✔
310

311
      void go() override {
×
312
         const std::string url = get_arg("url");
×
313
         const std::chrono::milliseconds timeout(get_arg_sz("timeout"));
×
314
         const size_t redirects = get_arg_sz("redirects");
×
315

316
         output() << Botan::HTTP::GET_sync(url, redirects, timeout) << "\n";
×
317
      }
×
318
};
319

320
BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
1✔
321

322
#endif  // http_util
323

324
}  // namespace
325

326
}  // namespace Botan_CLI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc