]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/proflogger: Add TeardownSetup and SimpleTest
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 18 Jul 2011 22:01:28 +0000 (15:01 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 18 Jul 2011 22:02:21 +0000 (15:02 -0700)
common/ProfLogger.cc: check the sock_path length.

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/ProfLogger.cc
src/test/proflogger.cc

index 12127787990ad02e1cb3407b67faff2c43a6befb..8e3c1b23bf2eef3a759f73e806f449de4f8d51ec 100644 (file)
@@ -71,6 +71,14 @@ public:
 
   static std::string bind_and_listen(const std::string &sock_path, int *fd)
   {
+    if (sock_path.size() > sizeof(sockaddr_un::sun_path) - 1) {
+      ostringstream oss;
+      oss << "ProfLogThread::bind_and_listen: "
+         << "The UNIX domain socket path " << sock_path << " is too long! The "
+         << "maximum length on this system is "
+         << (sizeof(sockaddr_un::sun_path) - 1);
+      return oss.str();
+    }
     int sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock_fd < 0) {
       int err = errno;
@@ -82,13 +90,14 @@ public:
     struct sockaddr_un address;
     memset(&address, 0, sizeof(struct sockaddr_un));
     address.sun_family = AF_UNIX;
-    snprintf(address.sun_path, sizeof(address.sun_path), sock_path.c_str());
+    snprintf(address.sun_path, sizeof(sockaddr_un::sun_path), sock_path.c_str());
     if (bind(sock_fd, (struct sockaddr*)&address,
             sizeof(struct sockaddr_un)) != 0) {
       int err = errno;
       ostringstream oss;
       oss << "ProfLogThread::bind_and_listen: "
-         << "failed to bind socket: " << cpp_strerror(err);
+         << "failed to bind the UNIX domain socket to '" << sock_path
+         << "': " << cpp_strerror(err);
       close(sock_fd);
       return oss.str();
     }
index f0aed6fd7965baab8f925bfd774f625ba596cf1b..ae753b786de22a4a9b243f05e100445d33bda4c0 100644 (file)
  *
  */
 
-#include "common/ceph_context.h"
 #include "common/ProfLogger.h"
-
+#include "common/ceph_context.h"
+#include "common/errno.h"
+#include "common/safe_io.h"
 #include "test/unit.h"
 
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <map>
+#include <poll.h>
+#include <sstream>
+#include <stdint.h>
+#include <string.h>
+#include <string>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <time.h>
+#include <unistd.h>
+
+static char g_socket_path[sizeof(sockaddr_un::sun_path)] = { 0 };
+
+static const char* get_socket_path()
+{
+  if (g_socket_path[0] == '\0') {
+    const char *tdir = getenv("TMPDIR");
+    if (tdir == NULL) {
+      tdir = "/tmp";
+    }
+    snprintf(g_socket_path, sizeof(sockaddr_un::sun_path),
+            "%s/proflogger_test_socket.%ld.%ld",
+            tdir, (long int)getpid(), time(NULL));
+  }
+  return g_socket_path;
+}
+
 class ProfLoggerCollectionTest
 {
 public:
@@ -40,8 +72,89 @@ private:
   ProfLoggerCollection *m_coll;
 };
 
-TEST(ProfLogger, SetupTeardown) {
+class Alarm
+{
+public:
+  Alarm(int s) {
+    alarm(s);
+  }
+  ~Alarm() {
+    alarm(0);
+  }
+};
+
+class ProfLoggerTestClient
+{
+public:
+  ProfLoggerTestClient(const std::string &uri)
+    : m_uri(uri)
+  {
+  }
+
+  std::string get_message(std::string *message)
+  {
+    //Alarm my_alarm(300);
+
+    int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
+    if(socket_fd < 0) {
+      int err = errno;
+      ostringstream oss;
+      oss << "socket(PF_UNIX, SOCK_STREAM, 0) failed: " << cpp_strerror(err);
+      return oss.str();
+    }
+
+    struct sockaddr_un address;
+    memset(&address, 0, sizeof(struct sockaddr_un));
+    address.sun_family = AF_UNIX;
+    snprintf(address.sun_path, sizeof(address.sun_path), m_uri.c_str());
+
+    if (connect(socket_fd, (struct sockaddr *) &address, 
+         sizeof(struct sockaddr_un)) != 0) {
+      int err = errno;
+      ostringstream oss;
+      oss << "connect(" << socket_fd << ") failed: " << cpp_strerror(err);
+      close(socket_fd);
+      return oss.str();
+    }
+
+    std::vector<uint8_t> vec(65536, 0);
+    uint8_t *buffer = &vec[0];
+
+    ssize_t res = safe_read(socket_fd, buffer, vec.size() - 1);
+    if (res < 0) {
+      int err = res;
+      ostringstream oss;
+      oss << "safe_read(" << socket_fd << ") failed: " << cpp_strerror(err);
+      close(socket_fd);
+      return oss.str();
+    }
+
+    printf("MESSAGE FROM SERVER: %s\n", buffer);
+    message->assign((const char*)buffer);
+    close(socket_fd);
+    return "";
+  }
+
+private:
+  std::string m_uri;
+};
+
+TEST(ProfLogger, Teardown) {
+  ProfLoggerCollectionTest plct(g_ceph_context->GetProfLoggerCollection());
+  ASSERT_EQ(true, plct.shutdown());
+}
+
+TEST(ProfLogger, TeardownSetup) {
+  ProfLoggerCollectionTest plct(g_ceph_context->GetProfLoggerCollection());
+  ASSERT_EQ(true, plct.shutdown());
+  ASSERT_EQ(true, plct.init(get_socket_path()));
+}
+
+TEST(ProfLogger, SimpleTest) {
   ProfLoggerCollectionTest plct(g_ceph_context->GetProfLoggerCollection());
-  int ret = plct.shutdown();
-  ASSERT_EQ(ret, true);
+  ASSERT_EQ(true, plct.shutdown());
+  ASSERT_EQ(true, plct.init(get_socket_path()));
+  ProfLoggerTestClient test_client(get_socket_path());
+  std::string message;
+  ASSERT_EQ("", test_client.get_message(&message));
 }