Skip to content

Commit f3cad95

Browse files
authored
Merge pull request #4967 from KaganCanSit/cppCheck-static-analysis-tests
Fix/reduce CppCheck warnings in 'tests' folder
2 parents c74e812 + a8862a2 commit f3cad95

11 files changed

Lines changed: 59 additions & 62 deletions

src/tests/runner/test_reporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class Reporter {
154154
*
155155
* Note that this merges test results with the same name
156156
*/
157-
void record(const std::string& test_name, const std::vector<Botan_Tests::Test::Result>& results);
157+
void record(const std::string& testsuite_name, const std::vector<Botan_Tests::Test::Result>& results);
158158

159159
/**
160160
* Called once all test results have been reported for a single run.

src/tests/runner/test_runner.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ Test_Runner::Test_Runner(std::ostream& out) : m_output(out) {}
3131

3232
Test_Runner::~Test_Runner() = default;
3333

34-
bool Test_Runner::run(const Test_Options& opts) {
35-
if(!opts.no_stdout()) {
36-
m_reporters.emplace_back(std::make_unique<StdoutReporter>(opts, output()));
34+
bool Test_Runner::run(const Test_Options& options) {
35+
if(!options.no_stdout()) {
36+
m_reporters.emplace_back(std::make_unique<StdoutReporter>(options, output()));
3737
}
38-
if(!opts.xml_results_dir().empty()) {
38+
if(!options.xml_results_dir().empty()) {
3939
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
40-
m_reporters.emplace_back(std::make_unique<XmlReporter>(opts, opts.xml_results_dir()));
40+
m_reporters.emplace_back(std::make_unique<XmlReporter>(options, options.xml_results_dir()));
4141
#else
4242
output() << "Generating test report files is not supported on this platform\n";
4343
#endif
4444
}
4545

46-
auto req = Botan_Tests::Test::filter_registered_tests(opts.requested_tests(), opts.skip_tests());
46+
auto req = Botan_Tests::Test::filter_registered_tests(options.requested_tests(), options.skip_tests());
4747

4848
// TODO: Test runner should not be aware of certain test's environmental requirements.
49-
if(opts.pkcs11_lib().empty()) {
49+
if(options.pkcs11_lib().empty()) {
5050
// do not run pkcs11 tests by default unless pkcs11-lib set
5151
for(auto iter = req.begin(); iter != req.end();) {
5252
if((*iter).find("pkcs11") != std::string::npos) {
@@ -61,7 +61,7 @@ bool Test_Runner::run(const Test_Options& opts) {
6161
throw Test_Error("No tests to run");
6262
}
6363

64-
std::vector<uint8_t> seed = Botan::hex_decode(opts.drbg_seed());
64+
std::vector<uint8_t> seed = Botan::hex_decode(options.drbg_seed());
6565
if(seed.empty()) {
6666
const uint64_t ts = Botan_Tests::Test::timestamp();
6767
seed.resize(8);
@@ -76,28 +76,28 @@ bool Test_Runner::run(const Test_Options& opts) {
7676
}
7777
#endif
7878

79-
if(!opts.pkcs11_lib().empty()) {
80-
reporter->set_property("pkcs11 library", opts.pkcs11_lib());
79+
if(!options.pkcs11_lib().empty()) {
80+
reporter->set_property("pkcs11 library", options.pkcs11_lib());
8181
}
8282

83-
if(!opts.provider().empty()) {
84-
reporter->set_property("provider", opts.provider());
83+
if(!options.provider().empty()) {
84+
reporter->set_property("provider", options.provider());
8585
}
8686

8787
reporter->set_property("drbg_seed", Botan::hex_encode(seed));
8888
}
8989

90-
Botan_Tests::Test::set_test_options(opts);
90+
Botan_Tests::Test::set_test_options(options);
9191

92-
for(size_t i = 0; i != opts.test_runs(); ++i) {
92+
for(size_t i = 0; i != options.test_runs(); ++i) {
9393
Botan_Tests::Test::set_test_rng_seed(seed, i);
9494

9595
for(const auto& reporter : m_reporters) {
9696
reporter->next_test_run();
9797
}
9898

9999
const bool passed =
100-
(opts.test_threads() == 1) ? run_tests(req) : run_tests_multithreaded(req, opts.test_threads());
100+
(options.test_threads() == 1) ? run_tests(req) : run_tests_multithreaded(req, options.test_threads());
101101

102102
for(const auto& reporter : m_reporters) {
103103
reporter->render();

src/tests/test_ecdsa.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,13 @@ class ECDSA_ExplicitCurveKey_Test : public Text_Based_Test {
331331
const auto* ecdsa = dynamic_cast<const Botan::ECDSA_PrivateKey*>(key.get());
332332
if(ecdsa != nullptr) {
333333
result.test_success("Returned key was ECDSA");
334+
335+
const auto& group = ecdsa->domain();
336+
result.test_eq("Key is marked as explicit encoding", group.used_explicit_encoding(), true);
337+
result.confirm("Group has expected OID", group.get_curve_oid() == expected_oid);
334338
} else {
335339
result.test_failure("Returned key was some other type");
336340
}
337-
338-
const auto& group = ecdsa->domain();
339-
result.test_eq("Key is marked as explicit encoding", group.used_explicit_encoding(), true);
340-
341-
result.confirm("Group has expected OID", group.get_curve_oid() == expected_oid);
342-
343341
} catch(Botan::Exception& e) {
344342
result.test_failure("Failed to parse key", e.what());
345343
}

src/tests/test_pubkey.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
337337
return {result};
338338
}
339339

340-
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo) {
340+
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo_name) {
341341
std::vector<std::string> pk_provider =
342-
Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
342+
Botan::probe_provider_private_key(algo_name, {"base", "commoncrypto", "openssl", "tpm"});
343343
return Test::provider_filter(pk_provider);
344344
}
345345

@@ -379,6 +379,7 @@ Test::Result PK_Encryption_Decryption_Test::run_one_test(const std::string& pad_
379379

380380
result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
381381
check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
382+
decryptors.push_back(std::move(decryptor));
382383
}
383384

384385
for(const auto& enc_provider : possible_providers(algo_name())) {
@@ -555,9 +556,9 @@ Test::Result PK_Key_Agreement_Test::run_one_test(const std::string& header, cons
555556
return result;
556557
}
557558

558-
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
559+
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo_name) {
559560
std::vector<std::string> pk_provider =
560-
Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
561+
Botan::probe_provider_private_key(algo_name, {"base", "commoncrypto", "openssl", "tpm"});
561562
return Test::provider_filter(pk_provider);
562563
}
563564

@@ -607,20 +608,20 @@ std::vector<Test::Result> PK_Key_Generation_Test::run() {
607608
std::vector<Test::Result> results;
608609

609610
for(const auto& param : keygen_params()) {
610-
const auto algo = algo_name(param);
611-
const std::string report_name = Botan::fmt("{}{}", algo, (param.empty() ? param : " " + param));
611+
const auto algorithm_name = algo_name(param);
612+
const std::string report_name = Botan::fmt("{}{}", algorithm_name, (param.empty() ? param : " " + param));
612613

613614
Test::Result result(report_name + " keygen");
614615

615-
const std::vector<std::string> providers = possible_providers(algo);
616+
const std::vector<std::string> providers = possible_providers(algorithm_name);
616617

617618
if(providers.empty()) {
618-
result.note_missing("provider key generation " + algo);
619+
result.note_missing("provider key generation " + algorithm_name);
619620
}
620621

621622
result.start_timer();
622623
for(auto&& prov : providers) {
623-
auto key_p = Botan::create_private_key(algo, this->rng(), param, prov);
624+
auto key_p = Botan::create_private_key(algorithm_name, this->rng(), param, prov);
624625

625626
if(key_p == nullptr) {
626627
continue;

src/tests/test_pubkey.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PK_Test : public Text_Based_Test {
2828
std::string algo_name() const { return m_algo; }
2929

3030
protected:
31-
std::vector<std::string> possible_providers(const std::string& params) override;
31+
std::vector<std::string> possible_providers(const std::string& algo_name) override;
3232

3333
virtual std::string default_padding(const VarMap&) const {
3434
throw Test_Error("No default padding scheme set for " + algo_name());
@@ -73,7 +73,7 @@ class PK_Signature_Verification_Test : public PK_Test {
7373
virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
7474

7575
private:
76-
Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
76+
Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
7777
};
7878

7979
class PK_Signature_NonVerification_Test : public PK_Test {
@@ -89,7 +89,7 @@ class PK_Signature_NonVerification_Test : public PK_Test {
8989
virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
9090

9191
private:
92-
Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
92+
Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
9393
};
9494

9595
class PK_Sign_Verify_DER_Test : public Test {
@@ -105,7 +105,7 @@ class PK_Sign_Verify_DER_Test : public Test {
105105

106106
virtual bool test_random_invalid_sigs() const { return true; }
107107

108-
std::vector<std::string> possible_providers(const std::string& params) override;
108+
std::vector<std::string> possible_providers(const std::string& algo_name) override;
109109

110110
private:
111111
std::string m_algo;
@@ -129,7 +129,7 @@ class PK_Encryption_Decryption_Test : public PK_Test {
129129
}
130130

131131
private:
132-
Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
132+
Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
133133
};
134134

135135
class PK_Decryption_Test : public PK_Test {
@@ -145,7 +145,7 @@ class PK_Decryption_Test : public PK_Test {
145145
std::string default_padding(const VarMap&) const override { return "Raw"; }
146146

147147
private:
148-
Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
148+
Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
149149
};
150150

151151
class PK_Key_Agreement_Test : public PK_Test {
@@ -207,7 +207,7 @@ class PK_Key_Generation_Test : public Test {
207207
std::string_view provider,
208208
std::span<const uint8_t> raw_key_bits) const = 0;
209209

210-
std::vector<std::string> possible_providers(const std::string& params) override;
210+
std::vector<std::string> possible_providers(const std::string& algo_name) override;
211211
};
212212

213213
class PK_Key_Generation_Stability_Test : public PK_Test {

src/tests/test_rng.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ class Request_Counting_RNG final : public Botan::RandomNumberGenerator {
181181
The HMAC_DRBG and ChaCha reseed KATs assume this RNG type
182182
outputs all 0x80
183183
*/
184-
for(auto& out : output) {
185-
out = 0x80;
186-
}
184+
std::ranges::fill(output, 0x80);
187185
if(!output.empty()) {
188186
m_randomize_count++;
189187
}
@@ -217,7 +215,7 @@ class CTR_DRBG_AES256 final : public Botan::RandomNumberGenerator {
217215

218216
void update(std::span<const uint8_t> provided_data);
219217

220-
uint64_t m_V0, m_V1;
218+
uint64_t m_V0 = 0, m_V1 = 0;
221219
std::unique_ptr<Botan::BlockCipher> m_cipher;
222220
};
223221

src/tests/test_rngs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void CTR_DRBG_AES256::fill_bytes_with_input(std::span<uint8_t> output, std::span
5454
}
5555
}
5656

57-
CTR_DRBG_AES256::CTR_DRBG_AES256(std::span<const uint8_t> seed) {
58-
m_cipher = Botan::BlockCipher::create_or_throw("AES-256");
57+
CTR_DRBG_AES256::CTR_DRBG_AES256(std::span<const uint8_t> seed) :
58+
m_cipher(Botan::BlockCipher::create_or_throw("AES-256")) {
5959
add_entropy(seed);
6060
}
6161

src/tests/test_tls_rfc8448.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class Test_TLS_13_Callbacks : public Botan::TLS::Callbacks {
359359
private:
360360
std::vector<uint8_t> send_buffer;
361361
std::vector<uint8_t> receive_buffer;
362-
uint64_t received_seq_no;
362+
uint64_t received_seq_no = 0;
363363
Modify_Exts_Fn m_modify_exts;
364364
std::vector<MockSignature> m_mock_signatures;
365365
std::chrono::system_clock::time_point m_timestamp;

src/tests/test_tls_stream_integration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Peer {
179179
net::system_timer m_timeout_timer;
180180
std::function<void(const std::string&)> m_on_timeout;
181181

182-
char m_data[MAX_MSG_LENGTH];
182+
char m_data[MAX_MSG_LENGTH]{};
183183
};
184184

185185
class Result_Wrapper {

src/tests/tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,13 @@ std::vector<std::string> Test::possible_providers(const std::string& /*unused*/)
412412
}
413413

414414
//static
415-
std::string Test::format_time(uint64_t ns) {
415+
std::string Test::format_time(uint64_t nanoseconds) {
416416
std::ostringstream o;
417417

418-
if(ns > 1000000000) {
419-
o << std::setprecision(2) << std::fixed << ns / 1000000000.0 << " sec";
418+
if(nanoseconds > 1000000000) {
419+
o << std::setprecision(2) << std::fixed << nanoseconds / 1000000000.0 << " sec";
420420
} else {
421-
o << std::setprecision(2) << std::fixed << ns / 1000000.0 << " msec";
421+
o << std::setprecision(2) << std::fixed << nanoseconds / 1000000.0 << " msec";
422422
}
423423

424424
return o.str();
@@ -830,11 +830,11 @@ std::string Test::data_file_as_temporary_copy(const std::string& what) {
830830
}
831831

832832
//static
833-
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& in) {
833+
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& providers) {
834834
if(m_opts.provider().empty()) {
835-
return in;
835+
return providers;
836836
}
837-
for(auto&& provider : in) {
837+
for(auto&& provider : providers) {
838838
if(provider == m_opts.provider()) {
839839
return std::vector<std::string>{provider};
840840
}

0 commit comments

Comments
 (0)