Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 17 | #include "perfetto_base/scoped_file.h" |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | namespace perfetto { |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 25 | namespace base { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 26 | namespace { |
| 27 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 28 | TEST(ScopedDir, CloseOutOfScope) { |
| 29 | DIR* dir_handle = opendir("."); |
| 30 | ASSERT_NE(nullptr, dir_handle); |
| 31 | int dir_handle_fd = dirfd(dir_handle); |
| 32 | ASSERT_GE(dir_handle_fd, 0); |
| 33 | { |
| 34 | ScopedDir scoped_dir(dir_handle); |
| 35 | ASSERT_EQ(dir_handle, scoped_dir.get()); |
| 36 | ASSERT_TRUE(scoped_dir); |
| 37 | } |
| 38 | ASSERT_NE(0, close(dir_handle_fd)); // Should fail when closing twice. |
| 39 | } |
| 40 | |
| 41 | TEST(ScopedFile, CloseOutOfScope) { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 42 | int raw_fd = open("/dev/null", O_RDONLY); |
| 43 | ASSERT_GE(raw_fd, 0); |
| 44 | { |
| 45 | ScopedFile scoped_file(raw_fd); |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 46 | ASSERT_EQ(raw_fd, scoped_file.get()); |
| 47 | ASSERT_EQ(raw_fd, *scoped_file); |
| 48 | ASSERT_TRUE(scoped_file); |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 49 | } |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 50 | ASSERT_NE(0, close(raw_fd)); // Should fail when closing twice. |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 53 | TEST(ScopedFile, Reset) { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 54 | int raw_fd1 = open("/dev/null", O_RDONLY); |
| 55 | int raw_fd2 = open("/dev/zero", O_RDONLY); |
| 56 | ASSERT_GE(raw_fd1, 0); |
| 57 | ASSERT_GE(raw_fd2, 0); |
| 58 | { |
| 59 | ScopedFile scoped_file(raw_fd1); |
| 60 | ASSERT_EQ(raw_fd1, scoped_file.get()); |
| 61 | scoped_file.reset(raw_fd2); |
| 62 | ASSERT_EQ(raw_fd2, scoped_file.get()); |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 63 | ASSERT_NE(0, close(raw_fd1)); // Should fail when closing twice. |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 64 | scoped_file.reset(); |
| 65 | ASSERT_NE(0, close(raw_fd2)); |
| 66 | scoped_file.reset(open("/dev/null", O_RDONLY)); |
| 67 | ASSERT_GE(scoped_file.get(), 0); |
| 68 | } |
| 69 | } |
| 70 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 71 | TEST(ScopedFile, MoveCtor) { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 72 | int raw_fd1 = open("/dev/null", O_RDONLY); |
| 73 | int raw_fd2 = open("/dev/zero", O_RDONLY); |
| 74 | ASSERT_GE(raw_fd1, 0); |
| 75 | ASSERT_GE(raw_fd2, 0); |
| 76 | { |
| 77 | ScopedFile scoped_file1(ScopedFile{raw_fd1}); |
| 78 | ScopedFile scoped_file2(std::move(scoped_file1)); |
| 79 | ASSERT_EQ(-1, scoped_file1.get()); |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 80 | ASSERT_EQ(-1, *scoped_file1); |
| 81 | ASSERT_FALSE(scoped_file1); |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 82 | ASSERT_EQ(raw_fd1, scoped_file2.get()); |
| 83 | |
| 84 | scoped_file1.reset(raw_fd2); |
| 85 | ASSERT_EQ(raw_fd2, scoped_file1.get()); |
| 86 | } |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 87 | ASSERT_NE(0, close(raw_fd1)); // Should fail when closing twice. |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 88 | ASSERT_NE(0, close(raw_fd2)); |
| 89 | } |
| 90 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 91 | TEST(ScopedFile, MoveAssignment) { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 92 | int raw_fd1 = open("/dev/null", O_RDONLY); |
| 93 | int raw_fd2 = open("/dev/zero", O_RDONLY); |
| 94 | ASSERT_GE(raw_fd1, 0); |
| 95 | ASSERT_GE(raw_fd2, 0); |
| 96 | { |
| 97 | ScopedFile scoped_file1(raw_fd1); |
| 98 | ScopedFile scoped_file2(raw_fd2); |
| 99 | scoped_file2 = std::move(scoped_file1); |
| 100 | ASSERT_EQ(-1, scoped_file1.get()); |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 101 | ASSERT_FALSE(scoped_file1); |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 102 | ASSERT_EQ(raw_fd1, scoped_file2.get()); |
| 103 | ASSERT_NE(0, close(raw_fd2)); |
| 104 | |
| 105 | scoped_file1 = std::move(scoped_file2); |
| 106 | ASSERT_EQ(raw_fd1, scoped_file1.get()); |
| 107 | ASSERT_EQ(-1, scoped_file2.get()); |
| 108 | } |
| 109 | ASSERT_NE(0, close(raw_fd1)); |
| 110 | } |
| 111 | |
| 112 | // File descriptors are capabilities and hence can be security critical. A |
| 113 | // failed close() suggests the memory ownership of the file is wrong and we |
| 114 | // might have leaked a capability. |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 115 | TEST(ScopedFile, CloseFailureIsFatal) { |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 116 | int raw_fd = open("/dev/null", O_RDONLY); |
| 117 | ASSERT_DEATH( |
| 118 | { |
| 119 | ScopedFile scoped_file(raw_fd); |
| 120 | ASSERT_EQ(0, close(raw_fd)); |
| 121 | }, |
| 122 | ""); |
| 123 | } |
| 124 | |
| 125 | } // namespace |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 126 | } // namespace base |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 127 | } // namespace perfetto |