]> git.proxmox.com Git - libgit2.git/blob - tests/core/posix.c
34a67bf47b533f92ceeeb3a3e6f440360e2ccfe4
[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 "clar_libgit2.h"
13 #include "posix.h"
14
15 void test_core_posix__initialize(void)
16 {
17 #ifdef GIT_WIN32
18 /* on win32, the WSA context needs to be initialized
19 * before any socket calls can be performed */
20 WSADATA wsd;
21
22 cl_git_pass(WSAStartup(MAKEWORD(2,2), &wsd));
23 cl_assert(LOBYTE(wsd.wVersion) == 2 && HIBYTE(wsd.wVersion) == 2);
24 #endif
25 }
26
27 static bool supports_ipv6(void)
28 {
29 #ifdef GIT_WIN32
30 /* IPv6 is supported on Vista and newer */
31 return git_has_win32_version(6, 0, 0);
32 #else
33 return 1;
34 #endif
35 }
36
37 void test_core_posix__inet_pton(void)
38 {
39 struct in_addr addr;
40 struct in6_addr addr6;
41 size_t i;
42
43 struct in_addr_data {
44 const char *p;
45 const uint8_t n[4];
46 };
47
48 struct in6_addr_data {
49 const char *p;
50 const uint8_t n[16];
51 };
52
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 } },
60 };
61
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 } },
69 };
70
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);
75 }
76
77 /* Test some ipv6 addresses */
78 if (supports_ipv6())
79 {
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);
83 }
84 }
85
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);
92
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);
96
97 cl_git_fail(p_inet_pton(5, "315.124", NULL)); /* AF_CHAOS */
98 cl_assert_equal_i(EAFNOSUPPORT, errno);
99 }
100
101 void test_core_posix__utimes(void)
102 {
103 struct p_timeval times[2];
104 struct stat st;
105 time_t curtime;
106 int fd;
107
108 /* test p_utimes */
109 times[0].tv_sec = 1234567890;
110 times[0].tv_usec = 0;
111 times[1].tv_sec = 1234567890;
112 times[1].tv_usec = 0;
113
114 cl_git_mkfile("foo", "Dummy file.");
115 cl_must_pass(p_utimes("foo", times));
116
117 p_stat("foo", &st);
118 cl_assert_equal_i(1234567890, st.st_atime);
119 cl_assert_equal_i(1234567890, st.st_mtime);
120
121
122 /* test p_futimes */
123 times[0].tv_sec = 1414141414;
124 times[0].tv_usec = 0;
125 times[1].tv_sec = 1414141414;
126 times[1].tv_usec = 0;
127
128 cl_must_pass(fd = p_open("foo", O_RDWR));
129 cl_must_pass(p_futimes(fd, times));
130 p_close(fd);
131
132 p_stat("foo", &st);
133 cl_assert_equal_i(1414141414, st.st_atime);
134 cl_assert_equal_i(1414141414, st.st_mtime);
135
136
137 /* test p_utimes with current time, assume that
138 * it takes < 5 seconds to get the time...!
139 */
140 cl_must_pass(p_utimes("foo", NULL));
141
142 curtime = time(NULL);
143 p_stat("foo", &st);
144 cl_assert((st.st_atime - curtime) < 5);
145 cl_assert((st.st_mtime - curtime) < 5);
146
147 p_unlink("foo");
148 }