Line data Source code
1 : #include "test_helpers.h"
2 : #include "fs_util.h"
3 : #include <sys/stat.h>
4 : #include <unistd.h>
5 : #include <stdlib.h>
6 : #include <stdio.h>
7 :
8 1 : void test_fs_util(void) {
9 : // 1. Test Home Dir with HOME set
10 1 : const char *home = fs_get_home_dir();
11 1 : ASSERT(home != NULL, "Home directory should not be NULL");
12 :
13 : // 2. Test fs_get_home_dir fallback via getpwuid when HOME is unset
14 1 : char *saved_home = getenv("HOME");
15 1 : unsetenv("HOME");
16 1 : home = fs_get_home_dir();
17 1 : ASSERT(home != NULL, "fs_get_home_dir should fall back to getpwuid");
18 1 : if (saved_home) setenv("HOME", saved_home, 1);
19 :
20 : // 3. Test Mkdir P (nested)
21 1 : char test_dir[256];
22 1 : snprintf(test_dir, sizeof(test_dir), "/tmp/email-cli-test-%d/a/b/c", getpid());
23 1 : int res = fs_mkdir_p(test_dir, 0700);
24 1 : ASSERT(res == 0, "fs_mkdir_p should return 0");
25 :
26 1 : struct stat st;
27 1 : ASSERT(stat(test_dir, &st) == 0, "Directory should exist");
28 1 : ASSERT((st.st_mode & 0777) == 0700, "Directory should have 0700 permissions");
29 :
30 : // 4. Test Mkdir P with trailing slash
31 1 : char test_dir_slash[256];
32 1 : snprintf(test_dir_slash, sizeof(test_dir_slash),
33 : "/tmp/email-cli-test-%d/a/b/c/", getpid());
34 1 : res = fs_mkdir_p(test_dir_slash, 0700);
35 1 : ASSERT(res == 0, "fs_mkdir_p should handle trailing slash");
36 :
37 : // 5. Test fs_mkdir_p on already-existing directory (idempotent)
38 1 : res = fs_mkdir_p(test_dir, 0700);
39 1 : ASSERT(res == 0, "fs_mkdir_p should succeed on existing directory");
40 :
41 : // 6. Test fs_ensure_permissions
42 1 : res = fs_ensure_permissions(test_dir, 0755);
43 1 : ASSERT(res == 0, "fs_ensure_permissions should succeed");
44 1 : ASSERT(stat(test_dir, &st) == 0, "Directory should still exist");
45 1 : ASSERT((st.st_mode & 0777) == 0755, "Directory should have 0755 permissions");
46 :
47 : // 7. Test fs_mkdir_p with "/" — strips trailing slash → empty tmp → return 0 (line 49)
48 1 : res = fs_mkdir_p("/", 0700);
49 1 : ASSERT(res == 0, "fs_mkdir_p(\"/\") should return 0");
50 : }
|