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"
15 void test_core_posix__initialize(void)
18 /* on win32, the WSA context needs to be initialized
19 * before any socket calls can be performed */
22 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd
));
23 cl_assert(LOBYTE(wsd
.wVersion
) == 2 && HIBYTE(wsd
.wVersion
) == 2);
27 static bool supports_ipv6(void)
30 /* IPv6 is supported on Vista and newer */
31 return git_has_win32_version(6, 0, 0);
37 void test_core_posix__inet_pton(void)
40 struct in6_addr addr6
;
48 struct in6_addr_data
{
53 static struct in_addr_data in_addr_data
[] = {
54 { "0.0.0.0", { 0, 0, 0, 0 } },
55 { "10.42.101.8", { 10, 42, 101, 8 } },
56 { "127.0.0.1", { 127, 0, 0, 1 } },
57 { "140.177.10.12", { 140, 177, 10, 12 } },
58 { "204.232.175.90", { 204, 232, 175, 90 } },
59 { "255.255.255.255", { 255, 255, 255, 255 } },
62 static struct in6_addr_data in6_addr_data
[] = {
63 { "::", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
64 { "::1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
65 { "0:0:0:0:0:0:0:1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
66 { "2001:db8:8714:3a90::12", { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12 } },
67 { "fe80::f8ba:c2d6:86be:3645", { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xba, 0xc2, 0xd6, 0x86, 0xbe, 0x36, 0x45 } },
68 { "::ffff:204.152.189.116", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x98, 0xbd, 0x74 } },
71 /* Test some ipv4 addresses */
72 for (i
= 0; i
< 6; i
++) {
73 cl_assert(p_inet_pton(AF_INET
, in_addr_data
[i
].p
, &addr
) == 1);
74 cl_assert(memcmp(&addr
, in_addr_data
[i
].n
, sizeof(struct in_addr
)) == 0);
77 /* Test some ipv6 addresses */
80 for (i
= 0; i
< 6; i
++) {
81 cl_assert(p_inet_pton(AF_INET6
, in6_addr_data
[i
].p
, &addr6
) == 1);
82 cl_assert(memcmp(&addr6
, in6_addr_data
[i
].n
, sizeof(struct in6_addr
)) == 0);
86 /* Test some invalid strings */
87 cl_assert(p_inet_pton(AF_INET
, "", &addr
) == 0);
88 cl_assert(p_inet_pton(AF_INET
, "foo", &addr
) == 0);
89 cl_assert(p_inet_pton(AF_INET
, " 127.0.0.1", &addr
) == 0);
90 cl_assert(p_inet_pton(AF_INET
, "bar", &addr
) == 0);
91 cl_assert(p_inet_pton(AF_INET
, "10.foo.bar.1", &addr
) == 0);
93 /* Test unsupported address families */
94 cl_git_fail(p_inet_pton(12, "52.472", NULL
)); /* AF_DECnet */
95 cl_assert_equal_i(EAFNOSUPPORT
, errno
);
97 cl_git_fail(p_inet_pton(5, "315.124", NULL
)); /* AF_CHAOS */
98 cl_assert_equal_i(EAFNOSUPPORT
, errno
);
101 void test_core_posix__utimes(void)
103 struct p_timeval times
[2];
109 times
[0].tv_sec
= 1234567890;
110 times
[0].tv_usec
= 0;
111 times
[1].tv_sec
= 1234567890;
112 times
[1].tv_usec
= 0;
114 cl_git_mkfile("foo", "Dummy file.");
115 cl_must_pass(p_utimes("foo", times
));
118 cl_assert_equal_i(1234567890, st
.st_atime
);
119 cl_assert_equal_i(1234567890, st
.st_mtime
);
123 times
[0].tv_sec
= 1414141414;
124 times
[0].tv_usec
= 0;
125 times
[1].tv_sec
= 1414141414;
126 times
[1].tv_usec
= 0;
128 cl_must_pass(fd
= p_open("foo", O_RDWR
));
129 cl_must_pass(p_futimes(fd
, times
));
133 cl_assert_equal_i(1414141414, st
.st_atime
);
134 cl_assert_equal_i(1414141414, st
.st_mtime
);
137 /* test p_utimes with current time, assume that
138 * it takes < 5 seconds to get the time...!
140 cl_must_pass(p_utimes("foo", NULL
));
142 curtime
= time(NULL
);
144 cl_assert((st
.st_atime
- curtime
) < 5);
145 cl_assert((st
.st_mtime
- curtime
) < 5);