Blame test/test-fs-copyfile.c

Packit b5b901
/* Copyright libuv project contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
#if defined(__unix__) || defined(__POSIX__) || \
Packit Service e08953
    defined(__APPLE__) || defined(__sun) || \
Packit Service e08953
    defined(_AIX) || defined(__MVS__) || \
Packit Service e08953
    defined(__HAIKU__)
Packit b5b901
#include <unistd.h> /* unlink, etc. */
Packit b5b901
#else
Packit b5b901
# include <direct.h>
Packit b5b901
# include <io.h>
Packit b5b901
# define unlink _unlink
Packit b5b901
#endif
Packit b5b901
Packit b5b901
static const char fixture[] = "test/fixtures/load_error.node";
Packit b5b901
static const char dst[] = "test_file_dst";
Packit b5b901
static int result_check_count;
Packit b5b901
Packit b5b901
Packit b5b901
static void fail_cb(uv_fs_t* req) {
Packit b5b901
  FATAL("fail_cb should not have been called");
Packit b5b901
}
Packit b5b901
Packit b5b901
static void handle_result(uv_fs_t* req) {
Packit b5b901
  uv_fs_t stat_req;
Packit b5b901
  uint64_t size;
Packit b5b901
  uint64_t mode;
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  ASSERT(req->fs_type == UV_FS_COPYFILE);
Packit b5b901
  ASSERT(req->result == 0);
Packit b5b901
Packit b5b901
  /* Verify that the file size and mode are the same. */
Packit b5b901
  r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  size = stat_req.statbuf.st_size;
Packit b5b901
  mode = stat_req.statbuf.st_mode;
Packit b5b901
  uv_fs_req_cleanup(&stat_req);
Packit b5b901
  r = uv_fs_stat(NULL, &stat_req, dst, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  ASSERT(stat_req.statbuf.st_size == size);
Packit b5b901
  ASSERT(stat_req.statbuf.st_mode == mode);
Packit b5b901
  uv_fs_req_cleanup(&stat_req);
Packit b5b901
  uv_fs_req_cleanup(req);
Packit b5b901
  result_check_count++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void touch_file(const char* name, unsigned int size) {
Packit b5b901
  uv_file file;
Packit b5b901
  uv_fs_t req;
Packit b5b901
  uv_buf_t buf;
Packit b5b901
  int r;
Packit b5b901
  unsigned int i;
Packit b5b901
Packit b5b901
  r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC,
Packit b5b901
                 S_IWUSR | S_IRUSR, NULL);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
  ASSERT(r >= 0);
Packit b5b901
  file = r;
Packit b5b901
Packit b5b901
  buf = uv_buf_init("a", 1);
Packit b5b901
Packit b5b901
  /* Inefficient but simple. */
Packit b5b901
  for (i = 0; i < size; i++) {
Packit b5b901
    r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL);
Packit b5b901
    uv_fs_req_cleanup(&req;;
Packit b5b901
    ASSERT(r >= 0);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  r = uv_fs_close(NULL, &req, file, NULL);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
TEST_IMPL(fs_copyfile) {
Packit b5b901
  const char src[] = "test_file_src";
Packit b5b901
  uv_loop_t* loop;
Packit b5b901
  uv_fs_t req;
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  loop = uv_default_loop();
Packit b5b901
Packit b5b901
  /* Fails with EINVAL if bad flags are passed. */
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL);
Packit b5b901
  ASSERT(r == UV_EINVAL);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
Packit b5b901
  /* Fails with ENOENT if source does not exist. */
Packit b5b901
  unlink(src);
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
Packit b5b901
  ASSERT(req.result == UV_ENOENT);
Packit b5b901
  ASSERT(r == UV_ENOENT);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
  /* The destination should not exist. */
Packit b5b901
  r = uv_fs_stat(NULL, &req, dst, NULL);
Packit b5b901
  ASSERT(r != 0);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
Packit Service e08953
  /* Succeeds if src and dst files are identical. */
Packit Service e08953
  touch_file(src, 12);
Packit Service e08953
  r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL);
Packit Service e08953
  ASSERT(r == 0);
Packit Service e08953
  uv_fs_req_cleanup(&req;;
Packit Service e08953
  unlink(src);
Packit Service e08953
Packit b5b901
  /* Copies file synchronously. Creates new file. */
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
Packit b5b901
  /* Copies a file of size zero. */
Packit b5b901
  unlink(dst);
Packit b5b901
  touch_file(src, 0);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
Packit b5b901
  /* Copies file synchronously. Overwrites existing file. */
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
Packit b5b901
  /* Fails to overwrites existing file. */
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL);
Packit b5b901
  ASSERT(r == UV_EEXIST);
Packit b5b901
  uv_fs_req_cleanup(&req;;
Packit b5b901
Packit b5b901
  /* Truncates when an existing destination is larger than the source file. */
Packit b5b901
  touch_file(src, 1);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
Packit b5b901
  /* Copies a larger file. */
Packit b5b901
  unlink(dst);
Packit b5b901
  touch_file(src, 4096 * 2);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
  unlink(src);
Packit b5b901
Packit b5b901
  /* Copies file asynchronously */
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  ASSERT(result_check_count == 5);
Packit b5b901
  uv_run(loop, UV_RUN_DEFAULT);
Packit b5b901
  ASSERT(result_check_count == 6);
Packit b5b901
Packit b5b901
  /* If the flags are invalid, the loop should not be kept open */
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb);
Packit b5b901
  ASSERT(r == UV_EINVAL);
Packit b5b901
  uv_run(loop, UV_RUN_DEFAULT);
Packit b5b901
Packit b5b901
  /* Copies file using UV_FS_COPYFILE_FICLONE. */
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  handle_result(&req;;
Packit b5b901
Packit b5b901
  /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */
Packit b5b901
  unlink(dst);
Packit b5b901
  r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE,
Packit b5b901
                     NULL);
Packit Service e08953
  ASSERT(r <= 0);
Packit b5b901
Packit b5b901
  if (r == 0)
Packit b5b901
    handle_result(&req;;
Packit b5b901
Packit Service e08953
#ifndef _WIN32
Packit Service e08953
  /* Copying respects permissions/mode. */
Packit Service e08953
  unlink(dst);
Packit Service e08953
  touch_file(dst, 0);
Packit Service e08953
  chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */
Packit Service e08953
  r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL);
Packit Service e08953
  /* On IBMi PASE, qsecofr users can overwrite read-only files */
Packit Service e08953
# ifndef __PASE__
Packit Service e08953
  ASSERT(req.result == UV_EACCES);
Packit Service e08953
  ASSERT(r == UV_EACCES);
Packit Service e08953
# endif
Packit Service e08953
  uv_fs_req_cleanup(&req;;
Packit Service e08953
#endif
Packit Service e08953
Packit b5b901
  unlink(dst); /* Cleanup */
Packit b5b901
  return 0;
Packit b5b901
}