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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

84.21
/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/os_utils.h>
13
#include <botan/internal/stl_util.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
namespace Botan_CLI {
26

27
class Print_Help final : public Command {
28
   public:
29
      Print_Help() : Command("help") {}
4✔
30

31
      std::string help_text() const override {
1✔
32
         std::map<std::string, std::vector<std::unique_ptr<Command>>> grouped_commands;
1✔
33

34
         auto reg_commands = Command::registered_cmds();
1✔
35
         for(const auto& cmd_name : reg_commands) {
73✔
36
            auto cmd = Command::get_cmd(cmd_name);
72✔
37
            if(cmd) {
72✔
38
               grouped_commands[cmd->group()].push_back(std::move(cmd));
72✔
39
            }
40
         }
72✔
41

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

61
         std::ostringstream oss;
1✔
62

63
         oss << "Usage: botan <cmd> <cmd-options>\n";
1✔
64
         oss << "All commands support --verbose --help --output= --error-output= --rng-type= --drbg-seed=\n\n";
1✔
65
         oss << "Available commands:\n\n";
1✔
66

67
         for(const auto& commands : grouped_commands) {
18✔
68
            std::string desc = commands.first;
17✔
69
            if(desc.empty()) {
17✔
70
               continue;
1✔
71
            }
72

73
            oss << Botan::search_map(groups_description, desc, desc) << ":\n";
32✔
74
            for(const auto& cmd : commands.second) {
87✔
75
               oss << "   " << std::setw(16) << std::left << cmd->cmd_name() << "   " << cmd->description() << "\n";
285✔
76
            }
77
            oss << "\n";
16✔
78
         }
17✔
79

80
         return oss.str();
1✔
81
      }
1✔
82

83
      std::string group() const override { return ""; }
1✔
84

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

87
      void go() override {
1✔
88
         this->set_return_code(1);
1✔
89
         output() << help_text();
1✔
90
      }
1✔
91
};
92

93
BOTAN_REGISTER_COMMAND("help", Print_Help);
2✔
94

95
class Has_Command final : public Command {
96
   public:
97
      Has_Command() : Command("has_command cmd") {}
20✔
98

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

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

103
      void go() override {
9✔
104
         const std::string cmd = get_arg("cmd");
9✔
105

106
         bool exists = false;
9✔
107
         for(const auto& registered_cmd : Command::registered_cmds()) {
480✔
108
            if(cmd == registered_cmd) {
480✔
109
               exists = true;
110
               break;
111
            }
112
         }
9✔
113

114
         if(verbose()) {
9✔
115
            output() << "Command '" << cmd << "' is " << (exists ? "" : "not ") << "available\n";
×
116
         }
117

118
         if(exists == false)
9✔
119
            this->set_return_code(1);
9✔
120
      }
9✔
121
};
122

123
BOTAN_REGISTER_COMMAND("has_command", Has_Command);
10✔
124

125
class Config_Info final : public Command {
126
   public:
127
      Config_Info() : Command("config info_type") {}
10✔
128

129
      std::string help_text() const override {
×
130
         return "Usage: config info_type\n"
×
131
                "   prefix: Print install prefix\n"
132
                "   cflags: Print include params\n"
133
                "   ldflags: Print linker params\n"
134
                "   libs: Print libraries\n";
×
135
      }
136

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

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

141
      void go() override {
4✔
142
         const std::string arg = get_arg("info_type");
4✔
143

144
         if(arg == "prefix") {
4✔
145
            output() << BOTAN_INSTALL_PREFIX << "\n";
1✔
146
         } else if(arg == "cflags") {
3✔
147
            output() << "-I" << BOTAN_INSTALL_PREFIX << "/" << BOTAN_INSTALL_HEADER_DIR << "\n";
1✔
148
         } else if(arg == "ldflags") {
2✔
149
            if(*BOTAN_LINK_FLAGS)
1✔
150
               output() << BOTAN_LINK_FLAGS << ' ';
1✔
151
            output() << "-L" << BOTAN_INSTALL_LIB_DIR << "\n";
1✔
152
         } else if(arg == "libs") {
1✔
153
            output() << "-lbotan-" << Botan::version_major() << " " << BOTAN_LIB_LINK << "\n";
1✔
154
         } else {
155
            throw CLI_Usage_Error("Unknown option to botan config " + arg);
×
156
         }
157
      }
4✔
158
};
159

160
BOTAN_REGISTER_COMMAND("config", Config_Info);
5✔
161

162
class Version_Info final : public Command {
163
   public:
164
      Version_Info() : Command("version --full") {}
6✔
165

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

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

170
      void go() override {
2✔
171
         if(flag_set("full")) {
2✔
172
            output() << Botan::version_string() << "\n";
2✔
173
         } else {
174
            output() << Botan::short_version_string() << "\n";
1✔
175
         }
176
      }
2✔
177
};
178

179
BOTAN_REGISTER_COMMAND("version", Version_Info);
3✔
180

181
class Print_Cpuid final : public Command {
182
   public:
183
      Print_Cpuid() : Command("cpuid") {}
26✔
184

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

187
      std::string description() const override {
1✔
188
         return "List available processor flags (aes_ni, SIMD extensions, ...)";
1✔
189
      }
190

191
      void go() override { output() << "CPUID flags: " << Botan::CPUID::to_string() << "\n"; }
24✔
192
};
193

194
BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
13✔
195

196
class Cycle_Counter final : public Command {
197
   public:
198
      Cycle_Counter() : Command("cpu_clock --test-duration=500") {}
4✔
199

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

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

204
      void go() override {
1✔
205
         if(Botan::OS::get_cpu_cycle_counter() == 0) {
1✔
206
            output() << "No CPU cycle counter on this machine\n";
×
207
            return;
×
208
         }
209

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

212
         if(test_duration_ns == 0) {
1✔
213
            output() << "Invalid test duration\n";
×
214
            return;
×
215
         }
216

217
         const uint64_t cc_start = Botan::OS::get_cpu_cycle_counter();
1✔
218
         const uint64_t ns_start = Botan::OS::get_system_timestamp_ns();
1✔
219

220
         uint64_t cc_end = 0;
1✔
221
         uint64_t ns_end = ns_start;
1✔
222

223
         while((ns_end - ns_start) < test_duration_ns) {
3,070,409✔
224
            ns_end = Botan::OS::get_system_timestamp_ns();
3,070,407✔
225
            cc_end = Botan::OS::get_cpu_cycle_counter();
3,070,407✔
226
         }
227

228
         if(cc_end <= cc_start) {
1✔
229
            output() << "Cycle counter seems to have wrapped, try again\n";
×
230
            return;
×
231
         }
232

233
         if(ns_end <= ns_start) {
1✔
234
            output() << "System clock seems to have wrapped (?!?)\n";
×
235
            return;
×
236
         }
237

238
         const uint64_t ns_duration = ns_end - ns_start;
1✔
239
         const uint64_t cc_duration = cc_end - cc_start;
1✔
240

241
         const double ratio = static_cast<double>(cc_duration) / ns_duration;
1✔
242

243
         if(ratio >= 1.0) {
1✔
244
            // GHz
245
            output() << "Estimated CPU clock " << std::setprecision(2) << ratio << " GHz\n";
1✔
246
         } else {
247
            // MHz
248
            output() << "Estimated CPU clock " << static_cast<size_t>(ratio * 1000) << " MHz\n";
×
249
         }
250
      }
251
};
252

253
BOTAN_REGISTER_COMMAND("cpu_clock", Cycle_Counter);
2✔
254

255
#if defined(BOTAN_HAS_UUID)
256

257
class Print_UUID final : public Command {
258
   public:
259
      Print_UUID() : Command("uuid") {}
6✔
260

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

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

265
      void go() override {
2✔
266
         Botan::UUID uuid(rng());
2✔
267
         output() << uuid.to_string() << "\n";
6✔
268
      }
2✔
269
};
270

271
BOTAN_REGISTER_COMMAND("uuid", Print_UUID);
3✔
272

273
#endif
274

275
#if defined(BOTAN_HAS_HTTP_UTIL)
276

277
class HTTP_Get final : public Command {
278
   public:
279
      HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {}
2✔
280

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

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

285
      void go() override {
×
286
         const std::string url = get_arg("url");
×
287
         const std::chrono::milliseconds timeout(get_arg_sz("timeout"));
×
288
         const size_t redirects = get_arg_sz("redirects");
×
289

290
         output() << Botan::HTTP::GET_sync(url, redirects, timeout) << "\n";
×
291
      }
×
292
};
293

294
BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
1✔
295

296
#endif  // http_util
297

298
}
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

© 2025 Coveralls, Inc