Mark a few constexpr locals in ThreadLocalTest as static
[folly.git] / folly / test / FileUtilTest.cpp
index d79dfd246d8f343bc24ca98ed334e234c32e9f6b..26cfde95b2495049608a6852809ad8cb343cfc20 100644 (file)
 #include <deque>
 
 #include <glog/logging.h>
-#include <gtest/gtest.h>
 
+#include <folly/File.h>
 #include <folly/Range.h>
 #include <folly/String.h>
+#include <folly/portability/GTest.h>
 
 namespace folly { namespace test {
 
@@ -265,16 +266,9 @@ TEST_F(FileUtilTest, preadv) {
 }
 
 TEST(String, readFile) {
-  srand(time(nullptr));
-  const string tmpPrefix = to<string>("/tmp/folly-file-util-test-",
-                                      getpid(), "-", rand(), "-");
-  const string afile = tmpPrefix + "myfile";
-  const string emptyFile = tmpPrefix + "myfile2";
-
-  SCOPE_EXIT {
-    unlink(afile.c_str());
-    unlink(emptyFile.c_str());
-  };
+  const TemporaryFile afileTemp, emptyFileTemp;
+  auto afile = afileTemp.path().string();
+  auto emptyFile = emptyFileTemp.path().string();
 
   EXPECT_TRUE(writeFile(string(), emptyFile.c_str()));
   EXPECT_TRUE(writeFile(StringPiece("bar"), afile.c_str()));
@@ -303,4 +297,47 @@ TEST(String, readFile) {
   }
 }
 
+class ReadFileFd : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    ASSERT_TRUE(writeFile(StringPiece("bar"), aFile.path().string().c_str()));
+  }
+
+  TemporaryFile aFile;
+};
+
+TEST_F(ReadFileFd, ReadZeroBytes) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents, 0));
+  EXPECT_EQ("", contents);
+}
+
+TEST_F(ReadFileFd, ReadPartial) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents, 2));
+  EXPECT_EQ("ba", contents);
+}
+
+TEST_F(ReadFileFd, ReadFull) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents));
+  EXPECT_EQ("bar", contents);
+}
+
+TEST_F(ReadFileFd, WriteOnlyFd) {
+  File f(aFile.path().string(), O_WRONLY);
+  std::string contents;
+  EXPECT_FALSE(readFile(f.fd(), contents));
+  PLOG(INFO);
+}
+
+TEST_F(ReadFileFd, InvalidFd) {
+  File f(aFile.path().string());
+  f.close();
+  std::string contents;
+  msvcSuppressAbortOnInvalidParams([&] {
+    EXPECT_FALSE(readFile(f.fd(), contents));
+  });
+  PLOG(INFO);
+}
 }}  // namespaces