File: test_compression.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (165 lines) | stat: -rw-r--r-- 7,120 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2023 One Identity LLC.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#ifdef SYSLOG_NG_HAVE_ZLIB

#include <criterion/criterion.h>
#include <criterion/redirect.h>
#include "compression.h"

#if SYSLOG_NG_HTTP_COMPRESSION_ENABLED

char *test_message =
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

guint8 test_message_deflated_bytes[] =
{
  0x78, 0x9c, 0x35, 0x90, 0xc1, 0x71, 0x43, 0x31, 0x08, 0x44, 0x5b, 0xd9, 0x02,
  0x3c, 0xbf, 0x8a, 0xe4, 0x96, 0x6b, 0x0a, 0x20, 0x88, 0xef, 0x30, 0x23, 0x09,
  0x59, 0x02, 0x8f, 0xcb, 0x0f, 0xca, 0x4f, 0x6e, 0x42, 0xc0, 0xb2, 0xfb, 0x3e,
  0x6c, 0x4a, 0x83, 0x8e, 0x15, 0x0d, 0xc5, 0xaa, 0x4d, 0x2c, 0x75, 0x50, 0x13,
  0xbf, 0x81, 0xad, 0x2f, 0x61, 0x17, 0x8f, 0x09, 0x2a, 0x3a, 0x74, 0xb1, 0xf6,
  0x3b, 0xa4, 0x6a, 0x36, 0x97, 0x94, 0x5c, 0x80, 0x68, 0xac, 0x66, 0x05, 0x2e,
  0x6d, 0xe4, 0xb2, 0x76, 0xd6, 0xa2, 0x25, 0xba, 0x23, 0x1c, 0x95, 0xbe, 0x52,
  0x1e, 0xe2, 0x97, 0xb4, 0xa0, 0xd1, 0xbd, 0x13, 0xa8, 0xea, 0x23, 0xe8, 0xc0,
  0xa7, 0x43, 0xba, 0xb6, 0xd4, 0x46, 0xd3, 0xfd, 0x78, 0x66, 0x49, 0xed, 0x86,
  0x47, 0xe8, 0x42, 0xb7, 0xe5, 0x33, 0x0a, 0xe4, 0x25, 0x93, 0xd5, 0xc9, 0xd5,
  0x3a, 0xa2, 0x56, 0x6a, 0x6c, 0x97, 0xf2, 0x1e, 0xd2, 0xa5, 0xfb, 0xd2, 0xaf,
  0xa4, 0x8e, 0x1c, 0x86, 0x50, 0x1a, 0x6f, 0xe9, 0xc9, 0xae, 0x00, 0x79, 0xca,
  0x0f, 0xbc, 0x6d, 0x49, 0x0a, 0x17, 0xe8, 0x8c, 0x74, 0x72, 0x65, 0xd5, 0x8e,
  0x29, 0x63, 0xca, 0xb7, 0xf4, 0x22, 0x33, 0x83, 0xe7, 0xc7, 0xd3, 0x6a, 0x8c,
  0x3c, 0x27, 0x69, 0x27, 0x93, 0x42, 0xd6, 0x12, 0xb0, 0xd6, 0xfa, 0x4f, 0x28,
  0x03, 0x05, 0xce, 0xb8, 0x2b, 0x39, 0xfa, 0x36, 0x84, 0x41, 0x33, 0x8b, 0x98,
  0x07, 0xde, 0x5f, 0x2c, 0xc3, 0x25, 0x36, 0xc6, 0x64, 0x60, 0xcc, 0x24, 0x9c,
  0x73, 0x1c, 0x43, 0x0b, 0xf9, 0xde, 0xc8, 0x14, 0x63, 0x9a, 0x16, 0xe9, 0x9b,
  0xe2, 0x26, 0x95, 0x47, 0x39, 0xea, 0xa0, 0x9d, 0x1b, 0x76, 0x9e, 0xca, 0x4a,
  0x28, 0xb2, 0x64, 0xee, 0x6e, 0xb3, 0xba, 0x6d, 0xd0, 0x06, 0xa4, 0x89, 0x63,
  0xfd, 0x71, 0x8d, 0x76, 0xfc, 0x00, 0xa0, 0x5c, 0xa5, 0x09
};

guint test_message_deflated_length = G_N_ELEMENTS(test_message_deflated_bytes);

typedef enum _GzipOSID
{
  GZIP_OS_UNIX = 0x03,
  GZIP_OS_MACOS = 0x13,
  GZIP_OS_UNKNOWN = 0xff
} GzipOSID;

guint8 test_message_gzipped_bytes[] =
{
  0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x35, 0x90, 0xc1,
  0x71, 0x43, 0x31, 0x08, 0x44, 0x5b, 0xd9, 0x02, 0x3c, 0xbf, 0x8a, 0xe4, 0x96,
  0x6b, 0x0a, 0x20, 0x88, 0xef, 0x30, 0x23, 0x09, 0x59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07, 0xde, 0x5f, 0x2c, 0xc3,
  0x25, 0x36, 0xc6, 0x64, 0x60, 0xcc, 0x24, 0x9c, 0x73, 0x1c, 0x43, 0x0b, 0xf9,
  0xde, 0xc8, 0x14, 0x63, 0x9a, 0x16, 0xe9, 0x9b, 0xe2, 0x26, 0x95, 0x47, 0x39,
  0xea, 0xa0, 0x9d, 0x1b, 0x76, 0x9e, 0xca, 0x4a, 0x28, 0xb2, 0x64, 0xee, 0x6e,
  0xb3, 0xba, 0x6d, 0xd0, 0x06, 0xa4, 0x89, 0x63, 0xfd, 0x71, 0x8d, 0x76, 0xfc,
  0x00, 0xbd, 0xc5, 0xb2, 0x98, 0xbd, 0x01, 0x00, 0x00
};

guint test_message_gzipped_length = G_N_ELEMENTS(test_message_gzipped_bytes);

void replace_gzip_header_os_id(guint8 *gzip_message)
{
  guint8 osid = GZIP_OS_UNKNOWN;

#if defined(__unix__) && defined(__linux__)
  osid = GZIP_OS_UNIX;
#endif
#ifdef __APPLE__
  osid = GZIP_OS_MACOS;
#endif

  // NOTE: gzip header OS ID is byte 9
  gzip_message[9] = osid;
}

// Needed because Criterion cr_assert_str_eq does not like random non-terminating \0s in a string.
static inline void
test_compression_results(GString *compression_result, const guint8 *expected_byte_array, const uint expected_size)
{
  cr_assert_eq(compression_result->len, expected_size);
  for(int i = 0; i < compression_result->len; i++)
    {
      cr_assert_eq((guint8) compression_result->str[i], expected_byte_array[i]);
    }
}

Compressor *compressor;
GString *input, *result;

static void
setup(void)
{
  cr_redirect_stdout();
  cr_redirect_stderr();
  input = g_string_new(test_message);
}

static void
teardown(void)
{
  g_string_free(input, 0);
}

TestSuite(compression, .init = setup, .fini = teardown);

Test(compression, compressor_gzip_compression)
{
  replace_gzip_header_os_id(test_message_gzipped_bytes);
  compressor = gzip_compressor_new();
  cr_assert_not_null(compressor);
  result = g_string_new("");
  cr_assert(compressor_compress(compressor, result, input));
  test_compression_results(result, test_message_gzipped_bytes, test_message_gzipped_length);
  compressor_free(compressor);
  g_string_free(result, TRUE);
}

Test(compression, compressor_deflate_compression)
{
  compressor = deflate_compressor_new();
  cr_assert_not_null(compressor);
  result = g_string_new("");
  cr_assert(compressor_compress(compressor, result, input));
  test_compression_results(result, test_message_deflated_bytes, test_message_deflated_length);
  compressor_free(compressor);
  g_string_free(result, TRUE);
}
#endif

#endif