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

randombit / botan / 13212092742

08 Feb 2025 12:33AM UTC coverage: 91.658% (-0.004%) from 91.662%
13212092742

push

github

web-flow
Merge pull request #4642 from randombit/jack/target-info-header

Add internal target_info.h header

94839 of 103471 relevant lines covered (91.66%)

11295178.12 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/cpuid.h>
12
#include <botan/internal/stl_util.h>
13
#include <botan/internal/target_info.h>
14
#include <iomanip>
15
#include <sstream>
16

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

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

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

29
namespace Botan_CLI {
30

31
class Print_Help final : public Command {
32
   public:
33
      Print_Help() : Command("help") {}
4✔
34

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

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

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

66
         std::ostringstream oss;
1✔
67

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

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

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

92
         return oss.str();
1✔
93
      }
2✔
94

95
      std::string group() const override { return ""; }
1✔
96

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

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

105
BOTAN_REGISTER_COMMAND("help", Print_Help);
2✔
106

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

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

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

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

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

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

130
         if(exists == false) {
10✔
131
            this->set_return_code(1);
×
132
         }
133
      }
10✔
134
};
135

136
BOTAN_REGISTER_COMMAND("has_command", Has_Command);
11✔
137

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

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

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

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

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

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

174
BOTAN_REGISTER_COMMAND("config", Config_Info);
5✔
175

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

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

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

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

193
BOTAN_REGISTER_COMMAND("version", Version_Info);
3✔
194

195
class Print_Cpuid final : public Command {
196
   public:
197
      Print_Cpuid() : Command("cpuid") {}
32✔
198

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

201
      std::string description() const override {
1✔
202
         return "List available processor flags (aes_ni, SIMD extensions, ...)";
1✔
203
      }
204

205
      void go() override { output() << "CPUID flags: " << Botan::CPUID::to_string() << "\n"; }
45✔
206
};
207

208
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
16✔
209

210
#if defined(BOTAN_HAS_OS_UTILS)
211

212
class Cycle_Counter final : public Command {
213
   public:
214
      Cycle_Counter() : Command("cpu_clock --test-duration=500") {}
4✔
215

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

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

220
      void go() override {
1✔
221
         if(Botan::OS::get_cpu_cycle_counter() == 0) {
1✔
222
            output() << "No CPU cycle counter on this machine\n";
×
223
            return;
×
224
         }
225

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

228
         if(test_duration_ns == 0) {
1✔
229
            output() << "Invalid test duration\n";
×
230
            return;
×
231
         }
232

233
         const uint64_t cc_start = Botan::OS::get_cpu_cycle_counter();
1✔
234
         const uint64_t ns_start = Botan::OS::get_system_timestamp_ns();
1✔
235

236
         uint64_t cc_end = 0;
1✔
237
         uint64_t ns_end = ns_start;
1✔
238

239
         while((ns_end - ns_start) < test_duration_ns) {
3,510,265✔
240
            ns_end = Botan::OS::get_system_timestamp_ns();
3,510,263✔
241
            cc_end = Botan::OS::get_cpu_cycle_counter();
3,510,263✔
242
         }
243

244
         if(cc_end <= cc_start) {
1✔
245
            output() << "Cycle counter seems to have wrapped, try again\n";
×
246
            return;
×
247
         }
248

249
         if(ns_end <= ns_start) {
1✔
250
            output() << "System clock seems to have wrapped (?!?)\n";
×
251
            return;
×
252
         }
253

254
         const uint64_t ns_duration = ns_end - ns_start;
1✔
255
         const uint64_t cc_duration = cc_end - cc_start;
1✔
256

257
         const double ratio = static_cast<double>(cc_duration) / ns_duration;
1✔
258

259
         if(ratio >= 1.0) {
1✔
260
            // GHz
261
            output() << "Estimated CPU clock " << std::setprecision(2) << ratio << " GHz\n";
1✔
262
         } else {
263
            // MHz
264
            output() << "Estimated CPU clock " << static_cast<size_t>(ratio * 1000) << " MHz\n";
×
265
         }
266
      }
267
};
268

269
BOTAN_REGISTER_COMMAND("cpu_clock", Cycle_Counter);
2✔
270

271
#endif
272

273
#if defined(BOTAN_HAS_UUID)
274

275
class Print_UUID final : public Command {
276
   public:
277
      Print_UUID() : Command("uuid") {}
6✔
278

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

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

283
      void go() override {
2✔
284
         Botan::UUID uuid(rng());
2✔
285
         output() << uuid.to_string() << "\n";
6✔
286
      }
2✔
287
};
288

289
BOTAN_REGISTER_COMMAND("uuid", Print_UUID);
3✔
290

291
#endif
292

293
#if defined(BOTAN_HAS_HTTP_UTIL)
294

295
class HTTP_Get final : public Command {
296
   public:
297
      HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {}
2✔
298

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

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

303
      void go() override {
×
304
         const std::string url = get_arg("url");
×
305
         const std::chrono::milliseconds timeout(get_arg_sz("timeout"));
×
306
         const size_t redirects = get_arg_sz("redirects");
×
307

308
         output() << Botan::HTTP::GET_sync(url, redirects, timeout) << "\n";
×
309
      }
×
310
};
311

312
BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
1✔
313

314
#endif  // http_util
315

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