Home | History | Annotate | Line # | Download | only in test
StrToBS.c revision 1.1
      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/StrToBS.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     { XtEnotUseful, NotUseful },
     37     { XtEwhenMapped, WhenMapped },
     38     { XtEalways, Always },
     39     { XtEdefault, Always + WhenMapped + NotUseful}
     40 };
     41 #define DATA_ENTRIES (sizeof(data) / sizeof(data[0]))
     42 
     43 static int warning_count;
     44 
     45 static void
     46 xt_warning_handler(String message)
     47 {
     48     g_test_message("Caught warning: %s", message ? message : "<NULL>");
     49     warning_count++;
     50 }
     51 
     52 
     53 
     54 static void
     55 test_XmuCvtStringToBackingStore(void)
     56 {
     57     XrmValue from, to, args;
     58     Cardinal nargs = 0;
     59 
     60     char namebuf[16];
     61 
     62     for (unsigned int i = 0; i < DATA_ENTRIES; i++) {
     63         g_test_message("StringToBackingStore(%s)", data[i].name);
     64 
     65         strncpy(namebuf, data[i].name, sizeof(namebuf) - 1);
     66         namebuf[sizeof(namebuf) - 1] = 0;
     67         from.addr = namebuf;
     68         from.size = sizeof(char *);
     69         XmuCvtStringToBackingStore(NULL, &nargs, &from, &to);
     70         g_assert_cmpint(*(int *)to.addr, ==, data[i].value);
     71         g_assert_cmpint(to.size, ==, sizeof(int));
     72 
     73 
     74         XmuNCopyISOLatin1Uppered(namebuf, data[i].name, sizeof(namebuf));
     75         from.addr = namebuf;
     76         from.size = sizeof(char *);
     77         XmuCvtStringToBackingStore(NULL, &nargs, &from, &to);
     78         g_assert_cmpint(*(int *)to.addr, ==, data[i].value);
     79         g_assert_cmpint(to.size, ==, sizeof(int));
     80     }
     81 
     82     /* Verify warning issued for unused args */
     83     warning_count = 0;
     84     nargs = 1;
     85     g_test_message("StringToBackingStore with extra args");
     86     XmuCvtStringToBackingStore(&args, &nargs, &from, &to);
     87     g_assert_cmpint(warning_count, >, 0);
     88 
     89     /* Verify warning issued for unknown string */
     90     warning_count = 0;
     91     from.addr = (char *) "DoesNotExist";
     92     nargs = 0;
     93     g_test_message("StringToBackingStore(%s)", from.addr);
     94     XmuCvtStringToBackingStore(NULL, &nargs, &from, &to);
     95     g_assert_cmpint(warning_count, >, 0);
     96 }
     97 
     98 static void
     99 test_XmuCvtBackingStoreToString(void)
    100 {
    101     XrmValue from, to;
    102     int value;
    103     Cardinal nargs = 0;
    104     Boolean ret;
    105     char namebuf[16];
    106 
    107 
    108     for (unsigned int i = 0; i < DATA_ENTRIES; i++) {
    109         g_test_message("BackingStoreToString(%d)", data[i].value);
    110 
    111         value = data[i].value;
    112         from.addr = (XPointer) &value;
    113         from.size = sizeof(int *);
    114 
    115         /* First test without providing a buffer to copy the string into */
    116         to.addr = NULL;
    117         to.size = 0;
    118         ret = XmuCvtBackingStoreToString(NULL, NULL, &nargs, &from, &to, NULL);
    119         g_assert_cmpint(ret, ==, True);
    120         g_assert_cmpstr(to.addr, ==, data[i].name);
    121         g_assert_cmpint(to.size, ==, sizeof(char *));
    122 
    123         /* Then test with a buffer that's too small to copy the string into */
    124         to.addr = namebuf;
    125         to.size = 4;
    126         ret = XmuCvtBackingStoreToString(NULL, NULL, &nargs, &from, &to, NULL);
    127         g_assert_cmpint(ret, ==, False);
    128         g_assert_cmpint(to.size, ==, strlen(data[i].name) + 1);
    129 
    130         /* Then test with a buffer that's big enough to copy the string into */
    131         to.addr = namebuf;
    132         to.size = sizeof(namebuf);
    133         ret = XmuCvtBackingStoreToString(NULL, NULL, &nargs, &from, &to, NULL);
    134         g_assert_cmpint(ret, ==, True);
    135         g_assert_cmpstr(to.addr, ==, data[i].name);
    136         g_assert_cmpint(to.size, ==, sizeof(char *));
    137     }
    138 
    139     /* Verify warning and return of False for invalid value */
    140     warning_count = 0;
    141     value = 1984;
    142     from.addr = (XPointer) &value;
    143     g_test_message("BackingStoreToString(%d)", value);
    144     ret = XmuCvtBackingStoreToString(NULL, NULL, &nargs, &from, &to, NULL);
    145     g_assert_cmpint(ret, ==, False);
    146     g_assert_cmpint(warning_count, >, 0);
    147 }
    148 
    149 int
    150 main(int argc, char** argv)
    151 {
    152     g_test_init(&argc, &argv, NULL);
    153     g_test_bug_base(PACKAGE_BUGREPORT);
    154 
    155     XtSetWarningHandler(xt_warning_handler);
    156 
    157     g_test_add_func("/StrToBS/XmuCvtStringToBackingStore",
    158                     test_XmuCvtStringToBackingStore);
    159     g_test_add_func("/StrToBS/XmuCvtBackingStoreToString",
    160                     test_XmuCvtBackingStoreToString);
    161 
    162 
    163     return g_test_run();
    164 }
    165