XS-libuv

 view release on metacpan or  search on metacpan

libuv-1.49.2/test/test-fs.c  view on Meta::CPAN

#ifndef _WIN32
# include <unistd.h> /* unlink, rmdir, etc. */
#else
# include <winioctl.h>
# include <direct.h>
# include <io.h>
# ifndef ERROR_SYMLINK_NOT_SUPPORTED
#  define ERROR_SYMLINK_NOT_SUPPORTED 1464
# endif
# ifndef S_IFIFO
#  define S_IFIFO _S_IFIFO
# endif
# define unlink _unlink
# define rmdir _rmdir
# define open _open
# define write _write
# define close _close
# ifndef stat
#  define stat _stati64
# endif
# ifndef lseek
#   define lseek _lseek
# endif
# define S_IFDIR _S_IFDIR
# define S_IFCHR _S_IFCHR
# define S_IFREG _S_IFREG
#endif

#define TOO_LONG_NAME_LENGTH 65536
#define PATHMAX 4096

typedef struct {
  const char* path;
  double atime;
  double mtime;
} utime_check_t;


static int dummy_cb_count;
static int close_cb_count;
static int create_cb_count;
static int open_cb_count;
static int read_cb_count;
static int write_cb_count;
static int unlink_cb_count;
static int mkdir_cb_count;
static int mkdtemp_cb_count;
static int mkstemp_cb_count;
static int rmdir_cb_count;
static int scandir_cb_count;
static int stat_cb_count;
static int rename_cb_count;
static int fsync_cb_count;
static int fdatasync_cb_count;
static int ftruncate_cb_count;
static int sendfile_cb_count;
static int fstat_cb_count;
static int access_cb_count;
static int chmod_cb_count;
static int fchmod_cb_count;
static int chown_cb_count;
static int fchown_cb_count;
static int lchown_cb_count;
static int link_cb_count;
static int symlink_cb_count;
static int readlink_cb_count;
static int realpath_cb_count;
static int utime_cb_count;
static int futime_cb_count;
static int lutime_cb_count;
static int statfs_cb_count;

static uv_loop_t* loop;

static uv_fs_t open_req1;
static uv_fs_t open_req2;
static uv_fs_t open_req_noclose;
static uv_fs_t read_req;
static uv_fs_t write_req;
static uv_fs_t unlink_req;
static uv_fs_t close_req;
static uv_fs_t mkdir_req;
static uv_fs_t mkdtemp_req1;
static uv_fs_t mkdtemp_req2;
static uv_fs_t mkstemp_req1;
static uv_fs_t mkstemp_req2;
static uv_fs_t mkstemp_req3;
static uv_fs_t rmdir_req;
static uv_fs_t scandir_req;
static uv_fs_t stat_req;
static uv_fs_t rename_req;
static uv_fs_t fsync_req;
static uv_fs_t fdatasync_req;
static uv_fs_t ftruncate_req;
static uv_fs_t sendfile_req;
static uv_fs_t utime_req;
static uv_fs_t futime_req;

static char buf[32];
static char buf2[32];
static char test_buf[] = "test-buffer\n";
static char test_buf2[] = "second-buffer\n";
static uv_buf_t iov;

#ifdef _WIN32
int uv_test_getiovmax(void) {
  return INT32_MAX; /* Emulated by libuv, so no real limit. */
}
#else
int uv_test_getiovmax(void) {
#if defined(IOV_MAX)
  return IOV_MAX;
#elif defined(_SC_IOV_MAX)
  static int iovmax = -1;
  if (iovmax == -1) {
    iovmax = sysconf(_SC_IOV_MAX);
    /* On some embedded devices (arm-linux-uclibc based ip camera),
     * sysconf(_SC_IOV_MAX) can not get the correct value. The return
     * value is -1 and the errno is EINPROGRESS. Degrade the value to 1.
     */
    if (iovmax == -1) iovmax = 1;
  }
  return iovmax;

libuv-1.49.2/test/test-fs.c  view on Meta::CPAN

static void symlink_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_SYMLINK);
  ASSERT_OK(req->result);
  symlink_cb_count++;
  uv_fs_req_cleanup(req);
}

static void readlink_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_READLINK);
  ASSERT_OK(req->result);
  ASSERT_OK(strcmp(req->ptr, "test_file_symlink2"));
  readlink_cb_count++;
  uv_fs_req_cleanup(req);
}


static void realpath_cb(uv_fs_t* req) {
  char test_file_abs_buf[PATHMAX];
  size_t test_file_abs_size = sizeof(test_file_abs_buf);
  ASSERT_EQ(req->fs_type, UV_FS_REALPATH);
  ASSERT_OK(req->result);

  uv_cwd(test_file_abs_buf, &test_file_abs_size);
#ifdef _WIN32
  strcat(test_file_abs_buf, "\\test_file");
  ASSERT_OK(_stricmp(req->ptr, test_file_abs_buf));
#else
  strcat(test_file_abs_buf, "/test_file");
  ASSERT_OK(strcmp(req->ptr, test_file_abs_buf));
#endif
  realpath_cb_count++;
  uv_fs_req_cleanup(req);
}


static void access_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_ACCESS);
  access_cb_count++;
  uv_fs_req_cleanup(req);
}


static void fchmod_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_FCHMOD);
  ASSERT_OK(req->result);
  fchmod_cb_count++;
  uv_fs_req_cleanup(req);
  check_permission("test_file", *(int*)req->data);
}


static void chmod_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_CHMOD);
  ASSERT_OK(req->result);
  chmod_cb_count++;
  uv_fs_req_cleanup(req);
  check_permission("test_file", *(int*)req->data);
}


static void fchown_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_FCHOWN);
  ASSERT_OK(req->result);
  fchown_cb_count++;
  uv_fs_req_cleanup(req);
}


static void chown_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_CHOWN);
  ASSERT_OK(req->result);
  chown_cb_count++;
  uv_fs_req_cleanup(req);
}

static void lchown_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_LCHOWN);
  ASSERT_OK(req->result);
  lchown_cb_count++;
  uv_fs_req_cleanup(req);
}

static void chown_root_cb(uv_fs_t* req) {
  ASSERT_EQ(req->fs_type, UV_FS_CHOWN);
#if defined(_WIN32) || defined(__MSYS__)
  /* On windows, chown is a no-op and always succeeds. */
  ASSERT_OK(req->result);
#else
  /* On unix, chown'ing the root directory is not allowed -
   * unless you're root, of course.
   */
  if (geteuid() == 0)
    ASSERT_OK(req->result);
  else
#   if defined(__CYGWIN__)
    /* On Cygwin, uid 0 is invalid (no root). */
    ASSERT_EQ(req->result, UV_EINVAL);
#   elif defined(__PASE__)
    /* On IBMi PASE, there is no root user. uid 0 is user qsecofr.
     * User may grant qsecofr's privileges, including changing
     * the file's ownership to uid 0.
     */
    ASSERT(req->result == 0 || req->result == UV_EPERM);
#   else
    ASSERT_EQ(req->result, UV_EPERM);
#   endif
#endif
  chown_cb_count++;
  uv_fs_req_cleanup(req);
}

static void unlink_cb(uv_fs_t* req) {
  ASSERT_PTR_EQ(req, &unlink_req);
  ASSERT_EQ(req->fs_type, UV_FS_UNLINK);
  ASSERT_OK(req->result);
  unlink_cb_count++;
  uv_fs_req_cleanup(req);
}

static void fstat_cb(uv_fs_t* req) {
  uv_stat_t* s = req->ptr;
  ASSERT_EQ(req->fs_type, UV_FS_FSTAT);
  ASSERT_OK(req->result);
  ASSERT_EQ(s->st_size, sizeof(test_buf));
  uv_fs_req_cleanup(req);
  fstat_cb_count++;
}


static void statfs_cb(uv_fs_t* req) {
  uv_statfs_t* stats;

  ASSERT_EQ(req->fs_type, UV_FS_STATFS);
  ASSERT_OK(req->result);
  ASSERT_NOT_NULL(req->ptr);
  stats = req->ptr;

#if defined(_WIN32) || defined(__sun) || defined(_AIX) || defined(__MVS__) || \
  defined(__OpenBSD__) || defined(__NetBSD__)
  ASSERT_OK(stats->f_type);
#else
  ASSERT_UINT64_GT(stats->f_type, 0);
#endif

  ASSERT_GT(stats->f_bsize, 0);
  ASSERT_GT(stats->f_blocks, 0);
  ASSERT_LE(stats->f_bfree, stats->f_blocks);
  ASSERT_LE(stats->f_bavail, stats->f_bfree);

#ifdef _WIN32
  ASSERT_OK(stats->f_files);
  ASSERT_OK(stats->f_ffree);
#else
  /* There is no assertion for stats->f_files that makes sense, so ignore it. */
  ASSERT_LE(stats->f_ffree, stats->f_files);
#endif
  uv_fs_req_cleanup(req);
  ASSERT_NULL(req->ptr);
  statfs_cb_count++;
}


static void close_cb(uv_fs_t* req) {
  int r;
  ASSERT_PTR_EQ(req, &close_req);
  ASSERT_EQ(req->fs_type, UV_FS_CLOSE);
  ASSERT_OK(req->result);
  close_cb_count++;

libuv-1.49.2/test/test-fs.c  view on Meta::CPAN

  return 0;
}

#ifdef _WIN32
TEST_IMPL(fs_unlink_archive_readonly) {
  int r;
  uv_fs_t req;
  uv_file file;

  /* Setup. */
  unlink("test_file");

  loop = uv_default_loop();

  r = uv_fs_open(NULL,
                 &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
                 S_IWUSR | S_IRUSR,
                 NULL);
  ASSERT_GE(r, 0);
  ASSERT_GE(req.result, 0);
  file = req.result;
  uv_fs_req_cleanup(&req);

  iov = uv_buf_init(test_buf, sizeof(test_buf));
  r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  ASSERT_EQ(r, sizeof(test_buf));
  ASSERT_EQ(req.result, sizeof(test_buf));
  uv_fs_req_cleanup(&req);

  uv_fs_close(loop, &req, file, NULL);

  /* Make the file read-only and clear archive flag */
  r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
  ASSERT(r);
  uv_fs_req_cleanup(&req);

  check_permission("test_file", 0400);

  /* Try to unlink the file */
  r = uv_fs_unlink(NULL, &req, "test_file", NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /*
  * Run the loop just to check we don't have make any extraneous uv_ref()
  * calls. This should drop out immediately.
  */
  uv_run(loop, UV_RUN_DEFAULT);

  /* Cleanup. */
  uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
  uv_fs_req_cleanup(&req);
  unlink("test_file");

  MAKE_VALGRIND_HAPPY(loop);
  return 0;
}
#endif

TEST_IMPL(fs_chown) {
  int r;
  uv_fs_t req;
  uv_file file;

  /* Setup. */
  unlink("test_file");
  unlink("test_file_link");

  loop = uv_default_loop();

  r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
      S_IWUSR | S_IRUSR, NULL);
  ASSERT_GE(r, 0);
  ASSERT_GE(req.result, 0);
  file = req.result;
  uv_fs_req_cleanup(&req);

  /* sync chown */
  r = uv_fs_chown(NULL, &req, "test_file", -1, -1, NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /* sync fchown */
  r = uv_fs_fchown(NULL, &req, file, -1, -1, NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /* async chown */
  r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
  ASSERT_OK(r);
  uv_run(loop, UV_RUN_DEFAULT);
  ASSERT_EQ(1, chown_cb_count);

#ifndef __MVS__
  /* chown to root (fail) */
  chown_cb_count = 0;
  r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
  ASSERT_OK(r);
  uv_run(loop, UV_RUN_DEFAULT);
  ASSERT_EQ(1, chown_cb_count);
#endif

  /* async fchown */
  r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
  ASSERT_OK(r);
  uv_run(loop, UV_RUN_DEFAULT);
  ASSERT_EQ(1, fchown_cb_count);

#ifndef __HAIKU__
  /* Haiku doesn't support hardlink */
  /* sync link */
  r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /* sync lchown */
  r = uv_fs_lchown(NULL, &req, "test_file_link", -1, -1, NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /* async lchown */
  r = uv_fs_lchown(loop, &req, "test_file_link", -1, -1, lchown_cb);
  ASSERT_OK(r);
  uv_run(loop, UV_RUN_DEFAULT);
  ASSERT_EQ(1, lchown_cb_count);
#endif

  /* Close file */
  r = uv_fs_close(NULL, &req, file, NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  /*
   * Run the loop just to check we don't have make any extraneous uv_ref()
   * calls. This should drop out immediately.
   */
  uv_run(loop, UV_RUN_DEFAULT);

  /* Cleanup. */
  unlink("test_file");
  unlink("test_file_link");

  MAKE_VALGRIND_HAPPY(loop);
  return 0;
}


TEST_IMPL(fs_link) {
  int r;
  uv_fs_t req;
  uv_file file;
  uv_file link;

  /* Setup. */
  unlink("test_file");
  unlink("test_file_link");
  unlink("test_file_link2");

  loop = uv_default_loop();

  r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
      S_IWUSR | S_IRUSR, NULL);
  ASSERT_GE(r, 0);
  ASSERT_GE(req.result, 0);
  file = req.result;
  uv_fs_req_cleanup(&req);

  iov = uv_buf_init(test_buf, sizeof(test_buf));
  r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  ASSERT_EQ(r, sizeof(test_buf));
  ASSERT_EQ(req.result, sizeof(test_buf));
  uv_fs_req_cleanup(&req);

  uv_fs_close(loop, &req, file, NULL);

  /* sync link */
  r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
  ASSERT_OK(r);
  ASSERT_OK(req.result);
  uv_fs_req_cleanup(&req);

  r = uv_fs_open(NULL, &req, "test_file_link", UV_FS_O_RDWR, 0, NULL);
  ASSERT_GE(r, 0);
  ASSERT_GE(req.result, 0);

libuv-1.49.2/test/test-fs.c  view on Meta::CPAN

  ASSERT_EQ(buf[0], 'e');
  uv_fs_req_cleanup(&read_req);

  fs_file_pos_close_check("abcde", 5);
}
TEST_IMPL(fs_file_pos_append) {
  fs_file_pos_append(0);
  fs_file_pos_append(UV_FS_O_FILEMAP);

  MAKE_VALGRIND_HAPPY(uv_default_loop());
  return 0;
}
#endif

TEST_IMPL(fs_null_req) {
  /* Verify that all fs functions return UV_EINVAL when the request is NULL. */
  int r;

  r = uv_fs_open(NULL, NULL, NULL, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_close(NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_write(NULL, NULL, 0, NULL, 0, -1, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_unlink(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_mkdir(NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_mkdtemp(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_mkstemp(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_rmdir(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_scandir(NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_link(NULL, NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_symlink(NULL, NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_readlink(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_realpath(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_chown(NULL, NULL, NULL, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_fchown(NULL, NULL, 0, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_stat(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_lstat(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_fstat(NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_rename(NULL, NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_fsync(NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_fdatasync(NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_ftruncate(NULL, NULL, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_copyfile(NULL, NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_sendfile(NULL, NULL, 0, 0, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_access(NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_chmod(NULL, NULL, NULL, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_fchmod(NULL, NULL, 0, 0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_utime(NULL, NULL, NULL, 0.0, 0.0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_futime(NULL, NULL, 0, 0.0, 0.0, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  r = uv_fs_statfs(NULL, NULL, NULL, NULL);
  ASSERT_EQ(r, UV_EINVAL);

  /* This should be a no-op. */
  uv_fs_req_cleanup(NULL);

  return 0;
}

#ifdef _WIN32
TEST_IMPL(fs_exclusive_sharing_mode) {
  int r;

  /* Setup. */
  unlink("test_file");



( run in 0.848 second using v1.01-cache-2.11-cpan-5511b514fd6 )