1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
* Copyright (c) 2008-2011 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#include "dispatch_test.h"
#include "bsdtests.h"
#ifdef __OBJC_GC__
#include <objc/objc-auto.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#if __has_include(<sys/event.h>)
#define HAS_SYS_EVENT_H 1
#include <sys/event.h>
#elif __has_include(<poll.h>)
#include <poll.h>
#else
#include <sys/poll.h>
#endif
#elif defined(_WIN32)
#include <Windows.h>
#include <bcrypt.h>
#endif
#include <assert.h>
#include <dispatch/dispatch.h>
void test_start(const char* desc);
void
dispatch_test_start(const char* desc)
{
#if defined(__OBJC_GC__) && MAC_OS_X_VERSION_MIN_REQUIRED < 1070
objc_startCollectorThread();
#endif
test_start(desc);
}
bool
dispatch_test_check_evfilt_read_for_fd(dispatch_fd_t fd)
{
#if HAS_SYS_EVENT_H
int kq = kqueue();
assert(kq != -1);
struct kevent ke = {
.ident = (uintptr_t)fd,
.filter = EVFILT_READ,
.flags = EV_ADD|EV_ENABLE,
};
struct timespec t = {
.tv_sec = 1,
};
int r = kevent(kq, &ke, 1, &ke, 1, &t);
close(kq);
return r > 0;
#elif defined(_WIN32)
HANDLE handle = (HANDLE)fd;
// A zero-distance move retrieves the file pointer
LARGE_INTEGER currentPosition;
LARGE_INTEGER distance = {.QuadPart = 0};
if (!SetFilePointerEx(handle, distance, ¤tPosition, FILE_CURRENT)) {
return false;
}
// If we are not at the end, assume the file is readable
LARGE_INTEGER fileSize;
if (GetFileSizeEx(handle, &fileSize) == 0) {
return false;
}
return currentPosition.QuadPart < fileSize.QuadPart;
#else
struct pollfd pfd = {
.fd = fd,
.events = POLLIN,
};
int rc;
do {
rc = poll(&pfd, 1, 0);
} while (rc == -1 && errno == EINTR);
assert(rc != -1);
return rc == 1;
#endif
}
char *
dispatch_test_get_large_file(void)
{
#if defined(__APPLE__)
return strdup("/usr/bin/vi");
#elif defined(__unix__) || defined(_WIN32)
// Depending on /usr/bin/vi being present is unreliable (especially on
// Android), so fill up a large-enough temp file with random bytes
#if defined(_WIN32)
char temp_dir_buf[MAX_PATH];
const char *temp_dir = getenv("TEMP") ?: getenv("TMP");
if (!temp_dir) {
DWORD len = GetTempPathA(sizeof(temp_dir_buf), temp_dir_buf);
if (len > 0 && len < sizeof(temp_dir_buf)) {
temp_dir = temp_dir_buf;
} else {
temp_dir = ".";
}
}
#else
const char *temp_dir = getenv("TMPDIR");
if (temp_dir == NULL || temp_dir[0] == '\0') {
temp_dir = "/tmp";
}
#endif
const char *const suffix = "/dispatch_test.XXXXXX";
size_t temp_dir_len = strlen(temp_dir);
size_t suffix_len = strlen(suffix);
char *path = malloc(temp_dir_len + suffix_len + 1);
assert(path != NULL);
memcpy(path, temp_dir, temp_dir_len);
memcpy(&path[temp_dir_len], suffix, suffix_len + 1);
dispatch_fd_t temp_fd = mkstemp(path);
if (temp_fd == -1) {
perror("mkstemp");
exit(EXIT_FAILURE);
}
const size_t file_size = 2 * 1024 * 1024;
char *file_buf = malloc(file_size);
assert(file_buf != NULL);
ssize_t num;
#if defined(_WIN32)
NTSTATUS status = BCryptGenRandom(NULL, (PUCHAR)file_buf, file_size,
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (status < 0) {
fprintf(stderr, "BCryptGenRandom failed with %ld\n", status);
dispatch_test_release_large_file(path);
exit(EXIT_FAILURE);
}
#else
int urandom_fd = open("/dev/urandom", O_RDONLY);
if (urandom_fd == -1) {
perror("/dev/urandom");
dispatch_test_release_large_file(path);
exit(EXIT_FAILURE);
}
size_t pos = 0;
while (pos < file_size) {
num = read(urandom_fd, &file_buf[pos], file_size - pos);
if (num > 0) {
pos += (size_t)num;
} else if (num == -1 && errno != EINTR) {
perror("read");
dispatch_test_release_large_file(path);
exit(EXIT_FAILURE);
}
}
close(urandom_fd);
#endif
do {
num = dispatch_test_fd_write(temp_fd, file_buf, file_size);
} while (num == -1 && errno == EINTR);
if (num == -1) {
perror("write");
dispatch_test_release_large_file(path);
exit(EXIT_FAILURE);
}
assert(num == file_size);
dispatch_test_fd_close(temp_fd);
free(file_buf);
return path;
#else
#error "dispatch_test_get_large_file not implemented on this platform"
#endif
}
void
dispatch_test_release_large_file(const char *path)
{
#if defined(__APPLE__)
// The path is fixed to a system file - do nothing
(void)path;
#elif defined(__unix__) || defined(_WIN32)
if (unlink(path) < 0) {
perror("unlink");
}
#else
#error "dispatch_test_release_large_file not implemented on this platform"
#endif
}
void
_dispatch_test_current(const char* file, long line, const char* desc, dispatch_queue_t expected)
{
dispatch_queue_t actual = dispatch_get_current_queue();
_test_ptr(file, line, desc, actual, expected);
}
dispatch_fd_t
dispatch_test_fd_open(const char *path, int flags)
{
#if defined(_WIN32)
DWORD desired_access = 0;
DWORD creation_disposition = OPEN_EXISTING;
switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
case O_RDONLY:
desired_access = GENERIC_READ;
break;
case O_WRONLY:
desired_access = GENERIC_WRITE;
break;
case O_RDWR:
desired_access = GENERIC_READ | GENERIC_WRITE;
break;
}
if (flags & O_CREAT) {
creation_disposition = OPEN_ALWAYS;
if (flags & O_EXCL) {
creation_disposition = CREATE_NEW;
}
}
// FILE_SHARE_DELETE is important here because tests must be able to delete
// temporary files after opening them
HANDLE handle = CreateFileA(path, desired_access,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
/* lpSecurityAttributes */ NULL, creation_disposition,
/* dwFlagsAndAttributes */ 0, /* hTemplateFile */ NULL);
if (handle == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
switch (error) {
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
case ERROR_FILE_EXISTS:
errno = EEXIST;
break;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
errno = ENOENT;
break;
default:
print_winapi_error("CreateFileA", GetLastError());
errno = EIO;
break;
}
return -1;
}
return (dispatch_fd_t)handle;
#else
return open(path, flags);
#endif
}
int
dispatch_test_fd_close(dispatch_fd_t fd)
{
#if defined(_WIN32)
if (!CloseHandle((HANDLE)fd)) {
errno = EBADF;
return -1;
}
return 0;
#else
return close(fd);
#endif
}
off_t
dispatch_test_fd_lseek(dispatch_fd_t fd, off_t offset, int whence)
{
#if defined(_WIN32)
DWORD method;
switch (whence) {
case SEEK_CUR:
method = FILE_CURRENT;
break;
case SEEK_END:
method = FILE_END;
break;
case SEEK_SET:
default:
method = FILE_BEGIN;
break;
}
LARGE_INTEGER distance = {.QuadPart = offset};
LARGE_INTEGER new_pos;
if (!SetFilePointerEx((HANDLE)fd, distance, &new_pos, method)) {
print_winapi_error("SetFilePointerEx", GetLastError());
errno = EINVAL;
return -1;
}
return (off_t)new_pos.QuadPart;
#else
return lseek(fd, offset, whence);
#endif
}
ssize_t
dispatch_test_fd_pread(dispatch_fd_t fd, void *buf, size_t count, off_t offset)
{
#if defined(_WIN32)
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
LARGE_INTEGER lioffset = {.QuadPart = offset};
overlapped.Offset = lioffset.LowPart;
overlapped.OffsetHigh = lioffset.HighPart;
DWORD num_read;
if (!ReadFile((HANDLE)fd, buf, count, &num_read, &overlapped)) {
print_winapi_error("ReadFile", GetLastError());
errno = EIO;
return -1;
}
return (ssize_t)num_read;
#else
return pread(fd, buf, count, offset);
#endif
}
ssize_t
dispatch_test_fd_read(dispatch_fd_t fd, void *buf, size_t count)
{
#if defined(_WIN32)
if (GetFileType((HANDLE)fd) == FILE_TYPE_PIPE) {
OVERLAPPED ov = {0};
DWORD num_read;
BOOL success = ReadFile((HANDLE)fd, buf, count, &num_read, &ov);
if (!success && GetLastError() == ERROR_IO_PENDING) {
success = GetOverlappedResult((HANDLE)fd, &ov, &num_read,
/* bWait */ TRUE);
}
if (!success) {
print_winapi_error("ReadFile", GetLastError());
errno = EIO;
return -1;
}
return (ssize_t)num_read;
}
DWORD num_read;
if (!ReadFile((HANDLE)fd, buf, count, &num_read, NULL)) {
print_winapi_error("ReadFile", GetLastError());
errno = EIO;
return -1;
}
return (ssize_t)num_read;
#else
return read(fd, buf, count);
#endif
}
ssize_t
dispatch_test_fd_write(dispatch_fd_t fd, const void *buf, size_t count)
{
#if defined(_WIN32)
if (GetFileType((HANDLE)fd) == FILE_TYPE_PIPE) {
OVERLAPPED ov = {0};
DWORD num_written;
BOOL success = WriteFile((HANDLE)fd, buf, count, &num_written, &ov);
if (!success && GetLastError() == ERROR_IO_PENDING) {
success = GetOverlappedResult((HANDLE)fd, &ov, &num_written,
/* bWait */ TRUE);
}
if (!success) {
print_winapi_error("WriteFile", GetLastError());
errno = EIO;
return -1;
}
return (ssize_t)num_written;
}
DWORD num_written;
if (!WriteFile((HANDLE)fd, buf, count, &num_written, NULL)) {
print_winapi_error("WriteFile", GetLastError());
errno = EIO;
return -1;
}
return (ssize_t)num_written;
#else
return write(fd, buf, count);
#endif
}
|