2 # include <arpa/inet.h>
3 # include <sys/socket.h>
4 # include <netinet/in.h>
8 # pragma comment(lib, "ws2_32")
14 #include "clar_libgit2.h"
18 void test_core_posix__initialize(void)
21 /* on win32, the WSA context needs to be initialized
22 * before any socket calls can be performed */
25 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd
));
26 cl_assert(LOBYTE(wsd
.wVersion
) == 2 && HIBYTE(wsd
.wVersion
) == 2);
30 static bool supports_ipv6(void)
33 /* IPv6 is supported on Vista and newer */
34 return git_has_win32_version(6, 0, 0);
40 void test_core_posix__inet_pton(void)
43 struct in6_addr addr6
;
51 struct in6_addr_data
{
56 static struct in_addr_data in_addr_data
[] = {
57 { "0.0.0.0", { 0, 0, 0, 0 } },
58 { "10.42.101.8", { 10, 42, 101, 8 } },
59 { "127.0.0.1", { 127, 0, 0, 1 } },
60 { "140.177.10.12", { 140, 177, 10, 12 } },
61 { "204.232.175.90", { 204, 232, 175, 90 } },
62 { "255.255.255.255", { 255, 255, 255, 255 } },
65 static struct in6_addr_data in6_addr_data
[] = {
66 { "::", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
67 { "::1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
68 { "0:0:0:0:0:0:0:1", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
69 { "2001:db8:8714:3a90::12", { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12 } },
70 { "fe80::f8ba:c2d6:86be:3645", { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xba, 0xc2, 0xd6, 0x86, 0xbe, 0x36, 0x45 } },
71 { "::ffff:204.152.189.116", { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x98, 0xbd, 0x74 } },
74 /* Test some ipv4 addresses */
75 for (i
= 0; i
< 6; i
++) {
76 cl_assert(p_inet_pton(AF_INET
, in_addr_data
[i
].p
, &addr
) == 1);
77 cl_assert(memcmp(&addr
, in_addr_data
[i
].n
, sizeof(struct in_addr
)) == 0);
80 /* Test some ipv6 addresses */
83 for (i
= 0; i
< 6; i
++) {
84 cl_assert(p_inet_pton(AF_INET6
, in6_addr_data
[i
].p
, &addr6
) == 1);
85 cl_assert(memcmp(&addr6
, in6_addr_data
[i
].n
, sizeof(struct in6_addr
)) == 0);
89 /* Test some invalid strings */
90 cl_assert(p_inet_pton(AF_INET
, "", &addr
) == 0);
91 cl_assert(p_inet_pton(AF_INET
, "foo", &addr
) == 0);
92 cl_assert(p_inet_pton(AF_INET
, " 127.0.0.1", &addr
) == 0);
93 cl_assert(p_inet_pton(AF_INET
, "bar", &addr
) == 0);
94 cl_assert(p_inet_pton(AF_INET
, "10.foo.bar.1", &addr
) == 0);
96 /* Test unsupported address families */
97 cl_git_fail(p_inet_pton(12, "52.472", &addr
)); /* AF_DECnet */
98 cl_assert_equal_i(EAFNOSUPPORT
, errno
);
100 cl_git_fail(p_inet_pton(5, "315.124", &addr
)); /* AF_CHAOS */
101 cl_assert_equal_i(EAFNOSUPPORT
, errno
);
104 void test_core_posix__utimes(void)
106 struct p_timeval times
[2];
112 times
[0].tv_sec
= 1234567890;
113 times
[0].tv_usec
= 0;
114 times
[1].tv_sec
= 1234567890;
115 times
[1].tv_usec
= 0;
117 cl_git_mkfile("foo", "Dummy file.");
118 cl_must_pass(p_utimes("foo", times
));
121 cl_assert_equal_i(1234567890, st
.st_atime
);
122 cl_assert_equal_i(1234567890, st
.st_mtime
);
126 times
[0].tv_sec
= 1414141414;
127 times
[0].tv_usec
= 0;
128 times
[1].tv_sec
= 1414141414;
129 times
[1].tv_usec
= 0;
131 cl_must_pass(fd
= p_open("foo", O_RDWR
));
132 cl_must_pass(p_futimes(fd
, times
));
136 cl_assert_equal_i(1414141414, st
.st_atime
);
137 cl_assert_equal_i(1414141414, st
.st_mtime
);
140 /* test p_utimes with current time, assume that
141 * it takes < 5 seconds to get the time...!
143 cl_must_pass(p_utimes("foo", NULL
));
145 curtime
= time(NULL
);
147 cl_assert((st
.st_atime
- curtime
) < 5);
148 cl_assert((st
.st_mtime
- curtime
) < 5);
153 void test_core_posix__p_regcomp_ignores_global_locale_ctype(void)
158 const char* oldlocale
= setlocale(LC_CTYPE
, NULL
);
160 if (!setlocale(LC_CTYPE
, "UTF-8") &&
161 !setlocale(LC_CTYPE
, "c.utf8") &&
162 !setlocale(LC_CTYPE
, "en_US.UTF-8"))
165 if (MB_CUR_MAX
== 1) {
166 setlocale(LC_CTYPE
, oldlocale
);
167 cl_fail("Expected locale to be switched to multibyte");
170 p_regcomp(&preg
, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED
);
173 setlocale(LC_CTYPE
, oldlocale
);
178 void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
182 for (idx
= 0; idx
< ARRAY_SIZE(builtin_defs
); ++idx
) {
183 git_diff_driver_definition ddef
= builtin_defs
[idx
];
187 error
= p_regcomp(&preg
, ddef
.fns
, REG_EXTENDED
| ddef
.flags
);
191 error
= p_regcomp(&preg
, ddef
.words
, REG_EXTENDED
);