<pre style='margin:0'>
Christopher Nielsen (mascguy) pushed a commit to branch master
in repository macports-legacy-support.
</pre>
<p><a href="https://github.com/macports/macports-legacy-support/commit/593e00ff3c7b7f212437eaf529f061f29e6c306a">https://github.com/macports/macports-legacy-support/commit/593e00ff3c7b7f212437eaf529f061f29e6c306a</a></p>
<pre style="white-space: pre; background: #F8F8F8"><span style='display:block; white-space:pre;color:#808000;'>commit 593e00ff3c7b7f212437eaf529f061f29e6c306a
</span>Author: Fred Wright <fw@fwright.net>
AuthorDate: Thu Feb 15 15:33:23 2024 -0800
<span style='display:block; white-space:pre;color:#404040;'> test_fdopendir: Commonize failure tests.
</span><span style='display:block; white-space:pre;color:#404040;'>
</span><span style='display:block; white-space:pre;color:#404040;'> This creates a common function for testing the cases where failure
</span><span style='display:block; white-space:pre;color:#404040;'> is expected, avoiding some code duplication.
</span><span style='display:block; white-space:pre;color:#404040;'>
</span><span style='display:block; white-space:pre;color:#404040;'> It also adds a bit more information to the error printout.
</span><span style='display:block; white-space:pre;color:#404040;'>
</span><span style='display:block; white-space:pre;color:#404040;'> TESTED:
</span><span style='display:block; white-space:pre;color:#404040;'> Succeeds where expected, and fails where expected.
</span>---
test/test_fdopendir.c | 50 ++++++++++++++++++++++++++------------------------
1 file changed, 26 insertions(+), 24 deletions(-)
<span style='display:block; white-space:pre;color:#808080;'>diff --git a/test/test_fdopendir.c b/test/test_fdopendir.c
</span><span style='display:block; white-space:pre;color:#808080;'>index d2fa671..d51bafa 100644
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>--- a/test/test_fdopendir.c
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>+++ b/test/test_fdopendir.c
</span><span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -28,12 +28,32 @@
</span>
#define FEEDBACK 1
<span style='display:block; white-space:pre;background:#e0ffe0;'>+/* Test expected failure case */
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+int check_failure(int fd, const char *name, const char *exp_sym, int exp_val)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+{
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ DIR *dir;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if ((dir = fdopendir(fd))) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fprintf(stderr, "error: fdopendir(%s) should have failed\n", name);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ (void)closedir(dir);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else if (errno != exp_val) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fprintf(stderr, "error: fdopendir(%s) failure should have returned"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ " %d (%s), actually returned %d (%s)\n",
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ name, exp_val, exp_sym, errno, strerror(errno));
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> int main() {
struct stat st;
struct dirent *entry;
int dfd = -1;
DIR *dir;
char *first_entry = NULL;
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ int err;
</span>
/* Test fdopendir with a valid directory fd, then use readdir */
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -174,36 +194,18 @@ int main() {
</span>
/* Try to use fdopendir with stdin - Should fail with ENOTDIR */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if ((dir = fdopendir(STDIN_FILENO))) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- fprintf(stderr, "error: fdopendir(stdin) should have failed\n");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- (void)closedir(dir);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (errno != ENOTDIR) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- perror("error: fdopendir(stdin) should have failed with ENOTDIR");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = check_failure(STDIN_FILENO, "stdin", "ENOTDIR", ENOTDIR);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (err) return 1;
</span>
/* Try to use fdopendir with -1 - Should fail with EBADF */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if ((dir = fdopendir(-1))) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- fprintf(stderr, "error: fdopendir(-1) should have failed\n");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- (void)closedir(dir);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (errno != EBADF) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- perror("error: fdopendir(-1) should have failed with EBADF");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = check_failure(-1, "-1", "EBADF", EBADF);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (err) return 1;
</span>
/* Try to use fdopendir with AT_FDCWD - Should fail with EBADF */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if ((dir = fdopendir(AT_FDCWD))) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- fprintf(stderr, "error: fdopendir(AT_FDCWD) should have failed\n");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- (void)closedir(dir);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else if (errno != EBADF) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- perror("error: fdopendir(AT_FDCWD) should have failed with EBADF");
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = check_failure(AT_FDCWD, "AT_FDCWD", "EBADF", EBADF);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (err) return 1;
</span>
return 0;
}
</pre><pre style='margin:0'>
</pre>