2 # include <arpa/inet.h>
3 # include <sys/socket.h>
4 # include <netinet/in.h>
8 # pragma comment(lib, "ws2_32")
12 #include "clar_libgit2.h"
16 void test_core_posix__initialize(void)
19 /* on win32, the WSA context needs to be initialized
20 * before any socket calls can be performed */
23 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd
));
24 cl_assert(LOBYTE(wsd
.wVersion
) == 2 && HIBYTE(wsd
.wVersion
) == 2);
28 static bool supports_ipv6(void)
31 /* IPv6 is supported on Vista and newer */
32 return git_has_win32_version(6, 0, 0);
38 void test_core_posix__inet_pton(void)
41 struct in6_addr addr6
;
49 struct in6_addr_data
{
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 } },
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 } },
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);
78 /* Test some ipv6 addresses */
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);
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);
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
);
99 void test_core_posix__utimes(void)
101 struct p_timeval times
[2];
107 times
[0].tv_sec
= 1234567890;
108 times
[0].tv_usec
= 0;
109 times
[1].tv_sec
= 1234567890;
110 times
[1].tv_usec
= 0;
112 cl_git_mkfile("foo", "Dummy file.");
113 cl_must_pass(p_utimes("foo", times
));
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
);
121 times
[0].tv_sec
= 1414141414;
122 times
[0].tv_usec
= 0;
123 times
[1].tv_sec
= 1414141414;
124 times
[1].tv_usec
= 0;
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
));
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
);
135 /* test p_utimes with current time, assume that
136 * it takes < 5 seconds to get the time...!
138 cl_must_pass(p_utimes("foo", NULL
));
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);
145 cl_must_pass(p_unlink("foo"));
148 void test_core_posix__unlink_removes_symlink(void)
150 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
153 cl_git_mkfile("file", "Dummy file.");
154 cl_git_pass(git_futils_mkdir("dir", 0777, 0));
156 cl_must_pass(p_symlink("file", "file-symlink"));
157 cl_must_pass(p_symlink("dir", "dir-symlink"));
159 cl_must_pass(p_unlink("file-symlink"));
160 cl_must_pass(p_unlink("dir-symlink"));
162 cl_assert(git_fs_path_exists("file"));
163 cl_assert(git_fs_path_exists("dir"));
165 cl_must_pass(p_unlink("file"));
166 cl_must_pass(p_rmdir("dir"));
169 void test_core_posix__symlink_resolves_to_correct_type(void)
171 git_str contents
= GIT_STR_INIT
;
173 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
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");
180 cl_git_pass(p_symlink("file", "dir/link"));
182 cl_git_pass(git_futils_readbuffer(&contents
, "dir/file"));
183 cl_assert_equal_s(contents
.ptr
, "symlink target");
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"));
190 git_str_dispose(&contents
);
193 void test_core_posix__relative_symlink(void)
195 git_str contents
= GIT_STR_INIT
;
197 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
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");
206 cl_must_pass(p_unlink("file"));
207 cl_must_pass(p_unlink("dir/link"));
208 cl_must_pass(p_rmdir("dir"));
210 git_str_dispose(&contents
);
213 void test_core_posix__symlink_to_file_across_dirs(void)
215 git_str contents
= GIT_STR_INIT
;
217 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
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.
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"));
230 cl_git_pass(git_futils_readbuffer(&contents
, "dir/target"));
231 cl_assert_equal_s(contents
.ptr
, "symlink target");
233 cl_must_pass(p_unlink("dir/target"));
234 cl_must_pass(p_unlink("link"));
235 cl_must_pass(p_rmdir("dir"));
237 git_str_dispose(&contents
);