]> git.proxmox.com Git - libgit2.git/blob - tests/core/stat.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / stat.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3 #include "posix.h"
4
5 void test_core_stat__initialize(void)
6 {
7 cl_git_pass(git_futils_mkdir("root/d1/d2", 0755, GIT_MKDIR_PATH));
8 cl_git_mkfile("root/file", "whatever\n");
9 cl_git_mkfile("root/d1/file", "whatever\n");
10 }
11
12 void test_core_stat__cleanup(void)
13 {
14 git_futils_rmdir_r("root", NULL, GIT_RMDIR_REMOVE_FILES);
15 }
16
17 #define cl_assert_error(val) \
18 do { err = errno; cl_assert_equal_i((val), err); } while (0)
19
20 void test_core_stat__0(void)
21 {
22 struct stat st;
23 int err;
24
25 cl_assert_equal_i(0, p_lstat("root", &st));
26 cl_assert(S_ISDIR(st.st_mode));
27 cl_assert_error(0);
28
29 cl_assert_equal_i(0, p_lstat("root/", &st));
30 cl_assert(S_ISDIR(st.st_mode));
31 cl_assert_error(0);
32
33 cl_assert_equal_i(0, p_lstat("root/file", &st));
34 cl_assert(S_ISREG(st.st_mode));
35 cl_assert_error(0);
36
37 cl_assert_equal_i(0, p_lstat("root/d1", &st));
38 cl_assert(S_ISDIR(st.st_mode));
39 cl_assert_error(0);
40
41 cl_assert_equal_i(0, p_lstat("root/d1/", &st));
42 cl_assert(S_ISDIR(st.st_mode));
43 cl_assert_error(0);
44
45 cl_assert_equal_i(0, p_lstat("root/d1/file", &st));
46 cl_assert(S_ISREG(st.st_mode));
47 cl_assert_error(0);
48
49 cl_assert(p_lstat("root/missing", &st) < 0);
50 cl_assert_error(ENOENT);
51
52 cl_assert(p_lstat("root/missing/but/could/be/created", &st) < 0);
53 cl_assert_error(ENOENT);
54
55 cl_assert(p_lstat_posixly("root/missing/but/could/be/created", &st) < 0);
56 cl_assert_error(ENOENT);
57
58 cl_assert(p_lstat("root/d1/missing", &st) < 0);
59 cl_assert_error(ENOENT);
60
61 cl_assert(p_lstat("root/d1/missing/deeper/path", &st) < 0);
62 cl_assert_error(ENOENT);
63
64 cl_assert(p_lstat_posixly("root/d1/missing/deeper/path", &st) < 0);
65 cl_assert_error(ENOENT);
66
67 cl_assert(p_lstat_posixly("root/d1/file/deeper/path", &st) < 0);
68 cl_assert_error(ENOTDIR);
69
70 cl_assert(p_lstat("root/file/invalid", &st) < 0);
71 #ifdef GIT_WIN32
72 cl_assert_error(ENOENT);
73 #else
74 cl_assert_error(ENOTDIR);
75 #endif
76
77 cl_assert(p_lstat_posixly("root/file/invalid", &st) < 0);
78 cl_assert_error(ENOTDIR);
79
80 cl_assert(p_lstat("root/file/invalid/deeper_path", &st) < 0);
81 #ifdef GIT_WIN32
82 cl_assert_error(ENOENT);
83 #else
84 cl_assert_error(ENOTDIR);
85 #endif
86
87 cl_assert(p_lstat_posixly("root/file/invalid/deeper_path", &st) < 0);
88 cl_assert_error(ENOTDIR);
89
90 cl_assert(p_lstat_posixly("root/d1/file/extra", &st) < 0);
91 cl_assert_error(ENOTDIR);
92
93 cl_assert(p_lstat_posixly("root/d1/file/further/invalid/items", &st) < 0);
94 cl_assert_error(ENOTDIR);
95 }
96
97 void test_core_stat__root(void)
98 {
99 const char *sandbox = clar_sandbox_path();
100 git_str root = GIT_STR_INIT;
101 int root_len;
102 struct stat st;
103
104 root_len = git_fs_path_root(sandbox);
105 cl_assert(root_len >= 0);
106
107 git_str_set(&root, sandbox, root_len+1);
108
109 cl_must_pass(p_stat(root.ptr, &st));
110 cl_assert(S_ISDIR(st.st_mode));
111
112 git_str_dispose(&root);
113 }