normalize_test.c (5607B)
1 #include <cwalk.h> 2 #include <limits.h> 3 #include <memory.h> 4 #include <stdint.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 int normalize_forward_slashes(void) 10 { 11 size_t count; 12 char result[FILENAME_MAX]; 13 char *input, *expected; 14 15 cwk_path_set_style(CWK_STYLE_WINDOWS); 16 17 input = "C:/this/is/a/test/path"; 18 strcpy(result, input); 19 expected = "C:\\this\\is\\a\\test\\path"; 20 count = cwk_path_normalize(result, result, sizeof(result)); 21 if (count != strlen(expected) || strcmp(result, expected) != 0) { 22 return EXIT_FAILURE; 23 } 24 25 return EXIT_SUCCESS; 26 } 27 28 int normalize_back_after_root(void) 29 { 30 size_t count; 31 char result[FILENAME_MAX]; 32 char *input, *expected; 33 34 cwk_path_set_style(CWK_STYLE_WINDOWS); 35 36 input = "C:\\..\\this\\is\\a\\test\\path"; 37 strcpy(result, input); 38 expected = "C:\\this\\is\\a\\test\\path"; 39 count = cwk_path_normalize(result, result, sizeof(result)); 40 if (count != strlen(expected) || strcmp(result, expected) != 0) { 41 return EXIT_FAILURE; 42 } 43 44 return EXIT_SUCCESS; 45 } 46 47 int normalize_only_separators(void) 48 { 49 size_t count; 50 char result[FILENAME_MAX]; 51 char *input, *expected; 52 53 cwk_path_set_style(CWK_STYLE_UNIX); 54 55 input = "////"; 56 strcpy(result, input); 57 expected = "/"; 58 count = cwk_path_normalize(result, result, sizeof(result)); 59 if (count != strlen(expected) || strcmp(result, expected) != 0) { 60 return EXIT_FAILURE; 61 } 62 63 return EXIT_SUCCESS; 64 } 65 66 int normalize_empty(void) 67 { 68 size_t count; 69 char result[FILENAME_MAX]; 70 char *input, *expected; 71 72 cwk_path_set_style(CWK_STYLE_UNIX); 73 74 input = "test/.."; 75 strcpy(result, input); 76 expected = "."; 77 count = cwk_path_normalize(result, result, sizeof(result)); 78 if (count != strlen(expected) || strcmp(result, expected) != 0) { 79 return EXIT_FAILURE; 80 } 81 82 return EXIT_SUCCESS; 83 } 84 85 int normalize_overlap(void) 86 { 87 size_t count; 88 char result[FILENAME_MAX]; 89 char *input, *expected; 90 91 cwk_path_set_style(CWK_STYLE_UNIX); 92 93 input = "/var/./logs/.//test/..//..//////"; 94 strcpy(result, input); 95 expected = "/var"; 96 count = cwk_path_normalize(result, result, sizeof(result)); 97 if (count != strlen(expected) || strcmp(result, expected) != 0) { 98 return EXIT_FAILURE; 99 } 100 101 return EXIT_SUCCESS; 102 } 103 104 int normalize_mixed(void) 105 { 106 size_t count; 107 char result[FILENAME_MAX]; 108 char *input, *expected; 109 110 cwk_path_set_style(CWK_STYLE_UNIX); 111 112 input = "/var/./logs/.//test/..//..//////"; 113 expected = "/var"; 114 count = cwk_path_normalize(input, result, sizeof(result)); 115 if (count != strlen(expected) || strcmp(result, expected) != 0) { 116 return EXIT_FAILURE; 117 } 118 119 return EXIT_SUCCESS; 120 } 121 122 int normalize_remove_current(void) 123 { 124 size_t count; 125 char result[FILENAME_MAX]; 126 char *input, *expected; 127 128 cwk_path_set_style(CWK_STYLE_UNIX); 129 130 input = "/var/././././"; 131 expected = "/var"; 132 count = cwk_path_normalize(input, result, sizeof(result)); 133 if (count != strlen(expected) || strcmp(result, expected) != 0) { 134 return EXIT_FAILURE; 135 } 136 137 return EXIT_SUCCESS; 138 } 139 140 int normalize_double_separator(void) 141 { 142 size_t count; 143 char result[FILENAME_MAX]; 144 char *input, *expected; 145 146 cwk_path_set_style(CWK_STYLE_UNIX); 147 148 input = "/var////logs//test/"; 149 expected = "/var/logs/test"; 150 count = cwk_path_normalize(input, result, sizeof(result)); 151 if (count != strlen(expected) || strcmp(result, expected) != 0) { 152 return EXIT_FAILURE; 153 } 154 155 return EXIT_SUCCESS; 156 } 157 158 int normalize_terminated(void) 159 { 160 size_t count; 161 char result[FILENAME_MAX]; 162 char *input, *expected; 163 size_t i, expected_size, n; 164 165 cwk_path_set_style(CWK_STYLE_UNIX); 166 167 input = "/var/logs/test/../../"; 168 expected = "/var"; 169 expected_size = strlen(expected); 170 171 memset(result, 1, sizeof(result)); 172 173 for (i = 0; i < 7; ++i) { 174 count = cwk_path_normalize(input, result, i); 175 176 if (i != 0 && expected_size < i) { 177 n = expected_size; 178 } else { 179 n = i - 1; 180 } 181 182 if (count != strlen(expected) || 183 (i > 0 && (strncmp(result, expected, n) != 0 || result[n] != '\0'))) { 184 return EXIT_FAILURE; 185 } 186 } 187 188 return EXIT_SUCCESS; 189 } 190 191 int normalize_relative_too_far(void) 192 { 193 size_t count; 194 char result[FILENAME_MAX]; 195 char *input, *expected; 196 197 cwk_path_set_style(CWK_STYLE_UNIX); 198 199 input = "rel/../../"; 200 expected = ".."; 201 count = cwk_path_normalize(input, result, sizeof(result)); 202 if (count != strlen(expected) || strcmp(result, expected) != 0) { 203 return EXIT_FAILURE; 204 } 205 206 return EXIT_SUCCESS; 207 } 208 209 int normalize_absolute_too_far(void) 210 { 211 size_t count; 212 char result[FILENAME_MAX]; 213 char *input, *expected; 214 215 cwk_path_set_style(CWK_STYLE_UNIX); 216 217 input = "/var/logs/test/../../../../../../"; 218 expected = "/"; 219 count = cwk_path_normalize(input, result, sizeof(result)); 220 if (count != strlen(expected) || strcmp(result, expected) != 0) { 221 return EXIT_FAILURE; 222 } 223 224 return EXIT_SUCCESS; 225 } 226 227 int normalize_navigate_back(void) 228 { 229 size_t count; 230 char result[FILENAME_MAX]; 231 char *input, *expected; 232 233 cwk_path_set_style(CWK_STYLE_UNIX); 234 235 input = "/var/logs/test/../../"; 236 expected = "/var"; 237 count = cwk_path_normalize(input, result, sizeof(result)); 238 if (count != strlen(expected) || strcmp(result, expected) != 0) { 239 return EXIT_FAILURE; 240 } 241 242 return EXIT_SUCCESS; 243 } 244 245 int normalize_do_nothing(void) 246 { 247 size_t count; 248 char result[FILENAME_MAX]; 249 char *input, *expected; 250 251 cwk_path_set_style(CWK_STYLE_UNIX); 252 253 input = "/var"; 254 expected = "/var"; 255 count = cwk_path_normalize(input, result, sizeof(result)); 256 if (count != strlen(expected) || strcmp(result, expected) != 0) { 257 return EXIT_FAILURE; 258 } 259 260 return EXIT_SUCCESS; 261 }