| 1 | /* |
| 2 | * Copyright 2023 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Evgenii Kolesnikov <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include "common/util.h" |
| 31 | #include "common/public/oscap_helpers.h" |
| 32 | |
| 33 | int test_oscap_path_startswith(void); |
| 34 | int test_oscap_strrm(void); |
| 35 | int test_oscap_indent(void); |
| 36 | int test_oscap_remove_excess_whitespace(void); |
| 37 | |
| 38 | int test_oscap_path_startswith() |
| 39 | { |
| 40 | if (!oscap_path_startswith("/", "/")) |
| 41 | return 1; |
| 42 | if (!oscap_path_startswith("/aaa", "/")) |
| 43 | return 2; |
| 44 | if (!oscap_path_startswith("/aaa", "/aaa")) |
| 45 | return 3; |
| 46 | if (!oscap_path_startswith("/aaa/bbb", "/aaa")) |
| 47 | return 4; |
| 48 | if (!oscap_path_startswith("/aaa/bbb/ccc", "/aaa")) |
| 49 | return 5; |
| 50 | if (!oscap_path_startswith("/aaa/bbb/ccc/", "/aaa/")) |
| 51 | return 6; |
| 52 | if (!oscap_path_startswith("/aaa/bbb/ccc", "/aaa/bbb/ccc")) |
| 53 | return 7; |
| 54 | if (!oscap_path_startswith("/aaa/bbb/ccc", "")) |
| 55 | return 8; |
| 56 | if (!oscap_path_startswith("", "")) |
| 57 | return 9; |
| 58 | if (!oscap_path_startswith("", "/")) |
| 59 | return 10; |
| 60 | |
| 61 | if (oscap_path_startswith("/bbb", "/aaa")) |
| 62 | return 101; |
| 63 | if (oscap_path_startswith("/bbb/aaa", "/aaa")) |
| 64 | return 102; |
| 65 | if (oscap_path_startswith("/bbb/aaa/", "/aaa")) |
| 66 | return 103; |
| 67 | if (oscap_path_startswith("/bbb/aaa/", "/aaa/")) |
| 68 | return 104; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int test_oscap_strrm() |
| 74 | { |
| 75 | char str[] = "abcdef12345678def90"; |
| 76 | size_t len = strlen(str); |
| 77 | |
| 78 | oscap_strrm(str, "0"); |
| 79 | if (strncmp(str, "abcdef12345678def9", len) != 0) |
| 80 | return 1; |
| 81 | |
| 82 | oscap_strrm(str, "abc"); |
| 83 | if (strcmp(str, "def12345678def9") != 0) |
| 84 | return 2; |
| 85 | |
| 86 | oscap_strrm(str, "def"); |
| 87 | if (strcmp(str, "123456789") != 0) |
| 88 | return 3; |
| 89 | |
| 90 | oscap_strrm(str, "5"); |
| 91 | if (strcmp(str, "12346789") != 0) |
| 92 | return 4; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int test_oscap_indent() |
| 98 | { |
| 99 | char *indented; |
| 100 | |
| 101 | char *str1 = "hello"; |
| 102 | indented = oscap_indent(str1, 2); |
| 103 | if (strcmp(indented, " hello") != 0) |
| 104 | return 1; |
| 105 | free(indented); |
| 106 | |
| 107 | char *str2 = "hello"; |
| 108 | indented = oscap_indent(str2, 0); |
| 109 | if (strcmp(indented, "hello") != 0) |
| 110 | return 2; |
| 111 | free(indented); |
| 112 | |
| 113 | char *str3 = "hello\nworld\nsee you tomorrow"; |
| 114 | indented = oscap_indent(str3, 4); |
| 115 | if (strcmp(indented, " hello\n world\n see you tomorrow") != 0) |
| 116 | return 3; |
| 117 | free(indented); |
| 118 | |
| 119 | char *str4 = ""; |
| 120 | indented = oscap_indent(str4, 2); |
| 121 | if (strcmp(indented, " ") != 0) |
| 122 | return 4; |
| 123 | free(indented); |
| 124 | |
| 125 | char *str5 = "hello\n\n\n\nbye\n"; |
| 126 | indented = oscap_indent(str5, 2); |
| 127 | if (strcmp(indented, " hello\n\n\n\n bye\n") != 0) |
| 128 | return 5; |
| 129 | free(indented); |
| 130 | |
| 131 | char *str6 = "\n\n\n\n\n\n\n\n"; |
| 132 | indented = oscap_indent(str6, 2); |
| 133 | if (strcmp(indented, "\n\n\n\n\n\n\n\n") != 0) |
| 134 | return 6; |
| 135 | free(indented); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int test_oscap_remove_excess_whitespace() |
| 141 | { |
| 142 | char *result; |
| 143 | |
| 144 | char *str1 = " hello \n world \n"; |
| 145 | result = oscap_remove_excess_whitespace(str1); |
| 146 | if (strcmp(result, "hello\nworld\n") != 0) |
| 147 | return 1; |
| 148 | free(result); |
| 149 | |
| 150 | char *str2 = " hello world "; |
| 151 | result = oscap_remove_excess_whitespace(str2); |
| 152 | if (strcmp(result, "hello world") != 0) |
| 153 | return 2; |
| 154 | free(result); |
| 155 | |
| 156 | char *str3 = " \n \n \n"; |
| 157 | result = oscap_remove_excess_whitespace(str3); |
| 158 | if (strcmp(result, "") != 0) |
| 159 | return 3; |
| 160 | free(result); |
| 161 | |
| 162 | char *str4 = ""; |
| 163 | result = oscap_remove_excess_whitespace(str4); |
| 164 | if (strcmp(result, "") != 0) |
| 165 | return 4; |
| 166 | free(result); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int main (int argc, char *argv[]) |
| 172 | { |
| 173 | int retval = 0; |
| 174 | |
| 175 | if ((retval = test_oscap_path_startswith()) != 0) |
| 176 | return retval; |
| 177 | if ((retval = test_oscap_strrm()) != 0) |
| 178 | return retval; |
| 179 | if ((retval = test_oscap_indent()) != 0) |
| 180 | return retval; |
| 181 | if ((retval = test_oscap_remove_excess_whitespace()) != 0) |
| 182 | return retval; |
| 183 | |
| 184 | return retval; |
| 185 | } |