test: add tests for bad inputs

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: https://github.com/nodejs/node/pull/18811
PR-URL: https://github.com/libuv/libuv/pull/1747
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Joyee Cheung 2018-02-21 09:53:57 -05:00 committed by cjihrig
parent dab311afe9
commit e485d28674
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 38 additions and 1 deletions

View File

@ -36,6 +36,10 @@ static const char dst[] = "test_file_dst";
static int result_check_count; 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) { static void handle_result(uv_fs_t* req) {
uv_fs_t stat_req; uv_fs_t stat_req;
uint64_t size; uint64_t size;
@ -158,7 +162,12 @@ TEST_IMPL(fs_copyfile) {
ASSERT(result_check_count == 5); ASSERT(result_check_count == 5);
uv_run(loop, UV_RUN_DEFAULT); uv_run(loop, UV_RUN_DEFAULT);
ASSERT(result_check_count == 6); 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; return 0;
} }

View File

@ -319,6 +319,9 @@ static void ftruncate_cb(uv_fs_t* req) {
ASSERT(r == 0); 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) { static void read_cb(uv_fs_t* req) {
int r; int r;
@ -2897,6 +2900,31 @@ TEST_IMPL(fs_read_write_null_arguments) {
ASSERT(r == UV_EINVAL); ASSERT(r == UV_EINVAL);
uv_fs_req_cleanup(&write_req); 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; return 0;
} }