2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
29 /* Unit tests for hb-blob.h */
31 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT) && defined(HAVE_MMAP)
35 #ifdef HAVE_SYS_MMAN_H
38 #endif /* HAVE_UNISTD_H */
40 #endif /* HAVE_SYS_MMAN_H */
46 test_blob_empty (void)
53 g_assert (hb_blob_is_immutable (hb_blob_get_empty ()));
54 g_assert (hb_blob_get_empty () != NULL);
55 g_assert (hb_blob_get_empty () == hb_blob_create (NULL, 0, HB_MEMORY_MODE_READONLY, NULL, NULL));
57 blob = hb_blob_get_empty ();
58 g_assert (blob == hb_blob_get_empty ());
60 len = hb_blob_get_length (blob);
61 g_assert_cmpint (len, ==, 0);
63 data = hb_blob_get_data (blob, NULL);
64 g_assert (data == NULL);
66 data = hb_blob_get_data (blob, &len);
67 g_assert (data == NULL);
68 g_assert_cmpint (len, ==, 0);
70 data_writable = hb_blob_get_data_writable (blob, NULL);
71 g_assert (data == NULL);
73 data_writable = hb_blob_get_data_writable (blob, &len);
74 g_assert (data_writable == NULL);
75 g_assert_cmpint (len, ==, 0);
78 static const char test_data[] = "test\0data";
80 static const char *blob_names[] = {
85 , "readonly-may-make-writable"
98 free_up (fixture_t *fixture)
100 g_assert_cmpint (fixture->freed, ==, 0);
105 free_up_free (fixture_t *fixture)
108 free (fixture->data);
116 uintptr_t pagesize = -1;
118 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
119 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
120 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
121 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
122 #elif defined(HAVE_GETPAGESIZE)
123 pagesize = (uintptr_t) getpagesize ();
126 g_assert (pagesize != (uintptr_t) -1);
132 free_up_munmap (fixture_t *fixture)
135 munmap (fixture->data, get_pagesize ());
141 fixture_init (fixture_t *fixture, gconstpointer user_data)
143 hb_memory_mode_t mm = (hb_memory_mode_t) GPOINTER_TO_INT (user_data);
146 hb_destroy_func_t free_func;
148 switch (GPOINTER_TO_INT (user_data))
150 case HB_MEMORY_MODE_DUPLICATE:
152 len = sizeof (test_data);
153 free_func = (hb_destroy_func_t) free_up;
156 case HB_MEMORY_MODE_READONLY:
158 len = sizeof (test_data);
159 free_func = (hb_destroy_func_t) free_up;
162 case HB_MEMORY_MODE_WRITABLE:
163 data = malloc (sizeof (test_data));
164 memcpy ((char *) data, test_data, sizeof (test_data));
165 len = sizeof (test_data);
166 free_func = (hb_destroy_func_t) free_up_free;
170 case HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE:
172 uintptr_t pagesize = get_pagesize ();
174 data = mmap (NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
175 g_assert (data != (char *) -1);
176 memcpy ((char *) data, test_data, sizeof (test_data));
177 mprotect ((char *) data, pagesize, PROT_READ);
178 len = sizeof (test_data);
179 free_func = (hb_destroy_func_t) free_up_munmap;
185 g_assert_not_reached ();
189 fixture->data = (char *) data;
191 fixture->blob = hb_blob_create (data, len, mm, fixture, free_func);
195 fixture_finish (fixture_t *fixture, gconstpointer user_data)
197 hb_blob_destroy (fixture->blob);
198 g_assert_cmpint (fixture->freed, ==, 1);
203 test_blob (fixture_t *fixture, gconstpointer user_data)
205 hb_blob_t *b = fixture->blob;
206 hb_memory_mode_t mm = GPOINTER_TO_INT (user_data);
214 len = hb_blob_get_length (b);
215 g_assert_cmpint (len, ==, fixture->len);
217 data = hb_blob_get_data (b, &len);
218 g_assert_cmpint (len, ==, fixture->len);
219 if (mm == HB_MEMORY_MODE_DUPLICATE) {
220 g_assert (data != fixture->data);
221 g_assert_cmpint (fixture->freed, ==, 1);
222 mm = HB_MEMORY_MODE_WRITABLE;
224 g_assert (data == fixture->data);
225 g_assert_cmpint (fixture->freed, ==, 0);
228 data_writable = hb_blob_get_data_writable (b, &len);
229 g_assert_cmpint (len, ==, fixture->len);
230 g_assert (data_writable);
231 g_assert (0 == memcmp (data_writable, fixture->data, fixture->len));
232 if (mm == HB_MEMORY_MODE_READONLY) {
233 g_assert (data_writable != data);
234 g_assert_cmpint (fixture->freed, ==, 1);
236 g_assert (data_writable == data);
239 data = hb_blob_get_data (b, &len);
240 g_assert_cmpint (len, ==, fixture->len);
241 g_assert (data == data_writable);
243 memset (data_writable, 0, fixture->len);
245 /* Now, make it immutable and watch get_data_writable() fail */
247 g_assert (!hb_blob_is_immutable (b));
248 hb_blob_make_immutable (b);
249 g_assert (hb_blob_is_immutable (b));
251 data_writable = hb_blob_get_data_writable (b, &len);
252 g_assert (!data_writable);
253 g_assert_cmpint (len, ==, 0);
255 data = hb_blob_get_data (b, &len);
256 g_assert_cmpint (len, ==, fixture->len);
257 for (i = 0; i < len; i++)
258 g_assert ('\0' == data[i]);
262 test_blob_subblob (fixture_t *fixture, gconstpointer user_data)
264 hb_blob_t *b = fixture->blob;
268 fixture->blob = hb_blob_create_sub_blob (b, 1, fixture->len);
271 test_blob (fixture, user_data);
279 main (int argc, char **argv)
283 hb_test_init (&argc, &argv);
285 hb_test_add (test_blob_empty);
287 for (i = 0; i < G_N_ELEMENTS (blob_names); i++)
289 const void *blob_type = GINT_TO_POINTER (i);
290 const char *blob_name = blob_names[i];
292 hb_test_add_fixture_flavor (fixture, blob_type, blob_name, test_blob);
293 hb_test_add_fixture_flavor (fixture, blob_type, blob_name, test_blob_subblob);
300 return hb_test_run ();