Merge changes from vendor to address several Coverity issues with

contrib/libarchive's tests

MFC after:	2 weeks
Obtained from:	libarchive (ebe29c, fd0ea2, f9e3de)
Reported by:	Coverity
This commit is contained in:
ngie 2016-12-12 02:21:56 +00:00
parent 719d6b9d9d
commit d68a9833c6
27 changed files with 149 additions and 78 deletions

View File

@ -47,10 +47,13 @@ DEFINE_TEST(test_option_J_upper)
} }
failure("-J option is broken"); failure("-J option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has an xz signature. */ /* Check that the archive file has an xz signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\3757zXZ", 5); assertEqualMem(p, "\3757zXZ", 5);
done:
free(p);
} }

View File

@ -47,10 +47,13 @@ DEFINE_TEST(test_option_Z_upper)
} }
failure("-Z option is broken"); failure("-Z option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has a compress signature. */ /* Check that the archive file has a compress signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\x1f\x9d", 2); assertEqualMem(p, "\x1f\x9d", 2);
done:
free(p);
} }

View File

@ -49,6 +49,7 @@ DEFINE_TEST(test_option_u)
p = slurpfile(&s, "copy/f"); p = slurpfile(&s, "copy/f");
assertEqualInt(s, 1); assertEqualInt(s, 1);
assertEqualMem(p, "a", 1); assertEqualMem(p, "a", 1);
free(p);
/* Recreate the file with a single "b" */ /* Recreate the file with a single "b" */
assertMakeFile("f", 0644, "b"); assertMakeFile("f", 0644, "b");
@ -68,6 +69,7 @@ DEFINE_TEST(test_option_u)
p = slurpfile(&s, "copy/f"); p = slurpfile(&s, "copy/f");
assertEqualInt(s, 1); assertEqualInt(s, 1);
assertEqualMem(p, "a", 1); assertEqualMem(p, "a", 1);
free(p);
/* Copy the file to the "copy" dir with -u (force) */ /* Copy the file to the "copy" dir with -u (force) */
r = systemf("echo f| %s -pud copy >copy.out 2>copy.err", r = systemf("echo f| %s -pud copy >copy.out 2>copy.err",
@ -78,4 +80,5 @@ DEFINE_TEST(test_option_u)
p = slurpfile(&s, "copy/f"); p = slurpfile(&s, "copy/f");
assertEqualInt(s, 1); assertEqualInt(s, 1);
assertEqualMem(p, "b", 1); assertEqualMem(p, "b", 1);
free(p);
} }

View File

@ -46,11 +46,14 @@ DEFINE_TEST(test_option_y)
} }
failure("-y option is broken"); failure("-y option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
assertTextFileContents("1 block\n", "archive.err"); assertTextFileContents("1 block\n", "archive.err");
/* Check that the archive file has a bzip2 signature. */ /* Check that the archive file has a bzip2 signature. */
free(p);
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "BZh9", 4); assertEqualMem(p, "BZh9", 4);
done:
free(p);
} }

View File

@ -86,21 +86,7 @@ static int
read_open_memory_internal(struct archive *a, const void *buff, read_open_memory_internal(struct archive *a, const void *buff,
size_t size, size_t read_size, int level) size_t size, size_t read_size, int level)
{ {
struct read_memory_data *mine; struct read_memory_data *mine = NULL;
mine = (struct read_memory_data *)malloc(sizeof(*mine));
if (mine == NULL) {
archive_set_error(a, ENOMEM, "No memory");
return (ARCHIVE_FATAL);
}
memset(mine, 0, sizeof(*mine));
mine->start = mine->p = (const unsigned char *)buff;
mine->end = mine->start + size;
mine->read_size = read_size;
mine->copy_buff_offset = 32;
mine->copy_buff_size = read_size + mine->copy_buff_offset * 2;
mine->copy_buff = malloc(mine->copy_buff_size);
memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
switch (level) { switch (level) {
case 3: case 3:
@ -109,6 +95,20 @@ read_open_memory_internal(struct archive *a, const void *buff,
archive_read_set_open_callback(a, memory_read_open); archive_read_set_open_callback(a, memory_read_open);
archive_read_set_skip_callback(a, memory_read_skip); archive_read_set_skip_callback(a, memory_read_skip);
case 1: case 1:
mine = malloc(sizeof(*mine));
if (mine == NULL) {
archive_set_error(a, ENOMEM, "No memory");
return (ARCHIVE_FATAL);
}
memset(mine, 0, sizeof(*mine));
mine->start = mine->p = (const unsigned char *)buff;
mine->end = mine->start + size;
mine->read_size = read_size;
mine->copy_buff_offset = 32;
mine->copy_buff_size = read_size + mine->copy_buff_offset * 2;
mine->copy_buff = malloc(mine->copy_buff_size);
memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
archive_read_set_read_callback(a, memory_read); archive_read_set_read_callback(a, memory_read);
archive_read_set_close_callback(a, memory_read_close); archive_read_set_close_callback(a, memory_read_close);
archive_read_set_callback_data(a, mine); archive_read_set_callback_data(a, mine);
@ -213,7 +213,8 @@ memory_read_close(struct archive *a, void *client_data)
{ {
struct read_memory_data *mine = (struct read_memory_data *)client_data; struct read_memory_data *mine = (struct read_memory_data *)client_data;
(void)a; /* UNUSED */ (void)a; /* UNUSED */
free(mine->copy_buff); if (mine != NULL)
free(mine->copy_buff);
free(mine); free(mine);
return (ARCHIVE_OK); return (ARCHIVE_OK);
} }

View File

@ -104,16 +104,19 @@ test_fuzz(const struct files *filesets)
} }
if (!assert(size < buffsize)) { if (!assert(size < buffsize)) {
free(rawimage); free(rawimage);
rawimage = NULL;
continue; continue;
} }
} else { } else {
for (i = 0; filesets[n].names[i] != NULL; ++i) for (i = 0; filesets[n].names[i] != NULL; ++i)
{ {
tmp = slurpfile(&size, filesets[n].names[i]); tmp = slurpfile(&size, filesets[n].names[i]);
char *newraw = (char *)realloc(rawimage, oldsize + size); char *newraw = realloc(rawimage, oldsize + size);
if (!assert(newraw != NULL)) if (!assert(newraw != NULL))
{ {
free(rawimage); free(rawimage);
rawimage = NULL;
free(tmp);
continue; continue;
} }
rawimage = newraw; rawimage = newraw;
@ -123,14 +126,21 @@ test_fuzz(const struct files *filesets)
free(tmp); free(tmp);
} }
} }
if (size == 0) if (size == 0) {
free(rawimage);
rawimage = NULL;
continue; continue;
}
image = malloc(size); image = malloc(size);
assert(image != NULL); assert(image != NULL);
if (image == NULL) { if (image == NULL) {
free(rawimage); free(rawimage);
rawimage = NULL;
return; return;
} }
assert(rawimage != NULL);
srand((unsigned)time(NULL)); srand((unsigned)time(NULL));
for (i = 0; i < 1000; ++i) { for (i = 0; i < 1000; ++i) {
@ -162,6 +172,7 @@ test_fuzz(const struct files *filesets)
Sleep(100); Sleep(100);
#endif #endif
} }
assert(f != NULL);
assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f)); assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
fclose(f); fclose(f);
@ -195,7 +206,7 @@ test_fuzz(const struct files *filesets)
archive_read_close(a); archive_read_close(a);
} }
archive_read_free(a); archive_read_free(a);
} }
free(image); free(image);
free(rawimage); free(rawimage);
} }

View File

@ -1327,6 +1327,7 @@ test_callbacks(void)
if (assert((m = archive_match_new()) != NULL)) { if (assert((m = archive_match_new()) != NULL)) {
archive_entry_free(ae); archive_entry_free(ae);
archive_read_free(a); archive_read_free(a);
archive_match_free(m);
return; return;
} }

View File

@ -219,7 +219,7 @@ DEFINE_TEST(test_read_append_filter_wrong_program)
/* bunzip2 will write to stderr, redirect it to a file */ /* bunzip2 will write to stderr, redirect it to a file */
fflush(stderr); fflush(stderr);
fgetpos(stderr, &pos); fgetpos(stderr, &pos);
fd = dup(fileno(stderr)); assert((fd = dup(fileno(stderr))) != -1);
fp = freopen("stderr1", "w", stderr); fp = freopen("stderr1", "w", stderr);
#endif #endif
@ -238,10 +238,10 @@ DEFINE_TEST(test_read_append_filter_wrong_program)
if (fp != NULL) { if (fp != NULL) {
fflush(stderr); fflush(stderr);
dup2(fd, fileno(stderr)); dup2(fd, fileno(stderr));
close(fd);
clearerr(stderr); clearerr(stderr);
fsetpos(stderr, &pos); (void)fsetpos(stderr, &pos);
} }
close(fd);
assertTextFileContents("bunzip2: (stdin) is not a bzip2 file.\n", "stderr1"); assertTextFileContents("bunzip2: (stdin) is not a bzip2 file.\n", "stderr1");
#endif #endif
} }

View File

@ -1060,7 +1060,7 @@ assertion_file_contains_lines_any_order(const char *file, int line,
char **expected = NULL; char **expected = NULL;
char *p, **actual = NULL; char *p, **actual = NULL;
char c; char c;
int expected_failure = 0, actual_failure = 0; int expected_failure = 0, actual_failure = 0, retval = 0;
assertion_count(file, line); assertion_count(file, line);
@ -1081,8 +1081,7 @@ assertion_file_contains_lines_any_order(const char *file, int line,
if (expected == NULL) { if (expected == NULL) {
failure_start(pathname, line, "Can't allocate memory"); failure_start(pathname, line, "Can't allocate memory");
failure_finish(NULL); failure_finish(NULL);
free(expected); goto done;
return (0);
} }
for (i = 0; lines[i] != NULL; ++i) { for (i = 0; lines[i] != NULL; ++i) {
expected[i] = strdup(lines[i]); expected[i] = strdup(lines[i]);
@ -1103,8 +1102,7 @@ assertion_file_contains_lines_any_order(const char *file, int line,
if (actual == NULL) { if (actual == NULL) {
failure_start(pathname, line, "Can't allocate memory"); failure_start(pathname, line, "Can't allocate memory");
failure_finish(NULL); failure_finish(NULL);
free(expected); goto done;
return (0);
} }
for (j = 0, p = buff; p < buff + buff_size; for (j = 0, p = buff; p < buff + buff_size;
p += 1 + strlen(p)) { p += 1 + strlen(p)) {
@ -1141,27 +1139,27 @@ assertion_file_contains_lines_any_order(const char *file, int line,
++actual_failure; ++actual_failure;
} }
if (expected_failure == 0 && actual_failure == 0) { if (expected_failure == 0 && actual_failure == 0) {
free(buff); retval = 1;
free(expected); goto done;
free(actual);
return (1);
} }
failure_start(file, line, "File doesn't match: %s", pathname); failure_start(file, line, "File doesn't match: %s", pathname);
for (i = 0; i < expected_count; ++i) { for (i = 0; i < expected_count; ++i) {
if (expected[i] != NULL) { if (expected[i] != NULL)
logprintf(" Expected but not present: %s\n", expected[i]); logprintf(" Expected but not present: %s\n", expected[i]);
free(expected[i]);
}
} }
for (j = 0; j < actual_count; ++j) { for (j = 0; j < actual_count; ++j) {
if (actual[j] != NULL) if (actual[j] != NULL)
logprintf(" Present but not expected: %s\n", actual[j]); logprintf(" Present but not expected: %s\n", actual[j]);
} }
failure_finish(NULL); failure_finish(NULL);
free(buff); done:
free(expected);
free(actual); free(actual);
return (0); free(buff);
for (i = 0; i < expected_count; ++i)
free(expected[i]);
free(expected);
return (retval);
} }
/* Verify that a text file does not contains the specified strings */ /* Verify that a text file does not contains the specified strings */
@ -1590,7 +1588,7 @@ is_symlink(const char *file, int line,
* really not much point in bothering with this. */ * really not much point in bothering with this. */
return (0); return (0);
#else #else
char buff[300]; char buff[301];
struct stat st; struct stat st;
ssize_t linklen; ssize_t linklen;
int r; int r;
@ -1607,7 +1605,7 @@ is_symlink(const char *file, int line,
return (0); return (0);
if (contents == NULL) if (contents == NULL)
return (1); return (1);
linklen = readlink(pathname, buff, sizeof(buff)); linklen = readlink(pathname, buff, sizeof(buff) - 1);
if (linklen < 0) { if (linklen < 0) {
failure_start(file, line, "Can't read symlink %s", pathname); failure_start(file, line, "Can't read symlink %s", pathname);
failure_finish(NULL); failure_finish(NULL);
@ -2324,7 +2322,7 @@ extract_reference_file(const char *name)
for (;;) { for (;;) {
if (fgets(buff, sizeof(buff), in) == NULL) { if (fgets(buff, sizeof(buff), in) == NULL) {
/* TODO: This is a failure. */ /* TODO: This is a failure. */
return; goto done;
} }
if (memcmp(buff, "begin ", 6) == 0) if (memcmp(buff, "begin ", 6) == 0)
break; break;
@ -2365,6 +2363,7 @@ extract_reference_file(const char *name)
} }
} }
fclose(out); fclose(out);
done:
fclose(in); fclose(in);
} }
@ -2958,8 +2957,8 @@ main(int argc, char **argv)
strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
"%Y-%m-%dT%H.%M.%S", "%Y-%m-%dT%H.%M.%S",
localtime(&now)); localtime(&now));
sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp,
tmpdir_timestamp, i); progname, tmpdir_timestamp, i);
if (assertMakeDir(tmpdir,0755)) if (assertMakeDir(tmpdir,0755))
break; break;
if (i >= 999) { if (i >= 999) {

View File

@ -44,6 +44,7 @@ DEFINE_TEST(test_leading_slash)
if (assertFileExists("test.err")) { if (assertFileExists("test.err")) {
errfile = slurpfile(&errfile_size, "test.err"); errfile = slurpfile(&errfile_size, "test.err");
assert(strstr(errfile, expected_errmsg) != NULL); assert(strstr(errfile, expected_errmsg) != NULL);
free(errfile);
} }
} }

View File

@ -43,6 +43,7 @@ DEFINE_TEST(test_option_a)
assert(s > 2); assert(s > 2);
failure("The archive should be compressed"); failure("The archive should be compressed");
assertEqualMem(p, "\x1f\x9d", 2); assertEqualMem(p, "\x1f\x9d", 2);
free(p);
/* Test2: archive it with .taZ suffix. */ /* Test2: archive it with .taZ suffix. */
assertEqualInt(0, assertEqualInt(0,
@ -53,6 +54,7 @@ DEFINE_TEST(test_option_a)
assert(s > 2); assert(s > 2);
failure("The archive should be compressed"); failure("The archive should be compressed");
assertEqualMem(p, "\x1f\x9d", 2); assertEqualMem(p, "\x1f\x9d", 2);
free(p);
/* Test3: archive it with .tar.Z.uu suffix. */ /* Test3: archive it with .tar.Z.uu suffix. */
assertEqualInt(0, assertEqualInt(0,
@ -63,6 +65,7 @@ DEFINE_TEST(test_option_a)
assert(s > 12); assert(s > 12);
failure("The archive should be uuencoded"); failure("The archive should be uuencoded");
assertEqualMem(p, "begin 644 -\n", 12); assertEqualMem(p, "begin 644 -\n", 12);
free(p);
/* Test4: archive it with .zip suffix. */ /* Test4: archive it with .zip suffix. */
assertEqualInt(0, assertEqualInt(0,
@ -73,6 +76,7 @@ DEFINE_TEST(test_option_a)
assert(s > 4); assert(s > 4);
failure("The archive should be zipped"); failure("The archive should be zipped");
assertEqualMem(p, "\x50\x4b\x03\x04", 4); assertEqualMem(p, "\x50\x4b\x03\x04", 4);
free(p);
/* Test5: archive it with .tar.Z suffix and --uuencode option. */ /* Test5: archive it with .tar.Z suffix and --uuencode option. */
assertEqualInt(0, assertEqualInt(0,
@ -84,6 +88,7 @@ DEFINE_TEST(test_option_a)
assert(s > 2); assert(s > 2);
failure("The archive should be compressed, ignoring --uuencode option"); failure("The archive should be compressed, ignoring --uuencode option");
assertEqualMem(p, "\x1f\x9d", 2); assertEqualMem(p, "\x1f\x9d", 2);
free(p);
/* Test6: archive it with .xxx suffix(unknown suffix) and /* Test6: archive it with .xxx suffix(unknown suffix) and
* --uuencode option. */ * --uuencode option. */
@ -96,6 +101,7 @@ DEFINE_TEST(test_option_a)
assert(s > 12); assert(s > 12);
failure("The archive should be uuencoded"); failure("The archive should be uuencoded");
assertEqualMem(p, "begin 644 -\n", 12); assertEqualMem(p, "begin 644 -\n", 12);
free(p);
/* Test7: archive it with .tar.Z suffix using a long-name option. */ /* Test7: archive it with .tar.Z suffix using a long-name option. */
assertEqualInt(0, assertEqualInt(0,
@ -107,4 +113,5 @@ DEFINE_TEST(test_option_a)
assert(s > 2); assert(s > 2);
failure("The archive should be compressed"); failure("The archive should be compressed");
assertEqualMem(p, "\x1f\x9d", 2); assertEqualMem(p, "\x1f\x9d", 2);
free(p);
} }

View File

@ -78,4 +78,6 @@ DEFINE_TEST(test_option_b)
* Note: It's not possible to verify at this level that blocks * Note: It's not possible to verify at this level that blocks
* are getting written with the * are getting written with the
*/ */
free(testprog_ustar);
} }

View File

@ -42,6 +42,7 @@ DEFINE_TEST(test_option_b64encode)
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "begin-base64 644", 16); assertEqualMem(p, "begin-base64 644", 16);
free(p);
/* Archive it with uuencode only. */ /* Archive it with uuencode only. */
assertEqualInt(0, assertEqualInt(0,
@ -51,4 +52,5 @@ DEFINE_TEST(test_option_b64encode)
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "begin-base64 644", 16); assertEqualMem(p, "begin-base64 644", 16);
free(p);
} }

View File

@ -53,6 +53,7 @@ DEFINE_TEST(test_option_gid_gname)
/* Should force gid and gname fields in ustar header. */ /* Should force gid and gname fields in ustar header. */
assertEqualMem(data + 116, "000021 \0", 8); assertEqualMem(data + 116, "000021 \0", 8);
assertEqualMem(data + 297, "foofoofoo\0", 10); assertEqualMem(data + 297, "foofoofoo\0", 10);
free(data);
/* Again with just --gname */ /* Again with just --gname */
failure("Error invoking %s c", testprog); failure("Error invoking %s c", testprog);
@ -65,6 +66,8 @@ DEFINE_TEST(test_option_gid_gname)
/* Gid should be unchanged from original reference. */ /* Gid should be unchanged from original reference. */
assertEqualMem(data + 116, reference + 116, 8); assertEqualMem(data + 116, reference + 116, 8);
assertEqualMem(data + 297, "foofoofoo\0", 10); assertEqualMem(data + 297, "foofoofoo\0", 10);
free(data);
free(reference);
/* Again with --gid and force gname to empty. */ /* Again with --gid and force gname to empty. */
failure("Error invoking %s c", testprog); failure("Error invoking %s c", testprog);
@ -77,6 +80,7 @@ DEFINE_TEST(test_option_gid_gname)
assertEqualMem(data + 116, "000021 \0", 8); assertEqualMem(data + 116, "000021 \0", 8);
/* Gname field in ustar header should be empty. */ /* Gname field in ustar header should be empty. */
assertEqualMem(data + 297, "\0", 1); assertEqualMem(data + 297, "\0", 1);
free(data);
/* TODO: It would be nice to verify that --gid= by itself /* TODO: It would be nice to verify that --gid= by itself
* will look up the associated gname and use that, but * will look up the associated gname and use that, but

View File

@ -45,8 +45,11 @@ DEFINE_TEST(test_option_grzip)
testprog)); testprog));
p = slurpfile(&s, "archive.err"); p = slurpfile(&s, "archive.err");
p[s] = '\0'; p[s] = '\0';
free(p);
/* Check that the archive file has an grzip signature. */ /* Check that the archive file has an grzip signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "GRZipII\x00\x02\x04:)", 12); assertEqualMem(p, "GRZipII\x00\x02\x04:)", 12);
free(p);
} }

View File

@ -42,15 +42,18 @@ DEFINE_TEST(test_option_j)
if (r != 0) { if (r != 0) {
if (!canBzip2()) { if (!canBzip2()) {
skipping("bzip2 is not supported on this platform"); skipping("bzip2 is not supported on this platform");
return; goto done;
} }
failure("-j option is broken"); failure("-j option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
assertEmptyFile("archive.err"); assertEmptyFile("archive.err");
/* Check that the archive file has a bzip2 signature. */ /* Check that the archive file has a bzip2 signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "BZh9", 4); assertEqualMem(p, "BZh9", 4);
done:
free(p);
} }

View File

@ -45,8 +45,10 @@ DEFINE_TEST(test_option_lrzip)
testprog)); testprog));
p = slurpfile(&s, "archive.err"); p = slurpfile(&s, "archive.err");
p[s] = '\0'; p[s] = '\0';
free(p);
/* Check that the archive file has an lzma signature. */ /* Check that the archive file has an lzma signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "LRZI\x00", 5); assertEqualMem(p, "LRZI\x00", 5);
free(p);
} }

View File

@ -43,7 +43,7 @@ DEFINE_TEST(test_option_lz4)
if (strstr(p, "Unsupported compression") != NULL) { if (strstr(p, "Unsupported compression") != NULL) {
skipping("This version of bsdtar was compiled " skipping("This version of bsdtar was compiled "
"without lz4 support"); "without lz4 support");
return; goto done;
} }
/* POSIX permits different handling of the spawnp /* POSIX permits different handling of the spawnp
* system call used to launch the subsidiary * system call used to launch the subsidiary
@ -52,7 +52,7 @@ DEFINE_TEST(test_option_lz4)
if (strstr(p, "Can't launch") != NULL && !canLz4()) { if (strstr(p, "Can't launch") != NULL && !canLz4()) {
skipping("This version of bsdtar uses an external lz4 program " skipping("This version of bsdtar uses an external lz4 program "
"but no such program is available on this system."); "but no such program is available on this system.");
return; goto done;
} }
/* Some systems successfully spawn the new process, /* Some systems successfully spawn the new process,
* but fail to exec a program within that process. * but fail to exec a program within that process.
@ -61,14 +61,18 @@ DEFINE_TEST(test_option_lz4)
if (strstr(p, "Can't write") != NULL && !canLz4()) { if (strstr(p, "Can't write") != NULL && !canLz4()) {
skipping("This version of bsdtar uses an external lz4 program " skipping("This version of bsdtar uses an external lz4 program "
"but no such program is available on this system."); "but no such program is available on this system.");
return; goto done;
} }
failure("--lz4 option is broken: %s", p); failure("--lz4 option is broken: %s", p);
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has an lz4 signature. */ /* Check that the archive file has an lz4 signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\x04\x22\x4d\x18", 4); assertEqualMem(p, "\x04\x22\x4d\x18", 4);
done:
free(p);
} }

View File

@ -48,10 +48,13 @@ DEFINE_TEST(test_option_lzma)
} }
failure("--lzma option is broken"); failure("--lzma option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has an lzma signature. */ /* Check that the archive file has an lzma signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\x5d\00\00", 3); assertEqualMem(p, "\x5d\00\00", 3);
done:
free(p);
} }

View File

@ -42,14 +42,17 @@ DEFINE_TEST(test_option_lzop)
if (r != 0) { if (r != 0) {
if (!canLzop()) { if (!canLzop()) {
skipping("lzop is not supported on this platform"); skipping("lzop is not supported on this platform");
return; goto done;
} }
failure("--lzop option is broken"); failure("--lzop option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has an lzma signature. */ /* Check that the archive file has an lzma signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a", 9); assertEqualMem(p, "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a", 9);
done:
free(p);
} }

View File

@ -36,6 +36,10 @@ DEFINE_TEST(test_option_r)
size_t s, buff_size_rounded; size_t s, buff_size_rounded;
int r, i; int r, i;
buff = NULL;
p0 = NULL;
p1 = NULL;
/* Create an archive with one file. */ /* Create an archive with one file. */
assertMakeFile("f1", 0644, "abc"); assertMakeFile("f1", 0644, "abc");
r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog); r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
@ -47,11 +51,9 @@ DEFINE_TEST(test_option_r)
/* Do some basic validation of the constructed archive. */ /* Do some basic validation of the constructed archive. */
p0 = slurpfile(&s, "archive.tar"); p0 = slurpfile(&s, "archive.tar");
if (!assert(p0 != NULL)) if (!assert(p0 != NULL))
return; goto done;
if (!assert(s >= 2048)) { if (!assert(s >= 2048))
free(p0); goto done;
return;
}
assertEqualMem(p0 + 0, "f1", 3); assertEqualMem(p0 + 0, "f1", 3);
assertEqualMem(p0 + 512, "abc", 3); assertEqualMem(p0 + 512, "abc", 3);
assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8); assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
@ -60,10 +62,8 @@ DEFINE_TEST(test_option_r)
/* Edit that file with a lot more data and update the archive with a new copy. */ /* Edit that file with a lot more data and update the archive with a new copy. */
buff = malloc(buff_size); buff = malloc(buff_size);
assert(buff != NULL); assert(buff != NULL);
if (buff == NULL) { if (buff == NULL)
free(p0); goto done;
return;
}
for (i = 0; i < (int)buff_size; ++i) for (i = 0; i < (int)buff_size; ++i)
buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
@ -77,10 +77,8 @@ DEFINE_TEST(test_option_r)
/* The constructed archive should just have the new entry appended. */ /* The constructed archive should just have the new entry appended. */
p1 = slurpfile(&s, "archive.tar"); p1 = slurpfile(&s, "archive.tar");
if (!assert(p1 != NULL)) { if (!assert(p1 != NULL))
free(p0); goto done;
return;
}
buff_size_rounded = ((buff_size + 511) / 512) * 512; buff_size_rounded = ((buff_size + 511) / 512) * 512;
assert(s >= 2560 + buff_size_rounded); assert(s >= 2560 + buff_size_rounded);
/* Verify first entry is unchanged. */ /* Verify first entry is unchanged. */
@ -105,10 +103,8 @@ DEFINE_TEST(test_option_r)
/* Validate the constructed archive. */ /* Validate the constructed archive. */
p1 = slurpfile(&s, "archive.tar"); p1 = slurpfile(&s, "archive.tar");
if (!assert(p1 != NULL)) { if (!assert(p1 != NULL))
free(p0); goto done;
return;
}
assert(s >= 3584 + buff_size_rounded); assert(s >= 3584 + buff_size_rounded);
/* Verify first two entries are unchanged. */ /* Verify first two entries are unchanged. */
assertEqualMem(p0, p1, 1536 + buff_size_rounded); assertEqualMem(p0, p1, 1536 + buff_size_rounded);
@ -118,7 +114,6 @@ DEFINE_TEST(test_option_r)
/* Verify end-of-archive marker. */ /* Verify end-of-archive marker. */
assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
free(p0);
free(p1); free(p1);
/* Unpack everything */ /* Unpack everything */
@ -132,4 +127,7 @@ DEFINE_TEST(test_option_r)
/* Verify that the second copy of f1 overwrote the first. */ /* Verify that the second copy of f1 overwrote the first. */
assertFileContents(buff, (int)strlen(buff), "f1"); assertFileContents(buff, (int)strlen(buff), "f1");
done:
free(buff);
free(p0);
} }

View File

@ -53,6 +53,7 @@ DEFINE_TEST(test_option_uid_uname)
/* Should force uid and uname fields in ustar header. */ /* Should force uid and uname fields in ustar header. */
assertEqualMem(data + 108, "000021 \0", 8); assertEqualMem(data + 108, "000021 \0", 8);
assertEqualMem(data + 265, "foofoofoo\0", 10); assertEqualMem(data + 265, "foofoofoo\0", 10);
free(data);
/* Again with just --uid */ /* Again with just --uid */
failure("Error invoking %s c", testprog); failure("Error invoking %s c", testprog);
@ -65,6 +66,7 @@ DEFINE_TEST(test_option_uid_uname)
assertEqualMem(data + 108, "000021 \0", 8); assertEqualMem(data + 108, "000021 \0", 8);
/* Uname field in ustar header should be empty. */ /* Uname field in ustar header should be empty. */
assertEqualMem(data + 265, "\0", 1); assertEqualMem(data + 265, "\0", 1);
free(data);
/* Again with just --uname */ /* Again with just --uname */
failure("Error invoking %s c", testprog); failure("Error invoking %s c", testprog);
@ -77,4 +79,7 @@ DEFINE_TEST(test_option_uid_uname)
/* Uid should be unchanged from original reference. */ /* Uid should be unchanged from original reference. */
assertEqualMem(data + 108, reference + 108, 8); assertEqualMem(data + 108, reference + 108, 8);
assertEqualMem(data + 265, "foofoofoo\0", 10); assertEqualMem(data + 265, "foofoofoo\0", 10);
free(data);
free(reference);
} }

View File

@ -42,6 +42,7 @@ DEFINE_TEST(test_option_uuencode)
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "begin 644", 9); assertEqualMem(p, "begin 644", 9);
free(p);
/* Archive it with uuencode only. */ /* Archive it with uuencode only. */
assertEqualInt(0, assertEqualInt(0,
@ -51,4 +52,5 @@ DEFINE_TEST(test_option_uuencode)
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "begin 644", 9); assertEqualMem(p, "begin 644", 9);
free(p);
} }

View File

@ -44,14 +44,17 @@ DEFINE_TEST(test_option_xz)
if (strstr(p, "Unsupported compression") != NULL) { if (strstr(p, "Unsupported compression") != NULL) {
skipping("This version of bsdtar was compiled " skipping("This version of bsdtar was compiled "
"without xz support"); "without xz support");
return; goto done;
} }
failure("--xz option is broken"); failure("--xz option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has an xz signature. */ /* Check that the archive file has an xz signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 2); assert(s > 2);
assertEqualMem(p, "\xFD\x37\x7A\x58\x5A\x00", 6); assertEqualMem(p, "\xFD\x37\x7A\x58\x5A\x00", 6);
done:
free(p);
} }

View File

@ -42,14 +42,17 @@ DEFINE_TEST(test_option_z)
if (r != 0) { if (r != 0) {
if (!canGzip()) { if (!canGzip()) {
skipping("gzip is not supported on this platform"); skipping("gzip is not supported on this platform");
return; goto done;
} }
failure("-z option is broken"); failure("-z option is broken");
assertEqualInt(r, 0); assertEqualInt(r, 0);
return; goto done;
} }
free(p);
/* Check that the archive file has a gzip signature. */ /* Check that the archive file has a gzip signature. */
p = slurpfile(&s, "archive.out"); p = slurpfile(&s, "archive.out");
assert(s > 4); assert(s > 4);
assertEqualMem(p, "\x1f\x8b\x08\x00", 4); assertEqualMem(p, "\x1f\x8b\x08\x00", 4);
done:
free(p);
} }

View File

@ -116,6 +116,7 @@ DEFINE_TEST(test_stdio)
assertEqualInt((int)s, 3); assertEqualInt((int)s, 3);
assertEqualMem(p, "abc", 3); assertEqualMem(p, "abc", 3);
/* TODO: Verify xvf.err */ /* TODO: Verify xvf.err */
free(p);
/* 'xvf -' should generate list on stderr, empty stdout. */ /* 'xvf -' should generate list on stderr, empty stdout. */
r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog); r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);

View File

@ -53,7 +53,7 @@ DEFINE_TEST(test_version)
assert(s > 6); assert(s > 6);
failure("Version must start with 'bsdtar': ``%s''", p); failure("Version must start with 'bsdtar': ``%s''", p);
if (!assertEqualMem(q, "bsdtar ", 7)) if (!assertEqualMem(q, "bsdtar ", 7))
return; goto done;
q += 7; s -= 7; q += 7; s -= 7;
/* Version number is a series of digits and periods. */ /* Version number is a series of digits and periods. */
while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) { while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
@ -98,5 +98,6 @@ DEFINE_TEST(test_version)
failure("Version output must end with \\n or \\r\\n"); failure("Version output must end with \\n or \\r\\n");
if (*q == '\r') { ++q; --s; } if (*q == '\r') { ++q; --s; }
assertEqualMem(q, "\n", 1); assertEqualMem(q, "\n", 1);
done:
free(p); free(p);
} }