Skip to content

Commit

Permalink
test: add tests for bad inputs
Browse files Browse the repository at this point in the history
This commit adds tests that pass bad options to uv_fs_copyfile(),
uv_fs_read(), and uv_fs_write(). These tests verify that the
asynchronous version of these functions do not hold the event
loop open on bad inputs.

Refs: nodejs/node#18811
PR-URL: libuv#1747
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
joyeecheung authored and cjihrig committed Feb 21, 2018
1 parent dab311a commit e485d28
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion test/test-fs-copyfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ static const char dst[] = "test_file_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;
Expand Down Expand Up @@ -158,7 +162,12 @@ TEST_IMPL(fs_copyfile) {
ASSERT(result_check_count == 5);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT(result_check_count == 6);
unlink(dst); /* Cleanup */

/* 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);
unlink(dst); /* Cleanup */
return 0;
}
28 changes: 28 additions & 0 deletions test/test-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ static void ftruncate_cb(uv_fs_t* req) {
ASSERT(r == 0);
}

static void fail_cb(uv_fs_t* req) {
FATAL("fail_cb should not have been called");
}

static void read_cb(uv_fs_t* req) {
int r;
Expand Down Expand Up @@ -2897,6 +2900,31 @@ TEST_IMPL(fs_read_write_null_arguments) {
ASSERT(r == UV_EINVAL);
uv_fs_req_cleanup(&write_req);

/* If the arguments are invalid, the loop should not be kept open */
loop = uv_default_loop();

r = uv_fs_read(loop, &read_req, 0, NULL, 0, -1, fail_cb);
ASSERT(r == UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&read_req);

r = uv_fs_write(loop, &write_req, 0, NULL, 0, -1, fail_cb);
ASSERT(r == UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&write_req);

iov = uv_buf_init(NULL, 0);
r = uv_fs_read(loop, &read_req, 0, &iov, 0, -1, fail_cb);
ASSERT(r == UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&read_req);

iov = uv_buf_init(NULL, 0);
r = uv_fs_write(loop, &write_req, 0, &iov, 0, -1, fail_cb);
ASSERT(r == UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&write_req);

return 0;
}

Expand Down

0 comments on commit e485d28

Please sign in to comment.