| 1 /* |
|
| 2 * Purple |
|
| 3 * |
|
| 4 * Purple is the legal property of its developers, whose names are too |
|
| 5 * numerous to list here. Please refer to the COPYRIGHT file distributed |
|
| 6 * with this source distribution |
|
| 7 * |
|
| 8 * This program is free software; you can redistribute it and/or modify |
|
| 9 * it under the terms of the GNU General Public License as published by |
|
| 10 * the Free Software Foundation; either version 2 of the License, or (at |
|
| 11 * your option) any later version. |
|
| 12 * |
|
| 13 * This program is distributed in the hope that it will be useful, but |
|
| 14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 16 * General Public License for more details. |
|
| 17 * |
|
| 18 * You should have received a copy of the GNU General Public License |
|
| 19 * along with this program; if not, write to the Free Software |
|
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 21 */ |
|
| 22 |
|
| 23 #include <glib.h> |
|
| 24 #include <string.h> |
|
| 25 |
|
| 26 #include <purple.h> |
|
| 27 |
|
| 28 // generated via: |
|
| 29 // $ cat test-image.png | hexdump -v -e '1 1 "0x%02x," " "' | xargs -n 8 echo |
|
| 30 static const gsize test_image_data_len = 160; |
|
| 31 static const guint8 test_image_data[] = { |
|
| 32 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, |
|
| 33 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, |
|
| 34 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, |
|
| 35 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, |
|
| 36 0x73, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, |
|
| 37 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b, |
|
| 38 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, |
|
| 39 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xe0, |
|
| 40 0x0a, 0x02, 0x16, 0x30, 0x22, 0x28, 0xa4, 0xc9, |
|
| 41 0xdd, 0x00, 0x00, 0x00, 0x1d, 0x69, 0x54, 0x58, |
|
| 42 0x74, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, |
|
| 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x72, 0x65, |
|
| 44 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, |
|
| 45 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x64, 0x2e, |
|
| 46 0x65, 0x07, 0x00, 0x00, 0x00, 0x16, 0x49, 0x44, |
|
| 47 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, 0xff, |
|
| 48 0x3f, 0x03, 0x03, 0x03, 0xe3, 0xb3, 0x4c, 0xb5, |
|
| 49 0x9b, 0x4e, 0x0b, 0x00, 0x2f, 0xa9, 0x06, 0x2f, |
|
| 50 0x8a, 0xd1, 0xc6, 0xb3, 0x00, 0x00, 0x00, 0x00, |
|
| 51 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, |
|
| 52 }; |
|
| 53 |
|
| 54 /****************************************************************************** |
|
| 55 * Helpers |
|
| 56 *****************************************************************************/ |
|
| 57 static void |
|
| 58 _test_image(PurpleImage *image, |
|
| 59 const guint8 *edata, |
|
| 60 gsize elen, |
|
| 61 const char *path, |
|
| 62 const char *ext, |
|
| 63 const char *mimetype) |
|
| 64 { |
|
| 65 GBytes *bytes = NULL; |
|
| 66 const guint8 *adata = NULL; |
|
| 67 gsize alen; |
|
| 68 |
|
| 69 g_assert(PURPLE_IS_IMAGE(image)); |
|
| 70 |
|
| 71 bytes = purple_image_get_contents(image); |
|
| 72 adata = g_bytes_get_data(bytes, &alen); |
|
| 73 g_assert_cmpmem(adata, alen, edata, elen); |
|
| 74 g_bytes_unref(bytes); |
|
| 75 |
|
| 76 /* if the caller provided a path, check it, otherwise just make sure we |
|
| 77 * have something. |
|
| 78 */ |
|
| 79 if(path != NULL) { |
|
| 80 g_assert_cmpstr(purple_image_get_path(image), ==, path); |
|
| 81 } else { |
|
| 82 const char *apath = purple_image_get_path(image); |
|
| 83 |
|
| 84 g_assert(apath); |
|
| 85 g_assert_cmpstr(apath, !=, ""); |
|
| 86 } |
|
| 87 |
|
| 88 g_assert_cmpstr(purple_image_get_extension(image), ==, ext); |
|
| 89 g_assert_cmpstr(purple_image_get_mimetype(image), ==, mimetype); |
|
| 90 |
|
| 91 g_object_unref(image); |
|
| 92 } |
|
| 93 |
|
| 94 /****************************************************************************** |
|
| 95 * Tests |
|
| 96 *****************************************************************************/ |
|
| 97 static void |
|
| 98 test_image_new_from_bytes(void) { |
|
| 99 GBytes *bytes = g_bytes_new(test_image_data, test_image_data_len); |
|
| 100 PurpleImage *image = purple_image_new_from_bytes(bytes); |
|
| 101 |
|
| 102 _test_image( |
|
| 103 image, |
|
| 104 g_bytes_get_data(bytes, NULL), |
|
| 105 g_bytes_get_size(bytes), |
|
| 106 NULL, |
|
| 107 "png", |
|
| 108 "image/png" |
|
| 109 ); |
|
| 110 |
|
| 111 g_bytes_unref(bytes); |
|
| 112 } |
|
| 113 |
|
| 114 |
|
| 115 static void |
|
| 116 test_image_new_from_data(void) { |
|
| 117 PurpleImage *image = purple_image_new_from_data( |
|
| 118 test_image_data, |
|
| 119 test_image_data_len |
|
| 120 ); |
|
| 121 |
|
| 122 _test_image( |
|
| 123 image, |
|
| 124 test_image_data, |
|
| 125 test_image_data_len, |
|
| 126 NULL, |
|
| 127 "png", |
|
| 128 "image/png" |
|
| 129 ); |
|
| 130 } |
|
| 131 |
|
| 132 static void |
|
| 133 test_image_new_from_file(void) { |
|
| 134 PurpleImage *image = NULL; |
|
| 135 GError *error = NULL; |
|
| 136 char *path = NULL; |
|
| 137 char *edata = NULL; |
|
| 138 gsize elen; |
|
| 139 |
|
| 140 path = g_build_filename(TEST_DATA_DIR, "test-image.png", NULL); |
|
| 141 image = purple_image_new_from_file(path, &error); |
|
| 142 g_assert_no_error(error); |
|
| 143 |
|
| 144 g_file_get_contents(path, &edata, &elen, &error); |
|
| 145 g_assert_no_error(error); |
|
| 146 |
|
| 147 _test_image( |
|
| 148 image, |
|
| 149 (guint8 *)edata, |
|
| 150 elen, |
|
| 151 path, |
|
| 152 "png", |
|
| 153 "image/png" |
|
| 154 ); |
|
| 155 |
|
| 156 g_free(edata); |
|
| 157 g_free(path); |
|
| 158 } |
|
| 159 |
|
| 160 /****************************************************************************** |
|
| 161 * Main |
|
| 162 *****************************************************************************/ |
|
| 163 int |
|
| 164 main(int argc, char **argv) { |
|
| 165 g_test_init(&argc, &argv, NULL); |
|
| 166 g_test_set_nonfatal_assertions(); |
|
| 167 |
|
| 168 g_test_add_func("/image/new-from-bytes", test_image_new_from_bytes); |
|
| 169 g_test_add_func("/image/new-from-data", test_image_new_from_data); |
|
| 170 g_test_add_func("/image/new-from-file", test_image_new_from_file); |
|
| 171 |
|
| 172 return g_test_run(); |
|
| 173 } |
|