diff options
Diffstat (limited to 'lib/libarchive/test/test_read_extract.c')
-rw-r--r-- | lib/libarchive/test/test_read_extract.c | 108 |
1 files changed, 38 insertions, 70 deletions
diff --git a/lib/libarchive/test/test_read_extract.c b/lib/libarchive/test/test_read_extract.c index 887ddfd..10bd014 100644 --- a/lib/libarchive/test/test_read_extract.c +++ b/lib/libarchive/test/test_read_extract.c @@ -32,22 +32,15 @@ DEFINE_TEST(test_read_extract) { struct archive_entry *ae; struct archive *a; -#if !defined(_WIN32) || defined(__CYGWIN__) - struct stat st; -#endif size_t used; - int i; + int i, numEntries = 0; char *buff, *file_buff; -#if !defined(_WIN32) || defined(__CYGWIN__) - int fd; - ssize_t bytes_read; -#endif buff = malloc(BUFF_SIZE); file_buff = malloc(FILE_BUFF_SIZE); /* Force the umask to something predictable. */ - umask(022); + assertUmask(022); /* Create a new archive in memory containing various types of entries. */ assert((a = archive_write_new()) != NULL); @@ -55,12 +48,14 @@ DEFINE_TEST(test_read_extract) assertA(0 == archive_write_set_compression_none(a)); assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used)); /* A directory to be restored with EXTRACT_PERM. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir_0775"); archive_entry_set_mode(ae, S_IFDIR | 0775); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* A regular file. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "file"); archive_entry_set_mode(ae, S_IFREG | 0755); @@ -71,49 +66,53 @@ DEFINE_TEST(test_read_extract) assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE)); archive_entry_free(ae); /* A directory that should obey umask when restored. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir"); archive_entry_set_mode(ae, S_IFDIR | 0777); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* A file in the directory. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir/file"); archive_entry_set_mode(ae, S_IFREG | 0700); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* A file in a dir that is not already in the archive. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir2/file"); archive_entry_set_mode(ae, S_IFREG | 0000); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* A dir with a trailing /. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir3/."); archive_entry_set_mode(ae, S_IFDIR | 0710); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* Multiple dirs with a single entry. */ + ++numEntries; assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "dir4/a/../b/../c/"); archive_entry_set_mode(ae, S_IFDIR | 0711); assertA(0 == archive_write_header(a, ae)); archive_entry_free(ae); /* A symlink. */ - assert((ae = archive_entry_new()) != NULL); - archive_entry_copy_pathname(ae, "symlink"); - archive_entry_set_mode(ae, S_IFLNK | 0755); - archive_entry_set_symlink(ae, "file"); - assertA(0 == archive_write_header(a, ae)); - archive_entry_free(ae); + if (canSymlink()) { + ++numEntries; + assert((ae = archive_entry_new()) != NULL); + archive_entry_copy_pathname(ae, "symlink"); + archive_entry_set_mode(ae, AE_IFLNK | 0755); + archive_entry_set_symlink(ae, "file"); + assertA(0 == archive_write_header(a, ae)); + archive_entry_free(ae); + } /* Close out the archive. */ assertA(0 == archive_write_close(a)); -#if ARCHIVE_VERSION_NUMBER < 2000000 - archive_write_finish(a); -#else assertA(0 == archive_write_finish(a)); -#endif /* Extract the entries to disk. */ assert((a = archive_read_new()) != NULL); @@ -125,75 +124,44 @@ DEFINE_TEST(test_read_extract) assertA(0 == archive_read_next_header(a, &ae)); assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM)); /* Rest of entries get restored with no flags. */ - for (i = 0; i < 7; i++) { - failure("Error reading entry %d", i+1); + for (i = 1; i < numEntries; i++) { + failure("Error reading entry %d", i); assertA(0 == archive_read_next_header(a, &ae)); + failure("Failed to extract entry %d: %s", i, + archive_entry_pathname(ae)); assertA(0 == archive_read_extract(a, ae, 0)); } assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae)); assert(0 == archive_read_close(a)); -#if ARCHIVE_VERSION_NUMBER < 2000000 - archive_read_finish(a); -#else assert(0 == archive_read_finish(a)); -#endif -#if !defined(_WIN32) || defined(__CYGWIN__) /* Test the entries on disk. */ /* This first entry was extracted with ARCHIVE_EXTRACT_PERM, * so the permissions should have been restored exactly, * including resetting the gid bit on those platforms * where gid is inherited by subdirs. */ - assert(0 == stat("dir_0775", &st)); failure("This was 0775 in archive, and should be 0775 on disk"); - assertEqualInt(st.st_mode, S_IFDIR | 0775); + assertIsDir("dir_0775", 0775); /* Everything else was extracted without ARCHIVE_EXTRACT_PERM, * so there may be some sloppiness about gid bits on directories. */ - assert(0 == stat("file", &st)); - failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755); - assertEqualInt(st.st_mode, S_IFREG | 0755); - failure("The file extracted to disk is the wrong size."); - assert(st.st_size == FILE_BUFF_SIZE); - fd = open("file", O_RDONLY); - failure("The file on disk could not be opened."); - assert(fd != 0); - bytes_read = read(fd, buff, FILE_BUFF_SIZE); - close(fd); - failure("The file contents read from disk are the wrong size"); - assert(bytes_read == FILE_BUFF_SIZE); - failure("The file contents on disk do not match the file contents that were put into the archive."); - assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0); - assert(0 == stat("dir", &st)); - failure("This was 0777 in archive, but umask should make it 0755"); + assertIsReg("file", 0755); + assertFileSize("file", FILE_BUFF_SIZE); + assertFileContents(file_buff, FILE_BUFF_SIZE, "file"); /* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit * when checking dir modes, as some systems inherit sgid bit * from the parent dir. */ - assertEqualInt(0755, st.st_mode & 0777); - assert(0 == stat("dir/file", &st)); - assert(st.st_mode == (S_IFREG | 0700)); - assert(0 == stat("dir2", &st)); - assertEqualInt(0755, st.st_mode & 0777); - assert(0 == stat("dir2/file", &st)); - assert(st.st_mode == (S_IFREG | 0000)); - assert(0 == stat("dir3", &st)); - assertEqualInt(0710, st.st_mode & 0777); - assert(0 == stat("dir4", &st)); - assertEqualInt(0755, st.st_mode & 0777); - assert(0 == stat("dir4/a", &st)); - assertEqualInt(0755, st.st_mode & 0777); - assert(0 == stat("dir4/b", &st)); - assertEqualInt(0755, st.st_mode & 0777); - assert(0 == stat("dir4/c", &st)); - assertEqualInt(0711, st.st_mode & 0777); - assert(0 == lstat("symlink", &st)); - assert(S_ISLNK(st.st_mode)); -#if HAVE_LCHMOD - /* Systems that lack lchmod() can't set symlink perms, so skip this. */ - assert((st.st_mode & 07777) == 0755); -#endif - assert(0 == stat("symlink", &st)); - assert(st.st_mode == (S_IFREG | 0755)); -#endif + failure("This was 0777 in archive, but umask should make it 0755"); + assertIsDir("dir", 0755); + assertIsReg("dir/file", 0700); + assertIsDir("dir2", 0755); + assertIsReg("dir2/file", 0000); + assertIsDir("dir3", 0710); + assertIsDir("dir4", 0755); + assertIsDir("dir4/a", 0755); + assertIsDir("dir4/b", 0755); + assertIsDir("dir4/c", 0711); + if (canSymlink()) + assertIsSymlink("symlink", "file"); free(buff); free(file_buff); |