aboutsummaryrefslogtreecommitdiff
path: root/src/base/scoped_file_unittest.cc
blob: dff44135f4267caa6cd79cc782c2760995f5d119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "perfetto/ext/base/scoped_file.h"
#include "perfetto/base/build_config.h"

#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
#include <corecrt_io.h>
#else
#include <fcntl.h>
#include <unistd.h>
// Double closing of file handles on Windows leads to invocation of the invalid
// parameter handler or asserts and therefore it cannot be tested, but it can
// be tested on other platforms.
#define TEST_INVALID_CLOSE
#endif

#include "test/gtest_and_gmock.h"

namespace perfetto {
namespace base {
namespace {

#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
const char kNullFilename[] = "NUL";
const char kZeroFilename[] = "NUL";
#else
const char kNullFilename[] = "/dev/null";
const char kZeroFilename[] = "/dev/zero";
#endif

#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
  TEST(ScopedDirTest, CloseOutOfScope) {
  DIR* dir_handle = opendir(".");
  ASSERT_NE(nullptr, dir_handle);
  int dir_handle_fd = dirfd(dir_handle);
  ASSERT_GE(dir_handle_fd, 0);
  {
    ScopedDir scoped_dir(dir_handle);
    ASSERT_EQ(dir_handle, scoped_dir.get());
    ASSERT_TRUE(scoped_dir);
  }
  ASSERT_NE(0, close(dir_handle_fd));  // Should fail when closing twice.
}
#endif

TEST(ScopedFileTest, CloseOutOfScope) {
  int raw_fd = open(kNullFilename, O_RDONLY);
  ASSERT_GE(raw_fd, 0);
  {
    ScopedFile scoped_file(raw_fd);
    ASSERT_EQ(raw_fd, scoped_file.get());
    ASSERT_EQ(raw_fd, *scoped_file);
    ASSERT_TRUE(scoped_file);
  }
#ifdef TEST_INVALID_CLOSE
  ASSERT_NE(0, close(raw_fd));  // Should fail when closing twice.
#endif
}

TEST(ScopedFstreamTest, CloseOutOfScope) {
  FILE* raw_stream = fopen(kNullFilename, "r");
  ASSERT_NE(nullptr, raw_stream);
  {
    ScopedFstream scoped_stream(raw_stream);
    ASSERT_EQ(raw_stream, scoped_stream.get());
    ASSERT_EQ(raw_stream, *scoped_stream);
    ASSERT_TRUE(scoped_stream);
  }
  // We don't have a direct way to see that the file was closed.
}

TEST(ScopedFileTest, Reset) {
  int raw_fd1 = open(kNullFilename, O_RDONLY);
  int raw_fd2 = open(kZeroFilename, O_RDONLY);
  ASSERT_GE(raw_fd1, 0);
  ASSERT_GE(raw_fd2, 0);
  {
    ScopedFile scoped_file(raw_fd1);
    ASSERT_EQ(raw_fd1, scoped_file.get());
    scoped_file.reset(raw_fd2);
    ASSERT_EQ(raw_fd2, scoped_file.get());
#ifdef TEST_INVALID_CLOSE
    ASSERT_NE(0, close(raw_fd1));  // Should fail when closing twice.
#endif
    scoped_file.reset();
#ifdef TEST_INVALID_CLOSE
    ASSERT_NE(0, close(raw_fd2));
#endif
    scoped_file.reset(open(kNullFilename, O_RDONLY));
    ASSERT_GE(scoped_file.get(), 0);
  }
}

TEST(ScopedFileTest, Release) {
  int raw_fd = open(kNullFilename, O_RDONLY);
  ASSERT_GE(raw_fd, 0);
  {
    ScopedFile scoped_file(raw_fd);
    ASSERT_EQ(raw_fd, scoped_file.release());
    ASSERT_FALSE(scoped_file);
  }
  ASSERT_EQ(0, close(raw_fd));
}

TEST(ScopedFileTest, MoveCtor) {
  int raw_fd1 = open(kNullFilename, O_RDONLY);
  int raw_fd2 = open(kZeroFilename, O_RDONLY);
  ASSERT_GE(raw_fd1, 0);
  ASSERT_GE(raw_fd2, 0);
  {
    ScopedFile scoped_file1(ScopedFile{raw_fd1});
    ScopedFile scoped_file2(std::move(scoped_file1));
    ASSERT_EQ(-1, scoped_file1.get());
    ASSERT_EQ(-1, *scoped_file1);
    ASSERT_FALSE(scoped_file1);
    ASSERT_EQ(raw_fd1, scoped_file2.get());

    scoped_file1.reset(raw_fd2);
    ASSERT_EQ(raw_fd2, scoped_file1.get());
  }
#ifdef TEST_INVALID_CLOSE
  ASSERT_NE(0, close(raw_fd1));  // Should fail when closing twice.
  ASSERT_NE(0, close(raw_fd2));
#endif
}

TEST(ScopedFileTest, MoveAssignment) {
  int raw_fd1 = open(kNullFilename, O_RDONLY);
  int raw_fd2 = open(kZeroFilename, O_RDONLY);
  ASSERT_GE(raw_fd1, 0);
  ASSERT_GE(raw_fd2, 0);
  {
    ScopedFile scoped_file1(raw_fd1);
    ScopedFile scoped_file2(raw_fd2);
    scoped_file2 = std::move(scoped_file1);
    ASSERT_EQ(-1, scoped_file1.get());
    ASSERT_FALSE(scoped_file1);
    ASSERT_EQ(raw_fd1, scoped_file2.get());
#ifdef TEST_INVALID_CLOSE
    ASSERT_NE(0, close(raw_fd2));
#endif

    scoped_file1 = std::move(scoped_file2);
    ASSERT_EQ(raw_fd1, scoped_file1.get());
    ASSERT_EQ(-1, scoped_file2.get());
  }
#ifdef TEST_INVALID_CLOSE
  ASSERT_NE(0, close(raw_fd1));
#endif
}

// File descriptors are capabilities and hence can be security critical. A
// failed close() suggests the memory ownership of the file is wrong and we
// might have leaked a capability.
#ifdef TEST_INVALID_CLOSE
TEST(ScopedFileTest, CloseFailureIsFatal) {
  int raw_fd = open(kNullFilename, O_RDONLY);
  ASSERT_DEATH_IF_SUPPORTED(
      {
        ScopedFile scoped_file(raw_fd);
        ASSERT_EQ(0, close(raw_fd));
      },
      "");
}
#endif

}  // namespace
}  // namespace base
}  // namespace perfetto