testing: improve sanitizer handling
[awesomized/libmemcached] / testing / lib / common.hpp
index 9f9578224d48581a798a6b717c9d600b35ea4641..ee177003bac8fbf3b2a5a0cff0b4be838158fb36 100644 (file)
@@ -11,6 +11,7 @@
 #include <variant>
 #include <vector>
 
+#include "testing/conf.h"
 #include "testing/lib/catch.hpp"
 #include "testing/lib/random.hpp"
 
@@ -55,6 +56,33 @@ inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &ke
   return rc;
 }
 
+#include <cstdlib>
+#include <unistd.h>
+
+class Tempfile {
+public:
+  explicit Tempfile(const char templ_[] = "memc.test.XXXXXX") {
+    *copy(S(templ_)+templ_, fn) = '\0';
+    fd = mkstemp(fn);
+  }
+  ~Tempfile() {
+    close(fd);
+    unlink(fn);
+  }
+  int getFd() const {
+    return fd;
+  }
+  const char *getFn() const {
+    return fn;
+  }
+  bool put(const char *buf, size_t len) const {
+    return static_cast<ssize_t >(len) == write(fd, buf, len);
+  }
+private:
+  char fn[80];
+  int fd;
+};
+
 class MemcachedPtr {
 public:
   memcached_st *memc;
@@ -78,6 +106,7 @@ template<class T>
 class Malloced {
   T *ptr;
 public:
+  explicit
   Malloced(T *ptr_)
   : ptr{ptr_}
   {}
@@ -88,4 +117,7 @@ public:
   auto operator *() {
     return ptr;
   }
+  auto operator ->() {
+    return ptr;
+  }
 };