xref: /aosp_15_r20/external/perfetto/src/base/temp_file_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/ext/base/temp_file.h"
18 
19 #include <sys/stat.h>
20 
21 #include "perfetto/base/build_config.h"
22 
23 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
24 #include <unistd.h>
25 #endif
26 
27 #include "test/gtest_and_gmock.h"
28 
29 namespace perfetto {
30 namespace base {
31 namespace {
32 
PathExists(const std::string & path)33 bool PathExists(const std::string& path) {
34   struct stat stat_buf;
35   return stat(path.c_str(), &stat_buf) == 0;
36 }
37 
TEST(TempFileTest,Create)38 TEST(TempFileTest, Create) {
39   std::string path;
40   int fd;
41   {
42     TempFile tf = TempFile::Create();
43     path = tf.path();
44     fd = tf.fd();
45     ASSERT_NE("", path);
46     ASSERT_GE(fd, 0);
47     ASSERT_TRUE(PathExists(path));
48     ASSERT_GE(write(fd, "foo", 4), 0);
49 
50     TempFile moved_tf(std::move(tf));
51     ASSERT_EQ("", tf.path());
52     ASSERT_EQ(-1, tf.fd());
53     ASSERT_EQ(path, moved_tf.path());
54     ASSERT_EQ(fd, moved_tf.fd());
55     ASSERT_GE(write(moved_tf.fd(), "foo", 4), 0);
56 
57     TempFile moved_tf2 = std::move(moved_tf);
58     ASSERT_EQ("", moved_tf.path());
59     ASSERT_EQ(-1, moved_tf.fd());
60     ASSERT_EQ(path, moved_tf2.path());
61     ASSERT_EQ(fd, moved_tf2.fd());
62     ASSERT_GE(write(moved_tf2.fd(), "foo", 4), 0);
63   }
64 
65   // The file should be deleted and closed now.
66   ASSERT_FALSE(PathExists(path));
67 
68 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
69   // Windows UCRT aborts when trying to write into a closed FD.
70   ASSERT_EQ(-1, write(fd, "foo", 4));
71 #endif
72 }
73 
TEST(TempFileTest,CreateUnlinked)74 TEST(TempFileTest, CreateUnlinked) {
75   int fd;
76   {
77     TempFile tf = TempFile::CreateUnlinked();
78     ASSERT_EQ("", tf.path());
79     fd = tf.fd();
80     ASSERT_GE(fd, 0);
81     ASSERT_GE(write(fd, "foo", 4), 0);
82   }
83 
84 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
85   // Windows UCRT aborts when trying to write into a closed FD.
86   ASSERT_EQ(-1, write(fd, "foo", 4));
87 #endif
88 }
89 
TEST(TempFileTest,ReleaseUnlinked)90 TEST(TempFileTest, ReleaseUnlinked) {
91   ScopedFile fd;
92   {
93     TempFile tf = TempFile::Create();
94     fd = tf.ReleaseFD();
95   }
96   ASSERT_GE(write(*fd, "foo", 4), 0);
97 }
98 
TEST(TempFileTest,ReleaseLinked)99 TEST(TempFileTest, ReleaseLinked) {
100   ScopedFile fd;
101   std::string path;
102   {
103     TempFile tf = TempFile::CreateUnlinked();
104     path = tf.path();
105     fd = tf.ReleaseFD();
106   }
107 
108   // The file should be unlinked from the filesystem.
109   ASSERT_FALSE(PathExists(path));
110 
111   // But still open and writable.
112   ASSERT_GE(write(*fd, "foo", 4), 0);
113 }
114 
TEST(TempFileTest,TempDir)115 TEST(TempFileTest, TempDir) {
116   std::string path;
117   {
118     TempDir td = TempDir::Create();
119     ASSERT_NE("", td.path());
120     ASSERT_TRUE(PathExists(td.path()));
121     path = td.path();
122   }
123   ASSERT_FALSE(PathExists(path));
124 }
125 
126 }  // namespace
127 }  // namespace base
128 }  // namespace perfetto
129