Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 22 | #include "TemporaryFile.h" |
| 23 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 24 | TEST(fcntl, fcntl_smoke) { |
| 25 | int fd = open("/proc/version", O_RDONLY); |
| 26 | ASSERT_TRUE(fd != -1); |
| 27 | |
| 28 | int flags = fcntl(fd, F_GETFD); |
| 29 | ASSERT_TRUE(flags != -1); |
| 30 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 31 | |
| 32 | int rc = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 33 | ASSERT_EQ(0, rc); |
| 34 | |
| 35 | flags = fcntl(fd, F_GETFD); |
| 36 | ASSERT_TRUE(flags != -1); |
| 37 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 38 | |
| 39 | close(fd); |
| 40 | } |
| 41 | |
| 42 | TEST(fcntl, open_open64) { |
| 43 | int fd; |
| 44 | |
| 45 | fd = open("/proc/version", O_RDONLY); |
| 46 | ASSERT_TRUE(fd != -1); |
| 47 | close(fd); |
| 48 | |
| 49 | fd = open64("/proc/version", O_RDONLY); |
| 50 | ASSERT_TRUE(fd != -1); |
| 51 | close(fd); |
| 52 | } |
| 53 | |
| 54 | TEST(fcntl, openat_openat64) { |
| 55 | int fd; |
| 56 | |
| 57 | fd = openat(AT_FDCWD, "/proc/version", O_RDONLY); |
| 58 | ASSERT_TRUE(fd != -1); |
| 59 | close(fd); |
| 60 | |
| 61 | fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY); |
| 62 | ASSERT_TRUE(fd != -1); |
| 63 | close(fd); |
| 64 | } |
| 65 | |
| 66 | TEST(fcntl, creat_creat64) { |
| 67 | ASSERT_EQ(-1, creat("", 0666)); |
| 68 | ASSERT_EQ(ENOENT, errno); |
| 69 | ASSERT_EQ(-1, creat64("", 0666)); |
| 70 | ASSERT_EQ(ENOENT, errno); |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 71 | } |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 72 | |
| 73 | TEST(fcntl, fallocate_EINVAL) { |
| 74 | TemporaryFile tf; |
| 75 | |
| 76 | #if !defined(__GLIBC__) |
| 77 | errno = 0; |
| 78 | ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1)); |
| 79 | ASSERT_EQ(EINVAL, errno); |
| 80 | |
| 81 | errno = 0; |
| 82 | ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1)); |
| 83 | ASSERT_EQ(EINVAL, errno); |
| 84 | #endif |
| 85 | |
| 86 | errno = 0; |
| 87 | ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1)); |
| 88 | ASSERT_EQ(0, errno); |
| 89 | |
| 90 | errno = 0; |
| 91 | ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1)); |
| 92 | ASSERT_EQ(0, errno); |
| 93 | } |
| 94 | |
| 95 | TEST(fcntl, fallocate) { |
| 96 | TemporaryFile tf; |
| 97 | struct stat sb; |
| 98 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 99 | ASSERT_EQ(0, sb.st_size); |
| 100 | |
| 101 | #if !defined(__GLIBC__) |
| 102 | ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1)); |
| 103 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 104 | ASSERT_EQ(1, sb.st_size); |
| 105 | |
| 106 | ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2)); |
| 107 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 108 | ASSERT_EQ(2, sb.st_size); |
| 109 | #endif |
| 110 | |
| 111 | ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3)); |
| 112 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 113 | ASSERT_EQ(3, sb.st_size); |
| 114 | |
| 115 | ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4)); |
| 116 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 117 | ASSERT_EQ(4, sb.st_size); |
| 118 | } |
Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame^] | 119 | |
| 120 | TEST(fcntl, f_getlk64) { |
| 121 | int fd = open64("/proc/version", O_RDONLY); |
| 122 | ASSERT_TRUE(fd != -1); |
| 123 | |
| 124 | struct flock64 check_lock; |
| 125 | check_lock.l_type = F_WRLCK; |
| 126 | check_lock.l_start = 0; |
| 127 | check_lock.l_whence = SEEK_SET; |
| 128 | check_lock.l_len = 0; |
| 129 | |
| 130 | int rc = fcntl(fd, F_GETLK64, &check_lock); |
| 131 | ASSERT_EQ(0, rc); |
| 132 | |
| 133 | close(fd); |
| 134 | } |