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

randombit / botan / 22022566023

14 Feb 2026 06:55PM UTC coverage: 90.067%. Remained the same
22022566023

push

github

web-flow
Merge pull request #5334 from randombit/jack/test-h-remove-confirm

Remove confirm test helper, use test_is_true or test_is_false instead

102260 of 113538 relevant lines covered (90.07%)

11510296.11 hits per line

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

87.37
/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
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_ASN1)
21

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

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

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

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

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

48
   Test::Result result("OID::to_string");
1✔
49

50
   result.test_eq("OID::to_string behaves as we expect", oid.to_string(), "1.2.1000.1001.1002000");
1✔
51

52
   return result;
1✔
53
}
1✔
54

55
Test::Result test_oid_registration() {
1✔
56
   Test::Result result("OID add");
1✔
57

58
   const std::string name = "botan-test-oid1";
1✔
59
   const Botan::OID oid("1.3.6.1.4.1.25258.1000.1");
1✔
60

61
   result.test_is_false("named OID not found", Botan::OID::from_name(name).has_value());
1✔
62

63
   Botan::OID::register_oid(oid, name);
1✔
64

65
   result.test_is_true("named OID found", Botan::OID::from_name(name).has_value());
1✔
66

67
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
1✔
68

69
   return result;
2✔
70
}
1✔
71

72
Test::Result test_add_and_lookup() {
1✔
73
   Test::Result result("OID add with redundant entries");
1✔
74

75
   const std::string name = "botan-test-oid2";
1✔
76
   const std::string name2 = "botan-test-oid2.2";
1✔
77
   const Botan::OID oid("1.3.6.1.4.1.25258.1001.1");
1✔
78
   const Botan::OID oid2("1.3.6.1.4.1.25258.1001.2");
1✔
79

80
   result.test_is_false("named OID not found", Botan::OID::from_name(name).has_value());
1✔
81

82
   Botan::OID::register_oid(oid, name);
1✔
83

84
   result.test_is_true("named OID found", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
2✔
85
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
1✔
86

87
   // completely redundant, nothing happens:
88
   Botan::OID::register_oid(oid, name);
1✔
89

90
   /*
91
   register a second OID to the same name; this is allowed but
92
   the name will still map back to the original OID
93
   */
94
   Botan::OID::register_oid(oid2, name);
1✔
95

96
   // name->oid map is unchanged:
97
   result.test_is_true("named OID found after second insert",
2✔
98
                       Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
2✔
99
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
1✔
100
   // now second OID maps back to the string as expected:
101
   result.test_eq("name of OID matches expected", oid2.to_formatted_string(), name);
1✔
102

103
   try {
1✔
104
      Botan::OID::register_oid(oid2, name2);
1✔
105
      result.test_failure("Registration of second name to the same OID was accepted");
×
106
   } catch(Botan::Invalid_State&) {
1✔
107
      result.test_success("Registration of second name to the same OID fails");
1✔
108
   }
1✔
109

110
   return result;
2✔
111
}
1✔
112

113
class OID_Tests final : public Test {
1✔
114
   public:
115
      std::vector<Test::Result> run() override {
1✔
116
         std::vector<Test::Result> results;
1✔
117

118
         std::vector<std::function<Test::Result()>> fns = {
1✔
119
            test_OID_to_string,
120
            test_oid_registration,
121
            test_add_and_lookup,
122
         };
4✔
123

124
         for(size_t i = 0; i != fns.size(); ++i) {
4✔
125
            try {
3✔
126
               results.emplace_back(fns[i]());
6✔
127
            } catch(const std::exception& e) {
×
128
               results.emplace_back(Test::Result::Failure("OID tests " + std::to_string(i), e.what()));
×
129
            }
×
130
         }
131

132
         return results;
1✔
133
      }
2✔
134
};
135

136
BOTAN_REGISTER_TEST("asn1", "oid", OID_Tests);
137

138
class OID_Encoding_Tests : public Text_Based_Test {
×
139
   public:
140
      OID_Encoding_Tests() : Text_Based_Test("asn1_oid.vec", "OID,DER") {}
2✔
141

142
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
19✔
143
         const auto oid_str = vars.get_req_str("OID");
19✔
144
         const auto expected_der = vars.get_req_bin("DER");
19✔
145

146
         Test::Result result("OID DER encode/decode");
19✔
147

148
         const Botan::OID oid(oid_str);
19✔
149

150
         try {
19✔
151
            std::vector<uint8_t> der;
19✔
152
            Botan::DER_Encoder enc(der);
19✔
153
            enc.encode(oid);
19✔
154
            result.test_eq("Encoding correct", der, expected_der);
19✔
155
         } catch(std::exception& e) {
19✔
156
            result.test_failure("Encoding OID failed", e.what());
×
157
         }
×
158

159
         try {
19✔
160
            Botan::BER_Decoder dec(expected_der);
19✔
161
            Botan::OID dec_oid;
19✔
162
            dec.decode(dec_oid);
19✔
163
            dec.verify_end();
19✔
164
            result.test_eq("Decoding OID correct", dec_oid.to_string(), oid_str);
19✔
165
         } catch(std::exception& e) {
19✔
166
            result.test_failure("Decoding OID failed", e.what());
×
167
         }
×
168

169
         return result;
38✔
170
      }
19✔
171
};
172

173
BOTAN_REGISTER_TEST("asn1", "oid_enc", OID_Encoding_Tests);
174

175
class OID_Invalid_Encoding_Tests : public Text_Based_Test {
×
176
   public:
177
      OID_Invalid_Encoding_Tests() : Text_Based_Test("asn1_oid_invalid.vec", "DER") {}
2✔
178

179
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
14✔
180
         const auto test_der = vars.get_req_bin("DER");
14✔
181

182
         Test::Result result("OID DER decode invalid");
14✔
183

184
         try {
14✔
185
            Botan::BER_Decoder dec(test_der);
14✔
186
            Botan::OID oid;
14✔
187
            dec.decode(oid);
14✔
188
            dec.verify_end();
×
189
            result.test_failure("Accepted invalid OID encoding", oid.to_string());
×
190
         } catch(Botan::Decoding_Error&) {
42✔
191
            result.test_success("Rejected invalid OID with Decoding_Error");
14✔
192
         }
14✔
193

194
         return result;
14✔
195
      }
14✔
196
};
197

198
BOTAN_REGISTER_TEST("asn1", "oid_dec_invalid", OID_Invalid_Encoding_Tests);
199

200
#endif
201

202
}  // namespace
203

204
}  // 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

© 2026 Coveralls, Inc