Fix using CURLOPT_XFERINFOFUNCTION curl option
[libisds.git] / test / test.c
blob6abd774c931c555eaeed1b1ba0ecc9b90e6c57bb
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <inttypes.h>
6 #include <stdarg.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
11 #ifndef _WIN32
12 #include <sys/mman.h>
13 #endif
15 #include <errno.h>
16 #include <string.h>
17 #include <unistd.h>
19 /* Global variables for each test unit */
20 char *unit_name = NULL;
21 char *reason = NULL;
22 unsigned int passed, failed, skipped;
23 void (*test_destructor_function)(void *) = NULL;
24 void *test_destructor_argument = NULL;
26 /* Print formated string into automtically reallocated @uffer.
27 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
28 * memory.
29 * @format format string as for printf(3)
30 * @ap list of variadic arguments, after call will be in udefined state
31 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
32 int test_vasprintf(char **buffer, const char *format, va_list ap) {
33 va_list aq;
34 int length, new_length;
35 char *new_buffer;
37 if (!buffer || !format) {
38 if (buffer) {
39 free(*buffer);
40 *buffer = NULL;
42 return -1;
45 va_copy(aq, ap);
46 length = vsnprintf(NULL, 0, format, aq) + 1;
47 va_end(aq);
48 if (length <= 0) {
49 free(*buffer);
50 *buffer = NULL;
51 return -1;
54 new_buffer = realloc(*buffer, length);
55 if (!new_buffer) {
56 free(*buffer);
57 *buffer = NULL;
58 return -1;
60 *buffer = new_buffer;
62 new_length = vsnprintf(*buffer, length, format, ap);
63 if (new_length >= length) {
64 free(*buffer);
65 *buffer = NULL;
66 return -1;
69 return new_length;
73 /* Print formated string into automtically reallocated @uffer.
74 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
75 * memory.
76 * @format format string as for printf(3)
77 * @... variadic arguments
78 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
79 int test_asprintf(char **buffer, const char *format, ...) {
80 int ret;
81 va_list ap;
82 va_start(ap, format);
83 ret = test_vasprintf(buffer, format, ap);
84 va_end(ap);
85 return ret;
89 #ifdef _WIN32
90 int test_mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
91 struct stat file_info;
92 int ret, pos = 0;
94 if (!file || !fd || !buffer || !length) return -1;
96 *fd = open(file, O_RDONLY);
97 if (*fd == -1) {
98 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
99 return -1;
102 if (-1 == fstat(*fd, &file_info)) {
103 fprintf(stderr, "%s: Could not get file size: %s\n", file,
104 strerror(errno));
105 close(*fd);
106 return -1;
108 if (file_info.st_size < 0) {
109 fprintf(stderr, "File `%s' has negative size: %" PRIdMAX "\n", file,
110 (intmax_t) file_info.st_size);
111 close(*fd);
112 return -1;
114 *length = file_info.st_size;
115 *buffer = malloc(*length);
117 if (!*buffer) {
118 fprintf(stderr, "%s: Could not allocate memory for file mapping: %s\n",
119 file, strerror(errno));
120 close(*fd);
121 return -1;
124 do {
125 ret = read(*fd, *buffer + pos, *length - pos);
127 if (ret > 0) {
128 pos += ret;
130 } while ((ret < 0 && errno == EINTR) || (ret > 0 && pos < *length));
132 if (ret < 0) {
133 fprintf(stderr, "%s: Could not map file to memory: %s\n", file,
134 strerror(errno));
135 free(*buffer);
136 *buffer = NULL;
137 close(*fd);
138 return -1;
141 return 0;
144 int test_munmap_file(int fd, void *buffer, size_t length) {
145 int err = 0;
146 free(buffer);
148 err = close(fd);
149 if (err) {
150 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
151 strerror(errno));
154 return err;
156 #else
157 int test_mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
158 struct stat file_info;
160 if (!file || !fd || !buffer || !length) return -1;
163 *fd = open(file, O_RDONLY);
164 if (*fd == -1) {
165 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
166 return -1;
169 if (-1 == fstat(*fd, &file_info)) {
170 fprintf(stderr, "%s: Could not get file size: %s\n", file,
171 strerror(errno));
172 close(*fd);
173 return -1;
175 if (file_info.st_size < 0) {
176 fprintf(stderr, "File `%s' has negative size: %" PRIdMAX "\n", file,
177 (intmax_t) file_info.st_size);
178 close(*fd);
179 return -1;
181 *length = file_info.st_size;
183 *buffer = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, *fd, 0);
184 if (*buffer == MAP_FAILED) {
185 fprintf(stderr, "%s: Could not map file to memory: %s\n", file,
186 strerror(errno));
187 close(*fd);
188 return -1;
191 return 0;
195 int test_munmap_file(int fd, void *buffer, size_t length) {
196 int err = 0;
197 long int page_size = sysconf(_SC_PAGE_SIZE);
198 size_t pages = (length % page_size) ?
199 ((length / page_size) + 1) * page_size:
200 length;
202 err = munmap(buffer, pages);
203 if (err) {
204 fprintf(stderr, "Could not unmap memory at %p and length %zu: %s\n",
205 buffer, pages, strerror(errno));
208 err = close(fd);
209 if (err) {
210 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
211 strerror(errno));
214 return err;
216 #endif