blob: 174f220ef5a1d857fce0fe9498cab01e218308f1 [file] [log] [blame]
Primiano Tucci13331342017-10-25 17:08:13 +01001/*
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
Primiano Tuccid7d1be02017-10-30 17:41:34 +000017#include "base/scoped_file.h"
Primiano Tucci13331342017-10-25 17:08:13 +010018
19#include <fcntl.h>
20#include <unistd.h>
21
22#include "gtest/gtest.h"
23
24namespace perfetto {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000025namespace base {
Primiano Tucci13331342017-10-25 17:08:13 +010026namespace {
27
Primiano Tuccid7d1be02017-10-30 17:41:34 +000028TEST(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
41TEST(ScopedFile, CloseOutOfScope) {
Primiano Tucci13331342017-10-25 17:08:13 +010042 int raw_fd = open("/dev/null", O_RDONLY);
43 ASSERT_GE(raw_fd, 0);
44 {
45 ScopedFile scoped_file(raw_fd);
Primiano Tuccid7d1be02017-10-30 17:41:34 +000046 ASSERT_EQ(raw_fd, scoped_file.get());
47 ASSERT_EQ(raw_fd, *scoped_file);
48 ASSERT_TRUE(scoped_file);
Primiano Tucci13331342017-10-25 17:08:13 +010049 }
Primiano Tuccid7d1be02017-10-30 17:41:34 +000050 ASSERT_NE(0, close(raw_fd)); // Should fail when closing twice.
Primiano Tucci13331342017-10-25 17:08:13 +010051}
52
Primiano Tuccid7d1be02017-10-30 17:41:34 +000053TEST(ScopedFile, Reset) {
Primiano Tucci13331342017-10-25 17:08:13 +010054 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 Tuccid7d1be02017-10-30 17:41:34 +000063 ASSERT_NE(0, close(raw_fd1)); // Should fail when closing twice.
Primiano Tucci13331342017-10-25 17:08:13 +010064 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 Tuccid7d1be02017-10-30 17:41:34 +000071TEST(ScopedFile, MoveCtor) {
Primiano Tucci13331342017-10-25 17:08:13 +010072 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 Tuccid7d1be02017-10-30 17:41:34 +000080 ASSERT_EQ(-1, *scoped_file1);
81 ASSERT_FALSE(scoped_file1);
Primiano Tucci13331342017-10-25 17:08:13 +010082 ASSERT_EQ(raw_fd1, scoped_file2.get());
83
84 scoped_file1.reset(raw_fd2);
85 ASSERT_EQ(raw_fd2, scoped_file1.get());
86 }
Primiano Tuccid7d1be02017-10-30 17:41:34 +000087 ASSERT_NE(0, close(raw_fd1)); // Should fail when closing twice.
Primiano Tucci13331342017-10-25 17:08:13 +010088 ASSERT_NE(0, close(raw_fd2));
89}
90
Primiano Tuccid7d1be02017-10-30 17:41:34 +000091TEST(ScopedFile, MoveAssignment) {
Primiano Tucci13331342017-10-25 17:08:13 +010092 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 Tuccid7d1be02017-10-30 17:41:34 +0000101 ASSERT_FALSE(scoped_file1);
Primiano Tucci13331342017-10-25 17:08:13 +0100102 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 Tuccid7d1be02017-10-30 17:41:34 +0000115TEST(ScopedFile, CloseFailureIsFatal) {
Primiano Tucci13331342017-10-25 17:08:13 +0100116 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 Tuccid7d1be02017-10-30 17:41:34 +0000126} // namespace base
Primiano Tucci13331342017-10-25 17:08:13 +0100127} // namespace perfetto