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