Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [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> |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame^] | 20 | #include <fcntl.h> |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 24 | #include "TemporaryFile.h" |
| 25 | |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 26 | TEST(sys_stat, futimens) { |
| 27 | FILE* fp = tmpfile(); |
| 28 | ASSERT_TRUE(fp != NULL); |
| 29 | |
| 30 | int fd = fileno(fp); |
| 31 | ASSERT_NE(fd, -1); |
| 32 | |
| 33 | timespec times[2]; |
| 34 | times[0].tv_sec = 123; |
| 35 | times[0].tv_nsec = 0; |
| 36 | times[1].tv_sec = 456; |
| 37 | times[1].tv_nsec = 0; |
| 38 | ASSERT_EQ(0, futimens(fd, times)) << strerror(errno); |
| 39 | |
| 40 | struct stat sb; |
| 41 | ASSERT_EQ(0, fstat(fd, &sb)); |
| 42 | ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime)); |
| 43 | ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime)); |
| 44 | |
| 45 | fclose(fp); |
| 46 | } |
| 47 | |
| 48 | TEST(sys_stat, futimens_EBADF) { |
| 49 | timespec times[2]; |
| 50 | times[0].tv_sec = 123; |
| 51 | times[0].tv_nsec = 0; |
| 52 | times[1].tv_sec = 456; |
| 53 | times[1].tv_nsec = 0; |
| 54 | ASSERT_EQ(-1, futimens(-1, times)); |
| 55 | ASSERT_EQ(EBADF, errno); |
| 56 | } |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 57 | |
| 58 | TEST(sys_stat, mkfifo) { |
| 59 | // Racy but probably sufficient way to get a suitable filename. |
| 60 | std::string path; |
| 61 | { |
| 62 | TemporaryFile tf; |
| 63 | path = tf.filename; |
| 64 | } |
| 65 | |
| 66 | ASSERT_EQ(0, mkfifo(path.c_str(), 0666)); |
| 67 | struct stat sb; |
| 68 | ASSERT_EQ(0, stat(path.c_str(), &sb)); |
| 69 | ASSERT_TRUE(S_ISFIFO(sb.st_mode)); |
| 70 | unlink(path.c_str()); |
| 71 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame^] | 72 | |
| 73 | TEST(sys_stat, stat64_lstat64_fstat64) { |
| 74 | struct stat64 sb; |
| 75 | ASSERT_EQ(0, stat64("/proc/version", &sb)); |
| 76 | ASSERT_EQ(0, lstat64("/proc/version", &sb)); |
| 77 | int fd = open("/proc/version", O_RDONLY); |
| 78 | ASSERT_EQ(0, fstat64(fd, &sb)); |
| 79 | close(fd); |
| 80 | } |