1 /* brotli-decompressor-test.c
2 *
3 * Copyright 2019 Igalia S.L.
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.0-or-later
19 */
20
21 #include "test-utils.h"
22 #include "libsoup/soup-brotli-decompressor.h"
23
24 static void
test_brotli(void)25 test_brotli (void)
26 {
27 SoupBrotliDecompressor *dec = soup_brotli_decompressor_new ();
28 char *compressed_filename = g_build_filename (g_test_get_dir (G_TEST_DIST), "brotli-data", "compressed.br", NULL);
29 char *uncompressed_filename = g_build_filename (g_test_get_dir (G_TEST_DIST), "brotli-data", "uncompressed.txt", NULL);
30 char *contents;
31 gsize length;
32 GByteArray *out_bytes = g_byte_array_new ();
33 char *in_buf;
34 GConverterResult result;
35
36 g_assert_true (g_file_get_contents (compressed_filename, &contents, &length, NULL));
37 in_buf = contents;
38
39 do {
40 GError *error = NULL;
41 guint8 out_buf[16]; /* This is stupidly small just to simulate common usage of converting in chunks */
42 gsize bytes_read, bytes_written;
43 result = g_converter_convert (G_CONVERTER (dec), in_buf, length, out_buf, sizeof out_buf, 0,
44 &bytes_read, &bytes_written, &error);
45
46 g_assert_no_error (error);
47 g_assert_cmpint (result, !=, G_CONVERTER_ERROR);
48
49 g_byte_array_append (out_bytes, out_buf, bytes_written);
50 in_buf += bytes_read;
51 length -= bytes_read;
52
53 } while (result == G_CONVERTER_CONVERTED);
54
55 g_assert_cmpint (result, ==, G_CONVERTER_FINISHED);
56
57 g_free (contents);
58 g_assert_true (g_file_get_contents (uncompressed_filename, &contents, &length, NULL));
59 g_assert_cmpstr ((char*)out_bytes->data, ==, contents);
60
61 g_byte_array_free (out_bytes, TRUE);
62 g_object_unref (dec);
63 g_free (compressed_filename);
64 g_free (uncompressed_filename);
65 g_free (contents);
66 }
67
68 static void
test_brotli_corrupt(void)69 test_brotli_corrupt (void)
70 {
71 SoupBrotliDecompressor *dec = soup_brotli_decompressor_new ();
72 char *compressed_filename = g_build_filename (g_test_get_dir (G_TEST_DIST), "brotli-data", "corrupt.br", NULL);
73 GError *error = NULL;
74 char *contents;
75 gsize length;
76 char *in_buf;
77 GConverterResult result;
78
79 g_assert_true (g_file_get_contents (compressed_filename, &contents, &length, NULL));
80 in_buf = contents;
81
82 do {
83 guint8 out_buf[4096];
84 gsize bytes_read, bytes_written;
85 result = g_converter_convert (G_CONVERTER (dec), in_buf, length, out_buf, sizeof out_buf, 0,
86 &bytes_read, &bytes_written, &error);
87
88 in_buf += bytes_read;
89 length -= bytes_read;
90 } while (result == G_CONVERTER_CONVERTED);
91
92 g_assert_cmpint (result, ==, G_CONVERTER_ERROR);
93 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
94
95 g_object_unref (dec);
96 g_free (compressed_filename);
97 g_free (contents);
98 g_error_free (error);
99 }
100
101 static void
test_brotli_reset(void)102 test_brotli_reset (void)
103 {
104 SoupBrotliDecompressor *dec = soup_brotli_decompressor_new ();
105 char *compressed_filename = g_build_filename (g_test_get_dir (G_TEST_DIST), "brotli-data", "compressed.br", NULL);
106 char *contents;
107 gsize length, in_len;
108 char *in_buf;
109 GConverterResult result;
110 int iterations = 0;
111
112 g_assert_true (g_file_get_contents (compressed_filename, &contents, &length, NULL));
113 in_buf = contents;
114 in_len = length;
115
116 do {
117 GError *error = NULL;
118 guint8 out_buf[16];
119 gsize bytes_read, bytes_written;
120 result = g_converter_convert (G_CONVERTER (dec), in_buf, in_len, out_buf, sizeof out_buf, 0,
121 &bytes_read, &bytes_written, &error);
122
123 /* Just randomly reset in the middle and ensure everything keeps working */
124 if (iterations == 6) {
125 g_converter_reset (G_CONVERTER (dec));
126 in_buf = contents;
127 in_len = length;
128 }
129
130 g_assert_no_error (error);
131 g_assert_cmpint (result, !=, G_CONVERTER_ERROR);
132 in_buf += bytes_read;
133 in_len -= bytes_read;
134 ++iterations;
135 } while (result == G_CONVERTER_CONVERTED);
136
137 g_assert_cmpint (result, ==, G_CONVERTER_FINISHED);
138
139 g_object_unref (dec);
140 g_free (compressed_filename);
141 g_free (contents);
142 }
143
144 int
main(int argc,char ** argv)145 main (int argc, char **argv)
146 {
147
148 int ret;
149
150 test_init (argc, argv, NULL);
151
152 g_test_add_func ("/brotli/basic", test_brotli);
153 g_test_add_func ("/brotli/corrupt", test_brotli_corrupt);
154 g_test_add_func ("/brotli/reset", test_brotli_reset);
155
156 ret = g_test_run ();
157 test_cleanup ();
158 return ret;
159 }
160