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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

82.26
/src/tests/test_codec.cpp
1
/*
2
* (C) 2015,2018,2021 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#include <botan/hex.h>
10

11
#if defined(BOTAN_HAS_BASE64_CODEC)
12
   #include <botan/base64.h>
13
#endif
14

15
#if defined(BOTAN_HAS_BASE32_CODEC)
16
   #include <botan/base32.h>
17
#endif
18

19
#if defined(BOTAN_HAS_BASE58_CODEC)
20
   #include <botan/base58.h>
21
#endif
22

23
namespace Botan_Tests {
24

25
namespace {
26

27
#if defined(BOTAN_HAS_BASE32_CODEC)
28

29
class Base32_Tests final : public Text_Based_Test {
×
30
   public:
31
      Base32_Tests() : Text_Based_Test("codec/base32.vec", "Base32", "Binary") {}
2✔
32

33
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
26✔
34
         Test::Result result("Base32");
26✔
35

36
         const bool is_valid = (type == "valid");
26✔
37
         const std::string base32 = vars.get_req_str("Base32");
26✔
38

39
         try {
26✔
40
            if(is_valid) {
26✔
41
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
24✔
42
               result.test_bin_eq("base32 decoding", Botan::base32_decode(base32), binary);
24✔
43
               result.test_str_eq("base32 encoding", Botan::base32_encode(binary), base32);
24✔
44
            } else {
24✔
45
               auto res = Botan::base32_decode(base32);
2✔
46
               result.test_failure("decoded invalid base32 to " + Botan::hex_encode(res));
×
47
            }
×
48
         } catch(std::exception& e) {
2✔
49
            if(is_valid) {
2✔
50
               result.test_failure("rejected valid base32", e.what());
×
51
            } else {
52
               result.test_note("rejected invalid base32");
2✔
53
            }
54
         }
2✔
55

56
         return result;
26✔
57
      }
26✔
58

59
      std::vector<Test::Result> run_final_tests() override {
1✔
60
         Test::Result result("Base32");
1✔
61
         const std::string valid_b32 = "MY======";
1✔
62

63
         for(const char ws_char : {' ', '\t', '\r', '\n'}) {
5✔
64
            for(size_t i = 0; i <= valid_b32.size(); ++i) {
40✔
65
               std::string b32_ws = valid_b32;
36✔
66
               b32_ws.insert(i, 1, ws_char);
36✔
67

68
               try {
36✔
69
                  result.test_failure("decoded whitespace base32", Botan::base32_decode(b32_ws, false));
36✔
70
               } catch(std::exception&) {}
36✔
71

72
               try {
36✔
73
                  result.test_bin_eq("base32 decoding with whitespace", Botan::base32_decode(b32_ws, true), "66");
72✔
74
               } catch(std::exception& e) {
×
75
                  result.test_failure(b32_ws, e.what());
×
76
               }
×
77
            }
36✔
78
         }
79

80
         return {result};
3✔
81
      }
2✔
82
};
83

84
BOTAN_REGISTER_TEST("codec", "base32", Base32_Tests);
85

86
#endif
87

88
#if defined(BOTAN_HAS_BASE58_CODEC)
89

90
class Base58_Tests final : public Text_Based_Test {
×
91
   public:
92
      Base58_Tests() : Text_Based_Test("codec/base58.vec", "Base58", "Binary") {}
2✔
93

94
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
50✔
95
         Test::Result result("Base58");
50✔
96

97
         const bool is_valid = (type == "valid");
50✔
98
         const std::string base58 = vars.get_req_str("Base58");
50✔
99

100
         try {
50✔
101
            if(is_valid) {
50✔
102
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
36✔
103
               result.test_bin_eq("base58 decoding", Botan::base58_decode(base58), binary);
36✔
104
               result.test_str_eq("base58 encoding", Botan::base58_encode(binary), base58);
36✔
105
            } else {
36✔
106
               auto res = Botan::base58_decode(base58);
14✔
107
               result.test_failure("decoded invalid base58 to " + Botan::hex_encode(res));
×
108
            }
×
109
         } catch(std::exception& e) {
14✔
110
            if(is_valid) {
14✔
111
               result.test_failure("rejected valid base58", e.what());
×
112
            } else {
113
               result.test_note("rejected invalid base58");
14✔
114
            }
115
         }
14✔
116

117
         return result;
50✔
118
      }
50✔
119
};
120

121
BOTAN_REGISTER_TEST("codec", "base58", Base58_Tests);
122

123
class Base58_Check_Tests final : public Text_Based_Test {
×
124
   public:
125
      Base58_Check_Tests() : Text_Based_Test("codec/base58c.vec", "Base58", "Binary") {}
2✔
126

127
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
9✔
128
         Test::Result result("Base58 Check");
9✔
129

130
         const bool is_valid = (type == "valid");
9✔
131
         const std::string base58 = vars.get_req_str("Base58");
9✔
132

133
         try {
9✔
134
            if(is_valid) {
9✔
135
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
4✔
136
               result.test_bin_eq("base58 decoding", Botan::base58_check_decode(base58), binary);
4✔
137
               result.test_str_eq("base58 encoding", Botan::base58_check_encode(binary), base58);
4✔
138
            } else {
4✔
139
               auto res = Botan::base58_check_decode(base58);
5✔
140
               result.test_failure("decoded invalid base58c to " + Botan::hex_encode(res));
×
141
            }
×
142
         } catch(std::exception& e) {
5✔
143
            if(is_valid) {
5✔
144
               result.test_failure("rejected valid base58c", e.what());
×
145
            } else {
146
               result.test_note("rejected invalid base58c");
5✔
147
            }
148
         }
5✔
149

150
         return result;
9✔
151
      }
9✔
152
};
153

154
BOTAN_REGISTER_TEST("codec", "base58c", Base58_Check_Tests);
155

156
#endif
157

158
#if defined(BOTAN_HAS_BASE64_CODEC)
159

160
class Base64_Tests final : public Text_Based_Test {
×
161
   public:
162
      Base64_Tests() : Text_Based_Test("codec/base64.vec", "Base64", "Binary") {}
2✔
163

164
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
24✔
165
         Test::Result result("Base64");
24✔
166

167
         const bool is_valid = (type == "valid");
24✔
168
         const std::string base64 = vars.get_req_str("Base64");
24✔
169

170
         try {
24✔
171
            if(is_valid) {
24✔
172
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
22✔
173
               result.test_bin_eq("base64 decoding", Botan::base64_decode(base64), binary);
22✔
174
               result.test_str_eq("base64 encoding", Botan::base64_encode(binary), base64);
22✔
175
            } else {
22✔
176
               auto res = Botan::base64_decode(base64);
2✔
177
               result.test_failure("decoded invalid base64 to " + Botan::hex_encode(res));
×
178
            }
×
179
         } catch(std::exception& e) {
2✔
180
            if(is_valid) {
2✔
181
               result.test_failure("rejected valid base64", e.what());
×
182
            } else {
183
               result.test_note("rejected invalid base64");
2✔
184
            }
185
         }
2✔
186

187
         return result;
24✔
188
      }
24✔
189

190
      std::vector<Test::Result> run_final_tests() override {
1✔
191
         Test::Result result("Base64");
1✔
192
         const std::string valid_b64 = "Zg==";
1✔
193

194
         for(const char ws_char : {' ', '\t', '\r', '\n'}) {
5✔
195
            for(size_t i = 0; i <= valid_b64.size(); ++i) {
24✔
196
               std::string b64_ws = valid_b64;
20✔
197
               b64_ws.insert(i, 1, ws_char);
20✔
198

199
               try {
20✔
200
                  result.test_failure("decoded whitespace base64", Botan::base64_decode(b64_ws, false));
20✔
201
               } catch(std::exception&) {}
20✔
202

203
               try {
20✔
204
                  result.test_bin_eq("base64 decoding with whitespace", Botan::base64_decode(b64_ws, true), "66");
40✔
205
               } catch(std::exception& e) {
×
206
                  result.test_failure(b64_ws, e.what());
×
207
               }
×
208
            }
20✔
209
         }
210

211
         return {result};
3✔
212
      }
2✔
213
};
214

215
BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests);
216

217
#endif
218

219
}  // namespace
220

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