f47e11700af1efe6fb7cd382175b93b0c2bce9e2
[folly.git] / folly / experimental / test / TestUtilTest.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/experimental/TestUtil.h"
18
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22
23 #include <system_error>
24
25 #include <boost/algorithm/string.hpp>
26 #include <glog/logging.h>
27 #include <gtest/gtest.h>
28
29 using namespace folly;
30 using namespace folly::test;
31
32 TEST(TemporaryFile, Simple) {
33   int fd = -1;
34   char c = 'x';
35   {
36     TemporaryFile f;
37     EXPECT_FALSE(f.path().empty());
38     EXPECT_TRUE(f.path().is_absolute());
39     fd = f.fd();
40     EXPECT_LE(0, fd);
41     ssize_t r = write(fd, &c, 1);
42     EXPECT_EQ(1, r);
43   }
44
45   // The file must have been closed.  This assumes that no other thread
46   // has opened another file in the meanwhile, which is a sane assumption
47   // to make in this test.
48   ssize_t r = write(fd, &c, 1);
49   int savedErrno = errno;
50   EXPECT_EQ(-1, r);
51   EXPECT_EQ(EBADF, savedErrno);
52 }
53
54 TEST(TemporaryFile, Prefix) {
55   TemporaryFile f("Foo");
56   EXPECT_TRUE(f.path().is_absolute());
57   EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
58                                             "Foo"));
59 }
60
61 TEST(TemporaryFile, PathPrefix) {
62   TemporaryFile f("Foo", ".");
63   EXPECT_EQ(fs::path("."), f.path().parent_path());
64   EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
65                                             "Foo"));
66 }
67
68 TEST(TemporaryFile, NoSuchPath) {
69   EXPECT_THROW({TemporaryFile f("", "/no/such/path");},
70                std::system_error);
71 }
72
73 void testTemporaryDirectory(TemporaryDirectory::Scope scope) {
74   fs::path path;
75   {
76     TemporaryDirectory d("", "", scope);
77     path = d.path();
78     EXPECT_FALSE(path.empty());
79     EXPECT_TRUE(path.is_absolute());
80     EXPECT_TRUE(fs::exists(path));
81     EXPECT_TRUE(fs::is_directory(path));
82
83     fs::path fp = path / "bar";
84     int fd = open(fp.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
85     EXPECT_NE(fd, -1);
86     close(fd);
87
88     TemporaryFile f("Foo", d.path());
89     EXPECT_EQ(d.path(), f.path().parent_path());
90   }
91   bool exists = (scope == TemporaryDirectory::Scope::PERMANENT);
92   EXPECT_EQ(exists, fs::exists(path));
93 }
94
95 TEST(TemporaryDirectory, Permanent) {
96   testTemporaryDirectory(TemporaryDirectory::Scope::PERMANENT);
97 }
98
99 TEST(TemporaryDirectory, DeleteOnDestruction) {
100   testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
101 }
102
103 int main(int argc, char *argv[]) {
104   testing::InitGoogleTest(&argc, argv);
105   google::ParseCommandLineFlags(&argc, &argv, true);
106   return RUN_ALL_TESTS();
107 }
108