• 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

90.74
/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&) {
1✔
107
      result.test_success("Registration of second name to the same OID fails");
1✔
108
   }
1✔
109

110
   return result;
2✔
111
}
3✔
112

113
class OID_Tests final : public Test {
×
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
      }
1✔
134
};
135

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

138
#endif
139

140
}  // namespace
141

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