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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

87.5
/src/tests/test_oid.cpp
1
/*
2
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
3
*     2023,2024 Jack Lloyd
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_ASN1)
11
   #include <botan/asn1_obj.h>
12
   #include <botan/ber_dec.h>
13
   #include <botan/der_enc.h>
14
   #include <botan/oids.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
namespace {
20

21
#if defined(BOTAN_HAS_ASN1)
22

23
Test::Result test_OID_to_string() {
1✔
24
   /*
25
   See #2730 and #2237
26

27
   Certain locales format integers with thousands seperators.  This
28
   caused a subtle bug which caused OID comparisons to fail because
29
   OID::to_string(), which used ostringstream, introduced a thousands
30
   seperator when the OID component had a value >= 1000. But this
31
   only failed in certain locales (pt_BR was reported).
32

33
   Nominally C++ requires std::to_string to also be locale-respecting.
34
   But, libc++, libstdc++, and MSVC's STL library all implement
35
   std::to_string in a way that ignores locales, because adding locale
36
   support means std::to_string will be both slow and a serialization
37
   point. So as a stopgap we assume this behavior from std::to_string.
38

39
   Here we test the original issue of #2237 to verify it works. If
40
   the compiler implements std::to_string in a way that respects locale,
41
   *and* this test is run in a locale that uses thousands seperators,
42
   then it will fail. Which is much better than a very subtle failure.
43
   However if it ever does fail then we must replace nearly every
44
   call to std::to_string with something else that ignores locale.
45
   */
46

47
   Botan::OID oid{1, 2, 1000, 1001, 1002000};
1✔
48

49
   Test::Result result("OID::to_string");
1✔
50
   result.start_timer();
1✔
51

52
   result.test_eq("OID::to_string behaves as we expect", oid.to_string(), "1.2.1000.1001.1002000");
2✔
53

54
   result.end_timer();
1✔
55
   return result;
1✔
56
}
1✔
57

58
Test::Result test_oid_registration() {
1✔
59
   Test::Result result("OID add");
1✔
60
   result.start_timer();
1✔
61

62
   const std::string name = "botan-test-oid1";
1✔
63
   const Botan::OID oid("1.3.6.1.4.1.25258.1000.1");
1✔
64

65
   result.test_eq("named OID not found", Botan::OID::from_name(name).has_value(), false);
2✔
66

67
   Botan::OID::register_oid(oid, name);
1✔
68

69
   result.test_eq("named OID found", Botan::OID::from_name(name).has_value(), true);
2✔
70

71
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
72

73
   result.end_timer();
1✔
74
   return result;
2✔
75
}
1✔
76

77
Test::Result test_add_and_lookup() {
1✔
78
   Test::Result result("OID add with redundant entries");
1✔
79
   result.start_timer();
1✔
80

81
   const std::string name = "botan-test-oid2";
1✔
82
   const std::string name2 = "botan-test-oid2.2";
1✔
83
   const Botan::OID oid("1.3.6.1.4.1.25258.1001.1");
1✔
84
   const Botan::OID oid2("1.3.6.1.4.1.25258.1001.2");
1✔
85

86
   result.test_eq("named OID not found", Botan::OID::from_name(name).has_value(), false);
2✔
87

88
   Botan::OID::register_oid(oid, name);
1✔
89

90
   result.confirm("named OID found", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
3✔
91
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
92

93
   // completely redundant, nothing happens:
94
   Botan::OID::register_oid(oid, name);
1✔
95

96
   /*
97
   register a second OID to the same name; this is allowed but
98
   the name will still map back to the original OID
99
   */
100
   Botan::OID::register_oid(oid2, name);
1✔
101

102
   // name->oid map is unchanged:
103
   result.confirm("named OID found after second insert", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
3✔
104
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
105
   // now second OID maps back to the string as expected:
106
   result.test_eq("name of OID matches expected", oid2.to_formatted_string(), name);
2✔
107

108
   try {
1✔
109
      Botan::OID::register_oid(oid2, name2);
1✔
110
      result.test_failure("Registration of second name to the same OID was accepted");
×
111
   } catch(Botan::Invalid_State&) {
1✔
112
      result.test_success("Registration of second name to the same OID fails");
1✔
113
   }
1✔
114

115
   result.end_timer();
1✔
116
   return result;
2✔
117
}
1✔
118

119
class OID_Tests final : public Test {
×
120
   public:
121
      std::vector<Test::Result> run() override {
1✔
122
         std::vector<Test::Result> results;
1✔
123

124
         std::vector<std::function<Test::Result()>> fns = {
1✔
125
            test_OID_to_string,
126
            test_oid_registration,
127
            test_add_and_lookup,
128
         };
4✔
129

130
         for(size_t i = 0; i != fns.size(); ++i) {
4✔
131
            try {
3✔
132
               results.emplace_back(fns[i]());
6✔
133
            } catch(const std::exception& e) {
×
134
               results.emplace_back(Test::Result::Failure("OID tests " + std::to_string(i), e.what()));
×
135
            }
×
136
         }
137

138
         return results;
1✔
139
      }
2✔
140
};
141

142
BOTAN_REGISTER_TEST("asn1", "oid", OID_Tests);
143

144
class OID_Encoding_Tests : public Text_Based_Test {
×
145
   public:
146
      OID_Encoding_Tests() : Text_Based_Test("asn1_oid.vec", "OID,DER") {}
2✔
147

148
      Test::Result run_one_test(const std::string&, const VarMap& vars) override {
19✔
149
         const auto oid_str = vars.get_req_str("OID");
19✔
150
         const auto expected_der = vars.get_req_bin("DER");
19✔
151

152
         Test::Result result("OID DER encode/decode");
19✔
153
         result.start_timer();
19✔
154

155
         const Botan::OID oid(oid_str);
19✔
156

157
         try {
19✔
158
            std::vector<uint8_t> der;
19✔
159
            Botan::DER_Encoder enc(der);
19✔
160
            enc.encode(oid);
19✔
161
            result.test_eq("Encoding correct", der, expected_der);
19✔
162
         } catch(std::exception& e) {
38✔
163
            result.test_failure("Encoding OID failed", e.what());
×
164
         }
×
165

166
         try {
19✔
167
            Botan::BER_Decoder dec(expected_der);
19✔
168
            Botan::OID dec_oid;
19✔
169
            dec.decode(dec_oid);
19✔
170
            dec.verify_end();
19✔
171
            result.test_eq("Decoding OID correct", dec_oid.to_string(), oid_str);
38✔
172
         } catch(std::exception& e) {
19✔
173
            result.test_failure("Decoding OID failed", e.what());
×
174
         }
×
175

176
         result.end_timer();
19✔
177
         return result;
38✔
178
      }
38✔
179
};
180

181
BOTAN_REGISTER_TEST("asn1", "oid_enc", OID_Encoding_Tests);
182

183
class OID_Invalid_Encoding_Tests : public Text_Based_Test {
×
184
   public:
185
      OID_Invalid_Encoding_Tests() : Text_Based_Test("asn1_oid_invalid.vec", "DER") {}
2✔
186

187
      Test::Result run_one_test(const std::string&, const VarMap& vars) override {
14✔
188
         const auto test_der = vars.get_req_bin("DER");
14✔
189

190
         Test::Result result("OID DER decode invalid");
14✔
191
         result.start_timer();
14✔
192

193
         try {
14✔
194
            Botan::BER_Decoder dec(test_der);
14✔
195
            Botan::OID oid;
14✔
196
            dec.decode(oid);
14✔
197
            dec.verify_end();
×
198
            result.test_failure("Accepted invalid OID encoding", oid.to_string());
×
199
         } catch(Botan::Decoding_Error&) {
42✔
200
            result.test_success("Rejected invalid OID with Decoding_Error");
14✔
201
         }
14✔
202

203
         result.end_timer();
14✔
204
         return result;
14✔
205
      }
14✔
206
};
207

208
BOTAN_REGISTER_TEST("asn1", "oid_dec_invalid", OID_Invalid_Encoding_Tests);
209

210
#endif
211

212
}  // namespace
213

214
}  // namespace Botan_Tests
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