]> git.proxmox.com Git - libgit2.git/blame - tests/core/posix.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / posix.c
CommitLineData
238b7614
ET
1#ifndef _WIN32
2# include <arpa/inet.h>
9d85f007
FT
3# include <sys/socket.h>
4# include <netinet/in.h>
238b7614
ET
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"
22a2d3d5 13#include "futils.h"
238b7614
ET
14#include "posix.h"
15
16void 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
1ff3a094
ET
28static 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
238b7614
ET
38void test_core_posix__inet_pton(void)
39{
40 struct in_addr addr;
41 struct in6_addr addr6;
42 size_t i;
ab96ca55 43
238b7614
ET
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 */
1ff3a094
ET
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 }
238b7614
ET
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 */
983979fa 95 cl_git_fail(p_inet_pton(INT_MAX-1, "52.472", &addr));
238b7614
ET
96 cl_assert_equal_i(EAFNOSUPPORT, errno);
97}
121c3171
ET
98
99void test_core_posix__utimes(void)
100{
35439f59 101 struct p_timeval times[2];
121c3171
ET
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
22a2d3d5 115 cl_must_pass(p_stat("foo", &st));
121c3171
ET
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));
22a2d3d5 128 cl_must_pass(p_close(fd));
121c3171 129
22a2d3d5 130 cl_must_pass(p_stat("foo", &st));
121c3171
ET
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);
22a2d3d5 141 cl_must_pass(p_stat("foo", &st));
121c3171
ET
142 cl_assert((st.st_atime - curtime) < 5);
143 cl_assert((st.st_mtime - curtime) < 5);
144
22a2d3d5 145 cl_must_pass(p_unlink("foo"));
121c3171 146}
ab96ca55 147
22a2d3d5 148void test_core_posix__unlink_removes_symlink(void)
ab96ca55 149{
e579e0f7 150 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
22a2d3d5 151 clar__skip();
36117978 152
22a2d3d5
UG
153 cl_git_mkfile("file", "Dummy file.");
154 cl_git_pass(git_futils_mkdir("dir", 0777, 0));
ab96ca55 155
22a2d3d5
UG
156 cl_must_pass(p_symlink("file", "file-symlink"));
157 cl_must_pass(p_symlink("dir", "dir-symlink"));
ab96ca55 158
22a2d3d5
UG
159 cl_must_pass(p_unlink("file-symlink"));
160 cl_must_pass(p_unlink("dir-symlink"));
161
e579e0f7
MB
162 cl_assert(git_fs_path_exists("file"));
163 cl_assert(git_fs_path_exists("dir"));
36117978 164
22a2d3d5
UG
165 cl_must_pass(p_unlink("file"));
166 cl_must_pass(p_rmdir("dir"));
167}
168
169void test_core_posix__symlink_resolves_to_correct_type(void)
170{
e579e0f7 171 git_str contents = GIT_STR_INIT;
36117978 172
e579e0f7 173 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
22a2d3d5 174 clar__skip();
36117978 175
22a2d3d5
UG
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
e579e0f7 190 git_str_dispose(&contents);
ab96ca55
AS
191}
192
22a2d3d5 193void test_core_posix__relative_symlink(void)
ab96ca55 194{
e579e0f7 195 git_str contents = GIT_STR_INIT;
ab96ca55 196
e579e0f7 197 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
22a2d3d5 198 clar__skip();
ab96ca55 199
22a2d3d5
UG
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");
ab96ca55 205
22a2d3d5
UG
206 cl_must_pass(p_unlink("file"));
207 cl_must_pass(p_unlink("dir/link"));
208 cl_must_pass(p_rmdir("dir"));
209
e579e0f7 210 git_str_dispose(&contents);
22a2d3d5
UG
211}
212
213void test_core_posix__symlink_to_file_across_dirs(void)
214{
e579e0f7 215 git_str contents = GIT_STR_INIT;
22a2d3d5 216
e579e0f7 217 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
22a2d3d5
UG
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
e579e0f7 237 git_str_dispose(&contents);
ab96ca55 238}