blob: 614894e3eca932c8f16e3e787c3407125e85a4f1 [file] [log] [blame]
Primiano Tucci941b2212018-03-14 22:46:31 +00001/*
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
Primiano Tucci2c5488f2019-06-01 03:27:28 +010017#include "perfetto/ext/base/temp_file.h"
Primiano Tucci941b2212018-03-14 22:46:31 +000018
19#include <sys/stat.h>
Primiano Tuccie4de2da2020-12-01 14:54:07 +010020
21#include "perfetto/base/build_config.h"
22
23#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
Primiano Tucci941b2212018-03-14 22:46:31 +000024#include <unistd.h>
Primiano Tuccie4de2da2020-12-01 14:54:07 +010025#endif
Primiano Tucci941b2212018-03-14 22:46:31 +000026
Primiano Tucci919ca1e2019-08-21 20:26:58 +020027#include "test/gtest_and_gmock.h"
Primiano Tucci941b2212018-03-14 22:46:31 +000028
29namespace perfetto {
30namespace base {
31namespace {
32
33bool PathExists(const std::string& path) {
34 struct stat stat_buf;
35 return stat(path.c_str(), &stat_buf) == 0;
36}
37
38TEST(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
Primiano Tuccie4de2da2020-12-01 14:54:07 +010068#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
69 // Windows UCRT aborts when trying to write into a closed FD.
Primiano Tucci941b2212018-03-14 22:46:31 +000070 ASSERT_EQ(-1, write(fd, "foo", 4));
Primiano Tuccie4de2da2020-12-01 14:54:07 +010071#endif
Primiano Tucci941b2212018-03-14 22:46:31 +000072}
73
74TEST(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 }
Primiano Tuccie4de2da2020-12-01 14:54:07 +010083
84#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
85 // Windows UCRT aborts when trying to write into a closed FD.
Primiano Tucci941b2212018-03-14 22:46:31 +000086 ASSERT_EQ(-1, write(fd, "foo", 4));
Primiano Tuccie4de2da2020-12-01 14:54:07 +010087#endif
Primiano Tucci941b2212018-03-14 22:46:31 +000088}
89
90TEST(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
99TEST(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
115TEST(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