• 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

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
#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) {
×
71
                  result.test_failure(b32_ws, e.what());
×
72
               }
×
73
            }
36✔
74
         }
75

76
         return {result};
3✔
77
      }
1✔
78
};
79

80
BOTAN_REGISTER_TEST("codec", "base32", Base32_Tests);
81

82
#endif
83

84
#if defined(BOTAN_HAS_BASE58_CODEC)
85

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

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

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

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

113
         return result;
27✔
114
      }
27✔
115
};
116

117
BOTAN_REGISTER_TEST("codec", "base58", Base58_Tests);
118

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

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

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

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

146
         return result;
9✔
147
      }
9✔
148
};
149

150
BOTAN_REGISTER_TEST("codec", "base58c", Base58_Check_Tests);
151

152
#endif
153

154
#if defined(BOTAN_HAS_BASE64_CODEC)
155

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

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

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

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

183
         return result;
24✔
184
      }
24✔
185

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

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

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

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

207
         return {result};
3✔
208
      }
1✔
209
};
210

211
BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests);
212

213
#endif
214

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