1 /* 2 * Copyright (c) 2023, Oracle and/or its affiliates. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 /* Test code for functions in src/StrToJust.c */ 25 #include "config.h" 26 #include <X11/Xmu/Converters.h> 27 #include <X11/Xmu/CharSet.h> 28 #include <glib.h> 29 30 struct TestData { 31 const char *name; 32 int value; 33 }; 34 35 static const struct TestData data[] = { 36 { XtEleft, XtJustifyLeft }, 37 { XtEcenter, XtJustifyCenter }, 38 { XtEright, XtJustifyRight }, 39 }; 40 #define DATA_ENTRIES (sizeof(data) / sizeof(data[0])) 41 42 static int warning_count; 43 44 static void 45 xt_warning_handler(String message) 46 { 47 g_test_message("Caught warning: %s", message ? message : "<NULL>"); 48 warning_count++; 49 } 50 51 52 53 static void 54 test_XmuCvtStringToJustify(void) 55 { 56 XrmValue from, to; 57 Cardinal nargs = 0; 58 59 char namebuf[16]; 60 61 for (unsigned int i = 0; i < DATA_ENTRIES; i++) { 62 g_test_message("StringToJustify(%s)", data[i].name); 63 64 strncpy(namebuf, data[i].name, sizeof(namebuf) - 1); 65 namebuf[sizeof(namebuf) - 1] = 0; 66 from.addr = namebuf; 67 from.size = sizeof(char *); 68 XmuCvtStringToJustify(NULL, &nargs, &from, &to); 69 g_assert_cmpint(*(int *)to.addr, ==, data[i].value); 70 g_assert_cmpint(to.size, ==, sizeof(int)); 71 72 73 XmuNCopyISOLatin1Uppered(namebuf, data[i].name, sizeof(namebuf)); 74 from.addr = namebuf; 75 from.size = sizeof(char *); 76 XmuCvtStringToJustify(NULL, &nargs, &from, &to); 77 g_assert_cmpint(*(int *)to.addr, ==, data[i].value); 78 g_assert_cmpint(to.size, ==, sizeof(int)); 79 } 80 81 /* No warning is currently issued for unused args */ 82 #if 0 83 warning_count = 0; 84 nargs = 1; 85 g_test_message("StringToJustify with extra args"); 86 XmuCvtStringToJustify(&args, &nargs, &from, &to); 87 g_assert_cmpint(warning_count, >, 0); 88 #endif 89 90 /* Verify warning issued for unknown string */ 91 warning_count = 0; 92 from.addr = (char *) "DoesNotExist"; 93 nargs = 0; 94 g_test_message("StringToJustify(%s)", from.addr); 95 XmuCvtStringToJustify(NULL, &nargs, &from, &to); 96 g_assert_cmpint(warning_count, >, 0); 97 } 98 99 static void 100 test_XmuCvtJustifyToString(void) 101 { 102 XrmValue from, to; 103 int value; 104 Cardinal nargs = 0; 105 Boolean ret; 106 char namebuf[16]; 107 108 109 for (unsigned int i = 0; i < DATA_ENTRIES; i++) { 110 g_test_message("JustifyToString(%d)", data[i].value); 111 112 value = data[i].value; 113 from.addr = (XPointer) &value; 114 from.size = sizeof(int *); 115 116 /* First test without providing a buffer to copy the string into */ 117 to.addr = NULL; 118 to.size = 0; 119 ret = XmuCvtJustifyToString(NULL, NULL, &nargs, &from, &to, NULL); 120 g_assert_cmpint(ret, ==, True); 121 g_assert_cmpstr(to.addr, ==, data[i].name); 122 g_assert_cmpint(to.size, ==, sizeof(char *)); 123 124 /* Then test with a buffer that's too small to copy the string into */ 125 to.addr = namebuf; 126 to.size = 4; 127 ret = XmuCvtJustifyToString(NULL, NULL, &nargs, &from, &to, NULL); 128 g_assert_cmpint(ret, ==, False); 129 g_assert_cmpint(to.size, ==, strlen(data[i].name) + 1); 130 131 /* Then test with a buffer that's big enough to copy the string into */ 132 to.addr = namebuf; 133 to.size = sizeof(namebuf); 134 ret = XmuCvtJustifyToString(NULL, NULL, &nargs, &from, &to, NULL); 135 g_assert_cmpint(ret, ==, True); 136 g_assert_cmpstr(to.addr, ==, data[i].name); 137 g_assert_cmpint(to.size, ==, sizeof(char *)); 138 } 139 140 /* Verify warning and return of False for invalid value */ 141 warning_count = 0; 142 value = 1984; 143 from.addr = (XPointer) &value; 144 g_test_message("JustifyToString(%d)", value); 145 ret = XmuCvtJustifyToString(NULL, NULL, &nargs, &from, &to, NULL); 146 g_assert_cmpint(ret, ==, False); 147 g_assert_cmpint(warning_count, >, 0); 148 } 149 150 int 151 main(int argc, char** argv) 152 { 153 g_test_init(&argc, &argv, NULL); 154 g_test_bug_base(PACKAGE_BUGREPORT); 155 156 XtSetWarningHandler(xt_warning_handler); 157 158 g_test_add_func("/StrToJust/XmuCvtStringToJustify", 159 test_XmuCvtStringToJustify); 160 g_test_add_func("/StrToJust/XmuCvtJustifyToString", 161 test_XmuCvtJustifyToString); 162 163 164 return g_test_run(); 165 } 166