Unverified Commit 84aad399 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4987 from shuujii/avoid-changing-directory-in-mruby-io-test

Avoid changing directory in `mruby-io` test
parents c89ca5eb aeb86d95
...@@ -110,26 +110,32 @@ assert('File.join') do ...@@ -110,26 +110,32 @@ assert('File.join') do
end end
assert('File.realpath') do assert('File.realpath') do
if File::ALT_SEPARATOR
readme_path = File._getwd + File::ALT_SEPARATOR + "README.md"
assert_equal readme_path, File.realpath("README.md")
else
dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX") dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
begin begin
dir1 = File.realpath($mrbtest_io_rfname) sep = File::ALT_SEPARATOR || File::SEPARATOR
dir2 = File.realpath("./#{dir}//./../#{$mrbtest_io_symlinkname}") relative_path = "#{File.basename(dir)}#{sep}realpath_test"
assert_equal dir1, dir2 path = "#{File._getwd}#{sep}#{relative_path}"
File.open(path, "w"){}
assert_equal path, File.realpath(relative_path)
unless MRubyIOTestUtil.win?
path1 = File.realpath($mrbtest_io_rfname)
path2 = File.realpath($mrbtest_io_symlinkname)
assert_equal path1, path2
end
ensure ensure
File.delete path rescue nil
MRubyIOTestUtil.rmdir dir MRubyIOTestUtil.rmdir dir
end end
end
assert_raise(ArgumentError) { File.realpath("TO\0DO") } assert_raise(ArgumentError) { File.realpath("TO\0DO") }
end end
assert("File.readlink") do assert("File.readlink") do
begin begin
assert_equal $mrbtest_io_rfname, File.readlink($mrbtest_io_symlinkname) exp = File.basename($mrbtest_io_rfname)
act = File.readlink($mrbtest_io_symlinkname)
assert_equal exp, act
rescue NotImplementedError => e rescue NotImplementedError => e
skip e.message skip e.message
end end
......
...@@ -54,6 +54,7 @@ mkdtemp(char *temp) ...@@ -54,6 +54,7 @@ mkdtemp(char *temp)
#include <unistd.h> #include <unistd.h>
#include <sys/un.h> #include <sys/un.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h>
#endif #endif
#include <sys/stat.h> #include <sys/stat.h>
...@@ -66,88 +67,53 @@ mkdtemp(char *temp) ...@@ -66,88 +67,53 @@ mkdtemp(char *temp)
#include "mruby/variable.h" #include "mruby/variable.h"
#include <mruby/ext/io.h> #include <mruby/ext/io.h>
int wd_save;
int socket_available_p;
#if !defined(_WIN32) && !defined(_WIN64)
static int mrb_io_socket_available()
{
int fd, retval = 0;
struct sockaddr_un sun0;
char socketname[] = "tmp.mruby-io-socket-ok.XXXXXXXX";
if (!(fd = mkstemp(socketname))) {
goto sock_test_out;
}
unlink(socketname);
close(fd);
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
goto sock_test_out;
}
sun0.sun_family = AF_UNIX;
strncpy(sun0.sun_path, socketname, sizeof(sun0.sun_path));
if (bind(fd, (struct sockaddr *)&sun0, sizeof(sun0)) == 0) {
retval = 1;
}
sock_test_out:
unlink(socketname);
close(fd);
return retval;
}
#endif
static mrb_value static mrb_value
mrb_io_test_io_setup(mrb_state *mrb, mrb_value self) mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
{ {
char rfname[] = "tmp.mruby-io-test-r.XXXXXXXX"; #define GVNAME(n) "$mrbtest_io_" #n "name"
char wfname[] = "tmp.mruby-io-test-w.XXXXXXXX"; enum {IDX_READ, IDX_WRITE, IDX_LINK, IDX_SOCKET, IDX_COUNT};
char symlinkname[] = "tmp.mruby-io-test-l.XXXXXXXX"; const char *gvnames[] = {GVNAME(rf), GVNAME(wf), GVNAME(symlink), GVNAME(socket)};
char socketname[] = "tmp.mruby-io-test-s.XXXXXXXX"; char *fnames[IDX_COUNT];
int fds[IDX_COUNT];
char msg[] = "mruby io test\n"; char msg[] = "mruby io test\n";
mode_t mask; mode_t mask;
int fd0, fd1;
FILE *fp; FILE *fp;
int i;
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
int fd2, fd3;
struct sockaddr_un sun0; struct sockaddr_un sun0;
if(!(socket_available_p = mrb_io_socket_available())) {
char *tmpdir;
wd_save = open(".", O_DIRECTORY);
tmpdir = getenv("TMPDIR");
if (tmpdir) chdir(tmpdir);
else chdir("/tmp");
}
#endif #endif
mask = umask(077); mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_str_new_cstr(mrb, msg));
fd0 = mkstemp(rfname);
fd1 = mkstemp(wfname);
if (fd0 == -1 || fd1 == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't create temporary file");
return mrb_nil_value();
}
close(fd0);
close(fd1);
mask = umask(077);
for (i = 0; i < IDX_COUNT; i++) {
mrb_value fname = mrb_str_new_capa(mrb, 0);
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
fd2 = mkstemp(symlinkname); /*
fd3 = mkstemp(socketname); * Workaround for not being able to bind a socket to some file systems
if (fd2 == -1 || fd3 == -1) { * (e.g. vboxsf, NFS). [#4981]
mrb_raise(mrb, E_RUNTIME_ERROR, "can't create temporary file"); */
return mrb_nil_value(); char *tmpdir = getenv("TMPDIR");
if (tmpdir && strlen(tmpdir) > 0) {
mrb_str_cat_cstr(mrb, fname, tmpdir);
if (*(RSTRING_END(fname)-1) != '/') mrb_str_cat_lit(mrb, fname, "/");
} else {
mrb_str_cat_lit(mrb, fname, "/tmp/");
} }
#endif #endif
mrb_str_cat_cstr(mrb, fname, gvnames[i]+1);
mrb_str_cat_cstr(mrb, fname, ".XXXXXXXX");
fnames[i] = RSTRING_PTR(fname);
fds[i] = mkstemp(fnames[i]);
if (fds[i] == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't create temporary file");
}
close(fds[i]);
mrb_gv_set(mrb, mrb_intern_cstr(mrb, gvnames[i]), fname);
}
umask(mask); umask(mask);
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"), mrb_str_new_cstr(mrb, rfname)); fp = fopen(fnames[IDX_READ], "wb");
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"), mrb_str_new_cstr(mrb, wfname));
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"), mrb_str_new_cstr(mrb, symlinkname));
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_str_new_cstr(mrb, socketname));
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_str_new_cstr(mrb, msg));
fp = fopen(rfname, "wb");
if (fp == NULL) { if (fp == NULL) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file"); mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
return mrb_nil_value(); return mrb_nil_value();
...@@ -155,7 +121,7 @@ mrb_io_test_io_setup(mrb_state *mrb, mrb_value self) ...@@ -155,7 +121,7 @@ mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
fputs(msg, fp); fputs(msg, fp);
fclose(fp); fclose(fp);
fp = fopen(wfname, "wb"); fp = fopen(fnames[IDX_WRITE], "wb");
if (fp == NULL) { if (fp == NULL) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file"); mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
return mrb_nil_value(); return mrb_nil_value();
...@@ -163,29 +129,29 @@ mrb_io_test_io_setup(mrb_state *mrb, mrb_value self) ...@@ -163,29 +129,29 @@ mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
fclose(fp); fclose(fp);
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
unlink(symlinkname); unlink(fnames[IDX_LINK]);
close(fd2); if (symlink(basename(fnames[IDX_READ]), fnames[IDX_LINK]) == -1) {
if (symlink(rfname, symlinkname) == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link"); mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link");
} }
unlink(socketname); unlink(fnames[IDX_SOCKET]);
close(fd3); fds[IDX_SOCKET] = socket(AF_UNIX, SOCK_STREAM, 0);
fd3 = socket(AF_UNIX, SOCK_STREAM, 0); if (fds[IDX_SOCKET] == -1) {
if (fd3 == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a socket"); mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a socket");
} }
sun0.sun_family = AF_UNIX; sun0.sun_family = AF_UNIX;
strncpy(sun0.sun_path, socketname, sizeof(sun0.sun_path)); strncpy(sun0.sun_path, fnames[IDX_SOCKET], sizeof(sun0.sun_path)-1);
if (bind(fd3, (struct sockaddr *)&sun0, sizeof(sun0)) == -1) { sun0.sun_path[sizeof(sun0.sun_path)-1] = 0;
if (bind(fds[IDX_SOCKET], (struct sockaddr *)&sun0, sizeof(sun0)) == -1) {
mrb_raisef(mrb, E_RUNTIME_ERROR, "can't bind AF_UNIX socket to %s: %d", mrb_raisef(mrb, E_RUNTIME_ERROR, "can't bind AF_UNIX socket to %s: %d",
sun0.sun_path, sun0.sun_path,
errno); errno);
} }
close(fd3); close(fds[IDX_SOCKET]);
#endif #endif
return mrb_true_value(); return mrb_true_value();
#undef GVNAME
} }
static mrb_value static mrb_value
...@@ -215,13 +181,6 @@ mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self) ...@@ -215,13 +181,6 @@ mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self)
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_nil_value()); mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_nil_value());
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_nil_value()); mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_nil_value());
#if !defined(_WIN32) && !defined(_WIN64)
if(!socket_available_p) {
fchdir(wd_save);
close(wd_save);
}
#endif
return mrb_nil_value(); return mrb_nil_value();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment