• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 "perfetto/base/temp_file.h"
18 
19 #include <sys/stat.h>
20 #include <unistd.h>
21 
22 #include "gtest/gtest.h"
23 
24 namespace perfetto {
25 namespace base {
26 namespace {
27 
PathExists(const std::string & path)28 bool PathExists(const std::string& path) {
29   struct stat stat_buf;
30   return stat(path.c_str(), &stat_buf) == 0;
31 }
32 
TEST(TempFileTest,Create)33 TEST(TempFileTest, Create) {
34   std::string path;
35   int fd;
36   {
37     TempFile tf = TempFile::Create();
38     path = tf.path();
39     fd = tf.fd();
40     ASSERT_NE("", path);
41     ASSERT_GE(fd, 0);
42     ASSERT_TRUE(PathExists(path));
43     ASSERT_GE(write(fd, "foo", 4), 0);
44 
45     TempFile moved_tf(std::move(tf));
46     ASSERT_EQ("", tf.path());
47     ASSERT_EQ(-1, tf.fd());
48     ASSERT_EQ(path, moved_tf.path());
49     ASSERT_EQ(fd, moved_tf.fd());
50     ASSERT_GE(write(moved_tf.fd(), "foo", 4), 0);
51 
52     TempFile moved_tf2 = std::move(moved_tf);
53     ASSERT_EQ("", moved_tf.path());
54     ASSERT_EQ(-1, moved_tf.fd());
55     ASSERT_EQ(path, moved_tf2.path());
56     ASSERT_EQ(fd, moved_tf2.fd());
57     ASSERT_GE(write(moved_tf2.fd(), "foo", 4), 0);
58   }
59 
60   // The file should be deleted and closed now.
61   ASSERT_FALSE(PathExists(path));
62 
63   ASSERT_EQ(-1, write(fd, "foo", 4));
64 }
65 
TEST(TempFileTest,CreateUnlinked)66 TEST(TempFileTest, CreateUnlinked) {
67   int fd;
68   {
69     TempFile tf = TempFile::CreateUnlinked();
70     ASSERT_EQ("", tf.path());
71     fd = tf.fd();
72     ASSERT_GE(fd, 0);
73     ASSERT_GE(write(fd, "foo", 4), 0);
74   }
75   ASSERT_EQ(-1, write(fd, "foo", 4));
76 }
77 
TEST(TempFileTest,ReleaseUnlinked)78 TEST(TempFileTest, ReleaseUnlinked) {
79   ScopedFile fd;
80   {
81     TempFile tf = TempFile::Create();
82     fd = tf.ReleaseFD();
83   }
84   ASSERT_GE(write(*fd, "foo", 4), 0);
85 }
86 
TEST(TempFileTest,ReleaseLinked)87 TEST(TempFileTest, ReleaseLinked) {
88   ScopedFile fd;
89   std::string path;
90   {
91     TempFile tf = TempFile::CreateUnlinked();
92     path = tf.path();
93     fd = tf.ReleaseFD();
94   }
95 
96   // The file should be unlinked from the filesystem.
97   ASSERT_FALSE(PathExists(path));
98 
99   // But still open and writable.
100   ASSERT_GE(write(*fd, "foo", 4), 0);
101 }
102 
TEST(TempFileTest,TempDir)103 TEST(TempFileTest, TempDir) {
104   std::string path;
105   {
106     TempDir td = TempDir::Create();
107     ASSERT_NE("", td.path());
108     ASSERT_TRUE(PathExists(td.path()));
109     path = td.path();
110   }
111   ASSERT_FALSE(PathExists(path));
112 }
113 
114 }  // namespace
115 }  // namespace base
116 }  // namespace perfetto
117