entity_test.c (2328B)
1 #include "config.h" 2 3 #include <glib.h> 4 #include <stdio.h> 5 6 #include "mock_debug_print.h" 7 8 #include "entity.h" 9 10 static void 11 test_entity_invalid(void) 12 { 13 gchar *result; 14 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); 30 31 /* Valid entity string, but with missing semicolon */ 32 result = entity_decode("Á"); 33 g_assert_null(result); 34 result = entity_decode("{"); 35 g_assert_null(result); 36 } 37 38 static void 39 test_entity_toolong(void) 40 { 41 gchar *result; 42 43 /* Largest unicode code point is 0x10ffff, let's test that, 44 * and one past it */ 45 result = entity_decode(""); 46 g_assert_nonnull(result); 47 g_free(result); 48 result = entity_decode("�"); 49 g_assert_null(result); 50 51 /* ENTITY_MAX_LEN is 8, test 8- and 9-char entity strings 52 * for possible buffer overflows */ 53 result = entity_decode("�"); 54 g_assert_null(result); 55 result = entity_decode("�"); 56 g_assert_null(result); 57 } 58 59 static void 60 test_entity_unprintable(void) 61 { 62 gchar *result, numstr[6]; /* "&#XX;" */ 63 gint i; 64 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); 71 } 72 } 73 74 static void 75 test_entity_valid(void) 76 { 77 gchar *result; 78 79 result = entity_decode("Á"); 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); 85 86 result = entity_decode("{"); 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); 92 93 } 94 95 int 96 main(int argc, char *argv[]) 97 { 98 g_test_init(&argc, &argv, NULL); 99 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); 104 105 return g_test_run(); 106 }