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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

84.46
/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/certstor_flatfile.h>
14
   #include <algorithm>
15

16
namespace Botan_Tests {
17

18
namespace {
19

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

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

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

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

40
   result.test_success();
1✔
41

42
   return result;
1✔
43
}
×
44

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

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

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

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

68
   return result;
1✔
69
}
×
70

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

74
   try {
1✔
75
      auto dn = get_dn();
1✔
76

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

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

91
   return result;
1✔
92
}
×
93

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

97
   try {
1✔
98
      auto dn = get_utf8_dn();
1✔
99

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

104
      result.end_timer();
1✔
105

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

115
   return result;
1✔
116
}
×
117

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

121
   try {
1✔
122
      auto dn = get_dn();
1✔
123

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

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

138
   return result;
1✔
139
}
×
140

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

144
   try {
1✔
145
      auto dn = get_dn();
1✔
146

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

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

162
   return result;
1✔
163
}
×
164

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

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

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

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

187
   return result;
1✔
188
}
×
189

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

193
   try {
1✔
194
      auto dn = get_unknown_dn();
1✔
195
      auto kid = get_unknown_key_id();
1✔
196

197
      result.start_timer();
1✔
198
      const Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
199

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

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

212
   return result;
1✔
213
}
×
214

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

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

226
   return result;
1✔
227
}
×
228

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

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

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

248
   return result;
1✔
249
}
×
250

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

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

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

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

273
}  // namespace
274

275
}  // namespace Botan_Tests
276

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