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 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #include "TemporaryFile.h"
25
TEST(sys_stat,futimens)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
TEST(sys_stat,futimens_EBADF)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 }
57
TEST(sys_stat,mkfifo)58 TEST(sys_stat, mkfifo) {
59 if (getuid() == 0) {
60 // Racy but probably sufficient way to get a suitable filename.
61 std::string path;
62 {
63 TemporaryFile tf;
64 path = tf.filename;
65 }
66
67 ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
68 struct stat sb;
69 ASSERT_EQ(0, stat(path.c_str(), &sb));
70 ASSERT_TRUE(S_ISFIFO(sb.st_mode));
71 unlink(path.c_str());
72 } else {
73 GTEST_LOG_(INFO) << "This test only performs a test when run as root.";
74 }
75 }
76
TEST(sys_stat,stat64_lstat64_fstat64)77 TEST(sys_stat, stat64_lstat64_fstat64) {
78 struct stat64 sb;
79 ASSERT_EQ(0, stat64("/proc/version", &sb));
80 ASSERT_EQ(0, lstat64("/proc/version", &sb));
81 int fd = open("/proc/version", O_RDONLY);
82 ASSERT_EQ(0, fstat64(fd, &sb));
83 close(fd);
84 }
85