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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 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
class Print_Help final : public Command {
34
   public:
35
      Print_Help() : Command("help") {}
4✔
36

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

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

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

68
         std::ostringstream oss;
1✔
69

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

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

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

94
         return oss.str();
1✔
95
      }
2✔
96

97
      std::string group() const override { return ""; }
1✔
98

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

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

107
BOTAN_REGISTER_COMMAND("help", Print_Help);
2✔
108

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

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

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

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

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

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

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

138
BOTAN_REGISTER_COMMAND("has_command", Has_Command);
11✔
139

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

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

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

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

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

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

176
BOTAN_REGISTER_COMMAND("config", Config_Info);
5✔
177

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

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

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

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

195
BOTAN_REGISTER_COMMAND("version", Version_Info);
3✔
196

197
#if defined(BOTAN_HAS_CPUID)
198

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

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

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

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

212
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
16✔
213

214
#endif
215

216
#if defined(BOTAN_HAS_OS_UTILS)
217

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

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

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

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

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

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

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

242
         uint64_t cc_end = 0;
1✔
243
         uint64_t ns_end = ns_start;
1✔
244

245
         while((ns_end - ns_start) < test_duration_ns) {
4,349,389✔
246
            ns_end = Botan::OS::get_system_timestamp_ns();
4,349,387✔
247
            cc_end = Botan::OS::get_cpu_cycle_counter();
4,349,387✔
248
         }
249

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

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

260
         const uint64_t ns_duration = ns_end - ns_start;
1✔
261
         const uint64_t cc_duration = cc_end - cc_start;
1✔
262

263
         const double ratio = static_cast<double>(cc_duration) / ns_duration;
1✔
264

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

275
BOTAN_REGISTER_COMMAND("cpu_clock", Cycle_Counter);
2✔
276

277
#endif
278

279
#if defined(BOTAN_HAS_UUID)
280

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

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

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

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

295
BOTAN_REGISTER_COMMAND("uuid", Print_UUID);
3✔
296

297
#endif
298

299
#if defined(BOTAN_HAS_HTTP_UTIL)
300

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

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

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

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

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

318
BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
1✔
319

320
#endif  // http_util
321

322
}  // 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