blob: 3fdf12a3028a96f9d5ed071b5ba4b692138297b8 [file] [log] [blame]
Josh Gao6b4ef852018-07-20 12:42:14 -07001/*
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
17#include "android-base/unique_fd.h"
18
19#include <gtest/gtest.h>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25using android::base::unique_fd;
26
27TEST(unique_fd, unowned_close) {
28#if defined(__BIONIC__)
29 unique_fd fd(open("/dev/null", O_RDONLY));
30 EXPECT_DEATH(close(fd.get()), "incorrect tag");
31#endif
32}
33
34TEST(unique_fd, untag_on_release) {
35 unique_fd fd(open("/dev/null", O_RDONLY));
36 close(fd.release());
37}
38
39TEST(unique_fd, move) {
40 unique_fd fd(open("/dev/null", O_RDONLY));
41 unique_fd fd_moved = std::move(fd);
42 ASSERT_EQ(-1, fd.get());
43 ASSERT_GT(fd_moved.get(), -1);
44}
45
46TEST(unique_fd, unowned_close_after_move) {
47#if defined(__BIONIC__)
48 unique_fd fd(open("/dev/null", O_RDONLY));
49 unique_fd fd_moved = std::move(fd);
50 ASSERT_EQ(-1, fd.get());
51 ASSERT_GT(fd_moved.get(), -1);
52 EXPECT_DEATH(close(fd_moved.get()), "incorrect tag");
53#endif
54}