• 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

92.26
/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() { return Test::data_file("x509/misc/certstor/valid_ca_bundle.pem"); }
18✔
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
      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) { result.test_failure(e.what()); }
1✔
37

38
   result.test_success();
1✔
39

40
   return result;
1✔
41
}
×
42

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

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

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

59
   result.test_throws("on invalid SHA1 hash data", [&] {
2✔
60
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
61
      certstore.find_cert_by_pubkey_sha1({});
2✔
62
   });
1✔
63

64
   return result;
1✔
65
}
×
66

67
Test::Result find_cert_by_subject_dn() {
1✔
68
   Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN");
1✔
69

70
   try {
1✔
71
      auto dn = get_dn();
1✔
72

73
      result.start_timer();
1✔
74
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
75
      auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
1✔
76
      result.end_timer();
1✔
77

78
      if(result.test_not_nullopt("found certificate", cert)) {
4✔
79
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
80
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
81
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
82
      }
1✔
83
   } catch(std::exception& e) { result.test_failure(e.what()); }
1✔
84

85
   return result;
1✔
86
}
×
87

88
Test::Result find_cert_by_utf8_subject_dn() {
1✔
89
   Test::Result result("Flatfile Certificate Store - Find Certificate by UTF8 subject DN");
1✔
90

91
   try {
1✔
92
      auto dn = get_utf8_dn();
1✔
93

94
      result.start_timer();
1✔
95
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
96
      auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
1✔
97

98
      result.end_timer();
1✔
99

100
      if(result.test_not_nullopt("found certificate", cert)) {
4✔
101
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
102
         result.test_is_eq("exactly one CN", cns.size(), size_t(1));
1✔
103
         result.test_eq("CN", cns.front(), "D-TRUST Root Class 3 CA 2 EV 2009");
2✔
104
      }
1✔
105
   } catch(std::exception& e) { result.test_failure(e.what()); }
1✔
106

107
   return result;
1✔
108
}
×
109

110
Test::Result find_cert_by_subject_dn_and_key_id() {
1✔
111
   Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN and key ID");
1✔
112

113
   try {
1✔
114
      auto dn = get_dn();
1✔
115

116
      result.start_timer();
1✔
117
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
118
      auto cert = certstore.find_cert(dn, get_key_id());
1✔
119
      result.end_timer();
1✔
120

121
      if(result.test_not_nullopt("found certificate", cert)) {
4✔
122
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
123
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
124
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
125
      }
1✔
126
   } catch(std::exception& e) { result.test_failure(e.what()); }
1✔
127

128
   return result;
1✔
129
}
×
130

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

134
   try {
1✔
135
      auto dn = get_dn();
1✔
136

137
      result.start_timer();
1✔
138
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
139
      auto certs = certstore.find_all_certs(dn, get_key_id());
1✔
140
      result.end_timer();
1✔
141

142
      if(result.confirm("result not empty", !certs.empty()) &&
3✔
143
         result.test_eq("exactly one certificate", certs.size(), 1)) {
3✔
144
         auto cns = certs.front().subject_dn().get_attribute("CN");
1✔
145
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
146
         result.test_eq("CN", cns.front(), get_subject_cn());
2✔
147
      }
1✔
148
   } catch(std::exception& e) { result.test_failure(e.what()); }
1✔
149

150
   return result;
1✔
151
}
×
152

153
Test::Result find_all_subjects() {
1✔
154
   Test::Result result("Flatfile Certificate Store - Find all Certificate Subjects");
1✔
155

156
   try {
1✔
157
      result.start_timer();
1✔
158
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
159
      auto subjects = certstore.all_subjects();
1✔
160
      result.end_timer();
1✔
161

162
      if(result.confirm("result not empty", !subjects.empty())) {
3✔
163
         auto dn = get_dn();
1✔
164
         auto needle = std::find_if(
1✔
165
            subjects.cbegin(), subjects.cend(), [=](const Botan::X509_DN& subject) { return subject == dn; });
8✔
166

167
         if(result.confirm("found expected certificate", needle != subjects.end())) {
3✔
168
            result.confirm("expected certificate", *needle == dn);
3✔
169
         }
170
      }
1✔
171
   } catch(std::exception& e) { result.test_failure(e.what()); }
1✔
172

173
   return result;
1✔
174
}
×
175

176
Test::Result no_certificate_matches() {
1✔
177
   Test::Result result("Flatfile Certificate Store - can deal with no matches (regression test)");
1✔
178

179
   try {
1✔
180
      auto dn = get_unknown_dn();
1✔
181
      auto kid = get_unknown_key_id();
1✔
182

183
      result.start_timer();
1✔
184
      Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
1✔
185

186
      auto certs = certstore.find_all_certs(dn, kid);
1✔
187
      auto cert = certstore.find_cert(dn, kid);
1✔
188
      auto pubk_cert = certstore.find_cert_by_pubkey_sha1(kid);
1✔
189
      result.end_timer();
1✔
190

191
      result.confirm("find_all_certs did not find the dummy", certs.empty());
2✔
192
      result.confirm("find_cert did not find the dummy", !cert);
2✔
193
      result.confirm("find_cert_by_pubkey_sha1 did not find the dummy", !pubk_cert);
3✔
194
   } catch(std::exception& e) { result.test_failure(e.what()); }
2✔
195

196
   return result;
1✔
197
}
×
198

199
Test::Result certstore_contains_user_certificate() {
1✔
200
   Test::Result result("Flatfile Certificate Store - rejects bundles with non-CA certs");
1✔
201

202
   try {
1✔
203
      result.start_timer();
1✔
204
      Botan::Flatfile_Certificate_Store certstore(get_ca_bundle_containing_user_cert());
1✔
205
      result.test_failure("CA bundle with non-CA certs should be rejected");
×
206
   } catch(Botan::Invalid_Argument&) { result.test_success(); }
2✔
207

208
   return result;
1✔
209
}
×
210

211
class Certstor_Flatfile_Tests final : public Test {
×
212
   public:
213
      std::vector<Test::Result> run() override {
1✔
214
         std::vector<Test::Result> results;
1✔
215

216
         results.push_back(open_certificate_store());
2✔
217
         results.push_back(find_certificate_by_pubkey_sha1());
2✔
218
         results.push_back(find_cert_by_subject_dn());
2✔
219
         results.push_back(find_cert_by_utf8_subject_dn());
2✔
220
         results.push_back(find_cert_by_subject_dn_and_key_id());
2✔
221
         results.push_back(find_certs_by_subject_dn_and_key_id());
2✔
222
         results.push_back(find_all_subjects());
2✔
223
         results.push_back(no_certificate_matches());
2✔
224
         results.push_back(certstore_contains_user_certificate());
2✔
225

226
         return results;
1✔
227
      }
×
228
};
229

230
BOTAN_REGISTER_TEST("x509", "certstor_flatfile", Certstor_Flatfile_Tests);
231

232
}
233

234
}
235

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