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

randombit / botan / 17159911587

22 Aug 2025 03:53PM UTC coverage: 90.656% (-0.006%) from 90.662%
17159911587

push

github

Brassinolide
fix clang-tidy & clang-format & macOS test failed

100250 of 110583 relevant lines covered (90.66%)

12199136.65 hits per line

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

83.85
/src/tests/test_certstor_flatfile.cpp
1
/*
2
* (C) 1999-2019 Jack Lloyd
3
* (C) 2019      René Meusel
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_CERTSTOR_FLATFILE)
11

12
   #include "test_certstor_utils.h"
13
   #include <botan/ber_dec.h>
14
   #include <botan/certstor_flatfile.h>
15
   #include <botan/der_enc.h>
16
   #include <botan/hex.h>
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
std::string get_valid_ca_bundle_path() {
10✔
23
   return Test::data_file("x509/misc/certstor/valid_ca_bundle.pem");
20✔
24
}
25

26
std::string get_ca_bundle_containing_user_cert() {
1✔
27
   return Test::data_file("x509/misc/certstor/ca_bundle_containing_non_ca.pem");
2✔
28
}
29

30
Test::Result open_certificate_store() {
1✔
31
   Test::Result result("Flatfile Certificate Store - Open Store");
1✔
32

33
   try {
1✔
34
      result.start_timer();
1✔
35
      Botan::Flatfile_Certificate_Store unused(get_valid_ca_bundle_path());
1✔
36
      result.end_timer();
1✔
37
      result.test_gt("found some certificates", unused.all_subjects().size(), 0);
2✔
38
   } catch(std::exception& e) {
1✔
39
      result.test_failure(e.what());
×
40
   }
×
41

42
   result.test_success();
1✔
43

44
   return result;
1✔
45
}
×
46

47
Test::Result find_certificate_by_pubkey_sha1() {
1✔
48
   Test::Result result("Flatfile Certificate Store - Find Certificate by SHA1(pubkey)");
1✔
49

50
   try {
1✔
51
      result.start_timer();
1✔
52
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
53
      auto cert = certstore.find_cert_by_pubkey_sha1(get_key_id());
1✔
54
      result.end_timer();
1✔
55

56
      if(result.test_not_nullopt("found certificate", cert)) {
1✔
57
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
58
         result.test_int_eq("exactly one CN", cns.size(), 1);
2✔
59
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
60
      }
1✔
61
   } catch(std::exception& e) {
1✔
62
      result.test_failure(e.what());
×
63
   }
×
64

65
   result.test_throws("on invalid SHA1 hash data", [&] {
2✔
66
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
67
      certstore.find_cert_by_pubkey_sha1({});
2✔
68
   });
1✔
69

70
   return result;
1✔
71
}
×
72

73
Test::Result find_cert_by_subject_dn() {
1✔
74
   Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN");
1✔
75

76
   try {
1✔
77
      auto dn = get_dn();
1✔
78

79
      result.start_timer();
1✔
80
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
81
      auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
1✔
82
      result.end_timer();
1✔
83

84
      if(result.test_not_nullopt("found certificate", cert)) {
1✔
85
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
86
         result.test_int_eq("exactly one CN", cns.size(), 1);
2✔
87
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
88
      }
1✔
89
   } catch(std::exception& e) {
1✔
90
      result.test_failure(e.what());
×
91
   }
×
92

93
   return result;
1✔
94
}
×
95

96
Test::Result find_cert_by_utf8_subject_dn() {
1✔
97
   Test::Result result("Flatfile Certificate Store - Find Certificate by UTF8 subject DN");
1✔
98

99
   try {
1✔
100
      auto dn = get_utf8_dn();
1✔
101

102
      result.start_timer();
1✔
103
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
104
      auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
1✔
105

106
      result.end_timer();
1✔
107

108
      if(result.test_not_nullopt("found certificate", cert)) {
1✔
109
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
110
         result.test_is_eq("exactly one CN", cns.size(), size_t(1));
1✔
111
         result.test_eq("CN", cns.front(), "D-TRUST Root Class 3 CA 2 EV 2009");
2✔
112
      }
1✔
113
   } catch(std::exception& e) {
1✔
114
      result.test_failure(e.what());
×
115
   }
×
116

117
   return result;
1✔
118
}
×
119

120
Test::Result find_cert_by_subject_dn_and_key_id() {
1✔
121
   Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN and key ID");
1✔
122

123
   try {
1✔
124
      auto dn = get_dn();
1✔
125

126
      result.start_timer();
1✔
127
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
128
      auto cert = certstore.find_cert(dn, get_key_id());
1✔
129
      result.end_timer();
1✔
130

131
      if(result.test_not_nullopt("found certificate", cert)) {
1✔
132
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
133
         result.test_int_eq("exactly one CN", cns.size(), 1);
2✔
134
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
135
      }
1✔
136
   } catch(std::exception& e) {
1✔
137
      result.test_failure(e.what());
×
138
   }
×
139

140
   return result;
1✔
141
}
×
142

143
Test::Result find_certs_by_subject_dn_and_key_id() {
1✔
144
   Test::Result result("Flatfile Certificate Store - Find Certificates by subject DN and key ID");
1✔
145

146
   try {
1✔
147
      auto dn = get_dn();
1✔
148

149
      result.start_timer();
1✔
150
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
151
      auto certs = certstore.find_all_certs(dn, get_key_id());
1✔
152
      result.end_timer();
1✔
153

154
      if(result.confirm("result not empty", !certs.empty()) &&
2✔
155
         result.test_eq("exactly one certificate", certs.size(), 1)) {
2✔
156
         auto cns = certs.front().subject_dn().get_attribute("CN");
1✔
157
         result.test_int_eq("exactly one CN", cns.size(), 1);
2✔
158
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
159
      }
1✔
160
   } catch(std::exception& e) {
1✔
161
      result.test_failure(e.what());
×
162
   }
×
163

164
   return result;
1✔
165
}
×
166

167
Test::Result find_all_subjects() {
1✔
168
   Test::Result result("Flatfile Certificate Store - Find all Certificate Subjects");
1✔
169

170
   try {
1✔
171
      result.start_timer();
1✔
172
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
173
      auto subjects = certstore.all_subjects();
1✔
174
      result.end_timer();
1✔
175

176
      if(result.confirm("result not empty", !subjects.empty())) {
2✔
177
         auto dn = get_dn();
1✔
178
         auto needle = std::find_if(
1✔
179
            subjects.cbegin(), subjects.cend(), [=](const Botan::X509_DN& subject) { return subject == dn; });
9✔
180

181
         if(result.confirm("found expected certificate", needle != subjects.end())) {
2✔
182
            result.confirm("expected certificate", *needle == dn);
2✔
183
         }
184
      }
1✔
185
   } catch(std::exception& e) {
1✔
186
      result.test_failure(e.what());
×
187
   }
×
188

189
   return result;
1✔
190
}
×
191

192
Test::Result no_certificate_matches() {
1✔
193
   Test::Result result("Flatfile Certificate Store - can deal with no matches (regression test)");
1✔
194

195
   try {
1✔
196
      auto dn = get_unknown_dn();
1✔
197
      auto kid = get_unknown_key_id();
1✔
198

199
      result.start_timer();
1✔
200
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
201

202
      auto certs = certstore.find_all_certs(dn, kid);
1✔
203
      auto cert = certstore.find_cert(dn, kid);
1✔
204
      auto pubk_cert = certstore.find_cert_by_pubkey_sha1(kid);
1✔
205
      result.end_timer();
1✔
206

207
      result.confirm("find_all_certs did not find the dummy", certs.empty());
2✔
208
      result.confirm("find_cert did not find the dummy", !cert);
2✔
209
      result.confirm("find_cert_by_pubkey_sha1 did not find the dummy", !pubk_cert);
2✔
210
   } catch(std::exception& e) {
2✔
211
      result.test_failure(e.what());
×
212
   }
×
213

214
   return result;
1✔
215
}
×
216

217
Test::Result certstore_contains_user_certificate() {
1✔
218
   Test::Result result("Flatfile Certificate Store - rejects bundles with non-CA certs");
1✔
219

220
   try {
1✔
221
      result.start_timer();
1✔
222
      Botan::Flatfile_Certificate_Store certstore(get_ca_bundle_containing_user_cert());
2✔
223
      result.test_failure("CA bundle with non-CA certs should be rejected");
×
224
   } catch(Botan::Invalid_Argument&) {
1✔
225
      result.test_success();
1✔
226
   }
1✔
227

228
   return result;
1✔
229
}
×
230

231
Test::Result find_cert_by_issuer_dn_and_serial_number() {
1✔
232
   Test::Result result("Flatfile Certificate Store - Find Certificate by issuer DN and serial number");
1✔
233

234
   try {
1✔
235
      result.start_timer();
1✔
236
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
237
      auto cert = certstore.find_cert_by_issuer_dn_and_serial_number(get_dn(), get_serial_number());
1✔
238
      result.end_timer();
1✔
239

240
      if(result.test_not_nullopt("found certificate", cert)) {
1✔
241
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
242
         result.test_int_eq("exactly one CN", cns.size(), 1);
2✔
243
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
244
      }
1✔
245
   } catch(std::exception& e) {
1✔
246
      result.test_failure(e.what());
×
247
   }
×
248

249
   return result;
1✔
250
}
×
251

252
class Certstor_Flatfile_Tests final : public Test {
×
253
   public:
254
      std::vector<Test::Result> run() override {
1✔
255
         std::vector<Test::Result> results;
1✔
256

257
         results.push_back(open_certificate_store());
2✔
258
         results.push_back(find_certificate_by_pubkey_sha1());
2✔
259
         results.push_back(find_cert_by_subject_dn());
2✔
260
         results.push_back(find_cert_by_utf8_subject_dn());
2✔
261
         results.push_back(find_cert_by_subject_dn_and_key_id());
2✔
262
         results.push_back(find_certs_by_subject_dn_and_key_id());
2✔
263
         results.push_back(find_all_subjects());
2✔
264
         results.push_back(no_certificate_matches());
2✔
265
         results.push_back(certstore_contains_user_certificate());
2✔
266
         results.push_back(find_cert_by_issuer_dn_and_serial_number());
2✔
267

268
         return results;
1✔
269
      }
×
270
};
271

272
BOTAN_REGISTER_TEST("x509", "certstor_flatfile", Certstor_Flatfile_Tests);
273

274
}  // namespace
275

276
}  // namespace Botan_Tests
277

278
#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

© 2025 Coveralls, Inc