From: Brian Aker Date: Fri, 3 May 2013 20:00:36 +0000 (-0400) Subject: Fixes to test memcp X-Git-Tag: 1.0.18~24^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=da8d9551f1c0fd5e9381357f6253cb125f336c68;p=m6w6%2Flibmemcached Fixes to test memcp --- diff --git a/clients/memcp.cc b/clients/memcp.cc index 7986e248..7aa805f9 100644 --- a/clients/memcp.cc +++ b/clients/memcp.cc @@ -194,7 +194,13 @@ int main(int argc, char *argv[]) } struct stat sbuf; - (void)fstat(fd, &sbuf); + if (fstat(fd, &sbuf) == -1) + { + std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl; + optind++; + exit_code= EXIT_FAILURE; + continue; + } char *ptr= rindex(argv[optind], '/'); if (ptr) @@ -215,27 +221,33 @@ int main(int argc, char *argv[]) ptr, opt_flags, (unsigned long)opt_expires); } - char *file_buffer_ptr; - if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL) + // The file may be empty + char *file_buffer_ptr= NULL; + if (sbuf.st_size > 0) { - std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl; - close(fd); - exit(EXIT_FAILURE); - } + if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL) + { + std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl; + close(fd); + exit(EXIT_FAILURE); + } - ssize_t read_length; - if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) - { - std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl; - close(fd); - exit(EXIT_FAILURE); - } + ssize_t read_length; + if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) + { + std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl; + close(fd); + free(file_buffer_ptr); + exit(EXIT_FAILURE); + } - if (read_length != sbuf.st_size) - { - std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl; - close(fd); - exit(EXIT_FAILURE); + if (read_length != sbuf.st_size) + { + std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl; + close(fd); + free(file_buffer_ptr); + exit(EXIT_FAILURE); + } } memcached_return_t rc; @@ -261,7 +273,6 @@ int main(int argc, char *argv[]) if (memcached_failed(rc)) { std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl; - ::close(fd); exit_code= EXIT_FAILURE; } diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index 3ecf63df..94c41814 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -409,7 +409,7 @@ bool Application::slurp() Application::error_t Application::join() { - pid_t waited_pid= waitpid(_pid, &_status, 0); + pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED); slurp(); if (waited_pid == _pid and WIFEXITED(_status) == false) { diff --git a/libtest/tmpfile.cc b/libtest/tmpfile.cc index a88c0ede..425a21e7 100644 --- a/libtest/tmpfile.cc +++ b/libtest/tmpfile.cc @@ -40,7 +40,7 @@ namespace libtest { -std::string create_tmpfile(const std::string& name) +std::string create_tmpfile(const std::string& name, int& fd) { libtest::vchar_t file_buffer; file_buffer.resize(FILENAME_MAX); @@ -49,15 +49,22 @@ std::string create_tmpfile(const std::string& name) int length= snprintf(&file_buffer[0], file_buffer.size(), "var/tmp/%s.XXXXXX", name.c_str()); fatal_assert(length > 0); - int fd; if ((fd= mkstemp(&file_buffer[0])) == -1) { throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno)); } - close(fd); - unlink(&file_buffer[0]); return &file_buffer[0]; } +std::string create_tmpfile(const std::string& name) +{ + int fd; + std::string ret_file= create_tmpfile(name, fd); + close(fd); + unlink(ret_file.c_str()); + + return ret_file.c_str(); +} + } // namespace libtest diff --git a/libtest/tmpfile.hpp b/libtest/tmpfile.hpp index 0eb8dca2..9283b84a 100644 --- a/libtest/tmpfile.hpp +++ b/libtest/tmpfile.hpp @@ -40,6 +40,7 @@ namespace libtest { +std::string create_tmpfile(const std::string&, int&); std::string create_tmpfile(const std::string&); } // namespace libtest diff --git a/tests/cli.am b/tests/cli.am index 573be2af..df206e5c 100644 --- a/tests/cli.am +++ b/tests/cli.am @@ -99,6 +99,12 @@ tests_memdump_LDADD= libtest/libtest.la $(TESTS_LDADDS) check_PROGRAMS+= tests/memdump noinst_PROGRAMS+= tests/memdump +test-memcp: tests/memcp + tests/memcp + +gdb-memcp: tests/memcp + @$(GDB_COMMAND) tests/memcp + test-memstat: tests/memstat tests/memstat diff --git a/tests/memcp.cc b/tests/memcp.cc index 440f1b1d..7fca2d36 100644 --- a/tests/memcp.cc +++ b/tests/memcp.cc @@ -44,6 +44,8 @@ #include #include +#include + using namespace libtest; #ifndef __INTEL_COMPILER @@ -63,11 +65,20 @@ static test_return_t help_test(void *) static test_return_t server_test(void *) { + int fd; + std::string tmp_file= create_tmpfile("memcp", fd); + ASSERT_TRUE(tmp_file.c_str()); + struct stat buf; + ASSERT_EQ(fstat(fd, &buf), 0); + ASSERT_EQ(buf.st_size, 0); + char buffer[1024]; snprintf(buffer, sizeof(buffer), "--servers=localhost:%d", int(default_port())); - const char *args[]= { buffer, 0 }; + const char *args[]= { buffer, tmp_file.c_str(), 0 }; test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true)); + close(fd); + unlink(tmp_file.c_str()); return TEST_SUCCESS; }