]> git.proxmox.com Git - libgit2.git/blob - tests/core/posix.c
Merge pull request #4143 from richardipsum/issue-4094
[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 <locale.h>
13
14 #include "clar_libgit2.h"
15 #include "posix.h"
16 #include "userdiff.h"
17
18 void test_core_posix__initialize(void)
19 {
20 #ifdef GIT_WIN32
21 /* on win32, the WSA context needs to be initialized
22 * before any socket calls can be performed */
23 WSADATA wsd;
24
25 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd));
26 cl_assert(LOBYTE(wsd.wVersion) == 2 && HIBYTE(wsd.wVersion) == 2);
27 #endif
28 }
29
30 static bool supports_ipv6(void)
31 {
32 #ifdef GIT_WIN32
33 /* IPv6 is supported on Vista and newer */
34 return git_has_win32_version(6, 0, 0);
35 #else
36 return 1;
37 #endif
38 }
39
40 void test_core_posix__inet_pton(void)
41 {
42 struct in_addr addr;
43 struct in6_addr addr6;
44 size_t i;
45
46 struct in_addr_data {
47 const char *p;
48 const uint8_t n[4];
49 };
50
51 struct in6_addr_data {
52 const char *p;
53 const uint8_t n[16];
54 };
55
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 } },
63 };
64
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 } },
72 };
73
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);
78 }
79
80 /* Test some ipv6 addresses */
81 if (supports_ipv6())
82 {
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);
86 }
87 }
88
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);
95
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);
99
100 cl_git_fail(p_inet_pton(5, "315.124", &addr)); /* AF_CHAOS */
101 cl_assert_equal_i(EAFNOSUPPORT, errno);
102 }
103
104 void test_core_posix__utimes(void)
105 {
106 struct p_timeval times[2];
107 struct stat st;
108 time_t curtime;
109 int fd;
110
111 /* test p_utimes */
112 times[0].tv_sec = 1234567890;
113 times[0].tv_usec = 0;
114 times[1].tv_sec = 1234567890;
115 times[1].tv_usec = 0;
116
117 cl_git_mkfile("foo", "Dummy file.");
118 cl_must_pass(p_utimes("foo", times));
119
120 p_stat("foo", &st);
121 cl_assert_equal_i(1234567890, st.st_atime);
122 cl_assert_equal_i(1234567890, st.st_mtime);
123
124
125 /* test p_futimes */
126 times[0].tv_sec = 1414141414;
127 times[0].tv_usec = 0;
128 times[1].tv_sec = 1414141414;
129 times[1].tv_usec = 0;
130
131 cl_must_pass(fd = p_open("foo", O_RDWR));
132 cl_must_pass(p_futimes(fd, times));
133 p_close(fd);
134
135 p_stat("foo", &st);
136 cl_assert_equal_i(1414141414, st.st_atime);
137 cl_assert_equal_i(1414141414, st.st_mtime);
138
139
140 /* test p_utimes with current time, assume that
141 * it takes < 5 seconds to get the time...!
142 */
143 cl_must_pass(p_utimes("foo", NULL));
144
145 curtime = time(NULL);
146 p_stat("foo", &st);
147 cl_assert((st.st_atime - curtime) < 5);
148 cl_assert((st.st_mtime - curtime) < 5);
149
150 p_unlink("foo");
151 }
152
153 void test_core_posix__p_regcomp_ignores_global_locale_ctype(void)
154 {
155 regex_t preg;
156 int error = 0;
157
158 const char* oldlocale = setlocale(LC_CTYPE, NULL);
159
160 if (!setlocale(LC_CTYPE, "UTF-8") &&
161 !setlocale(LC_CTYPE, "c.utf8") &&
162 !setlocale(LC_CTYPE, "en_US.UTF-8"))
163 cl_skip();
164
165 if (MB_CUR_MAX == 1) {
166 setlocale(LC_CTYPE, oldlocale);
167 cl_fail("Expected locale to be switched to multibyte");
168 }
169
170 p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED);
171 regfree(&preg);
172
173 setlocale(LC_CTYPE, oldlocale);
174
175 cl_must_pass(error);
176 }
177
178 void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
179 {
180 size_t idx;
181
182 for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
183 git_diff_driver_definition ddef = builtin_defs[idx];
184 int error = 0;
185 regex_t preg;
186
187 error = p_regcomp(&preg, ddef.fns, REG_EXTENDED | ddef.flags);
188 regfree(&preg);
189 cl_must_pass(error);
190
191 error = p_regcomp(&preg, ddef.words, REG_EXTENDED);
192 regfree(&preg);
193 cl_must_pass(error);
194 }
195 }