• 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

83.49
/src/tests/test_compression.cpp
1
/*
2
* (C) 2015 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_COMPRESSION)
10
   #include <botan/compression.h>
11
#endif
12

13
namespace Botan_Tests {
14

15
#if defined(BOTAN_HAS_COMPRESSION)
16

17
namespace {
18

19
const char* text_str =
20
   "'Twas brillig, and the slithy toves"
21
   "Did gyre and gimble in the wabe:"
22
   "All mimsy were the borogoves,"
23
   "And the mome raths outgrabe."
24

25
   "'Beware the Jabberwock, my son!"
26
   "The jaws that bite, the claws that catch!"
27
   "Beware the Jubjub bird, and shun"
28
   "The frumious Bandersnatch!'"
29

30
   "He took his vorpal sword in hand;"
31
   "Long time the manxome foe he sought-"
32
   "So rested he by the Tumtum tree"
33
   "And stood awhile in thought."
34

35
   "And, as in uffish thought he stood,"
36
   "The Jabberwock, with eyes of flame,"
37
   "Came whiffling through the tulgey wood,"
38
   "And burbled as it came!"
39

40
   "One, two! One, two! And through and through"
41
   "The vorpal blade went snicker-snack!"
42
   "He left it dead, and with its head"
43
   "He went galumphing back."
44

45
   "'And hast thou slain the Jabberwock?"
46
   "Come to my arms, my beamish boy!"
47
   "O frabjous day! Callooh! Callay!'"
48
   "He chortled in his joy."
49

50
   "'Twas brillig, and the slithy toves"
51
   "Did gyre and gimble in the wabe:"
52
   "All mimsy were the borogoves,"
53
   "And the mome raths outgrabe.";
54

55
class Compression_Tests final : public Test {
×
56
   public:
57
      std::vector<Test::Result> run() override {
1✔
58
         std::vector<Test::Result> results;
1✔
59
         const size_t text_len = std::strlen(text_str);
1✔
60

61
         for(std::string algo : {"zlib", "deflate", "gzip", "bz2", "lzma"}) {
6✔
62
            try {
5✔
63
               Test::Result result(algo + " compression");
5✔
64

65
               result.start_timer();
5✔
66

67
               auto c = Botan::Compression_Algorithm::create(algo);
5✔
68
               auto d = Botan::Decompression_Algorithm::create(algo);
5✔
69

70
               if(!c || !d) {
5✔
71
                  result.note_missing(algo);
×
72
                  continue;
×
73
               }
74

75
               result.test_ne("Not the same name", c->name(), d->name());
15✔
76

77
               const Botan::secure_vector<uint8_t> empty;
5✔
78
               const Botan::secure_vector<uint8_t> all_zeros(text_len, 0);
5✔
79
               const Botan::secure_vector<uint8_t> random_binary = Test::rng().random_vec(text_len);
5✔
80
               const Botan::secure_vector<uint8_t> short_text = {'f', 'o', 'o', '\n'};
5✔
81

82
               const uint8_t* textb = reinterpret_cast<const uint8_t*>(text_str);
5✔
83
               const Botan::secure_vector<uint8_t> text(textb, textb + text_len);
5✔
84

85
               const size_t c1_e = run_compression(result, 1, *c, *d, empty);
5✔
86
               const size_t c9_e = run_compression(result, 9, *c, *d, empty);
5✔
87
               const size_t c1_z = run_compression(result, 1, *c, *d, all_zeros);
5✔
88
               const size_t c9_z = run_compression(result, 9, *c, *d, all_zeros);
5✔
89
               const size_t c1_r = run_compression(result, 1, *c, *d, random_binary);
5✔
90
               const size_t c9_r = run_compression(result, 9, *c, *d, random_binary);
5✔
91
               const size_t c1_t = run_compression(result, 1, *c, *d, text);
5✔
92
               const size_t c9_t = run_compression(result, 9, *c, *d, text);
5✔
93
               const size_t c1_s = run_compression(result, 1, *c, *d, short_text);
5✔
94
               const size_t c9_s = run_compression(result, 9, *c, *d, short_text);
5✔
95

96
               result.test_gte("Empty input L1 compresses to non-empty output", c1_e, 1);
5✔
97
               result.test_gte("Empty input L9 compresses to non-empty output", c9_e, 1);
5✔
98

99
               result.test_gte("Level 9 compresses empty at least as well as level 1", c1_e, c9_e);
5✔
100
               result.test_gte("Level 9 compresses zeros at least as well as level 1", c1_z, c9_z);
5✔
101
               result.test_gte("Level 9 compresses random at least as well as level 1", c1_r, c9_r);
5✔
102
               result.test_gte("Level 9 compresses text at least as well as level 1", c1_t, c9_t);
5✔
103
               result.test_gte("Level 9 compresses short text at least as well as level 1", c1_s, c9_s);
5✔
104

105
               result.test_lt("Zeros compresses much better than text", c1_z / 8, c1_t);
5✔
106
               result.test_lt("Text compresses much better than random", c1_t / 2, c1_r);
5✔
107

108
               result.end_timer();
5✔
109

110
               results.emplace_back(result);
5✔
111
            } catch(std::exception& e) {
30✔
112
               results.emplace_back(Test::Result::Failure("testing " + algo, e.what()));
×
113
            }
×
114
         }
5✔
115

116
         return results;
1✔
117
      }
×
118

119
   private:
120
      // Returns # of bytes of compressed message
121
      size_t run_compression(Test::Result& result,
50✔
122
                             size_t level,
123
                             Botan::Compression_Algorithm& c,
124
                             Botan::Decompression_Algorithm& d,
125
                             const Botan::secure_vector<uint8_t>& msg) {
126
         Botan::secure_vector<uint8_t> compressed(2 * msg.size());
50✔
127

128
         for(bool with_flush : {true, false}) {
150✔
129
            try {
100✔
130
               compressed = msg;
100✔
131

132
               c.start(level);
100✔
133
               c.update(compressed, 0, false);
100✔
134

135
               if(with_flush) {
100✔
136
                  Botan::secure_vector<uint8_t> flush_bits;
50✔
137
                  c.update(flush_bits, 0, true);
50✔
138
                  compressed += flush_bits;
50✔
139
               }
50✔
140

141
               Botan::secure_vector<uint8_t> final_bits;
100✔
142
               c.finish(final_bits);
100✔
143
               compressed += final_bits;
100✔
144

145
               Botan::secure_vector<uint8_t> decompressed = compressed;
100✔
146
               d.start();
100✔
147
               d.update(decompressed);
100✔
148

149
               Botan::secure_vector<uint8_t> final_outputs;
100✔
150
               d.finish(final_outputs);
100✔
151

152
               decompressed += final_outputs;
100✔
153

154
               result.test_eq("compression round tripped", msg, decompressed);
200✔
155
            } catch(Botan::Exception& e) {
300✔
156
               result.test_failure(e.what());
×
157
            }
×
158
         }
159

160
         return compressed.size();
50✔
161
      }
50✔
162
};
163

164
BOTAN_REGISTER_TEST("compression", "compression_tests", Compression_Tests);
165

166
class CompressionCreate_Tests final : public Test {
×
167
   public:
168
      std::vector<Test::Result> run() override {
1✔
169
         std::vector<Test::Result> results;
1✔
170

171
         for(std::string algo : {"zlib", "deflate", "gzip", "bz2", "lzma"}) {
6✔
172
            try {
5✔
173
               Test::Result result(algo + " create compression");
5✔
174

175
               auto c1 = Botan::Compression_Algorithm::create(algo);
5✔
176
               auto d1 = Botan::Decompression_Algorithm::create(algo);
5✔
177

178
               if(!c1 || !d1) {
5✔
179
                  result.note_missing(algo);
×
180
                  continue;
×
181
               }
182
               result.test_ne("Not the same name after create", c1->name(), d1->name());
15✔
183

184
               auto c2 = Botan::Compression_Algorithm::create_or_throw(algo);
5✔
185
               auto d2 = Botan::Decompression_Algorithm::create_or_throw(algo);
5✔
186

187
               if(!c2 || !d2) {
5✔
188
                  result.note_missing(algo);
×
189
                  continue;
×
190
               }
191
               result.test_ne("Not the same name after create_or_throw", c2->name(), d2->name());
15✔
192

193
               results.emplace_back(result);
5✔
194
            } catch(std::exception& e) {
20✔
195
               results.emplace_back(Test::Result::Failure("testing " + algo, e.what()));
×
196
            }
×
197
         }
5✔
198

199
         {
1✔
200
            Test::Result result("create invalid compression");
1✔
201
            result.test_throws("lookup error", "Unavailable Compression bogocompress", [&]() {
3✔
202
               Botan::Compression_Algorithm::create_or_throw("bogocompress");
1✔
203
            });
×
204
            result.test_throws("lookup error", "Unavailable Decompression bogocompress", [&]() {
3✔
205
               Botan::Decompression_Algorithm::create_or_throw("bogocompress");
1✔
206
            });
×
207
            results.emplace_back(result);
1✔
208
         }
1✔
209

210
         return results;
1✔
211
      }
×
212
};
213

214
BOTAN_REGISTER_TEST("compression", "create_compression", CompressionCreate_Tests);
215

216
}  // namespace
217

218
#endif
219

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