Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 17 | #include "perfetto/ext/base/temp_file.h" |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 18 | |
| 19 | #include <sys/stat.h> |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 20 | |
| 21 | #include "perfetto/base/build_config.h" |
| 22 | |
| 23 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 25 | #endif |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 26 | |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 27 | #include "test/gtest_and_gmock.h" |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 28 | |
| 29 | namespace perfetto { |
| 30 | namespace base { |
| 31 | namespace { |
| 32 | |
| 33 | bool PathExists(const std::string& path) { |
| 34 | struct stat stat_buf; |
| 35 | return stat(path.c_str(), &stat_buf) == 0; |
| 36 | } |
| 37 | |
| 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 | |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 68 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 69 | // Windows UCRT aborts when trying to write into a closed FD. |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 70 | ASSERT_EQ(-1, write(fd, "foo", 4)); |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 71 | #endif |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 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 | } |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 83 | |
| 84 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 85 | // Windows UCRT aborts when trying to write into a closed FD. |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 86 | ASSERT_EQ(-1, write(fd, "foo", 4)); |
Primiano Tucci | e4de2da | 2020-12-01 14:54:07 +0100 | [diff] [blame] | 87 | #endif |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 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 | |
| 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 | |
| 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 |