| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "config.h" |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
#include "builtin_table.h" |
| 18 |
#include "detox_struct.h" |
| 19 |
#include "file.h" |
| 20 |
#include "filter.h" |
| 21 |
#include "parse_options.h" |
| 22 |
#include "sequence.h" |
| 23 |
#include "table.h" |
| 24 |
#include "wrapped.h" |
| 25 |
|
| 26 |
#define LOCAL_PADDING 32 |
| 27 |
|
| 28 |
struct test_values { |
| 29 |
int table_value; |
| 30 |
char *table_entry; |
| 31 |
|
| 32 |
char *input; |
| 33 |
char *expected; |
| 34 |
}; |
| 35 |
|
| 36 |
#define DATA_COUNT 9 |
| 37 |
static struct test_values data[DATA_COUNT] = { |
| 38 |
// 4 byte UTF-8 |
| 39 |
{ |
| 40 |
.table_value = 0x1F37A, |
| 41 |
.table_entry = "_root_beer_", |
| 42 |
|
| 43 |
.input = "\U0001F37A", |
| 44 |
.expected = "_root_beer_", |
| 45 |
}, |
| 46 |
|
| 47 |
// 3 byte UTF-8 |
| 48 |
{ |
| 49 |
.table_value = 0x2021, |
| 50 |
.table_entry = "_double_dagger_", |
| 51 |
|
| 52 |
.input = "\u2021", |
| 53 |
.expected = "_double_dagger_", |
| 54 |
}, |
| 55 |
|
| 56 |
// 2 byte UTF-8 |
| 57 |
{ |
| 58 |
.table_value = 0x00AE, |
| 59 |
.table_entry = "(r)", |
| 60 |
|
| 61 |
.input = "\u00AE", |
| 62 |
.expected = "(r)", |
| 63 |
}, |
| 64 |
|
| 65 |
// invalid 4 byte UTF-8 |
| 66 |
{ |
| 67 |
.table_value = 0x0001, |
| 68 |
.table_entry = "never...", |
| 69 |
|
| 70 |
.input = "\xF0" "\x9F" "\x8D", |
| 71 |
.expected = "_", |
| 72 |
}, |
| 73 |
|
| 74 |
// invalid 4 byte UTF-8 with more text |
| 75 |
{ |
| 76 |
.table_value = 0x0002, |
| 77 |
.table_entry = "never...", |
| 78 |
|
| 79 |
.input = "\xF0" "\x9F" "\x8D" "whatever", |
| 80 |
.expected = "_whatever", |
| 81 |
}, |
| 82 |
|
| 83 |
// invalid 3 byte UTF-8 |
| 84 |
{ |
| 85 |
.table_value = 0x0003, |
| 86 |
.table_entry = "never...", |
| 87 |
|
| 88 |
.input = "\xE2" "\x80", |
| 89 |
.expected = "_", |
| 90 |
}, |
| 91 |
|
| 92 |
// invalid 3 byte UTF-8 with more text |
| 93 |
{ |
| 94 |
.table_value = 0x0004, |
| 95 |
.table_entry = "never...", |
| 96 |
|
| 97 |
.input = "\xE2" "\x80" "whatever", |
| 98 |
.expected = "_whatever", |
| 99 |
}, |
| 100 |
|
| 101 |
// invalid 2 byte UTF-8 |
| 102 |
{ |
| 103 |
.table_value = 0x0005, |
| 104 |
.table_entry = "never...", |
| 105 |
|
| 106 |
.input = "\xC0", |
| 107 |
.expected = "_", |
| 108 |
}, |
| 109 |
|
| 110 |
// invalid 2 byte UTF-8 with more text |
| 111 |
{ |
| 112 |
.table_value = 0x0006, |
| 113 |
.table_entry = "never...", |
| 114 |
|
| 115 |
.input = "\xC0" "whatever", |
| 116 |
.expected = "_whatever", |
| 117 |
}, |
| 118 |
}; |
| 119 |
|
| 120 |
#define BAD_FILES_DATA_COUNT 6 |
| 121 |
static struct test_values bad_files_data[BAD_FILES_DATA_COUNT] = { |
| 122 |
{ |
| 123 |
.input = "..", |
| 124 |
.expected = "..", |
| 125 |
}, |
| 126 |
|
| 127 |
{ |
| 128 |
.input = ".git", |
| 129 |
.expected = ".git", |
| 130 |
}, |
| 131 |
|
| 132 |
{ |
| 133 |
.input = ".UPPER", |
| 134 |
.expected = ".upper", |
| 135 |
}, |
| 136 |
|
| 137 |
{ |
| 138 |
.input = "not.UPPER", |
| 139 |
.expected = "not.upper", |
| 140 |
}, |
| 141 |
|
| 142 |
{ |
| 143 |
.input = ".wipeup------no", |
| 144 |
.expected = ".wipeup-no", |
| 145 |
}, |
| 146 |
|
| 147 |
{ |
| 148 |
.input = "wipeup------yes", |
| 149 |
.expected = "wipeup-yes", |
| 150 |
}, |
| 151 |
}; |
| 152 |
|
| 153 |
#test test_parse_inline |
| 154 |
FILE *in_fp; |
| 155 |
FILE *out_fp; |
| 156 |
|
| 157 |
options_t *options; |
| 158 |
sequence_t *sequence; |
| 159 |
filter_t *filter; |
| 160 |
|
| 161 |
char *expected; |
| 162 |
char *input; |
| 163 |
char *input_filename; |
| 164 |
char *output; |
| 165 |
char *output_filename; |
| 166 |
char *walk; |
| 167 |
int buffer_size; |
| 168 |
int end; |
| 169 |
int start; |
| 170 |
int use_newline; |
| 171 |
int x_length; |
| 172 |
int half_x_length; |
| 173 |
int i; |
| 174 |
|
| 175 |
// |
| 176 |
|
| 177 |
options = options_init(); |
| 178 |
|
| 179 |
options->sequence_to_use = sequence = sequence_init("utf_8-and-lower"); |
| 180 |
sequence->source_filename = wrapped_strdup(__FILE__); |
| 181 |
|
| 182 |
sequence->filters = filter = filter_init(FILTER_UTF_8); |
| 183 |
filter->builtin = wrapped_strdup("safe"); |
| 184 |
filter->table = load_builtin_safe_table(); |
| 185 |
|
| 186 |
for (i = 0; i < DATA_COUNT; i++) { |
| 187 |
// table_put(filter->table, 0x0001F37A, "_root_beer_"); |
| 188 |
table_put(filter->table, data[i].table_value, data[i].table_entry); |
| 189 |
} |
| 190 |
|
| 191 |
filter->next = filter_init(FILTER_LOWER); |
| 192 |
|
| 193 |
// |
| 194 |
|
| 195 |
start = INLINE_BUF_SIZE - (INLINE_BUF_PADDING * 2); |
| 196 |
end = INLINE_BUF_SIZE + (INLINE_BUF_PADDING * 2); |
| 197 |
buffer_size = end + LOCAL_PADDING; |
| 198 |
|
| 199 |
expected = wrapped_malloc(buffer_size); |
| 200 |
input = wrapped_malloc(buffer_size); |
| 201 |
output = wrapped_malloc(buffer_size); |
| 202 |
|
| 203 |
input_filename = wrapped_malloc(buffer_size); |
| 204 |
output_filename = wrapped_malloc(buffer_size); |
| 205 |
|
| 206 |
for (i = 0; i < DATA_COUNT; i++) { |
| 207 |
for (x_length = start; x_length < end; x_length++) { |
| 208 |
for (use_newline = 0; use_newline < 2; use_newline++) { |
| 209 |
#ifdef DEBUG |
| 210 |
fprintf(stderr, "CHECKING VALUE: 0x%04X, LENGTH: %d, NEW LINE: %s\n", data[i].table_value, x_length, use_newline ? "yes" : "no"); |
| 211 |
#endif |
| 212 |
|
| 213 |
// prep |
| 214 |
|
| 215 |
sprintf(input_filename, "/tmp/detoxtest-in-%d-%d-XXXXXX", x_length, use_newline); |
| 216 |
sprintf(output_filename, "/tmp/detoxtest-out-%d-%d-XXXXXX", x_length, use_newline); |
| 217 |
|
| 218 |
mktemp(input_filename); |
| 219 |
mktemp(output_filename); |
| 220 |
|
| 221 |
#ifdef DEBUG |
| 222 |
fprintf(stderr, "in: %s, out: %s\n", input_filename, output_filename); |
| 223 |
#endif |
| 224 |
|
| 225 |
// wipe memory |
| 226 |
|
| 227 |
memset(expected, 0, buffer_size); |
| 228 |
memset(input, 0, buffer_size); |
| 229 |
memset(output, 0, buffer_size); |
| 230 |
|
| 231 |
// --------------------------------------------------------- |
| 232 |
// BUILD INPUT |
| 233 |
// --------------------------------------------------------- |
| 234 |
|
| 235 |
// set input to string filled with the same character, "x". |
| 236 |
// the length is x_length. |
| 237 |
|
| 238 |
memset(input, 'x', x_length); |
| 239 |
|
| 240 |
// stick a bunch of uppercase Xs in the middle |
| 241 |
|
| 242 |
half_x_length = (int)(x_length / 2); |
| 243 |
memset(input + (int)(half_x_length / 2), 'X', half_x_length); |
| 244 |
|
| 245 |
// at the end of the input, add a 4-byte UTF-8 character |
| 246 |
|
| 247 |
walk = input + x_length; |
| 248 |
sprintf(walk, "%s%s", data[i].input, use_newline ? "\n" : ""); |
| 249 |
|
| 250 |
// --------------------------------------------------------- |
| 251 |
// BUILD EXPECTED |
| 252 |
// --------------------------------------------------------- |
| 253 |
|
| 254 |
// set expected to string filled with the same character, "x". |
| 255 |
// the length is x_length. |
| 256 |
|
| 257 |
memset(expected, 'x', x_length); |
| 258 |
|
| 259 |
// at the end of the expected, add the translated character |
| 260 |
|
| 261 |
walk = expected + x_length; |
| 262 |
sprintf(walk, "%s%s", data[i].expected, use_newline ? "\n" : ""); |
| 263 |
|
| 264 |
// --------------------------------------------------------- |
| 265 |
|
| 266 |
// write the string to a file |
| 267 |
|
| 268 |
in_fp = fopen(input_filename, "w"); |
| 269 |
fputs(input, in_fp); |
| 270 |
fclose(in_fp); |
| 271 |
|
| 272 |
// pass the file to parse_inline |
| 273 |
|
| 274 |
parse_inline(input_filename, output_filename, options); |
| 275 |
|
| 276 |
// read the output file |
| 277 |
|
| 278 |
out_fp = fopen(output_filename, "r"); |
| 279 |
if (fgets(output, buffer_size, out_fp) == NULL) { |
| 280 |
ck_abort_msg("fgets() failed"); |
| 281 |
} |
| 282 |
fclose(out_fp); |
| 283 |
|
| 284 |
// compare |
| 285 |
|
| 286 |
ck_assert_str_eq(expected, output); |
| 287 |
|
| 288 |
// cleanup |
| 289 |
|
| 290 |
unlink(output_filename); |
| 291 |
unlink(input_filename); |
| 292 |
|
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
#test test_parse_inline_protected_file |
| 298 |
FILE *in_fp; |
| 299 |
FILE *out_fp; |
| 300 |
|
| 301 |
options_t *options; |
| 302 |
sequence_t *sequence; |
| 303 |
filter_t *filter; |
| 304 |
|
| 305 |
char *expected; |
| 306 |
char *input; |
| 307 |
char *input_filename; |
| 308 |
char *output; |
| 309 |
char *output_filename; |
| 310 |
char *walk; |
| 311 |
int buffer_size; |
| 312 |
int i; |
| 313 |
|
| 314 |
// |
| 315 |
|
| 316 |
options = options_init(); |
| 317 |
|
| 318 |
options->sequence_to_use = sequence = sequence_init("wipeup-lower"); |
| 319 |
sequence->source_filename = wrapped_strdup(__FILE__); |
| 320 |
|
| 321 |
sequence->filters = filter = filter_init(FILTER_WIPEUP); |
| 322 |
filter->remove_trailing = 1; |
| 323 |
|
| 324 |
filter->next = filter_init(FILTER_LOWER); |
| 325 |
|
| 326 |
// |
| 327 |
|
| 328 |
buffer_size = INLINE_BUF_SIZE * 2; |
| 329 |
|
| 330 |
expected = wrapped_malloc(buffer_size); |
| 331 |
input = wrapped_malloc(buffer_size); |
| 332 |
output = wrapped_malloc(buffer_size); |
| 333 |
|
| 334 |
input_filename = wrapped_malloc(buffer_size); |
| 335 |
output_filename = wrapped_malloc(buffer_size); |
| 336 |
|
| 337 |
// prep |
| 338 |
|
| 339 |
sprintf(input_filename, "/tmp/detoxtest-in--XXXXXX"); |
| 340 |
sprintf(output_filename, "/tmp/detoxtest-out-XXXXXX"); |
| 341 |
|
| 342 |
mktemp(input_filename); |
| 343 |
mktemp(output_filename); |
| 344 |
|
| 345 |
#ifdef DEBUG |
| 346 |
fprintf(stderr, "in: %s, out: %s\n", input_filename, output_filename); |
| 347 |
#endif |
| 348 |
|
| 349 |
// wipe memory |
| 350 |
|
| 351 |
memset(expected, 0, buffer_size); |
| 352 |
memset(input, 0, buffer_size); |
| 353 |
memset(output, 0, buffer_size); |
| 354 |
|
| 355 |
// --------------------------------------------------------- |
| 356 |
// BUILD INPUT |
| 357 |
// --------------------------------------------------------- |
| 358 |
|
| 359 |
walk = input; |
| 360 |
for (i = 0; i < BAD_FILES_DATA_COUNT; i++) { |
| 361 |
sprintf(walk, "%s\n", bad_files_data[i].input); |
| 362 |
walk = strchr(walk, '\0'); |
| 363 |
} |
| 364 |
|
| 365 |
// --------------------------------------------------------- |
| 366 |
// BUILD EXPECTED |
| 367 |
// --------------------------------------------------------- |
| 368 |
|
| 369 |
walk = expected; |
| 370 |
for (i = 0; i < BAD_FILES_DATA_COUNT; i++) { |
| 371 |
sprintf(walk, "%s\n", bad_files_data[i].expected); |
| 372 |
walk = strchr(walk, '\0'); |
| 373 |
} |
| 374 |
|
| 375 |
// --------------------------------------------------------- |
| 376 |
|
| 377 |
// write the string to a file |
| 378 |
|
| 379 |
in_fp = fopen(input_filename, "w"); |
| 380 |
fputs(input, in_fp); |
| 381 |
fclose(in_fp); |
| 382 |
|
| 383 |
// pass the file to parse_inline |
| 384 |
|
| 385 |
parse_inline(input_filename, output_filename, options); |
| 386 |
|
| 387 |
// read the output file |
| 388 |
|
| 389 |
out_fp = fopen(output_filename, "r"); |
| 390 |
walk = output; |
| 391 |
while (fgets(walk, buffer_size, out_fp) != NULL) { |
| 392 |
walk += strlen(walk); |
| 393 |
} |
| 394 |
fclose(out_fp); |
| 395 |
|
| 396 |
// compare |
| 397 |
|
| 398 |
ck_assert_str_eq(expected, output); |
| 399 |
|
| 400 |
// cleanup |
| 401 |
|
| 402 |
unlink(output_filename); |
| 403 |
unlink(input_filename); |