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

randombit / botan / 21782759586

07 Feb 2026 12:31PM UTC coverage: 90.073% (-0.001%) from 90.074%
21782759586

push

github

web-flow
Merge pull request #5289 from randombit/jack/header-patrol-2

Further misc header reductions, forward declarations, etc

102244 of 113513 relevant lines covered (90.07%)

11458499.72 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
#if defined(BOTAN_HAS_BASE32_CODEC)
26

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

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

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

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

54
         return result;
26 ✔
55
      }
26 ✔
56

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

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

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

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

78
         return {result};
3 ✔
79
      }
2 ✔
80
};
81

82
BOTAN_REGISTER_TEST("codec", "base32", Base32_Tests);
83

84
#endif
85

86
#if defined(BOTAN_HAS_BASE58_CODEC)
87

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

92
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
27 ✔
93
         Test::Result result("Base58");
27 ✔
94

95
         const bool is_valid = (type == "valid");
27 ✔
96
         const std::string base58 = vars.get_req_str("Base58");
27 ✔
97

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

115
         return result;
27 ✔
116
      }
27 ✔
117
};
118

119
BOTAN_REGISTER_TEST("codec", "base58", Base58_Tests);
120

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

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

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

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

148
         return result;
9 ✔
149
      }
9 ✔
150
};
151

152
BOTAN_REGISTER_TEST("codec", "base58c", Base58_Check_Tests);
153

154
#endif
155

156
#if defined(BOTAN_HAS_BASE64_CODEC)
157

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

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

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

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

185
         return result;
24 ✔
186
      }
24 ✔
187

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

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

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

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

209
         return {result};
3 ✔
210
      }
2 ✔
211
};
212

213
BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests);
214

215
#endif
216

217
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc