]> git.proxmox.com Git - libgit2.git/blob - tests/core/posix.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / posix.c
1 #ifndef _WIN32
2 # include <arpa/inet.h>
3 # include <sys/socket.h>
4 # include <netinet/in.h>
5 #else
6 # include <ws2tcpip.h>
7 # ifdef _MSC_VER
8 # pragma comment(lib, "ws2_32")
9 # endif
10 #endif
11
12 #include "clar_libgit2.h"
13 #include "futils.h"
14 #include "posix.h"
15
16 void test_core_posix__initialize(void)
17 {
18 #ifdef GIT_WIN32
19 /* on win32, the WSA context needs to be initialized
20 * before any socket calls can be performed */
21 WSADATA wsd;
22
23 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd));
24 cl_assert(LOBYTE(wsd.wVersion) == 2 && HIBYTE(wsd.wVersion) == 2);
25 #endif
26 }
27
28 static bool supports_ipv6(void)
29 {
30 #ifdef GIT_WIN32
31 /* IPv6 is supported on Vista and newer */
32 return git_has_win32_version(6, 0, 0);
33 #else
34 return 1;
35 #endif
36 }
37
38 void test_core_posix__inet_pton(void)
39 {
40 struct in_addr addr;
41 struct in6_addr addr6;
42 size_t i;
43
44 struct in_addr_data {
45 const char *p;
46 const uint8_t n[4];
47 };
48
49 struct in6_addr_data {
50 const char *p;
51 const uint8_t n[16];
52 };
53
54 static struct in_addr_data in_addr_data[] = {
55 { "0.0.0.0", { 0, 0, 0, 0 } },
56 { "10.42.101.8", { 10, 42, 101, 8 } },
57 { "127.0.0.1", { 127, 0, 0, 1 } },
58 { "140.177.10.12", { 140, 177, 10, 12 } },
59 { "204.232.175.90", { 204, 232, 175, 90 } },
60 { "255.255.255.255", { 255, 255, 255, 255 } },
61 };
62
63 static struct in6_addr_data in6_addr_data[] = {
64 { "::", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
65 { "::1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
66 { "0:0:0:0:0:0:0:1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
67 { "2001:db8:8714:3a90::12", { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12 } },
68 { "fe80::f8ba:c2d6:86be:3645", { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xba, 0xc2, 0xd6, 0x86, 0xbe, 0x36, 0x45 } },
69 { "::ffff:204.152.189.116", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x98, 0xbd, 0x74 } },
70 };
71
72 /* Test some ipv4 addresses */
73 for (i = 0; i < 6; i++) {
74 cl_assert(p_inet_pton(AF_INET, in_addr_data[i].p, &addr) == 1);
75 cl_assert(memcmp(&addr, in_addr_data[i].n, sizeof(struct in_addr)) == 0);
76 }
77
78 /* Test some ipv6 addresses */
79 if (supports_ipv6())
80 {
81 for (i = 0; i < 6; i++) {
82 cl_assert(p_inet_pton(AF_INET6, in6_addr_data[i].p, &addr6) == 1);
83 cl_assert(memcmp(&addr6, in6_addr_data[i].n, sizeof(struct in6_addr)) == 0);
84 }
85 }
86
87 /* Test some invalid strings */
88 cl_assert(p_inet_pton(AF_INET, "", &addr) == 0);
89 cl_assert(p_inet_pton(AF_INET, "foo", &addr) == 0);
90 cl_assert(p_inet_pton(AF_INET, " 127.0.0.1", &addr) == 0);
91 cl_assert(p_inet_pton(AF_INET, "bar", &addr) == 0);
92 cl_assert(p_inet_pton(AF_INET, "10.foo.bar.1", &addr) == 0);
93
94 /* Test unsupported address families */
95 cl_git_fail(p_inet_pton(INT_MAX-1, "52.472", &addr));
96 cl_assert_equal_i(EAFNOSUPPORT, errno);
97 }
98
99 void test_core_posix__utimes(void)
100 {
101 struct p_timeval times[2];
102 struct stat st;
103 time_t curtime;
104 int fd;
105
106 /* test p_utimes */
107 times[0].tv_sec = 1234567890;
108 times[0].tv_usec = 0;
109 times[1].tv_sec = 1234567890;
110 times[1].tv_usec = 0;
111
112 cl_git_mkfile("foo", "Dummy file.");
113 cl_must_pass(p_utimes("foo", times));
114
115 cl_must_pass(p_stat("foo", &st));
116 cl_assert_equal_i(1234567890, st.st_atime);
117 cl_assert_equal_i(1234567890, st.st_mtime);
118
119
120 /* test p_futimes */
121 times[0].tv_sec = 1414141414;
122 times[0].tv_usec = 0;
123 times[1].tv_sec = 1414141414;
124 times[1].tv_usec = 0;
125
126 cl_must_pass(fd = p_open("foo", O_RDWR));
127 cl_must_pass(p_futimes(fd, times));
128 cl_must_pass(p_close(fd));
129
130 cl_must_pass(p_stat("foo", &st));
131 cl_assert_equal_i(1414141414, st.st_atime);
132 cl_assert_equal_i(1414141414, st.st_mtime);
133
134
135 /* test p_utimes with current time, assume that
136 * it takes < 5 seconds to get the time...!
137 */
138 cl_must_pass(p_utimes("foo", NULL));
139
140 curtime = time(NULL);
141 cl_must_pass(p_stat("foo", &st));
142 cl_assert((st.st_atime - curtime) < 5);
143 cl_assert((st.st_mtime - curtime) < 5);
144
145 cl_must_pass(p_unlink("foo"));
146 }
147
148 void test_core_posix__unlink_removes_symlink(void)
149 {
150 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
151 clar__skip();
152
153 cl_git_mkfile("file", "Dummy file.");
154 cl_git_pass(git_futils_mkdir("dir", 0777, 0));
155
156 cl_must_pass(p_symlink("file", "file-symlink"));
157 cl_must_pass(p_symlink("dir", "dir-symlink"));
158
159 cl_must_pass(p_unlink("file-symlink"));
160 cl_must_pass(p_unlink("dir-symlink"));
161
162 cl_assert(git_fs_path_exists("file"));
163 cl_assert(git_fs_path_exists("dir"));
164
165 cl_must_pass(p_unlink("file"));
166 cl_must_pass(p_rmdir("dir"));
167 }
168
169 void test_core_posix__symlink_resolves_to_correct_type(void)
170 {
171 git_str contents = GIT_STR_INIT;
172
173 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
174 clar__skip();
175
176 cl_must_pass(git_futils_mkdir("dir", 0777, 0));
177 cl_must_pass(git_futils_mkdir("file", 0777, 0));
178 cl_git_mkfile("dir/file", "symlink target");
179
180 cl_git_pass(p_symlink("file", "dir/link"));
181
182 cl_git_pass(git_futils_readbuffer(&contents, "dir/file"));
183 cl_assert_equal_s(contents.ptr, "symlink target");
184
185 cl_must_pass(p_unlink("dir/link"));
186 cl_must_pass(p_unlink("dir/file"));
187 cl_must_pass(p_rmdir("dir"));
188 cl_must_pass(p_rmdir("file"));
189
190 git_str_dispose(&contents);
191 }
192
193 void test_core_posix__relative_symlink(void)
194 {
195 git_str contents = GIT_STR_INIT;
196
197 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
198 clar__skip();
199
200 cl_must_pass(git_futils_mkdir("dir", 0777, 0));
201 cl_git_mkfile("file", "contents");
202 cl_git_pass(p_symlink("../file", "dir/link"));
203 cl_git_pass(git_futils_readbuffer(&contents, "dir/link"));
204 cl_assert_equal_s(contents.ptr, "contents");
205
206 cl_must_pass(p_unlink("file"));
207 cl_must_pass(p_unlink("dir/link"));
208 cl_must_pass(p_rmdir("dir"));
209
210 git_str_dispose(&contents);
211 }
212
213 void test_core_posix__symlink_to_file_across_dirs(void)
214 {
215 git_str contents = GIT_STR_INIT;
216
217 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
218 clar__skip();
219
220 /*
221 * Create a relative symlink that points into another
222 * directory. This used to not work on Win32, where we
223 * forgot to convert directory separators to
224 * Windows-style ones.
225 */
226 cl_must_pass(git_futils_mkdir("dir", 0777, 0));
227 cl_git_mkfile("dir/target", "symlink target");
228 cl_git_pass(p_symlink("dir/target", "link"));
229
230 cl_git_pass(git_futils_readbuffer(&contents, "dir/target"));
231 cl_assert_equal_s(contents.ptr, "symlink target");
232
233 cl_must_pass(p_unlink("dir/target"));
234 cl_must_pass(p_unlink("link"));
235 cl_must_pass(p_rmdir("dir"));
236
237 git_str_dispose(&contents);
238 }