• 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

90.38
/src/tests/test_oid.cpp
1
/*
2
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
3
*     2023 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/oids.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
namespace {
18

19
#if defined(BOTAN_HAS_ASN1)
20

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

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

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

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

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

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

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

51
   return result;
1✔
52
}
1✔
53

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

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

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

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

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

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

68
   return result;
2✔
69
}
1✔
70

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

74
   const std::string name = "botan-test-oid2";
1✔
75
   const std::string name2 = "botan-test-oid2.2";
1✔
76
   const std::string name3 = "botan-test-oid2.3";
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
   const Botan::OID oid3("1.3.6.1.4.1.25258.1001.3");
1✔
80

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

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

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

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

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

97
   // name->oid map is unchanged:
98
   result.confirm("named OID found after second insert", Botan::OID::from_name(name).value_or(Botan::OID()) == oid);
4✔
99
   result.test_eq("name of OID matches expected", oid.to_formatted_string(), name);
2✔
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);
2✔
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&) { result.test_success("Registration of second name to the same OID fails"); }
2✔
107

108
   return result;
2✔
109
}
3✔
110

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

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

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

130
         return results;
1✔
131
      }
1✔
132
};
133

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

136
#endif
137

138
}
139

140
}
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