xfree86.c revision ed6184df
1/**
2 * Copyright © 2011 Red Hat, Inc.
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 relies on assert() */
25#undef NDEBUG
26
27#ifdef HAVE_DIX_CONFIG_H
28#include <dix-config.h>
29#endif
30
31#include <assert.h>
32
33#include "xf86.h"
34#include "xf86Parser.h"
35
36#include "tests-common.h"
37
38static void
39xfree86_option_list_duplicate(void)
40{
41    XF86OptionPtr options;
42    XF86OptionPtr duplicate;
43    const char *o1 = "foo", *o2 = "bar", *v1 = "one", *v2 = "two";
44    const char *o_null = "NULL";
45    char *val1, *val2;
46    XF86OptionPtr a, b;
47
48    duplicate = xf86OptionListDuplicate(NULL);
49    assert(!duplicate);
50
51    options = xf86AddNewOption(NULL, o1, v1);
52    assert(options);
53    options = xf86AddNewOption(options, o2, v2);
54    assert(options);
55    options = xf86AddNewOption(options, o_null, NULL);
56    assert(options);
57
58    duplicate = xf86OptionListDuplicate(options);
59    assert(duplicate);
60
61    val1 = xf86CheckStrOption(options, o1, "1");
62    val2 = xf86CheckStrOption(duplicate, o1, "2");
63
64    assert(strcmp(val1, v1) == 0);
65    assert(strcmp(val1, val2) == 0);
66
67    val1 = xf86CheckStrOption(options, o2, "1");
68    val2 = xf86CheckStrOption(duplicate, o2, "2");
69
70    assert(strcmp(val1, v2) == 0);
71    assert(strcmp(val1, val2) == 0);
72
73    a = xf86FindOption(options, o_null);
74    b = xf86FindOption(duplicate, o_null);
75    assert(a);
76    assert(b);
77}
78
79static void
80xfree86_add_comment(void)
81{
82    char *current = NULL;
83    const char *comment;
84    char compare[1024] = { 0 };
85
86    comment = "# foo";
87    current = xf86addComment(current, comment);
88    strcpy(compare, comment);
89    strcat(compare, "\n");
90
91    assert(!strcmp(current, compare));
92
93    /* this used to overflow */
94    strcpy(current, "\n");
95    comment = "foobar\n";
96    current = xf86addComment(current, comment);
97    strcpy(compare, "\n#");
98    strcat(compare, comment);
99    assert(!strcmp(current, compare));
100
101    free(current);
102}
103
104int
105xfree86_test(void)
106{
107    xfree86_option_list_duplicate();
108    xfree86_add_comment();
109
110    return 0;
111}
112