Make use of new checking capabilities in unit tests

This commit is contained in:
Mike Gelfand
2017-05-30 20:56:12 +03:00
parent 3d0b06ca0f
commit 5f7bc801c8
17 changed files with 386 additions and 398 deletions
+172 -166
View File
@@ -172,7 +172,7 @@ static int test_get_info(void)
/* Can't get info of non-existent file/directory */
check(!tr_sys_path_get_info(path1, 0, &info, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
t = time(NULL);
@@ -181,19 +181,21 @@ static int test_get_info(void)
/* Good file info */
clear_path_info(&info);
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
/* Good file info (by handle) */
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ, 0, NULL);
clear_path_info(&info);
check(tr_sys_file_get_info(fd, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
tr_sys_file_close(fd, NULL);
tr_sys_path_remove(path1, NULL);
@@ -203,17 +205,18 @@ static int test_get_info(void)
tr_sys_dir_create(path1, 0, 0777, NULL);
clear_path_info(&info);
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_DIRECTORY);
check(info.size != (uint64_t)-1);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_uint(info.size, !=, (uint64_t)-1);
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
tr_sys_path_remove(path1, NULL);
if (create_symlink(path1, path2, false))
{
/* Can't get info of non-existent file/directory */
check(!tr_sys_path_get_info(path1, 0, &info, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
t = time(NULL);
@@ -222,19 +225,21 @@ static int test_get_info(void)
/* Good file info */
clear_path_info(&info);
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
/* Good file info (by handle) */
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ, 0, NULL);
clear_path_info(&info);
check(tr_sys_file_get_info(fd, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
tr_sys_file_close(fd, NULL);
tr_sys_path_remove(path2, NULL);
@@ -244,10 +249,11 @@ static int test_get_info(void)
tr_sys_dir_create(path2, 0, 0777, NULL);
clear_path_info(&info);
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_DIRECTORY);
check(info.size != (uint64_t)-1);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
check_uint(info.size, !=, (uint64_t)-1);
check_int(info.last_modified_at, >=, t);
check_int(info.last_modified_at, <=, time(NULL));
tr_sys_path_remove(path2, NULL);
tr_sys_path_remove(path1, NULL);
@@ -276,19 +282,19 @@ static int test_path_exists(void)
/* Non-existent file does not exist */
check(!tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Create file and see that it exists */
libtest_create_file_with_string_contents(path1, "test");
check(tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path1, NULL);
/* Create directory and see that it exists */
tr_sys_dir_create(path1, 0, 0777, NULL);
check(tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path1, NULL);
@@ -296,19 +302,19 @@ static int test_path_exists(void)
{
/* Non-existent file does not exist (via symlink) */
check(!tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Create file and see that it exists (via symlink) */
libtest_create_file_with_string_contents(path2, "test");
check(tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
/* Create directory and see that it exists (via symlink) */
tr_sys_dir_create(path2, 0, 0777, NULL);
check(tr_sys_path_exists(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
tr_sys_path_remove(path1, NULL);
@@ -387,45 +393,45 @@ static int test_path_is_same(void)
/* Two non-existent files are not the same */
check(!tr_sys_path_is_same(path1, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Two same files are the same */
libtest_create_file_with_string_contents(path1, "test");
check(tr_sys_path_is_same(path1, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Existent and non-existent files are not the same */
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Two separate files (even with same content) are not the same */
libtest_create_file_with_string_contents(path2, "test");
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path1, NULL);
/* Two same directories are the same */
tr_sys_dir_create(path1, 0, 0777, NULL);
check(tr_sys_path_is_same(path1, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* File and directory are not the same */
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
/* Two separate directories are not the same */
tr_sys_dir_create(path2, 0, 0777, NULL);
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path1, NULL);
tr_sys_path_remove(path2, NULL);
@@ -434,61 +440,61 @@ static int test_path_is_same(void)
{
/* Directory and symlink pointing to it are the same */
check(tr_sys_path_is_same(path1, test_dir, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_path_is_same(test_dir, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Non-existent file and symlink are not the same */
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Symlinks pointing to different directories are not the same */
create_symlink(path2, "..", true);
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
/* Symlinks pointing to same directory are the same */
create_symlink(path2, ".", true);
check(tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
/* Directory and symlink pointing to another directory are not the same */
tr_sys_dir_create(path2, 0, 0777, NULL);
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Symlinks pointing to same directory are the same */
create_symlink(path3, "..", true);
check(tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path1, NULL);
/* File and symlink pointing to directory are not the same */
libtest_create_file_with_string_contents(path1, "test");
check(!tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path3, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path3, NULL);
/* File and symlink pointing to same file are the same */
create_symlink(path3, path1, false);
check(tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_path_is_same(path3, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Symlinks pointing to non-existent files are not the same */
tr_sys_path_remove(path1, NULL);
@@ -496,25 +502,25 @@ static int test_path_is_same(void)
tr_sys_path_remove(path3, NULL);
create_symlink(path3, "missing", false);
check(!tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path3, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path3, NULL);
/* Symlinks pointing to same non-existent file are not the same */
create_symlink(path3, ".." NATIVE_PATH_SEP "missing", false);
check(!tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path3, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Non-existent file and symlink pointing to non-existent file are not the same */
tr_sys_path_remove(path3, NULL);
check(!tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path3, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
tr_sys_path_remove(path1, NULL);
@@ -533,19 +539,19 @@ static int test_path_is_same(void)
{
/* File and hardlink to it are the same */
check(tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Two hardlinks to the same file are the same */
create_hardlink(path3, path2);
check(tr_sys_path_is_same(path2, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
check(tr_sys_path_is_same(path1, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path3, NULL);
@@ -553,9 +559,9 @@ static int test_path_is_same(void)
libtest_create_file_with_string_contents(path3, "test");
create_hardlink(path2, path3);
check(!tr_sys_path_is_same(path1, path2, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_is_same(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path3, NULL);
tr_sys_path_remove(path2, NULL);
@@ -568,7 +574,7 @@ static int test_path_is_same(void)
if (create_symlink(path2, path1, false) && create_hardlink(path3, path1))
{
check(tr_sys_path_is_same(path2, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
}
else
{
@@ -604,8 +610,8 @@ static int test_path_resolve(void)
char* tmp;
tmp = tr_sys_path_resolve(path2, &err);
check(tmp != NULL);
check(err == NULL);
check_str(tmp, !=, NULL);
check_ptr(err, ==, NULL);
check(path_contains_no_symlinks(tmp));
tr_free(tmp);
@@ -613,8 +619,8 @@ static int test_path_resolve(void)
tr_sys_dir_create(path1, 0, 0755, NULL);
tmp = tr_sys_path_resolve(path2, &err);
check(tmp != NULL);
check(err == NULL);
check_str(tmp, !=, NULL);
check_ptr(err, ==, NULL);
check(path_contains_no_symlinks(tmp));
tr_free(tmp);
}
@@ -648,15 +654,15 @@ static int test_path_xname(struct xname_test_data const* data, size_t data_size,
if (data[i].output != NULL)
{
check(name != NULL);
check(err == NULL);
check_str(name, !=, NULL);
check_ptr(err, ==, NULL);
check_str(name, ==, data[i].output);
tr_free(name);
}
else
{
check(name == NULL);
check(err != NULL);
check_str(name, ==, NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
}
}
@@ -802,43 +808,43 @@ static int test_path_rename(void)
check(tr_sys_path_rename(path1, path2, &err));
check(!tr_sys_path_exists(path1, NULL));
check(tr_sys_path_exists(path2, NULL));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Backward rename works */
check(tr_sys_path_rename(path2, path1, &err));
check(tr_sys_path_exists(path1, NULL));
check(!tr_sys_path_exists(path2, NULL));
check(err == NULL);
check_ptr(err, ==, NULL);
/* Another backward rename [of non-existent file] does not work */
check(!tr_sys_path_rename(path2, path1, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
/* Rename to file which couldn't be created does not work */
check(!tr_sys_path_rename(path1, path3, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
/* Rename of non-existent file does not work */
check(!tr_sys_path_rename(path3, path2, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
libtest_create_file_with_string_contents(path2, "test");
/* Renaming file does overwrite existing file */
check(tr_sys_path_rename(path2, path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_dir_create(path2, 0, 0777, NULL);
/* Renaming file does not overwrite existing directory, and vice versa */
check(!tr_sys_path_rename(path1, path2, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
check(!tr_sys_path_rename(path2, path1, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
tr_sys_path_remove(path2, NULL);
@@ -855,7 +861,7 @@ static int test_path_rename(void)
/* Rename of symlink works, files stay the same */
check(tr_sys_path_rename(path2, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_exists(path2, NULL));
check(tr_sys_path_exists(path3, NULL));
check(tr_sys_path_is_same(path1, path3, NULL));
@@ -876,7 +882,7 @@ static int test_path_rename(void)
/* Rename of hardlink works, files stay the same */
check(tr_sys_path_rename(path2, path3, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_exists(path2, NULL));
check(tr_sys_path_exists(path3, NULL));
check(tr_sys_path_is_same(path1, path3, NULL));
@@ -913,7 +919,7 @@ static int test_path_remove(void)
/* Can't remove non-existent file/directory */
check(!tr_sys_path_exists(path1, NULL));
check(!tr_sys_path_remove(path1, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
check(!tr_sys_path_exists(path1, NULL));
tr_error_clear(&err);
@@ -921,14 +927,14 @@ static int test_path_remove(void)
libtest_create_file_with_string_contents(path1, "test");
check(tr_sys_path_exists(path1, NULL));
check(tr_sys_path_remove(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_exists(path1, NULL));
/* Removing empty directory works */
tr_sys_dir_create(path1, 0, 0777, NULL);
check(tr_sys_path_exists(path1, NULL));
check(tr_sys_path_remove(path1, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(!tr_sys_path_exists(path1, NULL));
/* Removing non-empty directory fails */
@@ -937,7 +943,7 @@ static int test_path_remove(void)
check(tr_sys_path_exists(path2, NULL));
check(tr_sys_path_exists(path3, NULL));
check(!tr_sys_path_remove(path2, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
check(tr_sys_path_exists(path2, NULL));
check(tr_sys_path_exists(path3, NULL));
tr_error_clear(&err);
@@ -967,11 +973,11 @@ static int test_file_open(void)
/* Can't open non-existent file */
check(!tr_sys_path_exists(path1, NULL));
check(tr_sys_file_open(path1, TR_SYS_FILE_READ, 0600, &err) == TR_BAD_SYS_FILE);
check(err != NULL);
check_ptr(err, !=, NULL);
check(!tr_sys_path_exists(path1, NULL));
tr_error_clear(&err);
check(tr_sys_file_open(path1, TR_SYS_FILE_WRITE, 0600, &err) == TR_BAD_SYS_FILE);
check(err != NULL);
check_ptr(err, !=, NULL);
check(!tr_sys_path_exists(path1, NULL));
tr_error_clear(&err);
@@ -980,19 +986,19 @@ static int test_file_open(void)
#ifdef _WIN32
/* This works on *NIX */
check(tr_sys_file_open(path1, TR_SYS_FILE_READ, 0600, &err) == TR_BAD_SYS_FILE);
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
#endif
check(tr_sys_file_open(path1, TR_SYS_FILE_WRITE, 0600, &err) == TR_BAD_SYS_FILE);
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
tr_sys_path_remove(path1, NULL);
/* Can create non-existent file */
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0640, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
check_int(fd, !=, TR_BAD_SYS_FILE);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
check(tr_sys_path_exists(path1, NULL));
check(validate_permissions(path1, 0640));
@@ -1000,12 +1006,12 @@ static int test_file_open(void)
/* Can open existing file */
check(tr_sys_path_exists(path1, NULL));
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
check_int(fd, !=, TR_BAD_SYS_FILE);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
check_int(fd, !=, TR_BAD_SYS_FILE);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
tr_sys_path_remove(path1, NULL);
@@ -1013,8 +1019,8 @@ static int test_file_open(void)
/* Can't create new file if it already exists */
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE_NEW, 0640, &err);
check(fd == TR_BAD_SYS_FILE);
check(err != NULL);
check_int(fd, ==, TR_BAD_SYS_FILE);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint(info.size, ==, 4);
@@ -1023,8 +1029,8 @@ static int test_file_open(void)
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint(info.size, ==, 4);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_APPEND, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
check_int(fd, !=, TR_BAD_SYS_FILE);
check_ptr(err, ==, NULL);
tr_sys_file_write(fd, "s", 1, NULL, NULL); /* On *NIX, pointer is positioned on each write but not initially */
tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, NULL);
check_uint(n, ==, 5);
@@ -1034,8 +1040,8 @@ static int test_file_open(void)
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint(info.size, ==, 5);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_TRUNCATE, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
check_int(fd, !=, TR_BAD_SYS_FILE);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 0);
tr_sys_file_close(fd, NULL);
@@ -1066,72 +1072,72 @@ static int test_file_read_write_seek(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
check(tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 0);
check(tr_sys_file_write(fd, "test", 4, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 4);
check(tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 4);
check(tr_sys_file_seek(fd, 0, TR_SEEK_SET, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 4);
check_int(memcmp(buf, "test", 4), ==, 0);
check_mem(buf, ==, "test", 4);
check(tr_sys_file_seek(fd, -3, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 1);
check(tr_sys_file_write(fd, "E", 1, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 1);
check(tr_sys_file_seek(fd, -2, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 4);
check_int(memcmp(buf, "tEst", 4), ==, 0);
check_mem(buf, ==, "tEst", 4);
check(tr_sys_file_seek(fd, 0, TR_SEEK_END, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 4);
check(tr_sys_file_write(fd, " ok", 3, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 3);
check(tr_sys_file_seek(fd, 0, TR_SEEK_SET, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 7);
check_int(memcmp(buf, "tEst ok", 7), ==, 0);
check_mem(buf, ==, "tEst ok", 7);
check(tr_sys_file_write_at(fd, "-", 1, 4, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 1);
check(tr_sys_file_read_at(fd, buf, 5, 2, &n, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_uint(n, ==, 5);
check_int(memcmp(buf, "st-ok", 5), ==, 0);
check_mem(buf, ==, "st-ok", 5);
tr_sys_file_close(fd, NULL);
@@ -1156,22 +1162,22 @@ static int test_file_truncate(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
check(tr_sys_file_truncate(fd, 10, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 10);
check(tr_sys_file_truncate(fd, 20, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 20);
check(tr_sys_file_truncate(fd, 0, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 0);
check(tr_sys_file_truncate(fd, 50, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
@@ -1181,7 +1187,7 @@ static int test_file_truncate(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
check(tr_sys_file_truncate(fd, 25, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
@@ -1210,13 +1216,13 @@ static int test_file_preallocate(void)
if (tr_sys_file_preallocate(fd, 50, 0, &err))
{
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 50);
}
else
{
check(err != NULL);
check_ptr(err, !=, NULL);
fprintf(stderr, "WARNING: [%s] unable to preallocate file (full): %s (%d)\n", __FUNCTION__, err->message, err->code);
tr_error_clear(&err);
}
@@ -1229,13 +1235,13 @@ static int test_file_preallocate(void)
if (tr_sys_file_preallocate(fd, 500 * 1024 * 1024, TR_SYS_FILE_PREALLOC_SPARSE, &err))
{
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint(info.size, ==, 500 * 1024 * 1024);
}
else
{
check(err != NULL);
check_ptr(err, !=, NULL);
fprintf(stderr, "WARNING: [%s] unable to preallocate file (sparse): %s (%d)\n", __FUNCTION__, err->message, err->code);
tr_error_clear(&err);
}
@@ -1265,21 +1271,21 @@ static int test_file_map(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, 0600, NULL);
view = tr_sys_file_map_for_reading(fd, 0, 4, &err);
check(view != NULL);
check(err == NULL);
check_ptr(view, !=, NULL);
check_ptr(err, ==, NULL);
check_int(memcmp(view, "test", 4), ==, 0);
check_mem(view, ==, "test", 4);
#ifdef HAVE_UNIFIED_BUFFER_CACHE
tr_sys_file_write_at(fd, "E", 1, 1, NULL, NULL);
check_int(memcmp(view, "tEst", 4), ==, 0);
check_mem(view, ==, "tEst", 4);
#endif
check(tr_sys_file_unmap(view, 4, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_close(fd, NULL);
@@ -1306,31 +1312,31 @@ static int test_file_utilities(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ, 0, NULL);
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "a");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "bc");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "def");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "ghij");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "");
check(tr_sys_file_read_line(fd, buffer, 4, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "klmn");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "o");
check(!tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "o"); /* on EOF, buffer stays unchanged */
tr_sys_file_close(fd, NULL);
@@ -1338,45 +1344,45 @@ static int test_file_utilities(void)
fd = tr_sys_file_open(path1, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE | TR_SYS_FILE_TRUNCATE, 0, NULL);
check(tr_sys_file_write_line(fd, "p", &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_line(fd, "", &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_line(fd, "qr", &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_fmt(fd, "s%cu\r\n", &err, 't'));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_line(fd, "", &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_line(fd, "", &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_file_write_fmt(fd, "v%sy%d", &err, "wx", 2));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_file_seek(fd, 0, TR_SEEK_SET, NULL, NULL);
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "p");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "qr");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "stu");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "");
check(tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "vwxy2");
check(!tr_sys_file_read_line(fd, buffer, TR_N_ELEMENTS(buffer), &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check_str(buffer, ==, "vwxy2"); /* on EOF, buffer stays unchanged */
tr_sys_file_close(fd, NULL);
@@ -1401,7 +1407,7 @@ static int test_dir_create(void)
/* Can create directory which has parent */
check(tr_sys_dir_create(path1, 0, 0700, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_path_exists(path1, NULL));
check(validate_permissions(path1, 0700));
@@ -1410,23 +1416,23 @@ static int test_dir_create(void)
/* Can't create directory where file already exists */
check(!tr_sys_dir_create(path1, 0, 0700, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
check(!tr_sys_dir_create(path1, TR_SYS_DIR_CREATE_PARENTS, 0700, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
tr_error_clear(&err);
tr_sys_path_remove(path1, NULL);
/* Can't create directory which has no parent */
check(!tr_sys_dir_create(path2, 0, 0700, &err));
check(err != NULL);
check_ptr(err, !=, NULL);
check(!tr_sys_path_exists(path2, NULL));
tr_error_clear(&err);
/* Can create directory with parent directories */
check(tr_sys_dir_create(path2, TR_SYS_DIR_CREATE_PARENTS, 0751, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_path_exists(path1, NULL));
check(tr_sys_path_exists(path2, NULL));
check(validate_permissions(path1, 0751));
@@ -1434,9 +1440,9 @@ static int test_dir_create(void)
/* Can create existing directory (no-op) */
check(tr_sys_dir_create(path1, 0, 0700, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_dir_create(path1, TR_SYS_DIR_CREATE_PARENTS, 0700, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
tr_sys_path_remove(path2, NULL);
tr_sys_path_remove(path1, NULL);
@@ -1457,12 +1463,12 @@ static int test_dir_read_impl(char const* path, bool* have1, bool* have2)
*have1 = *have2 = false;
dd = tr_sys_dir_open(path, &err);
check(dd != TR_BAD_SYS_DIR);
check(err == NULL);
check_ptr(dd, !=, TR_BAD_SYS_DIR);
check_ptr(err, ==, NULL);
while ((name = tr_sys_dir_read_name(dd, &err)) != NULL)
{
check(err == NULL);
check_ptr(err, ==, NULL);
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
{
@@ -1483,10 +1489,10 @@ static int test_dir_read_impl(char const* path, bool* have1, bool* have2)
}
}
check(err == NULL);
check_ptr(err, ==, NULL);
check(tr_sys_dir_close(dd, &err));
check(err == NULL);
check_ptr(err, ==, NULL);
return 0;
}