]> git.proxmox.com Git - libgit2.git/blame - tests/core/posix.c
Make sure we use the `C` locale for `regcomp` on macOS.
[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"
13#include "posix.h"
ab96ca55 14#include "userdiff.h"
238b7614
ET
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 */
95 cl_git_fail(p_inet_pton(12, "52.472", NULL)); /* AF_DECnet */
96 cl_assert_equal_i(EAFNOSUPPORT, errno);
97
98 cl_git_fail(p_inet_pton(5, "315.124", NULL)); /* AF_CHAOS */
99 cl_assert_equal_i(EAFNOSUPPORT, errno);
100}
121c3171
ET
101
102void test_core_posix__utimes(void)
103{
35439f59 104 struct p_timeval times[2];
121c3171
ET
105 struct stat st;
106 time_t curtime;
107 int fd;
108
109 /* test p_utimes */
110 times[0].tv_sec = 1234567890;
111 times[0].tv_usec = 0;
112 times[1].tv_sec = 1234567890;
113 times[1].tv_usec = 0;
114
115 cl_git_mkfile("foo", "Dummy file.");
116 cl_must_pass(p_utimes("foo", times));
117
118 p_stat("foo", &st);
119 cl_assert_equal_i(1234567890, st.st_atime);
120 cl_assert_equal_i(1234567890, st.st_mtime);
121
122
123 /* test p_futimes */
124 times[0].tv_sec = 1414141414;
125 times[0].tv_usec = 0;
126 times[1].tv_sec = 1414141414;
127 times[1].tv_usec = 0;
128
129 cl_must_pass(fd = p_open("foo", O_RDWR));
130 cl_must_pass(p_futimes(fd, times));
131 p_close(fd);
132
133 p_stat("foo", &st);
134 cl_assert_equal_i(1414141414, st.st_atime);
135 cl_assert_equal_i(1414141414, st.st_mtime);
136
137
138 /* test p_utimes with current time, assume that
139 * it takes < 5 seconds to get the time...!
140 */
141 cl_must_pass(p_utimes("foo", NULL));
142
143 curtime = time(NULL);
144 p_stat("foo", &st);
145 cl_assert((st.st_atime - curtime) < 5);
146 cl_assert((st.st_mtime - curtime) < 5);
147
148 p_unlink("foo");
149}
ab96ca55
AS
150
151void test_core_posix__p_regcomp_compile_single_byte_regexps(void)
152{
153 regex_t preg;
154
155 cl_must_pass(p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED));
156
157 regfree(&preg);
158}
159
160void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
161{
162 regex_t preg;
163 size_t idx;
164
165 for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
166 git_diff_driver_definition ddef = builtin_defs[idx];
167
168 cl_must_pass(p_regcomp(&preg, ddef.fns, REG_EXTENDED | ddef.flags));
169 cl_must_pass(p_regcomp(&preg, ddef.words, REG_EXTENDED));
170 }
171
172 regfree(&preg);
173}