aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/coding_tests/zlib_test.cpp
blob: e2f368948d9aa27999f743590ae91e69a2ab98c1 (plain) (blame)
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
#include "testing/testing.hpp"

#include "coding/zlib.hpp"

#include "base/macros.hpp"
#include "base/string_utils.hpp"

#include <cstddef>
#include <cstdint>
#include <iterator>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace zlib_test
{
using namespace coding;
using namespace std;

using Deflate = ZLib::Deflate;
using Inflate = ZLib::Inflate;

pair<Deflate::Format, Inflate::Format> const g_combinations[] = {{Deflate::Format::ZLib, Inflate::Format::ZLib},
                                                                 {Deflate::Format::ZLib, Inflate::Format::Both},
                                                                 {Deflate::Format::GZip, Inflate::Format::GZip},
                                                                 {Deflate::Format::GZip, Inflate::Format::Both},
                                                                 {Deflate::Format::Raw, Inflate::Format::Raw}};

namespace
{
void TestDeflateInflate(string const & original)
{
  for (auto const & p : g_combinations)
  {
    Deflate const deflate(p.first /* format */, Deflate::Level::BestCompression);
    Inflate const inflate(p.second /* format */);

    string compressed;
    TEST(deflate(original, back_inserter(compressed)), ());

    string decompressed;
    TEST(inflate(compressed, back_inserter(decompressed)), ());

    TEST_EQUAL(original, decompressed, ());
  }
}

UNIT_TEST(ZLib_Smoke)
{
  Deflate const deflate(Deflate::Format::ZLib, Deflate::Level::BestCompression);
  Inflate const inflate(Inflate::Format::ZLib);

  {
    string s;
    TEST(!deflate(nullptr /* data */, 0 /* size */, back_inserter(s) /* out */), ());
    TEST(!deflate(nullptr /* data */, 4 /* size */, back_inserter(s) /* out */), ());
    TEST(!inflate(nullptr /* data */, 0 /* size */, back_inserter(s) /* out */), ());
    TEST(!inflate(nullptr /* data */, 4 /* size */, back_inserter(s) /* out */), ());
  }

  TestDeflateInflate("");
  TestDeflateInflate("Hello, World!");
}

UNIT_TEST(ZLib_Large)
{
  string original;
  for (size_t i = 0; i < 1000; ++i)
    original += strings::to_string(i);

  TestDeflateInflate(original);
}

UNIT_TEST(GZip_ForeignData)
{
  // To get this array of bytes, type following:
  //
  // echo -n 'Hello World!' | gzip -c | od -t x1
  uint8_t const data[] = {0x1f, 0x8b, 0x08, 0x08, 0x6d, 0x55, 0x08, 0x59, 0x00, 0x03, 0x73, 0x61, 0x6d, 0x70, 0x6c,
                          0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x08, 0xcf,
                          0x2f, 0xca, 0x49, 0x51, 0x04, 0x00, 0xd0, 0xc3, 0x4a, 0xec, 0x0d, 0x00, 0x00, 0x00};

  string s;

  Inflate const inflate(Inflate::Format::GZip);
  TEST(inflate(data, ARRAY_SIZE(data), back_inserter(s)), ());
  TEST_EQUAL(s, "Hello, World!", ());
}

UNIT_TEST(GZip_ExtraDataInBuffer)
{
  // Data from GZip_ForeignData + extra \n at the end of the buffer.
  uint8_t const data[] = {0x1f, 0x8b, 0x08, 0x08, 0x6d, 0x55, 0x08, 0x59, 0x00, 0x03, 0x73, 0x61, 0x6d, 0x70, 0x6c,
                          0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x08, 0xcf,
                          0x2f, 0xca, 0x49, 0x51, 0x04, 0x00, 0xd0, 0xc3, 0x4a, 0xec, 0x0d, 0x00, 0x00, 0x00, 0x0a};

  string s;

  Inflate const inflate(Inflate::Format::GZip);
  // inflate should fail becase there is unconsumed data at the end of buffer.
  TEST(!inflate(data, ARRAY_SIZE(data), back_inserter(s)), ());
  // inflate should decompress everything but the last byte.
  TEST_EQUAL(s, "Hello, World!", ());
}
}  // namespace
}  // namespace zlib_test