summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/tdutils/test/gzip.cpp
blob: 5e1baff9d346093afa89a73cf628bf003af0fee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/ByteFlow.h"
#include "td/utils/common.h"
#include "td/utils/Gzip.h"
#include "td/utils/GzipByteFlow.h"
#include "td/utils/logging.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/tests.h"
#include "td/utils/Time.h"

static void encode_decode(const td::string &s) {
  auto r = td::gzencode(s, 2);
  ASSERT_TRUE(!r.empty());
  ASSERT_EQ(s, td::gzdecode(r.as_slice()));
}

TEST(Gzip, gzencode_gzdecode) {
  encode_decode(td::rand_string(0, 255, 1000));
  encode_decode(td::rand_string('a', 'z', 1000000));
  encode_decode(td::string(1000000, 'a'));
}

static void test_gzencode(const td::string &s) {
  auto begin_time = td::Time::now();
  auto r = td::gzencode(s, td::max(2, static_cast<int>(100 / s.size())));
  ASSERT_TRUE(!r.empty());
  LOG(INFO) << "Encoded string of size " << s.size() << " in " << (td::Time::now() - begin_time)
            << " seconds with compression ratio " << static_cast<double>(r.size()) / static_cast<double>(s.size());
}

TEST(Gzip, gzencode) {
  for (size_t len = 1; len <= 10000000; len *= 10) {
    test_gzencode(td::rand_string('a', 'a', len));
    test_gzencode(td::rand_string('a', 'z', len));
    test_gzencode(td::rand_string(0, 255, len));
  }
}

TEST(Gzip, flow) {
  auto str = td::rand_string('a', 'z', 1000000);
  auto parts = td::rand_split(str);

  td::ChainBufferWriter input_writer;
  auto input = input_writer.extract_reader();
  td::ByteFlowSource source(&input);
  td::GzipByteFlow gzip_flow(td::Gzip::Mode::Encode);
  gzip_flow = td::GzipByteFlow(td::Gzip::Mode::Encode);
  td::ByteFlowSink sink;

  source >> gzip_flow >> sink;

  ASSERT_TRUE(!sink.is_ready());
  for (auto &part : parts) {
    input_writer.append(part);
    source.wakeup();
  }
  ASSERT_TRUE(!sink.is_ready());
  source.close_input(td::Status::OK());
  ASSERT_TRUE(sink.is_ready());
  ASSERT_TRUE(sink.status().is_ok());
  auto res = sink.result()->move_as_buffer_slice().as_slice().str();
  ASSERT_TRUE(!res.empty());
  ASSERT_EQ(td::gzencode(str, 2).as_slice().str(), res);
}
TEST(Gzip, flow_error) {
  auto str = td::rand_string('a', 'z', 1000000);
  auto zip = td::gzencode(str, 0.9).as_slice().str();
  ASSERT_TRUE(!zip.empty());
  zip.resize(zip.size() - 1);
  auto parts = td::rand_split(zip);

  auto input_writer = td::ChainBufferWriter();
  auto input = input_writer.extract_reader();
  td::ByteFlowSource source(&input);
  td::GzipByteFlow gzip_flow(td::Gzip::Mode::Decode);
  td::ByteFlowSink sink;

  source >> gzip_flow >> sink;

  ASSERT_TRUE(!sink.is_ready());
  for (auto &part : parts) {
    input_writer.append(part);
    source.wakeup();
  }
  ASSERT_TRUE(!sink.is_ready());
  source.close_input(td::Status::OK());
  ASSERT_TRUE(sink.is_ready());
  ASSERT_TRUE(!sink.status().is_ok());
}

TEST(Gzip, encode_decode_flow) {
  auto str = td::rand_string('a', 'z', 1000000);
  auto parts = td::rand_split(str);
  td::ChainBufferWriter input_writer;
  auto input = input_writer.extract_reader();
  td::ByteFlowSource source(&input);
  td::GzipByteFlow gzip_encode_flow(td::Gzip::Mode::Encode);
  td::GzipByteFlow gzip_decode_flow(td::Gzip::Mode::Decode);
  td::GzipByteFlow gzip_encode_flow2(td::Gzip::Mode::Encode);
  td::GzipByteFlow gzip_decode_flow2(td::Gzip::Mode::Decode);
  td::ByteFlowSink sink;
  source >> gzip_encode_flow >> gzip_decode_flow >> gzip_encode_flow2 >> gzip_decode_flow2 >> sink;

  ASSERT_TRUE(!sink.is_ready());
  for (auto &part : parts) {
    input_writer.append(part);
    source.wakeup();
  }
  ASSERT_TRUE(!sink.is_ready());
  source.close_input(td::Status::OK());
  ASSERT_TRUE(sink.is_ready());
  LOG_IF(ERROR, sink.status().is_error()) << sink.status();
  ASSERT_TRUE(sink.status().is_ok());
  ASSERT_EQ(str, sink.result()->move_as_buffer_slice().as_slice().str());
}

TEST(Gzip, encode_decode_flow_big) {
  td::clear_thread_locals();
  auto start_mem = td::BufferAllocator::get_buffer_mem();
  {
    auto str = td::string(200000, 'a');
    td::ChainBufferWriter input_writer;
    auto input = input_writer.extract_reader();
    td::ByteFlowSource source(&input);
    td::GzipByteFlow gzip_encode_flow(td::Gzip::Mode::Encode);
    td::GzipByteFlow gzip_decode_flow(td::Gzip::Mode::Decode);
    td::GzipByteFlow gzip_encode_flow2(td::Gzip::Mode::Encode);
    td::GzipByteFlow gzip_decode_flow2(td::Gzip::Mode::Decode);
    td::ByteFlowSink sink;
    source >> gzip_encode_flow >> gzip_decode_flow >> gzip_encode_flow2 >> gzip_decode_flow2 >> sink;

    ASSERT_TRUE(!sink.is_ready());
    size_t n = 200;
    size_t left_size = n * str.size();
    auto validate = [&](td::Slice chunk) {
      CHECK(chunk.size() <= left_size);
      left_size -= chunk.size();
      ASSERT_TRUE(td::all_of(chunk, [](auto c) { return c == 'a'; }));
    };

    for (size_t i = 0; i < n; i++) {
      input_writer.append(str);
      source.wakeup();
      auto extra_mem = td::BufferAllocator::get_buffer_mem() - start_mem;
      // limit means nothing. just check that we do not use 200Mb or so
      CHECK(extra_mem < (10 << 20));

      auto size = sink.get_output()->size();
      validate(sink.get_output()->cut_head(size).move_as_buffer_slice().as_slice());
    }
    ASSERT_TRUE(!sink.is_ready());
    source.close_input(td::Status::OK());
    ASSERT_TRUE(sink.is_ready());
    LOG_IF(ERROR, sink.status().is_error()) << sink.status();
    ASSERT_TRUE(sink.status().is_ok());
    validate(sink.result()->move_as_buffer_slice().as_slice());
    ASSERT_EQ(0u, left_size);
  }
  td::clear_thread_locals();
  ASSERT_EQ(start_mem, td::BufferAllocator::get_buffer_mem());
}

TEST(Gzip, decode_encode_flow_bomb) {
  td::string gzip_bomb_str;
  size_t N = 200;
  {
    td::ChainBufferWriter input_writer;
    auto input = input_writer.extract_reader();
    td::GzipByteFlow gzip_flow(td::Gzip::Mode::Encode);
    td::ByteFlowSource source(&input);
    td::ByteFlowSink sink;
    source >> gzip_flow >> sink;

    td::string s(1 << 16, 'a');
    for (size_t i = 0; i < N; i++) {
      input_writer.append(s);
      source.wakeup();
    }
    source.close_input(td::Status::OK());
    ASSERT_TRUE(sink.is_ready());
    LOG_IF(ERROR, sink.status().is_error()) << sink.status();
    ASSERT_TRUE(sink.status().is_ok());
    gzip_bomb_str = sink.result()->move_as_buffer_slice().as_slice().str();
  }

  td::clear_thread_locals();
  auto start_mem = td::BufferAllocator::get_buffer_mem();
  {
    td::ChainBufferWriter input_writer;
    auto input = input_writer.extract_reader();
    td::ByteFlowSource source(&input);
    td::GzipByteFlow::Options decode_options;
    decode_options.write_watermark.low = 2 << 20;
    decode_options.write_watermark.high = 4 << 20;
    td::GzipByteFlow::Options encode_options;
    encode_options.read_watermark.low = 2 << 20;
    encode_options.read_watermark.high = 4 << 20;
    td::GzipByteFlow gzip_decode_flow(td::Gzip::Mode::Decode);
    gzip_decode_flow.set_options(decode_options);
    td::GzipByteFlow gzip_encode_flow(td::Gzip::Mode::Encode);
    gzip_encode_flow.set_options(encode_options);
    td::GzipByteFlow gzip_decode_flow2(td::Gzip::Mode::Decode);
    gzip_decode_flow2.set_options(decode_options);
    td::GzipByteFlow gzip_encode_flow2(td::Gzip::Mode::Encode);
    gzip_encode_flow2.set_options(encode_options);
    td::GzipByteFlow gzip_decode_flow3(td::Gzip::Mode::Decode);
    gzip_decode_flow3.set_options(decode_options);
    td::ByteFlowSink sink;
    source >> gzip_decode_flow >> gzip_encode_flow >> gzip_decode_flow2 >> gzip_encode_flow2 >> gzip_decode_flow3 >>
        sink;

    ASSERT_TRUE(!sink.is_ready());
    size_t left_size = N * (1 << 16);
    auto validate = [&](td::Slice chunk) {
      CHECK(chunk.size() <= left_size);
      left_size -= chunk.size();
      ASSERT_TRUE(td::all_of(chunk, [](auto c) { return c == 'a'; }));
    };

    input_writer.append(gzip_bomb_str);
    source.close_input(td::Status::OK());

    do {
      gzip_decode_flow3.wakeup();
      gzip_decode_flow2.wakeup();
      gzip_decode_flow.wakeup();
      source.wakeup();
      auto extra_mem = td::BufferAllocator::get_buffer_mem() - start_mem;
      // limit means nothing. just check that we do not use 15Mb or so
      CHECK(extra_mem < (5 << 20));
      auto size = sink.get_output()->size();
      validate(sink.get_output()->cut_head(size).move_as_buffer_slice().as_slice());
    } while (!sink.is_ready());
    ASSERT_EQ(0u, left_size);
  }
  td::clear_thread_locals();
  ASSERT_EQ(start_mem, td::BufferAllocator::get_buffer_mem());
}