Skip to content

Commit

Permalink
fix bu_file test failure by adapting our test.
Browse files Browse the repository at this point in the history
test previously assumed we're running on a unix-like filesystem with distinct read/write/execute bits that are set a particular way by default.  this was both wrong for different umask settings as well as different filesystems (e.g., exfat) that are fixed-permissive (akin to immutable 777 everywhere).  thus, changed the test to force the files permissive with an bu_fchmod(777) where we can at least ensure read/write/execute are set (and we can't reliably check if unset after bu_fchmod(000) or variations thereof without knowing the FS).

also added a few more checks to make sure files exist and do not exist at the expected times.
  • Loading branch information
brlcad committed Aug 7, 2024
1 parent 238e630 commit bb06e53
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions src/libbu/tests/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "bu.h"
#include "bio.h"


int
main(int ac, char *av[])
{
Expand All @@ -55,35 +56,75 @@ main(int ac, char *av[])
ret = mkdir(tdir, S_IRWXU);
#endif

/* make sure can create an output directory */
if (ret != 0 && !bu_file_exists(tdir, NULL)) {
bu_log("%s [FAIL] - could not make \"%s\" directory\n", av[0], tdir);
return ret;
bu_exit(ret, "%s [FAIL] - could not make \"%s\" directory\n", av[0], tdir);
}
if (!bu_file_directory(tdir)) {
bu_exit(1, "%s [FAIL] %s is not recognized as a directory\n", av[0], tdir);
}


/* make sure can create files on open */
for (int i = 1; i < file_cnt+1; i++) {
bu_vls_sprintf(&fname, "%s/bu_file_%d", tdir,i);

/* file does NOT exist pre-fopen() */
if (bu_file_exists(bu_vls_cstr(&fname), NULL)) {
bu_exit(1, "%s [FAIL] test file %s already exists\n", av[0], bu_vls_cstr(&fname));
}

/* opening file should create it */
fp = fopen(bu_vls_cstr(&fname), "wb");
if (!fp) {
bu_exit(1, "%s [FAIL] Unable to create test input file %s\n", av[0], bu_vls_cstr(&fname));
bu_exit(1, "%s [FAIL] Unable to create test file %s\n", av[0], bu_vls_cstr(&fname));
}

/* file exists post-fopen() */
if (!bu_file_exists(bu_vls_cstr(&fname), NULL)) {
bu_exit(1, "%s [FAIL] test file %s does not exist\n", av[0], bu_vls_cstr(&fname));
}

/* make file permissive.
*
* NOTE: some filesystems like exfat are fixed 777, so we
* cannot test non-execution/non-read/non-write without taking
* filesystem into consideration. should be able to make
* permissive, however, and test positive case accordingly.
*/
ret = bu_fchmod(fileno(fp), 0777);
if (ret) {
perror("bu_fchmod");
bu_exit(1, "%s [FAIL] test file %s threw an fchmod() error\n", av[0], bu_vls_cstr(&fname));
}

/* write something to it */
fprintf(fp, "%d", i);
fclose(fp);

/* file exists post-fclose() */
if (!bu_file_exists(bu_vls_cstr(&fname), NULL)) {
bu_exit(1, "%s [FAIL] test file %s does not exist\n", av[0], bu_vls_cstr(&fname));
}
}

/* Do some tests on the files */
for (int i = 1; i < file_cnt+1; i++) {

/* NOTE: some filesystems (e.g., exfat) are ONLY permissible
* (e.g., 777), so we can only trigger on not being readable,
* not writeable, not executable after running bu_fchmod(777).
*/

bu_vls_sprintf(&fname, "%s/bu_file_%d", tdir,i);
if (!bu_file_exists(bu_vls_cstr(&fname), NULL)) {
bu_exit(1, "%s [FAIL] test input file %s does not exist\n", av[0], bu_vls_cstr(&fname));
}
if (!bu_file_readable(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] test input file %s is not readable\n", av[0], bu_vls_cstr(&fname));
bu_exit(1, "%s [FAIL] test file %s not readable after bu_fchmod(777)\n", av[0], bu_vls_cstr(&fname));
}
if (!bu_file_writable(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] test input file %s is not readable\n", av[0], bu_vls_cstr(&fname));
bu_exit(1, "%s [FAIL] test file %s not readable after bu_fchmod(777)\n", av[0], bu_vls_cstr(&fname));
}
if (bu_file_executable(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] test input file %s is incorrectly reported to be executable\n", av[0], bu_vls_cstr(&fname));
if (!bu_file_executable(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] test file %s not executable after bu_fchmod(777)\n", av[0], bu_vls_cstr(&fname));
}
}

Expand All @@ -94,10 +135,6 @@ main(int ac, char *av[])
bu_exit(1, "%s [FAIL] test input files %s and %s are incorrectly reported to be the same file\n", av[0], bu_vls_cstr(&fname), bu_vls_cstr(&fname2));
}

if (!bu_file_directory(tdir)) {
bu_exit(1, "%s [FAIL] %s is not recognized as being a directory\n", av[0], tdir);
}

if (bu_file_directory(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] %s is incorrectly identified as being a directory\n", av[0], bu_vls_cstr(&fname));
}
Expand Down Expand Up @@ -141,6 +178,11 @@ main(int ac, char *av[])
if (!bu_file_delete(bu_vls_cstr(&fname))) {
bu_exit(1, "%s [FAIL] could not delete file %s\n", av[0], bu_vls_cstr(&fname));
}

/* file exists post-delete */
if (bu_file_exists(bu_vls_cstr(&fname), NULL)) {
bu_exit(1, "%s [FAIL] test file %s was deleted but still exists\n", av[0], bu_vls_cstr(&fname));
}
}

if (!bu_file_delete(tdir)) {
Expand Down

0 comments on commit bb06e53

Please sign in to comment.