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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

86.17
/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
   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");
2✔
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_eq("named OID not found", Botan::OID::from_name(name).has_value(), false);
2✔
62

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

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

67
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
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_eq("named OID not found", Botan::OID::from_name(name).has_value(), false);
2✔
81

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

84
   result.confirm("named OID found", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
3✔
85
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
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.confirm("named OID found after second insert", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
3✔
98
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
99
   // now second OID maps back to the string as expected:
100
   result.test_eq("name of OID matches expected", oid2.to_formatted_string(), name);
2✔
101

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

109
   return result;
2✔
110
}
1✔
111

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

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

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

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

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

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

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

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

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

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

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

168
         return result;
38✔
169
      }
38✔
170
};
171

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

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

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

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

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

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

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

199
#endif
200

201
}  // namespace
202

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