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

randombit / botan / 22020292790

14 Feb 2026 04:03PM UTC coverage: 90.067% (+0.008%) from 90.059%
22020292790

push

github

web-flow
Merge pull request #5330 from randombit/jack/tests-no-assert

Avoid including assert.h into tests.h

102241 of 113517 relevant lines covered (90.07%)

11570076.55 hits per line

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

92.86
/src/tests/test_pubkey.h
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#ifndef BOTAN_TEST_PUBKEY_H_
8
#define BOTAN_TEST_PUBKEY_H_
9

10
#include "tests.h"
11

12
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
13

14
   #include <botan/pk_keys.h>
15
   #include <string>
16
   #include <vector>
17

18
namespace Botan {
19

20
class PK_Decryptor;
21

22
}
23

24
namespace Botan_Tests {
25

26
class PK_Test : public Text_Based_Test {
27
   public:
28
      PK_Test(const std::string& algo,
49✔
29
              const std::string& test_src,
30
              const std::string& required_keys,
31
              const std::string& optional_keys = {}) :
49✔
32
            Text_Based_Test(test_src, required_keys, optional_keys), m_algo(algo) {}
98✔
33

34
      std::string algo_name() const { return m_algo; }
70,392✔
35

36
   protected:
37
      std::vector<std::string> possible_providers(const std::string& algo_name) override;
38

39
      virtual std::string default_padding(const VarMap& /*vars*/) const {
×
40
         throw Test_Error("No default padding scheme set for " + algo_name());
×
41
      }
42

43
      virtual std::string choose_padding(const VarMap& vars, const std::string& pad_hdr);
44

45
   private:
46
      std::string m_algo;
47
};
48

49
class PK_Signature_Generation_Test : public PK_Test {
50
   public:
51
      PK_Signature_Generation_Test(const std::string& algo,
14✔
52
                                   const std::string& test_src,
53
                                   const std::string& required_keys,
54
                                   const std::string& optional_keys = "") :
14✔
55
            PK_Test(algo, test_src, required_keys, optional_keys) {}
14✔
56

57
      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;
58

59
      // Default is a Fixed_Output_RNG returning the nonce
60
      virtual std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const;
61

62
   private:
63
      Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
64
};
65

66
class PK_Signature_Verification_Test : public PK_Test {
67
   public:
68
      PK_Signature_Verification_Test(const std::string& algo,
13✔
69
                                     const std::string& test_src,
70
                                     const std::string& required_keys,
71
                                     const std::string& optional_keys = "") :
13✔
72
            PK_Test(algo, test_src, required_keys, optional_keys) {}
13✔
73

74
      virtual Botan::Signature_Format sig_format() const;
75

76
      virtual bool test_random_invalid_sigs() const { return true; }
1,041✔
77

78
      virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
79

80
   private:
81
      Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
82
};
83

84
class PK_Signature_NonVerification_Test : public PK_Test {
85
   public:
86
      PK_Signature_NonVerification_Test(const std::string& algo,
3✔
87
                                        const std::string& test_src,
88
                                        const std::string& required_keys,
89
                                        const std::string& optional_keys = "") :
3✔
90
            PK_Test(algo, test_src, required_keys, optional_keys) {}
3✔
91

92
      bool clear_between_callbacks() const override { return false; }
706✔
93

94
      virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
95

96
   private:
97
      Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
98
};
99

100
class PK_Sign_Verify_DER_Test : public Test {
101
   public:
102
      PK_Sign_Verify_DER_Test(const std::string& algo, const std::string& padding) : m_algo(algo), m_padding(padding) {}
3✔
103

104
      std::string algo_name() const { return m_algo; }
4✔
105

106
   protected:
107
      std::vector<Test::Result> run() final;
108

109
      virtual std::unique_ptr<Botan::Private_Key> key() = 0;
110

111
      virtual bool test_random_invalid_sigs() const { return true; }
2✔
112

113
      std::vector<std::string> possible_providers(const std::string& algo_name) override;
114

115
   private:
116
      std::string m_algo;
117
      std::string m_padding;
118
};
119

120
class PK_Encryption_Decryption_Test : public PK_Test {
121
   public:
122
      PK_Encryption_Decryption_Test(const std::string& algo,
3✔
123
                                    const std::string& test_src,
124
                                    const std::string& required_keys,
125
                                    const std::string& optional_keys = "") :
3✔
126
            PK_Test(algo, test_src, required_keys, optional_keys) {}
3✔
127

128
      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;
129

130
      std::string default_padding(const VarMap& /*vars*/) const override { return "Raw"; }
6✔
131

132
      // Default is Fixed_Output_RNG returning the nonce
133
      virtual std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const;
134

135
   private:
136
      Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
137
};
138

139
class PK_Decryption_Test : public PK_Test {
140
   public:
141
      PK_Decryption_Test(const std::string& algo,
2✔
142
                         const std::string& test_src,
143
                         const std::string& required_keys,
144
                         const std::string& optional_keys = "") :
2✔
145
            PK_Test(algo, test_src, required_keys, optional_keys) {}
2✔
146

147
      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;
148

149
      std::string default_padding(const VarMap& /*vars*/) const override { return "Raw"; }
16✔
150

151
   private:
152
      Test::Result run_one_test(const std::string& pad_hdr, const VarMap& vars) final;
153
};
154

155
class PK_Key_Agreement_Test : public PK_Test {
156
   public:
157
      PK_Key_Agreement_Test(const std::string& algo,
4✔
158
                            const std::string& test_src,
159
                            const std::string& required_keys,
160
                            const std::string& optional_keys = "") :
4✔
161
            PK_Test(algo, test_src, required_keys, optional_keys) {}
4✔
162

163
      virtual bool agreement_should_fail(const std::string& /*header*/, const VarMap& /*vars*/) const { return false; }
196✔
164

165
      virtual std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& header, const VarMap& vars) = 0;
166

167
      virtual std::vector<uint8_t> load_their_key(const std::string& header, const VarMap& vars) = 0;
168

169
      virtual std::string default_kdf(const VarMap& /*vars*/) const { return "Raw"; }
×
170

171
   private:
172
      Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
173
};
174

175
class PK_KEM_Test : public PK_Test {
176
   public:
177
      PK_KEM_Test(const std::string& algo,
1✔
178
                  const std::string& test_src,
179
                  const std::string& required_keys,
180
                  const std::string& optional_keys = "") :
1✔
181
            PK_Test(algo, test_src, required_keys, optional_keys) {}
1✔
182

183
      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;
184

185
   private:
186
      Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
187
};
188

189
class PK_Key_Generation_Test : public Test {
21✔
190
   protected:
191
      std::vector<Test::Result> run() final;
192

193
      virtual std::vector<std::string> keygen_params() const = 0;
194

195
      virtual std::string algo_name(std::string_view /*param*/) const { return algo_name(); }
93✔
196

197
      virtual std::string algo_name() const = 0;
198

199
      /**
200
       * Algorithm-specific decoding of raw key bits returned from
201
       * `Public_Key::raw_public_key_bits()`. If an algorithm does not support reading
202
       * the raw key bits, this method should return nullptr.
203
       */
204
      virtual std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
205
                                                                     std::string_view provider,
206
                                                                     std::span<const uint8_t> raw_key_bits) const = 0;
207

208
      std::vector<std::string> possible_providers(const std::string& algo_name) override;
209
};
210

211
class PK_Key_Generation_Stability_Test : public PK_Test {
212
   public:
213
      PK_Key_Generation_Stability_Test(const std::string& algo, const std::string& test_src);
214

215
      Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
216

217
      bool clear_between_callbacks() const override { return false; }
4✔
218
};
219

220
class PK_Key_Validity_Test : public PK_Test {
221
   protected:
222
      PK_Key_Validity_Test(const std::string& algo,
1✔
223
                           const std::string& test_src,
224
                           const std::string& required_keys,
225
                           const std::string& optional_keys = "") :
1✔
226
            PK_Test(algo, test_src, required_keys, optional_keys) {}
1✔
227

228
      virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
229

230
   private:
231
      Test::Result run_one_test(const std::string& header, const VarMap& vars) final;
232
};
233

234
void check_invalid_ciphertexts(Test::Result& result,
235
                               Botan::PK_Decryptor& decryptor,
236
                               const std::vector<uint8_t>& plaintext,
237
                               const std::vector<uint8_t>& ciphertext,
238
                               Botan::RandomNumberGenerator& rng);
239

240
}  // namespace Botan_Tests
241

242
#endif
243

244
#endif
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