• 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

85.0
/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
#if defined(BOTAN_HAS_BASE64_CODEC)
10
   #include <botan/base64.h>
11
#endif
12

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

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

21
namespace Botan_Tests {
22

23
#if defined(BOTAN_HAS_BASE32_CODEC)
24

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

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

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

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

52
         return result;
26✔
53
      }
26✔
54

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

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

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

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

74
         return {result};
3✔
75
      }
1✔
76
};
77

78
BOTAN_REGISTER_TEST("codec", "base32", Base32_Tests);
79

80
#endif
81

82
#if defined(BOTAN_HAS_BASE58_CODEC)
83

84
class Base58_Tests final : public Text_Based_Test {
×
85
   public:
86
      Base58_Tests() : Text_Based_Test("codec/base58.vec", "Base58", "Binary") {}
2✔
87

88
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
27✔
89
         Test::Result result("Base58");
27✔
90

91
         const bool is_valid = (type == "valid");
27✔
92
         const std::string base58 = vars.get_req_str("Base58");
27✔
93

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

111
         return result;
27✔
112
      }
27✔
113
};
114

115
BOTAN_REGISTER_TEST("codec", "base58", Base58_Tests);
116

117
class Base58_Check_Tests final : public Text_Based_Test {
×
118
   public:
119
      Base58_Check_Tests() : Text_Based_Test("codec/base58c.vec", "Base58", "Binary") {}
2✔
120

121
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
9✔
122
         Test::Result result("Base58 Check");
9✔
123

124
         const bool is_valid = (type == "valid");
9✔
125
         const std::string base58 = vars.get_req_str("Base58");
9✔
126

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

144
         return result;
9✔
145
      }
9✔
146
};
147

148
BOTAN_REGISTER_TEST("codec", "base58c", Base58_Check_Tests);
149

150
#endif
151

152
#if defined(BOTAN_HAS_BASE64_CODEC)
153

154
class Base64_Tests final : public Text_Based_Test {
×
155
   public:
156
      Base64_Tests() : Text_Based_Test("codec/base64.vec", "Base64", "Binary") {}
3✔
157

158
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
24✔
159
         Test::Result result("Base64");
24✔
160

161
         const bool is_valid = (type == "valid");
24✔
162
         const std::string base64 = vars.get_req_str("Base64");
24✔
163

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

181
         return result;
24✔
182
      }
24✔
183

184
      std::vector<Test::Result> run_final_tests() override {
1✔
185
         Test::Result result("Base64");
1✔
186
         const std::string valid_b64 = "Zg==";
1✔
187

188
         for(char ws_char : {' ', '\t', '\r', '\n'}) {
5✔
189
            for(size_t i = 0; i <= valid_b64.size(); ++i) {
24✔
190
               std::string b64_ws = valid_b64;
20✔
191
               b64_ws.insert(i, 1, ws_char);
20✔
192

193
               try {
20✔
194
                  result.test_failure("decoded whitespace base64", Botan::base64_decode(b64_ws, false));
20✔
195
               } catch(std::exception&) {}
20✔
196

197
               try {
20✔
198
                  result.test_eq("base64 decoding with whitespace", Botan::base64_decode(b64_ws, true), "66");
60✔
199
               } catch(std::exception& e) { result.test_failure(b64_ws, e.what()); }
×
200
            }
20✔
201
         }
202

203
         return {result};
3✔
204
      }
1✔
205
};
206

207
BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests);
208

209
#endif
210

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