dispatch_test.c 9.5 KB

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