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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

83.82
/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) {
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
      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)) {
4✔
55
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
56
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
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
      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
      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)) {
4✔
83
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
84
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
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
      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)) {
4✔
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
      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)) {
4✔
130
         auto cns = cert->subject_dn().get_attribute("CN");
1✔
131
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
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
      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()) &&
3✔
153
         result.test_eq("exactly one certificate", certs.size(), 1)) {
3✔
154
         auto cns = certs.front().subject_dn().get_attribute("CN");
1✔
155
         result.test_int_eq("exactly one CN", cns.size(), 1);
1✔
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
      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; });
8✔
178

179
         if(result.confirm("found expected certificate", needle != subjects.end())) {
3✔
180
            result.confirm("expected certificate", *needle == dn);
3✔
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
      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);
3✔
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
      Botan::Flatfile_Certificate_Store certstore(get_ca_bundle_containing_user_cert());
1✔
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
class Certstor_Flatfile_Tests final : public Test {
×
230
   public:
231
      std::vector<Test::Result> run() override {
1✔
232
         std::vector<Test::Result> results;
1✔
233

234
         results.push_back(open_certificate_store());
2✔
235
         results.push_back(find_certificate_by_pubkey_sha1());
2✔
236
         results.push_back(find_cert_by_subject_dn());
2✔
237
         results.push_back(find_cert_by_utf8_subject_dn());
2✔
238
         results.push_back(find_cert_by_subject_dn_and_key_id());
2✔
239
         results.push_back(find_certs_by_subject_dn_and_key_id());
2✔
240
         results.push_back(find_all_subjects());
2✔
241
         results.push_back(no_certificate_matches());
2✔
242
         results.push_back(certstore_contains_user_certificate());
2✔
243

244
         return results;
1✔
245
      }
×
246
};
247

248
BOTAN_REGISTER_TEST("x509", "certstor_flatfile", Certstor_Flatfile_Tests);
249

250
}  // namespace
251

252
}  // namespace Botan_Tests
253

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