Fix CID 1491093: attrib leaked if attvalue is null
[claws.git] / src / tests / entity_test.c
blob8ff23f90140c57f09d41223eeabb4268a32c69f1
1 #include "config.h"
3 #include <glib.h>
4 #include <stdio.h>
6 #include "mock_debug_print.h"
8 #include "entity.h"
10 static void
11 test_entity_invalid(void)
13 gchar *result;
15 /* Invalid entity strings */
16 result = entity_decode(NULL);
17 g_assert_null(result);
18 result = entity_decode("");
19 g_assert_null(result);
20 result = entity_decode("foo");
21 g_assert_null(result);
22 result = entity_decode("&");
23 g_assert_null(result);
24 result = entity_decode("&;");
25 g_assert_null(result);
26 result = entity_decode("&#");
27 g_assert_null(result);
28 result = entity_decode("&#;");
29 g_assert_null(result);
31 /* Valid entity string, but with missing semicolon */
32 result = entity_decode("&Aacute");
33 g_assert_null(result);
34 result = entity_decode("&#123");
35 g_assert_null(result);
38 static void
39 test_entity_toolong(void)
41 gchar *result;
43 /* Largest unicode code point is 0x10ffff, let's test that,
44 * and one past it */
45 result = entity_decode("&#1114111;");
46 g_assert_nonnull(result);
47 g_free(result);
48 result = entity_decode("&#1114112;");
49 g_assert_null(result);
51 /* ENTITY_MAX_LEN is 8, test 8- and 9-char entity strings
52 * for possible buffer overflows */
53 result = entity_decode("&#88888888;");
54 g_assert_null(result);
55 result = entity_decode("&#999999999;");
56 g_assert_null(result);
59 static void
60 test_entity_unprintable(void)
62 gchar *result, numstr[6]; /* "&#XX;" */
63 gint i;
65 for (i = 0; i < 32; i++) {
66 sprintf(numstr, "&#%d;", i);
67 result = entity_decode(numstr);
68 g_assert_nonnull(result);
69 g_assert_cmpstr(result, ==, "\xef\xbf\xbd");
70 g_free(result);
74 static void
75 test_entity_valid(void)
77 gchar *result;
79 result = entity_decode("&Aacute;");
80 g_assert_nonnull(result);
81 if (g_test_verbose())
82 g_printerr("result '%s'\n", result);
83 g_assert_cmpstr(result, ==, "Á");
84 g_free(result);
86 result = entity_decode("&#123;");
87 g_assert_nonnull(result);
88 if (g_test_verbose())
89 g_printerr("result '%s'\n", result);
90 g_assert_cmpstr(result, ==, "{");
91 g_free(result);
95 int
96 main(int argc, char *argv[])
98 g_test_init(&argc, &argv, NULL);
100 g_test_add_func("/core/entity/invalid", test_entity_invalid);
101 g_test_add_func("/core/entity/toolong", test_entity_toolong);
102 g_test_add_func("/core/entity/unprintable", test_entity_unprintable);
103 g_test_add_func("/core/entity/valid", test_entity_valid);
105 return g_test_run();