Branch data Line data Source code
1 : : /* GLib testing framework examples and tests
2 : : * Copyright (C) 2024 Red Hat, Inc.
3 : : * Authors: Matthias Clasen
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include <glib/glib.h>
22 : : #include <gio/gio.h>
23 : : #include <stdlib.h>
24 : : #include <string.h>
25 : :
26 : : static void
27 : 2 : test_charset_unsupported (void)
28 : : {
29 : : GCharsetConverter *converter;
30 : 2 : GError *error = NULL;
31 : :
32 : 2 : converter = g_charset_converter_new ("UTF-8",
33 : : "this-charset-does-not-exist",
34 : : &error);
35 : 2 : g_assert_null (converter);
36 : 2 : g_assert_nonnull (error);
37 : 2 : g_assert_cmpuint (error->domain, ==, G_IO_ERROR);
38 : 2 : g_clear_error (&error);
39 : 2 : }
40 : :
41 : : static void
42 : 2 : test_charset_cancellable (void)
43 : : {
44 : : GCharsetConverter *converter;
45 : : GCancellable *cancellable;
46 : 2 : GError *error = NULL;
47 : :
48 : 2 : cancellable = g_cancellable_new ();
49 : 2 : g_cancellable_cancel (cancellable);
50 : 3 : converter = g_initable_new (G_TYPE_CHARSET_CONVERTER,
51 : 1 : cancellable,
52 : : &error,
53 : : "to-charset", "UTF-8",
54 : : "from-charset", "UTF-8",
55 : : NULL);
56 : 2 : g_assert_null (converter);
57 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
58 : :
59 : 2 : g_clear_error (&error);
60 : 2 : g_object_unref (cancellable);
61 : 2 : }
62 : :
63 : : static void
64 : 2 : test_extra_bytes_at_end (void)
65 : : {
66 : : char data[1024];
67 : : gsize size;
68 : : GBytes *bytes;
69 : : GConverter *converter;
70 : 2 : GError *error = NULL;
71 : : GBytes *result;
72 : :
73 : : /* Create some simple data to encode */
74 : 2 : data[0] = 0;
75 : 2 : bytes = g_bytes_new_static (data, 1);
76 : :
77 : : /* encode the data */
78 : 2 : converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9));
79 : 2 : result = g_converter_convert_bytes (converter, bytes, &error);
80 : 2 : g_assert_no_error (error);
81 : 2 : g_assert_nonnull (result);
82 : 2 : g_bytes_unref (bytes);
83 : 2 : g_clear_object (&converter);
84 : :
85 : : /* Append a 0 byte to the encoded data */
86 : 2 : size = g_bytes_get_size (result);
87 : 2 : g_assert_cmpuint (size, <, G_N_ELEMENTS (data)); /* just to be very sure */
88 : 2 : memcpy (data, g_bytes_get_data (result, NULL), size);
89 : 2 : data[size] = 0;
90 : 2 : bytes = g_bytes_new_static (data, size + 1);
91 : 2 : g_bytes_unref (result);
92 : :
93 : : /* Decompress the just compressed bytes with the extra 0 */
94 : 2 : converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
95 : 2 : result = g_converter_convert_bytes (converter, bytes, &error);
96 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_MESSAGE_TOO_LARGE);
97 : 2 : g_assert_null (result);
98 : 2 : g_clear_error (&error);
99 : :
100 : 2 : g_object_unref (converter);
101 : 2 : g_bytes_unref (bytes);
102 : 2 : }
103 : :
104 : : static void
105 : 2 : test_convert_bytes (void)
106 : : {
107 : : char data[8192];
108 : : GBytes *bytes;
109 : : GConverter *converter;
110 : 2 : GError *error = NULL;
111 : : GBytes *result;
112 : :
113 : 16386 : for (gsize i = 0; i < sizeof (data); i++)
114 : 16384 : data[i] = g_test_rand_int_range (0, 256);
115 : :
116 : 2 : bytes = g_bytes_new_static (data, sizeof (data));
117 : :
118 : 2 : converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9));
119 : 2 : result = g_converter_convert_bytes (converter, bytes, &error);
120 : 2 : g_assert_no_error (error);
121 : 2 : g_assert_nonnull (result);
122 : :
123 : 2 : g_bytes_unref (result);
124 : 2 : g_object_unref (converter);
125 : :
126 : 2 : converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
127 : 2 : result = g_converter_convert_bytes (converter, bytes, &error);
128 : 2 : g_assert_nonnull (error);
129 : 2 : g_error_free (error);
130 : 2 : g_assert_true (result == NULL);
131 : :
132 : 2 : g_object_unref (converter);
133 : 2 : g_bytes_unref (bytes);
134 : 2 : }
135 : :
136 : : static void
137 : 2 : test_gzip_os_property (void)
138 : : {
139 : 2 : const int os_testvalue = 42;
140 : 2 : const char *data = "Hello World";
141 : : GBytes *bytes;
142 : : GZlibCompressor *compressor;
143 : 2 : GError *error = NULL;
144 : : GBytes *result;
145 : : int os;
146 : : const char *encoded;
147 : :
148 : 2 : bytes = g_bytes_new_static (data, sizeof (data));
149 : :
150 : 2 : compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9);
151 : 2 : g_zlib_compressor_set_os (compressor, os_testvalue);
152 : 2 : g_assert_cmpint (g_zlib_compressor_get_os (compressor), ==, os_testvalue);
153 : :
154 : 2 : g_object_get (compressor, "os", &os, NULL);
155 : 2 : g_assert_cmpint (os, ==, os_testvalue);
156 : :
157 : 2 : result = g_converter_convert_bytes (G_CONVERTER (compressor), bytes, &error);
158 : 2 : g_assert_no_error (error);
159 : 2 : g_assert_nonnull (result);
160 : :
161 : : /* The OS is stored in the 10th byte of the header, see RFC 1952 */
162 : 2 : g_assert_cmpint (g_bytes_get_size (result), >, 10);
163 : 2 : encoded = g_bytes_get_data (result, NULL);
164 : 2 : g_assert_cmpint (encoded[9], ==, os_testvalue);
165 : :
166 : 2 : g_bytes_unref (result);
167 : 2 : g_object_unref (compressor);
168 : 2 : g_bytes_unref (bytes);
169 : 2 : }
170 : :
171 : : int
172 : 2 : main (int argc,
173 : : char *argv[])
174 : : {
175 : 2 : g_test_init (&argc, &argv, NULL);
176 : :
177 : 2 : g_test_add_func ("/converter/charset-unsupported", test_charset_unsupported);
178 : 2 : g_test_add_func ("/converter/charset-cancellable", test_charset_cancellable);
179 : 2 : g_test_add_func ("/converter/bytes", test_convert_bytes);
180 : 2 : g_test_add_func ("/converter/extra-bytes-at-end", test_extra_bytes_at_end);
181 : 2 : g_test_add_func ("/converter/gzip-os-property", test_gzip_os_property);
182 : :
183 : 2 : return g_test_run();
184 : : }
|