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 */
17 #include <gtest/gtest.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
24 #include "TemporaryFile.h"
26 TEST(sys_stat, futimens) {
27 FILE* fp = tmpfile();
28 ASSERT_TRUE(fp != NULL);
30 int fd = fileno(fp);
31 ASSERT_NE(fd, -1);
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);
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));
45 fclose(fp);
46 }
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 }
58 TEST(sys_stat, mkfifo_failure) {
59 errno = 0;
60 ASSERT_EQ(-1, mkfifo("/", 0666));
61 ASSERT_EQ(EEXIST, errno);
62 }
64 TEST(sys_stat, mkfifoat_failure) {
65 errno = 0;
66 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
67 ASSERT_EQ(EBADF, errno);
68 }
70 TEST(sys_stat, mkfifo) {
71 if (getuid() == 0) {
72 // Racy but probably sufficient way to get a suitable filename.
73 std::string path;
74 {
75 TemporaryFile tf;
76 path = tf.filename;
77 }
79 ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
80 struct stat sb;
81 ASSERT_EQ(0, stat(path.c_str(), &sb));
82 ASSERT_TRUE(S_ISFIFO(sb.st_mode));
83 unlink(path.c_str());
84 } else {
85 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
86 GTEST_LOG_(INFO) << "This test only performs a test when run as root.";
87 }
88 }
90 TEST(sys_stat, stat64_lstat64_fstat64) {
91 struct stat64 sb;
92 ASSERT_EQ(0, stat64("/proc/version", &sb));
93 ASSERT_EQ(0, lstat64("/proc/version", &sb));
94 int fd = open("/proc/version", O_RDONLY);
95 ASSERT_EQ(0, fstat64(fd, &sb));
96 close(fd);
97 }