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); |
| 38 | } |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 39 | |
| 40 | TEST(fcntl, fallocate_EINVAL) { |
| 41 | TemporaryFile tf; |
| 42 | |
| 43 | #if !defined(__GLIBC__) |
| 44 | errno = 0; |
| 45 | ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1)); |
| 46 | ASSERT_EQ(EINVAL, errno); |
| 47 | |
| 48 | errno = 0; |
| 49 | ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1)); |
| 50 | ASSERT_EQ(EINVAL, errno); |
| 51 | #endif |
| 52 | |
| 53 | errno = 0; |
| 54 | ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1)); |
| 55 | ASSERT_EQ(0, errno); |
| 56 | |
| 57 | errno = 0; |
| 58 | ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1)); |
| 59 | ASSERT_EQ(0, errno); |
| 60 | } |
| 61 | |
| 62 | TEST(fcntl, fallocate) { |
| 63 | TemporaryFile tf; |
| 64 | struct stat sb; |
| 65 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 66 | ASSERT_EQ(0, sb.st_size); |
| 67 | |
| 68 | #if !defined(__GLIBC__) |
| 69 | ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1)); |
| 70 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 71 | ASSERT_EQ(1, sb.st_size); |
| 72 | |
| 73 | ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2)); |
| 74 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 75 | ASSERT_EQ(2, sb.st_size); |
| 76 | #endif |
| 77 | |
| 78 | ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3)); |
| 79 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 80 | ASSERT_EQ(3, sb.st_size); |
| 81 | |
| 82 | ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4)); |
| 83 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 84 | ASSERT_EQ(4, sb.st_size); |
| 85 | } |