]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core-contrib.git/commitdiff
io-uring-writev: add simple test for writting file with io_uring II
authorMartin Jansa <martin.jansa@gmail.com>
Thu, 19 Oct 2023 11:11:11 +0000 (13:11 +0200)
committerMartin Jansa <martin.jansa@gmail.com>
Fri, 1 Aug 2025 07:34:35 +0000 (09:34 +0200)
* pseudo doesn't support io_uring yet as shown after nodejs was upgraded
  and nodejs-native >= 20.3.0 with libuv >= 1.45.0 which has:
  https://github.com/libuv/libuv/pull/3952

* files created in do_install with nodejs-native aren't tracked by pseudo
  and will result in host-user-contamination QA issue or
  "KeyError: 'getpwuid(): uid not found" as documented in:
  https://github.com/shr-project/com.webos.app.minimal/commit/bd238047c8ce3cd085041d276613396b863213cf

* this is much simpler test for io_uring without the need to build whole
  nodejs-native, it's based on:
  https://unixism.net/2020/04/io-uring-by-example-part-1-introduction/
  just using writev instead of readv

* if it works fine, the file "test" will be tracked in pseudo database
  since the creation in ${D} like:
  core2-64-oe-linux/io-uring-writev/1.0 $ sqlite3 pseudo/files.db "select * from files"
  1|/OE/build/oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0/image|66305|48357743|0|0|16877|0|0
  2|/OE/build/oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0/image/test|66305|48316709|0|0|33188|0|0

  and it does in this case, because I haven't figured out how to call writev()
  without opening the fd of output file first where the openat() call gets
  intercepted by pseudo

  io-uring-writev/1.0 $ strace -v ./io-uring-writev test2 2>&1 | grep openat
  openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
  openat(AT_FDCWD, "/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
  openat(AT_FDCWD, "test2", O_WRONLY|O_CREAT, 0666) = 4

  while with libuv there was no openat() for the output files in strace

* add test with libuv as well and surprisingly it's still working in current pseudo
  oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0 $ sqlite3 pseudo/files.db "select * from files"
  1|/OE/build/oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0/image|66305|62072917|0|0|16877|0|0
  2|/OE/build/oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0/image/test|66305|62061857|0|0|33188|0|0
  3|/OE/build/oe-core/tmp-glibc/work/core2-64-oe-linux/io-uring-writev/1.0/image/task-copy.h|66305|62061858|0|0|33188|0|0

Signed-off-by: Martin Jansa <martin.jansa@gmail.com>
meta-selftest/recipes-test/io-uring/io-uring-writev.bb
meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c [new file with mode: 0644]
meta-selftest/recipes-test/io-uring/io-uring-writev/task.h [new file with mode: 0644]

index 001d4b409827c3eca541554010e2bcd815db0605..af45b0e2cc4d6b0e3b0404aa315b5073b22079a8 100644 (file)
@@ -3,17 +3,24 @@ SECTION = "examples"
 LICENSE = "MIT"
 LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
 
-SRC_URI = "file://io-uring-writev.c"
+DEPENDS += "libuv-native"
+
+SRC_URI = "file://io-uring-writev.c \
+    file://task.h \
+    file://libuv-fs-copyfile.c \
+"
 
 S = "${WORKDIR}/sources"
 UNPACKDIR = "${S}"
 
 do_compile() {
        ${BUILD_CC} io-uring-writev.c -o io-uring-writev
+       ${BUILD_CC} -luv libuv-fs-copyfile.c -o libuv-fs-copyfile
 }
 
 do_install() {
         ${S}/io-uring-writev ${D}/test
+        ${S}/libuv-fs-copyfile ${S}/task.h ${D}/task-copy.h
 }
 
-FILES:${PN} = "test"
+FILES:${PN} = "test test2 task-copy.h"
diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c b/meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c
new file mode 100644 (file)
index 0000000..28783fb
--- /dev/null
@@ -0,0 +1,258 @@
+/*
+ * test example from:
+ * https://github.com/libuv/libuv/blob/v1.x/test/test-fs-copyfile.c
+*/
+
+/* Copyright libuv project contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#if defined(__unix__) || defined(__POSIX__) || \
+    defined(__APPLE__) || defined(__sun) || \
+    defined(_AIX) || defined(__MVS__) || \
+    defined(__HAIKU__) || defined(__QNX__)
+#include <unistd.h> /* unlink, etc. */
+#else
+# include <direct.h>
+# include <io.h>
+# define unlink _unlink
+#endif
+
+static const char fixture[] = "test/fixtures/load_error.node";
+//static const char dst[] = "test2";
+static const char *dst;
+static int result_check_count;
+
+
+static void fail_cb(uv_fs_t* req) {
+  FATAL("fail_cb should not have been called");
+}
+
+static void handle_result(uv_fs_t* req) {
+  uv_fs_t stat_req;
+  uint64_t size;
+  uint64_t mode;
+  int r;
+
+  ASSERT(req->fs_type == UV_FS_COPYFILE);
+  ASSERT(req->result == 0);
+
+  /* Verify that the file size and mode are the same. */
+  r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
+  ASSERT(r == 0);
+  size = stat_req.statbuf.st_size;
+  mode = stat_req.statbuf.st_mode;
+  uv_fs_req_cleanup(&stat_req);
+  r = uv_fs_stat(NULL, &stat_req, dst, NULL);
+  ASSERT(r == 0);
+  ASSERT(stat_req.statbuf.st_size == size);
+  ASSERT(stat_req.statbuf.st_mode == mode);
+  uv_fs_req_cleanup(&stat_req);
+  uv_fs_req_cleanup(req);
+  result_check_count++;
+}
+
+
+static void touch_file(const char* name, unsigned int size) {
+  uv_file file;
+  uv_fs_t req;
+  uv_buf_t buf;
+  int r;
+  unsigned int i;
+
+  r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC,
+                 S_IWUSR | S_IRUSR, NULL);
+  uv_fs_req_cleanup(&req);
+  ASSERT(r >= 0);
+  file = r;
+
+  buf = uv_buf_init("a", 1);
+
+  /* Inefficient but simple. */
+  for (i = 0; i < size; i++) {
+    r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL);
+    uv_fs_req_cleanup(&req);
+    ASSERT(r >= 0);
+  }
+
+  r = uv_fs_close(NULL, &req, file, NULL);
+  uv_fs_req_cleanup(&req);
+  ASSERT(r == 0);
+}
+
+
+TEST_IMPL(fs_copyfile) {
+  const char src[] = "test_file_src";
+  uv_loop_t* loop;
+  uv_fs_t req;
+  int r;
+
+  loop = uv_default_loop();
+
+  /* Fails with EINVAL if bad flags are passed. */
+  r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL);
+  ASSERT(r == UV_EINVAL);
+  uv_fs_req_cleanup(&req);
+
+  /* Fails with ENOENT if source does not exist. */
+  unlink(src);
+  unlink(dst);
+  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
+  ASSERT(req.result == UV_ENOENT);
+  ASSERT(r == UV_ENOENT);
+  uv_fs_req_cleanup(&req);
+  /* The destination should not exist. */
+  r = uv_fs_stat(NULL, &req, dst, NULL);
+  ASSERT(r != 0);
+  uv_fs_req_cleanup(&req);
+
+  /* Succeeds if src and dst files are identical. */
+  touch_file(src, 12);
+  r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL);
+  ASSERT(r == 0);
+  uv_fs_req_cleanup(&req);
+  /* Verify that the src file did not get truncated. */
+  r = uv_fs_stat(NULL, &req, src, NULL);
+  ASSERT_EQ(r, 0);
+  ASSERT_EQ(req.statbuf.st_size, 12);
+  uv_fs_req_cleanup(&req);
+  unlink(src);
+
+  /* Copies file synchronously. Creates new file. */
+  unlink(dst);
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
+  ASSERT(r == 0);
+  handle_result(&req);
+
+  /* Copies a file of size zero. */
+  unlink(dst);
+  touch_file(src, 0);
+  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
+  ASSERT(r == 0);
+  handle_result(&req);
+
+  /* Copies file synchronously. Overwrites existing file. */
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
+  ASSERT(r == 0);
+  handle_result(&req);
+
+  /* Fails to overwrites existing file. */
+  ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0);
+  uv_fs_req_cleanup(&req);
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
+  ASSERT(r == UV_EEXIST);
+  uv_fs_req_cleanup(&req);
+
+  /* Truncates when an existing destination is larger than the source file. */
+  ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0);
+  uv_fs_req_cleanup(&req);
+  touch_file(src, 1);
+  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
+  ASSERT_EQ(r, 0);
+  handle_result(&req);
+
+  /* Copies a larger file. */
+  unlink(dst);
+  touch_file(src, 4096 * 2);
+  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
+  ASSERT(r == 0);
+  handle_result(&req);
+  unlink(src);
+
+  /* Copies file asynchronously */
+  unlink(dst);
+  r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
+  ASSERT(r == 0);
+  ASSERT(result_check_count == 5);
+  uv_run(loop, UV_RUN_DEFAULT);
+  ASSERT(result_check_count == 6);
+  /* Ensure file is user-writable (not copied from src). */
+  ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0);
+  uv_fs_req_cleanup(&req);
+
+  /* If the flags are invalid, the loop should not be kept open */
+  unlink(dst);
+  r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
+  ASSERT(r == UV_EINVAL);
+  uv_run(loop, UV_RUN_DEFAULT);
+
+  /* Copies file using UV_FS_COPYFILE_FICLONE. */
+  unlink(dst);
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
+  ASSERT(r == 0);
+  handle_result(&req);
+
+  /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
+  unlink(dst);
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
+                     NULL);
+  ASSERT(r <= 0);
+
+  if (r == 0)
+    handle_result(&req);
+
+#ifndef _WIN32
+  /* Copying respects permissions/mode. */
+  unlink(dst);
+  touch_file(dst, 0);
+  chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */
+  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
+  /* On IBMi PASE, qsecofr users can overwrite read-only files */
+# ifndef __PASE__
+  ASSERT(req.result == UV_EACCES);
+  ASSERT(r == UV_EACCES);
+# endif
+  uv_fs_req_cleanup(&req);
+#endif
+
+  unlink(dst); /* Cleanup */
+  MAKE_VALGRIND_HAPPY(loop);
+  return 0;
+}
+
+int main(int argc, char *argv[]) {
+    uv_loop_t* loop;
+    uv_fs_t req;
+    int r;
+    char *src;
+
+    if (argc < 3) {
+        fprintf(stderr, "Usage: %s <filename> <filename>\n", argv[0]);
+        return 1;
+    }
+    src = argv[1];
+    dst = argv[2];
+
+    loop = uv_default_loop();
+/*
+    r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
+    ASSERT(r == 0);
+    uv_fs_req_cleanup(&req);
+*/
+    r = uv_fs_copyfile(loop, &req, src, dst, 0, handle_result);
+//    ASSERT(r == 0);
+//    ASSERT(result_check_count == 1);
+    uv_run(loop, UV_RUN_DEFAULT);
+//    ASSERT(result_check_count == 6);
+    uv_fs_req_cleanup(&req);
+}
diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev/task.h b/meta-selftest/recipes-test/io-uring/io-uring-writev/task.h
new file mode 100644 (file)
index 0000000..8b83532
--- /dev/null
@@ -0,0 +1,384 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef TASK_H_
+#define TASK_H_
+
+#include "uv.h"
+
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+#if !defined(_WIN32)
+# include <sys/time.h>
+# include <sys/resource.h>  /* setrlimit() */
+#endif
+
+#ifdef __clang__
+# pragma clang diagnostic ignored "-Wvariadic-macros"
+# pragma clang diagnostic ignored "-Wc99-extensions"
+#endif
+
+#ifdef __GNUC__
+# pragma GCC diagnostic ignored "-Wvariadic-macros"
+#endif
+
+#define TEST_PORT 9123
+#define TEST_PORT_2 9124
+#define TEST_PORT_3 9125
+
+#ifdef _WIN32
+# define TEST_PIPENAME "\\\\.\\pipe\\uv-test"
+# define TEST_PIPENAME_2 "\\\\.\\pipe\\uv-test2"
+# define TEST_PIPENAME_3 "\\\\.\\pipe\\uv-test3"
+#else
+# define TEST_PIPENAME "/tmp/uv-test-sock"
+# define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
+# define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
+#endif
+
+#ifdef _WIN32
+# include <io.h>
+# ifndef S_IRUSR
+#  define S_IRUSR _S_IREAD
+# endif
+# ifndef S_IWUSR
+#  define S_IWUSR _S_IWRITE
+# endif
+#endif
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+#define container_of(ptr, type, member) \
+  ((type *) ((char *) (ptr) - offsetof(type, member)))
+
+typedef enum {
+  TCP = 0,
+  UDP,
+  PIPE
+} stream_type;
+
+/* Die with fatal error. */
+#define FATAL(msg)                                        \
+  do {                                                    \
+    fprintf(stderr,                                       \
+            "Fatal error in %s on line %d: %s\n",         \
+            __FILE__,                                     \
+            __LINE__,                                     \
+            msg);                                         \
+    fflush(stderr);                                       \
+    abort();                                              \
+  } while (0)
+
+/* Have our own assert, so we are sure it does not get optimized away in
+ * a release build.
+ */
+#define ASSERT(expr)                                      \
+ do {                                                     \
+  if (!(expr)) {                                          \
+    fprintf(stderr,                                       \
+            "Assertion failed in %s on line %d: %s\n",    \
+            __FILE__,                                     \
+            __LINE__,                                     \
+            #expr);                                       \
+    abort();                                              \
+  }                                                       \
+ } while (0)
+
+#define ASSERT_BASE(a, operator, b, type, conv)              \
+ do {                                                        \
+  volatile type eval_a = (type) (a);                         \
+  volatile type eval_b = (type) (b);                         \
+  if (!(eval_a operator eval_b)) {                           \
+    fprintf(stderr,                                          \
+            "Assertion failed in %s on line %d: `%s %s %s` " \
+            "(%"conv" %s %"conv")\n",                        \
+            __FILE__,                                        \
+            __LINE__,                                        \
+            #a,                                              \
+            #operator,                                       \
+            #b,                                              \
+            eval_a,                                          \
+            #operator,                                       \
+            eval_b);                                         \
+    abort();                                                 \
+  }                                                          \
+ } while (0)
+
+#define ASSERT_BASE_STR(expr, a, operator, b, type, conv)      \
+ do {                                                          \
+  if (!(expr)) {                                               \
+    fprintf(stderr,                                            \
+            "Assertion failed in %s on line %d: `%s %s %s` "   \
+            "(%"conv" %s %"conv")\n",                          \
+            __FILE__,                                          \
+            __LINE__,                                          \
+            #a,                                                \
+            #operator,                                         \
+            #b,                                                \
+            (type)a,                                           \
+            #operator,                                         \
+            (type)b);                                          \
+    abort();                                                   \
+  }                                                            \
+ } while (0)
+
+#define ASSERT_BASE_LEN(expr, a, operator, b, conv, len)     \
+ do {                                                        \
+  if (!(expr)) {                                             \
+    fprintf(stderr,                                          \
+            "Assertion failed in %s on line %d: `%s %s %s` " \
+            "(%.*"#conv" %s %.*"#conv")\n",                  \
+            __FILE__,                                        \
+            __LINE__,                                        \
+            #a,                                              \
+            #operator,                                       \
+            #b,                                              \
+            (int)len,                                        \
+            a,                                               \
+            #operator,                                       \
+            (int)len,                                        \
+            b);                                              \
+    abort();                                                 \
+  }                                                          \
+ } while (0)
+
+#define ASSERT_BASE_HEX(expr, a, operator, b, size)            \
+ do {                                                          \
+  if (!(expr)) {                                               \
+    int i;                                                     \
+    unsigned char* a_ = (unsigned char*)a;                     \
+    unsigned char* b_ = (unsigned char*)b;                     \
+    fprintf(stderr,                                            \
+            "Assertion failed in %s on line %d: `%s %s %s` (", \
+            __FILE__,                                          \
+            __LINE__,                                          \
+            #a,                                                \
+            #operator,                                         \
+            #b);                                               \
+    for (i = 0; i < size; ++i) {                               \
+      if (i > 0) fprintf(stderr, ":");                         \
+      fprintf(stderr, "%02X", a_[i]);                          \
+    }                                                          \
+    fprintf(stderr, " %s ", #operator);                        \
+    for (i = 0; i < size; ++i) {                               \
+      if (i > 0) fprintf(stderr, ":");                         \
+      fprintf(stderr, "%02X", b_[i]);                          \
+    }                                                          \
+    fprintf(stderr, ")\n");                                    \
+    abort();                                                   \
+  }                                                            \
+ } while (0)
+
+#define ASSERT_EQ(a, b) ASSERT_BASE(a, ==, b, int64_t, PRId64)
+#define ASSERT_GE(a, b) ASSERT_BASE(a, >=, b, int64_t, PRId64)
+#define ASSERT_GT(a, b) ASSERT_BASE(a, >, b, int64_t, PRId64)
+#define ASSERT_LE(a, b) ASSERT_BASE(a, <=, b, int64_t, PRId64)
+#define ASSERT_LT(a, b) ASSERT_BASE(a, <, b, int64_t, PRId64)
+#define ASSERT_NE(a, b) ASSERT_BASE(a, !=, b, int64_t, PRId64)
+#define ASSERT_OK(a) ASSERT_BASE(a, ==, 0, int64_t, PRId64)
+
+#define ASSERT_UINT64_EQ(a, b) ASSERT_BASE(a, ==, b, uint64_t, PRIu64)
+#define ASSERT_UINT64_GE(a, b) ASSERT_BASE(a, >=, b, uint64_t, PRIu64)
+#define ASSERT_UINT64_GT(a, b) ASSERT_BASE(a, >, b, uint64_t, PRIu64)
+#define ASSERT_UINT64_LE(a, b) ASSERT_BASE(a, <=, b, uint64_t, PRIu64)
+#define ASSERT_UINT64_LT(a, b) ASSERT_BASE(a, <, b, uint64_t, PRIu64)
+#define ASSERT_UINT64_NE(a, b) ASSERT_BASE(a, !=, b, uint64_t, PRIu64)
+
+#define ASSERT_DOUBLE_EQ(a, b) ASSERT_BASE(a, ==, b, double, "f")
+#define ASSERT_DOUBLE_GE(a, b) ASSERT_BASE(a, >=, b, double, "f")
+#define ASSERT_DOUBLE_GT(a, b) ASSERT_BASE(a, >, b, double, "f")
+#define ASSERT_DOUBLE_LE(a, b) ASSERT_BASE(a, <=, b, double, "f")
+#define ASSERT_DOUBLE_LT(a, b) ASSERT_BASE(a, <, b, double, "f")
+#define ASSERT_DOUBLE_NE(a, b) ASSERT_BASE(a, !=, b, double, "f")
+
+#define ASSERT_STR_EQ(a, b) \
+  ASSERT_BASE_STR(strcmp(a, b) == 0, a, == , b, char*, "s")
+
+#define ASSERT_STR_NE(a, b) \
+  ASSERT_BASE_STR(strcmp(a, b) != 0, a, !=, b, char*, "s")
+
+#define ASSERT_MEM_EQ(a, b, size) \
+  ASSERT_BASE_LEN(memcmp(a, b, size) == 0, a, ==, b, s, size)
+
+#define ASSERT_MEM_NE(a, b, size) \
+  ASSERT_BASE_LEN(memcmp(a, b, size) != 0, a, !=, b, s, size)
+
+#define ASSERT_MEM_HEX_EQ(a, b, size) \
+  ASSERT_BASE_HEX(memcmp(a, b, size) == 0, a, ==, b, size)
+
+#define ASSERT_MEM_HEX_NE(a, b, size) \
+  ASSERT_BASE_HEX(memcmp(a, b, size) != 0, a, !=, b, size)
+
+#define ASSERT_NULL(a) \
+  ASSERT_BASE(a, ==, NULL, void*, "p")
+
+#define ASSERT_NOT_NULL(a) \
+  ASSERT_BASE(a, !=, NULL, void*, "p")
+
+#define ASSERT_PTR_EQ(a, b) \
+  ASSERT_BASE(a, ==, b, void*, "p")
+
+#define ASSERT_PTR_NE(a, b) \
+  ASSERT_BASE(a, !=, b, void*, "p")
+
+#define ASSERT_PTR_LT(a, b) \
+  ASSERT_BASE(a, <, b, void*, "p")
+
+/* This macro cleans up the event loop. This is used to avoid valgrind
+ * warnings about memory being "leaked" by the event loop.
+ */
+#define MAKE_VALGRIND_HAPPY(loop)                   \
+  do {                                              \
+    close_loop(loop);                               \
+    ASSERT_EQ(0, uv_loop_close(loop));              \
+    uv_library_shutdown();                          \
+  } while (0)
+
+/* Just sugar for wrapping the main() for a task or helper. */
+#define TEST_IMPL(name)                                                       \
+  int run_test_##name(void);                                                  \
+  int run_test_##name(void)
+
+#define BENCHMARK_IMPL(name)                                                  \
+  int run_benchmark_##name(void);                                             \
+  int run_benchmark_##name(void)
+
+#define HELPER_IMPL(name)                                                     \
+  int run_helper_##name(void);                                                \
+  int run_helper_##name(void)
+
+/* Format big numbers nicely. */
+char* fmt(char (*buf)[32], double d);
+
+/* Reserved test exit codes. */
+enum test_status {
+  TEST_OK = 0,
+  TEST_SKIP = 7
+};
+
+#define RETURN_OK()                                                           \
+  do {                                                                        \
+    return TEST_OK;                                                           \
+  } while (0)
+
+#define RETURN_SKIP(explanation)                                              \
+  do {                                                                        \
+    fprintf(stderr, "%s\n", explanation);                                     \
+    fflush(stderr);                                                           \
+    return TEST_SKIP;                                                         \
+  } while (0)
+
+#if !defined(_WIN32)
+
+# define TEST_FILE_LIMIT(num)                                                 \
+    do {                                                                      \
+      struct rlimit lim;                                                      \
+      lim.rlim_cur = (num);                                                   \
+      lim.rlim_max = lim.rlim_cur;                                            \
+      if (setrlimit(RLIMIT_NOFILE, &lim))                                     \
+        RETURN_SKIP("File descriptor limit too low.");                        \
+    } while (0)
+
+#else  /* defined(_WIN32) */
+
+# define TEST_FILE_LIMIT(num) do {} while (0)
+
+#endif
+
+#if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
+extern int snprintf(char*, size_t, const char*, ...);
+#endif
+
+#if defined(__clang__) ||                                \
+    defined(__GNUC__) ||                                 \
+    defined(__INTEL_COMPILER)
+# define UNUSED __attribute__((unused))
+#else
+# define UNUSED
+#endif
+
+#if defined(_WIN32)
+#define notify_parent_process() ((void) 0)
+#else
+extern void notify_parent_process(void);
+#endif
+
+/* Fully close a loop */
+static void close_walk_cb(uv_handle_t* handle, void* arg) {
+  if (!uv_is_closing(handle))
+    uv_close(handle, NULL);
+}
+
+UNUSED static void close_loop(uv_loop_t* loop) {
+  uv_walk(loop, close_walk_cb, NULL);
+  uv_run(loop, UV_RUN_DEFAULT);
+}
+
+UNUSED static int can_ipv6(void) {
+  uv_interface_address_t* addr;
+  int supported;
+  int count;
+  int i;
+
+  if (uv_interface_addresses(&addr, &count))
+    return 0;  /* Assume no IPv6 support on failure. */
+
+  supported = 0;
+  for (i = 0; supported == 0 && i < count; i += 1)
+    supported = (AF_INET6 == addr[i].address.address6.sin6_family);
+
+  uv_free_interface_addresses(addr, count);
+  return supported;
+}
+
+#if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
+# define NO_FS_EVENTS "Filesystem watching not supported on this platform."
+#endif
+
+#if defined(__MSYS__)
+# define NO_SEND_HANDLE_ON_PIPE \
+  "MSYS2 runtime does not support sending handles on pipes."
+#elif defined(__CYGWIN__)
+# define NO_SEND_HANDLE_ON_PIPE \
+  "Cygwin runtime does not support sending handles on pipes."
+#endif
+
+#if defined(__MSYS__)
+# define NO_SELF_CONNECT \
+  "MSYS2 runtime hangs on listen+connect in same process."
+#elif defined(__CYGWIN__)
+# define NO_SELF_CONNECT \
+  "Cygwin runtime hangs on listen+connect in same process."
+#endif
+
+#if !defined(__linux__) && \
+    !(defined(__FreeBSD__) && __FreeBSD_version >= 1301000) && \
+    !defined(_WIN32)
+# define NO_CPU_AFFINITY \
+  "affinity not supported on this platform."
+#endif
+
+#endif /* TASK_H_ */