Mon, 12 May 2025 20:25:16 -0500
Handle formatting in server messages
Testing Done:
Used the default motd on my local ergo server to verify that formatting was working.
Reviewed at https://reviews.imfreedom.org/r/3991/
| 38286 | 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> | |
|
38426
e35d277efcce
tests: Add missing string.h includes
Mike Ruprecht <cmaiku@gmail.com>
parents:
38286
diff
changeset
|
24 | #include <string.h> |
| 38286 | 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, | |
|
42866
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
61 | const char *path, |
|
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
62 | const char *ext, |
|
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
63 | const char *mimetype) |
| 38286 | 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 | ||
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
76 | /* if the caller provided a path, check it, otherwise just make sure we |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
77 | * have something. |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
78 | */ |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
79 | if(path != NULL) { |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
80 | g_assert_cmpstr(purple_image_get_path(image), ==, path); |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
81 | } else { |
|
42866
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
82 | const char *apath = purple_image_get_path(image); |
|
38573
e890b91f60fe
Use g_test_set_nonfatal_assertions when available
Gary Kramlich <grim@reaperworld.com>
parents:
38570
diff
changeset
|
83 | |
|
e890b91f60fe
Use g_test_set_nonfatal_assertions when available
Gary Kramlich <grim@reaperworld.com>
parents:
38570
diff
changeset
|
84 | g_assert(apath); |
|
e890b91f60fe
Use g_test_set_nonfatal_assertions when available
Gary Kramlich <grim@reaperworld.com>
parents:
38570
diff
changeset
|
85 | g_assert_cmpstr(apath, !=, ""); |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
86 | } |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
87 | |
| 38286 | 88 | g_assert_cmpstr(purple_image_get_extension(image), ==, ext); |
| 89 | g_assert_cmpstr(purple_image_get_mimetype(image), ==, mimetype); | |
| 90 | ||
|
42592
6b65c0e4ba15
Remove unnecessary casts for GObject methods
Gary Kramlich <grim@reaperworld.com>
parents:
42184
diff
changeset
|
91 | g_object_unref(image); |
| 38286 | 92 | } |
| 93 | ||
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
94 | /****************************************************************************** |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
95 | * Tests |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
96 | *****************************************************************************/ |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
97 | static void |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
98 | test_image_new_from_bytes(void) { |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
99 | GBytes *bytes = g_bytes_new(test_image_data, test_image_data_len); |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
100 | PurpleImage *image = purple_image_new_from_bytes(bytes); |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
101 | |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
102 | _test_image( |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
103 | image, |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
104 | g_bytes_get_data(bytes, NULL), |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
105 | g_bytes_get_size(bytes), |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
106 | NULL, |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
107 | "png", |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
108 | "image/png" |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
109 | ); |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
110 | |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
111 | g_bytes_unref(bytes); |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
112 | } |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
113 | |
|
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
114 | |
| 38286 | 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, | |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
126 | NULL, |
| 38286 | 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; | |
|
42866
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
136 | char *path = NULL; |
|
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
137 | char *edata = NULL; |
| 38286 | 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, | |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
151 | path, |
| 38286 | 152 | "png", |
| 153 | "image/png" | |
| 154 | ); | |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
155 | |
|
42184
4e1bf25f5575
Fix several leaks in tests
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
38994
diff
changeset
|
156 | g_free(edata); |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
157 | g_free(path); |
| 38286 | 158 | } |
| 159 | ||
| 160 | /****************************************************************************** | |
| 161 | * Main | |
| 162 | *****************************************************************************/ | |
|
42866
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
163 | int |
|
4b201e18638f
Modernize most of the unit tests
Gary Kramlich <grim@reaperworld.com>
parents:
42592
diff
changeset
|
164 | main(int argc, char **argv) { |
| 38286 | 165 | g_test_init(&argc, &argv, NULL); |
|
38573
e890b91f60fe
Use g_test_set_nonfatal_assertions when available
Gary Kramlich <grim@reaperworld.com>
parents:
38570
diff
changeset
|
166 | g_test_set_nonfatal_assertions(); |
|
e890b91f60fe
Use g_test_set_nonfatal_assertions when available
Gary Kramlich <grim@reaperworld.com>
parents:
38570
diff
changeset
|
167 | |
|
38568
9463c7db20fa
Set the path on the image for purple_image_new_from_file, update/add some tests
Gary Kramlich <grim@reaperworld.com>
parents:
38286
diff
changeset
|
168 | g_test_add_func("/image/new-from-bytes", test_image_new_from_bytes); |
| 38286 | 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 | } |