| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/allocator.h> |
| 7 |
#include <aws/common/device_random.h> |
| 8 |
#include <aws/common/file.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
#include <aws/common/system_info.h> |
| 11 |
|
| 12 |
#include <aws/testing/aws_test_harness.h> |
| 13 |
|
| 14 |
static int s_aws_fopen_test_helper(char *file_path, char *content) { |
| 15 |
char read_result[100]; |
| 16 |
AWS_ZERO_ARRAY(read_result); |
| 17 |
FILE *file = aws_fopen(file_path, "w+"); |
| 18 |
ASSERT_NOT_NULL(file); |
| 19 |
fprintf(file, "%s", content); |
| 20 |
fclose(file); |
| 21 |
FILE *readfile = aws_fopen(file_path, "r"); |
| 22 |
ASSERT_NOT_NULL(readfile); |
| 23 |
size_t read_len = fread(read_result, sizeof(char), strlen(content), readfile); |
| 24 |
ASSERT_UINT_EQUALS(strlen(content), read_len); |
| 25 |
fclose(readfile); |
| 26 |
ASSERT_SUCCESS(strcmp(content, read_result)); |
| 27 |
|
| 28 |
#ifdef _WIN32 |
| 29 |
wchar_t w_file_path[1000]; |
| 30 |
|
| 31 |
size_t file_path_len = strlen(file_path) + 1; |
| 32 |
MultiByteToWideChar(CP_UTF8, 0, file_path, -1, w_file_path, (int)file_path_len); |
| 33 |
ASSERT_SUCCESS(_wremove(w_file_path)); |
| 34 |
#else |
| 35 |
ASSERT_SUCCESS(remove(file_path)); |
| 36 |
#endif |
| 37 |
return AWS_OP_SUCCESS; |
| 38 |
} |
| 39 |
|
| 40 |
static int s_aws_fopen_content_matches(char *file_path, char *content) { |
| 41 |
char read_result[100]; |
| 42 |
AWS_ZERO_ARRAY(read_result); |
| 43 |
FILE *file = aws_fopen(file_path, "rb"); |
| 44 |
ASSERT_NOT_NULL(file); |
| 45 |
size_t read_len = fread(read_result, sizeof(char), strlen(content), file); |
| 46 |
ASSERT_UINT_EQUALS(strlen(content), read_len); |
| 47 |
fclose(file); |
| 48 |
ASSERT_SUCCESS(strcmp(content, read_result)); |
| 49 |
|
| 50 |
return AWS_OP_SUCCESS; |
| 51 |
} |
| 52 |
|
| 53 |
static int s_aws_fopen_non_ascii_read_existing_file_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 54 |
(void)allocator; |
| 55 |
(void)ctx; |
| 56 |
|
| 57 |
char expected_content[] = "This is a non-ascii file path file."; |
| 58 |
char file_path[] = "Å Éxample.txt"; |
| 59 |
char read_result[100]; |
| 60 |
AWS_ZERO_ARRAY(read_result); |
| 61 |
FILE *readfile = aws_fopen(file_path, "r"); |
| 62 |
ASSERT_NOT_NULL(readfile); |
| 63 |
size_t read_len = fread(read_result, sizeof(char), strlen(expected_content), readfile); |
| 64 |
ASSERT_UINT_EQUALS(strlen(expected_content), read_len); |
| 65 |
fclose(readfile); |
| 66 |
ASSERT_SUCCESS(strcmp(expected_content, read_result)); |
| 67 |
return AWS_OP_SUCCESS; |
| 68 |
} |
| 69 |
|
| 70 |
AWS_TEST_CASE(aws_fopen_non_ascii_read_existing_file_test, s_aws_fopen_non_ascii_read_existing_file_test_fn) |
| 71 |
|
| 72 |
static int s_aws_fopen_non_ascii_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 73 |
|
| 74 |
(void)allocator; |
| 75 |
(void)ctx; |
| 76 |
char file_path[] = "Éxample.txt"; |
| 77 |
char content[] = "samples"; |
| 78 |
ASSERT_SUCCESS(s_aws_fopen_test_helper(file_path, content)); |
| 79 |
return AWS_OP_SUCCESS; |
| 80 |
} |
| 81 |
|
| 82 |
AWS_TEST_CASE(aws_fopen_non_ascii_test, s_aws_fopen_non_ascii_test_fn) |
| 83 |
|
| 84 |
static int s_aws_fopen_ascii_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 85 |
|
| 86 |
(void)allocator; |
| 87 |
(void)ctx; |
| 88 |
char file_path[] = "sample.txt"; |
| 89 |
char content[] = "samples"; |
| 90 |
ASSERT_SUCCESS(s_aws_fopen_test_helper(file_path, content)); |
| 91 |
return AWS_OP_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
AWS_TEST_CASE(aws_fopen_ascii_test, s_aws_fopen_ascii_test_fn) |
| 95 |
|
| 96 |
struct directory_traversal_test_data { |
| 97 |
bool child_dir_verified; |
| 98 |
bool child_file_verified; |
| 99 |
bool root_file_verified; |
| 100 |
}; |
| 101 |
|
| 102 |
static const char *s_first_child_dir_path = "dir_traversal_test" AWS_PATH_DELIM_STR "first_child_dir"; |
| 103 |
|
| 104 |
static const char *s_first_child_file_path = |
| 105 |
"dir_traversal_test" AWS_PATH_DELIM_STR "first_child_dir" AWS_PATH_DELIM_STR "child.txt"; |
| 106 |
|
| 107 |
static const char *s_root_child_path = "dir_traversal_test" AWS_PATH_DELIM_STR "root_child.txt"; |
| 108 |
|
| 109 |
bool s_on_directory_entry(const struct aws_directory_entry *entry, void *user_data) { |
| 110 |
struct directory_traversal_test_data *test_data = user_data; |
| 111 |
|
| 112 |
if (aws_byte_cursor_eq_c_str(&entry->relative_path, s_root_child_path)) { |
| 113 |
test_data->root_file_verified = |
| 114 |
entry->file_type & AWS_FILE_TYPE_FILE && entry->file_size && |
| 115 |
s_aws_fopen_content_matches((char *)entry->relative_path.ptr, "dir_traversal_test->root_child.txt") == |
| 116 |
AWS_OP_SUCCESS; |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
if (aws_byte_cursor_eq_c_str(&entry->relative_path, s_first_child_file_path)) { |
| 121 |
test_data->child_file_verified = |
| 122 |
entry->file_type & AWS_FILE_TYPE_FILE && entry->file_size && |
| 123 |
s_aws_fopen_content_matches( |
| 124 |
(char *)entry->relative_path.ptr, "dir_traversal_test->first_child_dir->child.txt") == AWS_OP_SUCCESS; |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
if (aws_byte_cursor_eq_c_str(&entry->relative_path, s_first_child_dir_path)) { |
| 129 |
test_data->child_dir_verified = entry->file_type & AWS_FILE_TYPE_DIRECTORY; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
static int s_directory_traversal_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 137 |
(void)ctx; |
| 138 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test"); |
| 139 |
struct directory_traversal_test_data test_data; |
| 140 |
AWS_ZERO_STRUCT(test_data); |
| 141 |
|
| 142 |
ASSERT_SUCCESS(aws_directory_traverse(allocator, path, true, s_on_directory_entry, &test_data)); |
| 143 |
ASSERT_TRUE(test_data.child_dir_verified); |
| 144 |
ASSERT_TRUE(test_data.root_file_verified); |
| 145 |
ASSERT_TRUE(test_data.child_file_verified); |
| 146 |
|
| 147 |
AWS_ZERO_STRUCT(test_data); |
| 148 |
ASSERT_SUCCESS(aws_directory_traverse(allocator, path, false, s_on_directory_entry, &test_data)); |
| 149 |
ASSERT_TRUE(test_data.child_dir_verified); |
| 150 |
ASSERT_TRUE(test_data.root_file_verified); |
| 151 |
ASSERT_FALSE(test_data.child_file_verified); |
| 152 |
|
| 153 |
aws_string_destroy(path); |
| 154 |
|
| 155 |
return AWS_OP_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
AWS_TEST_CASE(directory_traversal_test, s_directory_traversal_test_fn) |
| 159 |
|
| 160 |
static int s_directory_iteration_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 161 |
(void)ctx; |
| 162 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test"); |
| 163 |
|
| 164 |
struct aws_directory_iterator *iterator = aws_directory_entry_iterator_new(allocator, path); |
| 165 |
ASSERT_NOT_NULL(iterator); |
| 166 |
const struct aws_directory_entry *first_entry = aws_directory_entry_iterator_get_value(iterator); |
| 167 |
ASSERT_NOT_NULL(first_entry); |
| 168 |
|
| 169 |
bool first_child_dir_found = false; |
| 170 |
bool root_file_found = false; |
| 171 |
|
| 172 |
do { |
| 173 |
const struct aws_directory_entry *entry = aws_directory_entry_iterator_get_value(iterator); |
| 174 |
if (entry->file_type == AWS_FILE_TYPE_DIRECTORY) { |
| 175 |
struct aws_byte_cursor first_child_dir_path_cur = aws_byte_cursor_from_c_str(s_first_child_dir_path); |
| 176 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 177 |
first_child_dir_path_cur.ptr, |
| 178 |
first_child_dir_path_cur.len, |
| 179 |
entry->relative_path.ptr, |
| 180 |
entry->relative_path.len); |
| 181 |
first_child_dir_found = true; |
| 182 |
|
| 183 |
struct aws_string *next_path = aws_string_new_from_cursor(allocator, &entry->relative_path); |
| 184 |
struct aws_directory_iterator *next_iter = aws_directory_entry_iterator_new(allocator, next_path); |
| 185 |
aws_string_destroy(next_path); |
| 186 |
ASSERT_NOT_NULL(next_iter); |
| 187 |
|
| 188 |
entry = aws_directory_entry_iterator_get_value(next_iter); |
| 189 |
struct aws_byte_cursor first_child_file_path_cur = aws_byte_cursor_from_c_str(s_first_child_file_path); |
| 190 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 191 |
first_child_file_path_cur.ptr, |
| 192 |
first_child_file_path_cur.len, |
| 193 |
entry->relative_path.ptr, |
| 194 |
entry->relative_path.len); |
| 195 |
ASSERT_INT_EQUALS(AWS_FILE_TYPE_FILE, entry->file_type); |
| 196 |
|
| 197 |
ASSERT_ERROR(AWS_ERROR_LIST_EMPTY, aws_directory_entry_iterator_next(next_iter)); |
| 198 |
aws_directory_entry_iterator_destroy(next_iter); |
| 199 |
} else { |
| 200 |
struct aws_byte_cursor root_child_file_path_cur = aws_byte_cursor_from_c_str(s_root_child_path); |
| 201 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 202 |
root_child_file_path_cur.ptr, |
| 203 |
root_child_file_path_cur.len, |
| 204 |
entry->relative_path.ptr, |
| 205 |
entry->relative_path.len); |
| 206 |
ASSERT_INT_EQUALS(AWS_FILE_TYPE_FILE, entry->file_type); |
| 207 |
root_file_found = true; |
| 208 |
} |
| 209 |
} while (aws_directory_entry_iterator_next(iterator) == AWS_OP_SUCCESS); |
| 210 |
|
| 211 |
ASSERT_ERROR(AWS_ERROR_LIST_EMPTY, aws_directory_entry_iterator_next(iterator)); |
| 212 |
ASSERT_SUCCESS(aws_directory_entry_iterator_previous(iterator)); |
| 213 |
ASSERT_PTR_EQUALS(first_entry, aws_directory_entry_iterator_get_value(iterator)); |
| 214 |
aws_directory_entry_iterator_destroy(iterator); |
| 215 |
aws_string_destroy(path); |
| 216 |
|
| 217 |
ASSERT_TRUE(root_file_found); |
| 218 |
ASSERT_TRUE(first_child_dir_found); |
| 219 |
|
| 220 |
return AWS_OP_SUCCESS; |
| 221 |
} |
| 222 |
|
| 223 |
AWS_TEST_CASE(directory_iteration_test, s_directory_iteration_test_fn) |
| 224 |
|
| 225 |
static int s_directory_iteration_non_existent_directory_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 226 |
(void)ctx; |
| 227 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test_non_existent"); |
| 228 |
|
| 229 |
struct aws_directory_iterator *iterator = aws_directory_entry_iterator_new(allocator, path); |
| 230 |
ASSERT_NULL(iterator); |
| 231 |
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_FILE_INVALID_PATH); |
| 232 |
|
| 233 |
aws_string_destroy(path); |
| 234 |
return AWS_OP_SUCCESS; |
| 235 |
} |
| 236 |
|
| 237 |
AWS_TEST_CASE(directory_iteration_non_existent_directory_test, s_directory_iteration_non_existent_directory_test_fn) |
| 238 |
|
| 239 |
struct directory_traversal_abort_test_data { |
| 240 |
int times_called; |
| 241 |
}; |
| 242 |
|
| 243 |
bool directory_traversal_abort_test_data(const struct aws_directory_entry *entry, void *user_data) { |
| 244 |
(void)entry; |
| 245 |
struct directory_traversal_abort_test_data *test_data = user_data; |
| 246 |
test_data->times_called += 1; |
| 247 |
|
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
static int s_directory_traversal_stop_traversal_fn(struct aws_allocator *allocator, void *ctx) { |
| 252 |
(void)ctx; |
| 253 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test"); |
| 254 |
struct directory_traversal_abort_test_data test_data; |
| 255 |
AWS_ZERO_STRUCT(test_data); |
| 256 |
|
| 257 |
ASSERT_ERROR( |
| 258 |
AWS_ERROR_OPERATION_INTERUPTED, |
| 259 |
aws_directory_traverse(allocator, path, true, directory_traversal_abort_test_data, &test_data)); |
| 260 |
ASSERT_INT_EQUALS(1, test_data.times_called); |
| 261 |
|
| 262 |
aws_string_destroy(path); |
| 263 |
|
| 264 |
return AWS_OP_SUCCESS; |
| 265 |
} |
| 266 |
|
| 267 |
AWS_TEST_CASE(directory_traversal_stop_traversal, s_directory_traversal_stop_traversal_fn) |
| 268 |
|
| 269 |
static int s_directory_traversal_on_file_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 270 |
(void)ctx; |
| 271 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test/root_child.txt"); |
| 272 |
struct directory_traversal_test_data test_data; |
| 273 |
AWS_ZERO_STRUCT(test_data); |
| 274 |
|
| 275 |
ASSERT_ERROR( |
| 276 |
AWS_ERROR_FILE_INVALID_PATH, aws_directory_traverse(allocator, path, true, s_on_directory_entry, &test_data)); |
| 277 |
|
| 278 |
aws_string_destroy(path); |
| 279 |
|
| 280 |
return AWS_OP_SUCCESS; |
| 281 |
} |
| 282 |
|
| 283 |
AWS_TEST_CASE(directory_traversal_on_file_test, s_directory_traversal_on_file_test_fn) |
| 284 |
|
| 285 |
static int s_directory_existence_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 286 |
(void)ctx; |
| 287 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test"); |
| 288 |
ASSERT_TRUE(aws_directory_exists(path)); |
| 289 |
aws_string_destroy(path); |
| 290 |
|
| 291 |
path = aws_string_new_from_c_str(allocator, "dir_traversal_test_blah"); |
| 292 |
ASSERT_FALSE(aws_directory_exists(path)); |
| 293 |
aws_string_destroy(path); |
| 294 |
|
| 295 |
path = aws_string_new_from_c_str(allocator, "dir_traversal_test/root_child.txt"); |
| 296 |
ASSERT_FALSE(aws_directory_exists(path)); |
| 297 |
aws_string_destroy(path); |
| 298 |
return AWS_OP_SUCCESS; |
| 299 |
} |
| 300 |
|
| 301 |
AWS_TEST_CASE(directory_existence_test, s_directory_existence_test_fn) |
| 302 |
|
| 303 |
static int s_directory_creation_deletion_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 304 |
(void)ctx; |
| 305 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "temp_dir"); |
| 306 |
ASSERT_SUCCESS(aws_directory_create(path)); |
| 307 |
|
| 308 |
|
| 309 |
ASSERT_SUCCESS(aws_directory_create(path)); |
| 310 |
|
| 311 |
ASSERT_TRUE(aws_directory_exists(path)); |
| 312 |
ASSERT_SUCCESS(aws_directory_delete(path, false)); |
| 313 |
ASSERT_FALSE(aws_directory_exists(path)); |
| 314 |
|
| 315 |
aws_string_destroy(path); |
| 316 |
|
| 317 |
return AWS_OP_SUCCESS; |
| 318 |
} |
| 319 |
|
| 320 |
AWS_TEST_CASE(directory_creation_deletion_test, s_directory_creation_deletion_test_fn) |
| 321 |
|
| 322 |
static int s_directory_non_empty_deletion_fails_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 323 |
(void)ctx; |
| 324 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "dir_traversal_test"); |
| 325 |
ASSERT_TRUE(aws_directory_exists(path)); |
| 326 |
ASSERT_ERROR(AWS_ERROR_DIRECTORY_NOT_EMPTY, aws_directory_delete(path, false)); |
| 327 |
ASSERT_TRUE(aws_directory_exists(path)); |
| 328 |
|
| 329 |
aws_string_destroy(path); |
| 330 |
return AWS_OP_SUCCESS; |
| 331 |
} |
| 332 |
|
| 333 |
AWS_TEST_CASE(directory_non_empty_deletion_fails_test, s_directory_non_empty_deletion_fails_test_fn) |
| 334 |
|
| 335 |
static int s_directory_non_empty_deletion_recursively_succeeds_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 336 |
(void)ctx; |
| 337 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "non_empty_dir_del_test_dir_1"); |
| 338 |
ASSERT_SUCCESS(aws_directory_create(path)); |
| 339 |
|
| 340 |
const char *nested_dir = "non_empty_dir_del_test_dir_1" AWS_PATH_DELIM_STR "test_dir_2"; |
| 341 |
struct aws_string *nested_dir_path = aws_string_new_from_c_str(allocator, nested_dir); |
| 342 |
ASSERT_SUCCESS(aws_directory_create(nested_dir_path)); |
| 343 |
|
| 344 |
const char *nested_file = |
| 345 |
"non_empty_dir_del_test_dir_1" AWS_PATH_DELIM_STR "test_dir_2" AWS_PATH_DELIM_STR "nested_file.txt"; |
| 346 |
|
| 347 |
FILE *nested_file_ptr = aws_fopen(nested_file, "w"); |
| 348 |
ASSERT_NOT_NULL(nested_file_ptr); |
| 349 |
fclose(nested_file_ptr); |
| 350 |
|
| 351 |
ASSERT_SUCCESS(aws_directory_delete(path, true)); |
| 352 |
ASSERT_FALSE(aws_directory_exists(path)); |
| 353 |
|
| 354 |
aws_string_destroy(nested_dir_path); |
| 355 |
aws_string_destroy(path); |
| 356 |
return AWS_OP_SUCCESS; |
| 357 |
} |
| 358 |
|
| 359 |
AWS_TEST_CASE( |
| 360 |
directory_non_empty_deletion_recursively_succeeds_test, |
| 361 |
s_directory_non_empty_deletion_recursively_succeeds_test_fn) |
| 362 |
|
| 363 |
static int s_directory_move_succeeds_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 364 |
(void)ctx; |
| 365 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "directory_move_succeeds_test_dir_1"); |
| 366 |
ASSERT_SUCCESS(aws_directory_create(path)); |
| 367 |
|
| 368 |
struct aws_string *to_path = aws_string_new_from_c_str(allocator, "directory_move_succeeds_test_dir_2"); |
| 369 |
ASSERT_SUCCESS(aws_directory_or_file_move(path, to_path)); |
| 370 |
|
| 371 |
ASSERT_FALSE(aws_directory_exists(path)); |
| 372 |
ASSERT_TRUE(aws_directory_exists(to_path)); |
| 373 |
|
| 374 |
ASSERT_SUCCESS(aws_directory_delete(to_path, true)); |
| 375 |
|
| 376 |
aws_string_destroy(to_path); |
| 377 |
aws_string_destroy(path); |
| 378 |
return AWS_OP_SUCCESS; |
| 379 |
} |
| 380 |
|
| 381 |
AWS_TEST_CASE(directory_move_succeeds_test, s_directory_move_succeeds_test_fn) |
| 382 |
|
| 383 |
static int s_directory_move_src_non_existent_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 384 |
(void)ctx; |
| 385 |
struct aws_string *path = aws_string_new_from_c_str(allocator, "directory_move_src_non_existent_test_dir_1"); |
| 386 |
|
| 387 |
struct aws_string *to_path = aws_string_new_from_c_str(allocator, "directory_move_src_non_existent_test_dir_2"); |
| 388 |
ASSERT_ERROR(AWS_ERROR_FILE_INVALID_PATH, aws_directory_or_file_move(path, to_path)); |
| 389 |
|
| 390 |
aws_string_destroy(to_path); |
| 391 |
aws_string_destroy(path); |
| 392 |
return AWS_OP_SUCCESS; |
| 393 |
} |
| 394 |
|
| 395 |
AWS_TEST_CASE(directory_move_src_non_existent_test, s_directory_move_src_non_existent_test_fn) |
| 396 |
|
| 397 |
static int s_test_home_directory_not_null(struct aws_allocator *allocator, void *ctx) { |
| 398 |
(void)ctx; |
| 399 |
|
| 400 |
struct aws_string *home_directory = aws_get_home_directory(allocator); |
| 401 |
ASSERT_TRUE(home_directory != NULL); |
| 402 |
|
| 403 |
aws_string_destroy(home_directory); |
| 404 |
|
| 405 |
return AWS_OP_SUCCESS; |
| 406 |
} |
| 407 |
|
| 408 |
AWS_TEST_CASE(test_home_directory_not_null, s_test_home_directory_not_null); |
| 409 |
|
| 410 |
static int s_test_normalize_posix_directory_separator(struct aws_allocator *allocator, void *ctx) { |
| 411 |
(void)ctx; |
| 412 |
|
| 413 |
struct aws_string *buffer = aws_string_new_from_c_str(allocator, "./test/path/abc"); |
| 414 |
struct aws_byte_buf path_buf = aws_byte_buf_from_array(buffer->bytes, buffer->len); |
| 415 |
aws_normalize_directory_separator(&path_buf); |
| 416 |
for (size_t i = 0; i < path_buf.len; ++i) { |
| 417 |
if (aws_is_any_directory_separator((char)path_buf.buffer[i])) { |
| 418 |
ASSERT_INT_EQUALS(aws_get_platform_directory_separator(), path_buf.buffer[i]); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
aws_string_destroy(buffer); |
| 423 |
return AWS_OP_SUCCESS; |
| 424 |
} |
| 425 |
|
| 426 |
AWS_TEST_CASE(test_normalize_posix_directory_separator, s_test_normalize_posix_directory_separator); |
| 427 |
|
| 428 |
static int s_test_normalize_windows_directory_separator(struct aws_allocator *allocator, void *ctx) { |
| 429 |
(void)ctx; |
| 430 |
|
| 431 |
struct aws_string *buffer = aws_string_new_from_c_str(allocator, ".\\test\\path\\abc"); |
| 432 |
struct aws_byte_buf path_buf = aws_byte_buf_from_array(buffer->bytes, buffer->len); |
| 433 |
aws_normalize_directory_separator(&path_buf); |
| 434 |
for (size_t i = 0; i < path_buf.len; ++i) { |
| 435 |
if (aws_is_any_directory_separator((char)path_buf.buffer[i])) { |
| 436 |
ASSERT_INT_EQUALS(aws_get_platform_directory_separator(), path_buf.buffer[i]); |
| 437 |
} |
| 438 |
} |
| 439 |
aws_string_destroy(buffer); |
| 440 |
return AWS_OP_SUCCESS; |
| 441 |
} |
| 442 |
|
| 443 |
AWS_TEST_CASE(test_normalize_windows_directory_separator, s_test_normalize_windows_directory_separator); |
| 444 |
|
| 445 |
static int s_check_byte_buf_from_file(const struct aws_byte_buf *buf, struct aws_byte_cursor expected_contents) { |
| 446 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected_contents, buf), "Contents should match"); |
| 447 |
ASSERT_TRUE(buf->capacity > buf->len, "Buffer should end with null-terminator"); |
| 448 |
ASSERT_UINT_EQUALS(0, buf->buffer[buf->len], "Buffer should end with null-terminator"); |
| 449 |
return AWS_OP_SUCCESS; |
| 450 |
} |
| 451 |
|
| 452 |
static int s_create_file_then_read_it(struct aws_allocator *allocator, struct aws_byte_cursor contents) { |
| 453 |
|
| 454 |
const char *filename = "testy"; |
| 455 |
FILE *f = aws_fopen(filename, "wb"); |
| 456 |
ASSERT_UINT_EQUALS(contents.len, fwrite(contents.ptr, 1, contents.len, f)); |
| 457 |
ASSERT_INT_EQUALS(0, fclose(f)); |
| 458 |
|
| 459 |
struct aws_byte_buf buf; |
| 460 |
|
| 461 |
|
| 462 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&buf, allocator, filename)); |
| 463 |
ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); |
| 464 |
aws_byte_buf_clean_up(&buf); |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
size_t size_hint = contents.len * 2; |
| 470 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); |
| 471 |
ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); |
| 472 |
aws_byte_buf_clean_up(&buf); |
| 473 |
|
| 474 |
|
| 475 |
size_hint = contents.len; |
| 476 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); |
| 477 |
ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); |
| 478 |
aws_byte_buf_clean_up(&buf); |
| 479 |
|
| 480 |
|
| 481 |
size_hint = 0; |
| 482 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); |
| 483 |
ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); |
| 484 |
aws_byte_buf_clean_up(&buf); |
| 485 |
|
| 486 |
|
| 487 |
size_hint = 1; |
| 488 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file_with_size_hint(&buf, allocator, filename, size_hint)); |
| 489 |
ASSERT_SUCCESS(s_check_byte_buf_from_file(&buf, contents)); |
| 490 |
aws_byte_buf_clean_up(&buf); |
| 491 |
|
| 492 |
remove(filename); |
| 493 |
return AWS_OP_SUCCESS; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
static int s_read_special_file(struct aws_allocator *allocator, const char *filename) { |
| 498 |
struct aws_string *filename_str = aws_string_new_from_c_str(allocator, filename); |
| 499 |
bool exists = aws_path_exists(filename_str); |
| 500 |
aws_string_destroy(filename_str); |
| 501 |
if (!exists) { |
| 502 |
return AWS_OP_SUCCESS; |
| 503 |
} |
| 504 |
|
| 505 |
struct aws_byte_buf buf; |
| 506 |
ASSERT_SUCCESS(aws_byte_buf_init_from_file(&buf, allocator, filename)); |
| 507 |
ASSERT_TRUE(buf.capacity > buf.len, "Buffer should end with null-terminator"); |
| 508 |
ASSERT_UINT_EQUALS(0, buf.buffer[buf.len], "Buffer should end with null-terminator"); |
| 509 |
|
| 510 |
if (strcmp("/dev/null", filename) == 0) { |
| 511 |
ASSERT_UINT_EQUALS(0, buf.len, "expected /dev/null to be empty"); |
| 512 |
} else { |
| 513 |
ASSERT_TRUE(buf.len > 0, "expected special file to have data"); |
| 514 |
} |
| 515 |
|
| 516 |
aws_byte_buf_clean_up(&buf); |
| 517 |
return AWS_OP_SUCCESS; |
| 518 |
} |
| 519 |
|
| 520 |
static int s_test_byte_buf_init_from_file(struct aws_allocator *allocator, void *ctx) { |
| 521 |
(void)ctx; |
| 522 |
|
| 523 |
|
| 524 |
ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_c_str("asdf"))); |
| 525 |
|
| 526 |
|
| 527 |
ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_c_str(""))); |
| 528 |
|
| 529 |
|
| 530 |
struct aws_byte_buf big_rando; |
| 531 |
aws_byte_buf_init(&big_rando, allocator, (1024 * 1024 * 3) + 1); |
| 532 |
ASSERT_SUCCESS(aws_device_random_buffer(&big_rando)); |
| 533 |
ASSERT_SUCCESS(s_create_file_then_read_it(allocator, aws_byte_cursor_from_buf(&big_rando))); |
| 534 |
aws_byte_buf_clean_up(&big_rando); |
| 535 |
|
| 536 |
|
| 537 |
ASSERT_SUCCESS(s_read_special_file(allocator, "/proc/cpuinfo")); |
| 538 |
ASSERT_SUCCESS(s_read_special_file(allocator, "/proc/net/tcp")); |
| 539 |
ASSERT_SUCCESS(s_read_special_file(allocator, "/sys/devices/virtual/dmi/id/sys_vendor")); |
| 540 |
ASSERT_SUCCESS(s_read_special_file(allocator, "/dev/null")); |
| 541 |
|
| 542 |
return AWS_OP_SUCCESS; |
| 543 |
} |
| 544 |
|
| 545 |
AWS_TEST_CASE(test_byte_buf_init_from_file, s_test_byte_buf_init_from_file) |
| 546 |
|
| 547 |
struct aws_file_path_read_from_offset_tester { |
| 548 |
struct aws_allocator *alloc; |
| 549 |
struct aws_allocator *aligned_allocator; |
| 550 |
size_t page_size; |
| 551 |
struct aws_string *file_path; |
| 552 |
struct aws_byte_buf content; |
| 553 |
size_t file_length; |
| 554 |
}; |
| 555 |
|
| 556 |
static int s_file_path_read_from_offset_tester_init( |
| 557 |
struct aws_file_path_read_from_offset_tester *tester, |
| 558 |
struct aws_allocator *allocator, |
| 559 |
char *file_path, |
| 560 |
size_t file_length) { |
| 561 |
|
| 562 |
tester->alloc = allocator; |
| 563 |
tester->page_size = aws_system_info_page_size(); |
| 564 |
tester->aligned_allocator = aws_explicit_aligned_allocator_new(tester->page_size); |
| 565 |
if (!tester->aligned_allocator) { |
| 566 |
return AWS_OP_ERR; |
| 567 |
} |
| 568 |
tester->file_path = aws_string_new_from_c_str(tester->aligned_allocator, file_path); |
| 569 |
tester->file_length = file_length; |
| 570 |
struct aws_byte_cursor content = aws_byte_cursor_from_c_str("0123456789abcdef"); |
| 571 |
aws_byte_buf_init_copy_from_cursor(&tester->content, allocator, content); |
| 572 |
|
| 573 |
FILE *file = aws_fopen(file_path, "w+"); |
| 574 |
ASSERT_NOT_NULL(file); |
| 575 |
|
| 576 |
|
| 577 |
size_t bytes_written = 0; |
| 578 |
while (bytes_written < file_length) { |
| 579 |
size_t bytes_to_write = aws_min_size(content.len, file_length - bytes_written); |
| 580 |
size_t written = fwrite(content.ptr, 1, bytes_to_write, file); |
| 581 |
if (written != bytes_to_write) { |
| 582 |
fclose(file); |
| 583 |
return AWS_OP_ERR; |
| 584 |
} |
| 585 |
bytes_written += written; |
| 586 |
} |
| 587 |
|
| 588 |
fclose(file); |
| 589 |
return AWS_OP_SUCCESS; |
| 590 |
} |
| 591 |
|
| 592 |
static void s_file_path_read_from_offset_tester_cleanup(struct aws_file_path_read_from_offset_tester *tester) { |
| 593 |
aws_byte_buf_clean_up(&tester->content); |
| 594 |
remove(aws_string_c_str(tester->file_path)); |
| 595 |
aws_string_destroy(tester->file_path); |
| 596 |
aws_explicit_aligned_allocator_destroy(tester->aligned_allocator); |
| 597 |
} |
| 598 |
|
| 599 |
static int s_test_file_path_read_from_offset_direct_io(struct aws_allocator *allocator, void *ctx) { |
| 600 |
(void)ctx; |
| 601 |
|
| 602 |
#if defined(AWS_OS_LINUX) |
| 603 |
struct aws_file_path_read_from_offset_tester tester; |
| 604 |
char file_path[] = "test_file_path_read_from_offset_direct_io.txt"; |
| 605 |
|
| 606 |
|
| 607 |
size_t page_size = aws_system_info_page_size(); |
| 608 |
size_t file_size = page_size * 2; |
| 609 |
|
| 610 |
ASSERT_SUCCESS(s_file_path_read_from_offset_tester_init(&tester, allocator, file_path, file_size)); |
| 611 |
|
| 612 |
|
| 613 |
struct aws_byte_buf output_buf; |
| 614 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, page_size)); |
| 615 |
|
| 616 |
size_t actual_read = 0; |
| 617 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io(tester.file_path, 0, page_size, &output_buf, &actual_read)); |
| 618 |
|
| 619 |
ASSERT_UINT_EQUALS(page_size, actual_read); |
| 620 |
ASSERT_UINT_EQUALS(page_size, output_buf.len); |
| 621 |
|
| 622 |
|
| 623 |
struct aws_byte_cursor expected_content = aws_byte_cursor_from_c_str("0123456789abcdef"); |
| 624 |
for (size_t i = 0; i < page_size; i++) { |
| 625 |
size_t pattern_index = i % expected_content.len; |
| 626 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 627 |
} |
| 628 |
|
| 629 |
aws_byte_buf_clean_up(&output_buf); |
| 630 |
|
| 631 |
|
| 632 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, page_size)); |
| 633 |
|
| 634 |
actual_read = 0; |
| 635 |
ASSERT_SUCCESS( |
| 636 |
aws_file_path_read_from_offset_direct_io(tester.file_path, page_size, page_size, &output_buf, &actual_read)); |
| 637 |
|
| 638 |
ASSERT_UINT_EQUALS(page_size, actual_read); |
| 639 |
ASSERT_UINT_EQUALS(page_size, output_buf.len); |
| 640 |
|
| 641 |
|
| 642 |
for (size_t i = 0; i < page_size; i++) { |
| 643 |
size_t file_position = page_size + i; |
| 644 |
size_t pattern_index = file_position % expected_content.len; |
| 645 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 646 |
} |
| 647 |
|
| 648 |
aws_byte_buf_clean_up(&output_buf); |
| 649 |
|
| 650 |
|
| 651 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, page_size)); |
| 652 |
|
| 653 |
size_t unaligned_offset = 1; |
| 654 |
ASSERT_FAILS(aws_file_path_read_from_offset_direct_io( |
| 655 |
tester.file_path, unaligned_offset, page_size, &output_buf, &actual_read)); |
| 656 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error()); |
| 657 |
|
| 658 |
aws_byte_buf_clean_up(&output_buf); |
| 659 |
|
| 660 |
|
| 661 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, page_size)); |
| 662 |
|
| 663 |
size_t unaligned_size = page_size - 1; |
| 664 |
ASSERT_FAILS( |
| 665 |
aws_file_path_read_from_offset_direct_io(tester.file_path, 0, unaligned_size, &output_buf, &actual_read)); |
| 666 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error()); |
| 667 |
|
| 668 |
aws_byte_buf_clean_up(&output_buf); |
| 669 |
|
| 670 |
|
| 671 |
s_file_path_read_from_offset_tester_cleanup(&tester); |
| 672 |
#else |
| 673 |
struct aws_string *file_path = |
| 674 |
aws_string_new_from_c_str(allocator, "test_file_path_read_from_offset_direct_io.txt"); |
| 675 |
ASSERT_FAILS(aws_file_path_read_from_offset_direct_io(file_path, 0, 10, NULL, NULL)); |
| 676 |
ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error()); |
| 677 |
aws_string_destroy(file_path); |
| 678 |
#endif |
| 679 |
|
| 680 |
return AWS_OP_SUCCESS; |
| 681 |
} |
| 682 |
|
| 683 |
AWS_TEST_CASE(test_file_path_read_from_offset_direct_io, s_test_file_path_read_from_offset_direct_io) |
| 684 |
|
| 685 |
static int s_test_file_path_read_from_offset(struct aws_allocator *allocator, void *ctx) { |
| 686 |
(void)ctx; |
| 687 |
|
| 688 |
struct aws_file_path_read_from_offset_tester tester; |
| 689 |
char file_path[] = "test_file_path_read_from_offset.txt"; |
| 690 |
|
| 691 |
|
| 692 |
size_t file_size = 1024; |
| 693 |
|
| 694 |
ASSERT_SUCCESS(s_file_path_read_from_offset_tester_init(&tester, allocator, file_path, file_size)); |
| 695 |
|
| 696 |
|
| 697 |
struct aws_byte_buf output_buf; |
| 698 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 100)); |
| 699 |
|
| 700 |
size_t actual_read = 0; |
| 701 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, 0, 100, &output_buf, &actual_read)); |
| 702 |
|
| 703 |
ASSERT_UINT_EQUALS(100, actual_read); |
| 704 |
ASSERT_UINT_EQUALS(100, output_buf.len); |
| 705 |
|
| 706 |
|
| 707 |
struct aws_byte_cursor expected_content = aws_byte_cursor_from_c_str("0123456789abcdef"); |
| 708 |
for (size_t i = 0; i < 100; i++) { |
| 709 |
size_t pattern_index = i % expected_content.len; |
| 710 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 711 |
} |
| 712 |
|
| 713 |
aws_byte_buf_clean_up(&output_buf); |
| 714 |
|
| 715 |
|
| 716 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 50)); |
| 717 |
|
| 718 |
size_t offset = 200; |
| 719 |
actual_read = 0; |
| 720 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, offset, 50, &output_buf, &actual_read)); |
| 721 |
|
| 722 |
ASSERT_UINT_EQUALS(50, actual_read); |
| 723 |
ASSERT_UINT_EQUALS(50, output_buf.len); |
| 724 |
|
| 725 |
|
| 726 |
for (size_t i = 0; i < 50; i++) { |
| 727 |
size_t file_position = offset + i; |
| 728 |
size_t pattern_index = file_position % expected_content.len; |
| 729 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 730 |
} |
| 731 |
|
| 732 |
aws_byte_buf_clean_up(&output_buf); |
| 733 |
|
| 734 |
|
| 735 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 100)); |
| 736 |
|
| 737 |
offset = file_size - 50; |
| 738 |
actual_read = 0; |
| 739 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, offset, 100, &output_buf, &actual_read)); |
| 740 |
|
| 741 |
ASSERT_UINT_EQUALS(50, actual_read); |
| 742 |
ASSERT_UINT_EQUALS(50, output_buf.len); |
| 743 |
|
| 744 |
|
| 745 |
for (size_t i = 0; i < 50; i++) { |
| 746 |
size_t file_position = offset + i; |
| 747 |
size_t pattern_index = file_position % expected_content.len; |
| 748 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 749 |
} |
| 750 |
|
| 751 |
aws_byte_buf_clean_up(&output_buf); |
| 752 |
|
| 753 |
|
| 754 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 100)); |
| 755 |
|
| 756 |
offset = file_size + 10; |
| 757 |
actual_read = 0; |
| 758 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, offset, 100, &output_buf, &actual_read)); |
| 759 |
|
| 760 |
ASSERT_UINT_EQUALS(0, actual_read); |
| 761 |
ASSERT_UINT_EQUALS(0, output_buf.len); |
| 762 |
|
| 763 |
aws_byte_buf_clean_up(&output_buf); |
| 764 |
|
| 765 |
|
| 766 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 37)); |
| 767 |
|
| 768 |
offset = 13; |
| 769 |
actual_read = 0; |
| 770 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, offset, 37, &output_buf, &actual_read)); |
| 771 |
|
| 772 |
ASSERT_UINT_EQUALS(37, actual_read); |
| 773 |
ASSERT_UINT_EQUALS(37, output_buf.len); |
| 774 |
|
| 775 |
|
| 776 |
for (size_t i = 0; i < 37; i++) { |
| 777 |
size_t file_position = offset + i; |
| 778 |
size_t pattern_index = file_position % expected_content.len; |
| 779 |
ASSERT_UINT_EQUALS(expected_content.ptr[pattern_index], output_buf.buffer[i]); |
| 780 |
} |
| 781 |
|
| 782 |
aws_byte_buf_clean_up(&output_buf); |
| 783 |
|
| 784 |
|
| 785 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 0)); |
| 786 |
|
| 787 |
actual_read = 0; |
| 788 |
ASSERT_SUCCESS(aws_file_path_read_from_offset(tester.file_path, 0, 0, &output_buf, &actual_read)); |
| 789 |
|
| 790 |
ASSERT_UINT_EQUALS(0, actual_read); |
| 791 |
ASSERT_UINT_EQUALS(0, output_buf.len); |
| 792 |
|
| 793 |
aws_byte_buf_clean_up(&output_buf); |
| 794 |
|
| 795 |
|
| 796 |
s_file_path_read_from_offset_tester_cleanup(&tester); |
| 797 |
|
| 798 |
return AWS_OP_SUCCESS; |
| 799 |
} |
| 800 |
|
| 801 |
AWS_TEST_CASE(test_file_path_read_from_offset, s_test_file_path_read_from_offset) |
| 802 |
|
| 803 |
static int s_test_file_path_read_from_offset_direct_io_chunking(struct aws_allocator *allocator, void *ctx) { |
| 804 |
(void)ctx; |
| 805 |
|
| 806 |
#if defined(AWS_OS_LINUX) |
| 807 |
struct aws_file_path_read_from_offset_tester tester; |
| 808 |
char file_path[] = "test_direct_io_chunking.txt"; |
| 809 |
|
| 810 |
size_t chunk_size = 8192; |
| 811 |
size_t page_size = aws_system_info_page_size(); |
| 812 |
size_t file_size = chunk_size * 2 + page_size; |
| 813 |
|
| 814 |
ASSERT_SUCCESS(s_file_path_read_from_offset_tester_init(&tester, allocator, file_path, file_size)); |
| 815 |
|
| 816 |
|
| 817 |
struct aws_byte_buf output_buf; |
| 818 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, chunk_size)); |
| 819 |
|
| 820 |
size_t actual_read = 0; |
| 821 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io_with_chunk_size( |
| 822 |
tester.file_path, 0, chunk_size, chunk_size, &output_buf, &actual_read)); |
| 823 |
|
| 824 |
ASSERT_UINT_EQUALS(chunk_size, actual_read); |
| 825 |
ASSERT_UINT_EQUALS(chunk_size, output_buf.len); |
| 826 |
|
| 827 |
|
| 828 |
struct aws_byte_cursor expected_pattern = aws_byte_cursor_from_c_str("0123456789abcdef"); |
| 829 |
for (size_t i = 0; i < chunk_size; i++) { |
| 830 |
size_t pattern_index = i % expected_pattern.len; |
| 831 |
ASSERT_UINT_EQUALS(expected_pattern.ptr[pattern_index], output_buf.buffer[i]); |
| 832 |
} |
| 833 |
|
| 834 |
aws_byte_buf_clean_up(&output_buf); |
| 835 |
|
| 836 |
|
| 837 |
size_t large_read_size = chunk_size + page_size; |
| 838 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, large_read_size)); |
| 839 |
|
| 840 |
actual_read = 0; |
| 841 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io_with_chunk_size( |
| 842 |
tester.file_path, 0, large_read_size, chunk_size, &output_buf, &actual_read)); |
| 843 |
|
| 844 |
ASSERT_UINT_EQUALS(large_read_size, actual_read); |
| 845 |
ASSERT_UINT_EQUALS(large_read_size, output_buf.len); |
| 846 |
|
| 847 |
|
| 848 |
for (size_t i = 0; i < large_read_size; i++) { |
| 849 |
size_t pattern_index = i % expected_pattern.len; |
| 850 |
ASSERT_UINT_EQUALS(expected_pattern.ptr[pattern_index], output_buf.buffer[i]); |
| 851 |
} |
| 852 |
|
| 853 |
aws_byte_buf_clean_up(&output_buf); |
| 854 |
|
| 855 |
|
| 856 |
size_t very_large_read_size = file_size; |
| 857 |
|
| 858 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, tester.aligned_allocator, very_large_read_size)); |
| 859 |
|
| 860 |
actual_read = 0; |
| 861 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io_with_chunk_size( |
| 862 |
tester.file_path, 0, very_large_read_size, chunk_size, &output_buf, &actual_read)); |
| 863 |
|
| 864 |
ASSERT_UINT_EQUALS(very_large_read_size, actual_read); |
| 865 |
ASSERT_UINT_EQUALS(very_large_read_size, output_buf.len); |
| 866 |
|
| 867 |
|
| 868 |
for (size_t i = 0; i < very_large_read_size; i++) { |
| 869 |
size_t pattern_index = i % expected_pattern.len; |
| 870 |
ASSERT_UINT_EQUALS(expected_pattern.ptr[pattern_index], output_buf.buffer[i]); |
| 871 |
} |
| 872 |
|
| 873 |
aws_byte_buf_clean_up(&output_buf); |
| 874 |
|
| 875 |
|
| 876 |
s_file_path_read_from_offset_tester_cleanup(&tester); |
| 877 |
|
| 878 |
#else |
| 879 |
|
| 880 |
struct aws_string *file_path = aws_string_new_from_c_str(allocator, "test_direct_io_chunking.txt"); |
| 881 |
struct aws_byte_buf dummy_buf; |
| 882 |
aws_byte_buf_init(&dummy_buf, allocator, 1024); |
| 883 |
size_t dummy_read = 0; |
| 884 |
|
| 885 |
ASSERT_FAILS(aws_file_path_read_from_offset_direct_io(file_path, 0, 1024, &dummy_buf, &dummy_read)); |
| 886 |
ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error()); |
| 887 |
|
| 888 |
aws_byte_buf_clean_up(&dummy_buf); |
| 889 |
aws_string_destroy(file_path); |
| 890 |
#endif |
| 891 |
|
| 892 |
return AWS_OP_SUCCESS; |
| 893 |
} |
| 894 |
|
| 895 |
AWS_TEST_CASE(test_file_path_read_from_offset_direct_io_chunking, s_test_file_path_read_from_offset_direct_io_chunking) |
| 896 |
|
| 897 |
static int s_test_file_path_write_to_offset_direct_io(struct aws_allocator *allocator, void *ctx) { |
| 898 |
(void)ctx; |
| 899 |
|
| 900 |
#if defined(AWS_OS_LINUX) |
| 901 |
size_t page_size = aws_system_info_page_size(); |
| 902 |
struct aws_allocator *aligned_alloc = aws_explicit_aligned_allocator_new(page_size); |
| 903 |
ASSERT_NOT_NULL(aligned_alloc); |
| 904 |
|
| 905 |
char file_path_cstr[] = "test_file_path_write_to_offset_direct_io.txt"; |
| 906 |
struct aws_string *file_path = aws_string_new_from_c_str(allocator, file_path_cstr); |
| 907 |
|
| 908 |
|
| 909 |
FILE *f = aws_fopen(file_path_cstr, "wb"); |
| 910 |
ASSERT_NOT_NULL(f); |
| 911 |
fclose(f); |
| 912 |
|
| 913 |
|
| 914 |
size_t two_pages = page_size * 2; |
| 915 |
struct aws_byte_buf write_buf; |
| 916 |
ASSERT_SUCCESS(aws_byte_buf_init(&write_buf, aligned_alloc, two_pages)); |
| 917 |
memset(write_buf.buffer, 'A', two_pages); |
| 918 |
write_buf.len = two_pages; |
| 919 |
struct aws_byte_cursor write_cursor = aws_byte_cursor_from_buf(&write_buf); |
| 920 |
ASSERT_SUCCESS(aws_file_path_write_to_offset_direct_io(file_path, 0, write_cursor)); |
| 921 |
|
| 922 |
|
| 923 |
struct aws_byte_buf read_buf; |
| 924 |
ASSERT_SUCCESS(aws_byte_buf_init(&read_buf, aligned_alloc, two_pages)); |
| 925 |
size_t actual_read = 0; |
| 926 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io(file_path, 0, two_pages, &read_buf, &actual_read)); |
| 927 |
ASSERT_UINT_EQUALS(two_pages, actual_read); |
| 928 |
for (size_t i = 0; i < two_pages; i++) { |
| 929 |
ASSERT_UINT_EQUALS('A', read_buf.buffer[i]); |
| 930 |
} |
| 931 |
aws_byte_buf_clean_up(&read_buf); |
| 932 |
|
| 933 |
|
| 934 |
struct aws_byte_buf overwrite_buf; |
| 935 |
ASSERT_SUCCESS(aws_byte_buf_init(&overwrite_buf, aligned_alloc, page_size)); |
| 936 |
memset(overwrite_buf.buffer, 'C', page_size); |
| 937 |
overwrite_buf.len = page_size; |
| 938 |
struct aws_byte_cursor overwrite_cursor = aws_byte_cursor_from_buf(&overwrite_buf); |
| 939 |
ASSERT_SUCCESS(aws_file_path_write_to_offset_direct_io(file_path, (uint64_t)page_size, overwrite_cursor)); |
| 940 |
|
| 941 |
|
| 942 |
ASSERT_SUCCESS(aws_byte_buf_init(&read_buf, aligned_alloc, two_pages)); |
| 943 |
actual_read = 0; |
| 944 |
ASSERT_SUCCESS(aws_file_path_read_from_offset_direct_io(file_path, 0, two_pages, &read_buf, &actual_read)); |
| 945 |
ASSERT_UINT_EQUALS(two_pages, actual_read); |
| 946 |
for (size_t i = 0; i < page_size; i++) { |
| 947 |
ASSERT_UINT_EQUALS('A', read_buf.buffer[i]); |
| 948 |
} |
| 949 |
for (size_t i = page_size; i < two_pages; i++) { |
| 950 |
ASSERT_UINT_EQUALS('C', read_buf.buffer[i]); |
| 951 |
} |
| 952 |
aws_byte_buf_clean_up(&read_buf); |
| 953 |
aws_byte_buf_clean_up(&overwrite_buf); |
| 954 |
|
| 955 |
|
| 956 |
struct aws_byte_buf unaligned_buf; |
| 957 |
size_t unaligned_len = page_size + 37; |
| 958 |
ASSERT_SUCCESS(aws_byte_buf_init(&unaligned_buf, aligned_alloc, unaligned_len)); |
| 959 |
memset(unaligned_buf.buffer, 'X', unaligned_len); |
| 960 |
unaligned_buf.len = unaligned_len; |
| 961 |
struct aws_byte_cursor unaligned_cursor = aws_byte_cursor_from_buf(&unaligned_buf); |
| 962 |
ASSERT_FAILS(aws_file_path_write_to_offset_direct_io(file_path, 37, unaligned_cursor)); |
| 963 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error()); |
| 964 |
aws_byte_buf_clean_up(&unaligned_buf); |
| 965 |
|
| 966 |
|
| 967 |
struct aws_string *nonexistent = aws_string_new_from_c_str(allocator, "nonexistent_direct_io_file.txt"); |
| 968 |
struct aws_byte_cursor one_page_cursor = {.ptr = write_buf.buffer, .len = page_size}; |
| 969 |
ASSERT_FAILS(aws_file_path_write_to_offset_direct_io(nonexistent, 0, one_page_cursor)); |
| 970 |
aws_string_destroy(nonexistent); |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
remove(file_path_cstr); |
| 975 |
f = aws_fopen(file_path_cstr, "wb"); |
| 976 |
ASSERT_NOT_NULL(f); |
| 977 |
fclose(f); |
| 978 |
|
| 979 |
|
| 980 |
memset(write_buf.buffer, 'D', page_size); |
| 981 |
write_cursor = (struct aws_byte_cursor){.ptr = write_buf.buffer, .len = page_size}; |
| 982 |
ASSERT_SUCCESS(aws_file_path_write_to_offset_direct_io(file_path, 0, write_cursor)); |
| 983 |
|
| 984 |
memset(write_buf.buffer, 'E', page_size); |
| 985 |
ASSERT_SUCCESS(aws_file_path_write_to_offset_direct_io(file_path, (uint64_t)page_size, write_cursor)); |
| 986 |
|
| 987 |
memset(write_buf.buffer, 'F', page_size); |
| 988 |
ASSERT_SUCCESS(aws_file_path_write_to_offset_direct_io(file_path, (uint64_t)(page_size * 2), write_cursor)); |
| 989 |
|
| 990 |
|
| 991 |
size_t tail_len = 37; |
| 992 |
uint8_t tail_data[37]; |
| 993 |
memset(tail_data, 'G', tail_len); |
| 994 |
FILE *tail_file = aws_fopen(file_path_cstr, "r+b"); |
| 995 |
ASSERT_NOT_NULL(tail_file); |
| 996 |
ASSERT_TRUE(fseek(tail_file, (long)(page_size * 3), SEEK_SET) == 0); |
| 997 |
size_t written = fwrite(tail_data, 1, tail_len, tail_file); |
| 998 |
ASSERT_INT_EQUALS((int)tail_len, (int)written); |
| 999 |
fclose(tail_file); |
| 1000 |
|
| 1001 |
|
| 1002 |
size_t total_size = page_size * 3 + tail_len; |
| 1003 |
FILE *verify_file = aws_fopen(file_path_cstr, "rb"); |
| 1004 |
ASSERT_NOT_NULL(verify_file); |
| 1005 |
uint8_t *verify_buf = aws_mem_calloc(allocator, 1, total_size); |
| 1006 |
ASSERT_UINT_EQUALS(total_size, fread(verify_buf, 1, total_size, verify_file)); |
| 1007 |
fclose(verify_file); |
| 1008 |
|
| 1009 |
for (size_t i = 0; i < page_size; i++) { |
| 1010 |
ASSERT_UINT_EQUALS('D', verify_buf[i]); |
| 1011 |
} |
| 1012 |
for (size_t i = page_size; i < page_size * 2; i++) { |
| 1013 |
ASSERT_UINT_EQUALS('E', verify_buf[i]); |
| 1014 |
} |
| 1015 |
for (size_t i = page_size * 2; i < page_size * 3; i++) { |
| 1016 |
ASSERT_UINT_EQUALS('F', verify_buf[i]); |
| 1017 |
} |
| 1018 |
for (size_t i = page_size * 3; i < total_size; i++) { |
| 1019 |
ASSERT_UINT_EQUALS('G', verify_buf[i]); |
| 1020 |
} |
| 1021 |
aws_mem_release(allocator, verify_buf); |
| 1022 |
|
| 1023 |
|
| 1024 |
aws_byte_buf_clean_up(&write_buf); |
| 1025 |
remove(file_path_cstr); |
| 1026 |
aws_string_destroy(file_path); |
| 1027 |
aws_explicit_aligned_allocator_destroy(aligned_alloc); |
| 1028 |
#else |
| 1029 |
(void)allocator; |
| 1030 |
struct aws_string *file_path = aws_string_new_from_c_str(allocator, "dummy.txt"); |
| 1031 |
struct aws_byte_cursor empty = {.ptr = NULL, .len = 0}; |
| 1032 |
ASSERT_FAILS(aws_file_path_write_to_offset_direct_io(file_path, 0, empty)); |
| 1033 |
ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error()); |
| 1034 |
aws_string_destroy(file_path); |
| 1035 |
#endif |
| 1036 |
|
| 1037 |
return AWS_OP_SUCCESS; |
| 1038 |
} |
| 1039 |
|
| 1040 |
AWS_TEST_CASE(test_file_path_write_to_offset_direct_io, s_test_file_path_write_to_offset_direct_io) |