blob: 64049ab61b0c7554c8dc2c6ad75a37ea63590245 [file] [log] [blame]
Elliott Hughesd0be7c82013-08-08 17:13:33 -07001/*
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 Hughesdb1ea342014-01-17 18:42:49 -080020#include <fcntl.h>
Elliott Hughesd0be7c82013-08-08 17:13:33 -070021#include <stdlib.h>
22#include <sys/stat.h>
23
Elliott Hughes594b1a42013-10-22 10:54:11 -070024#include "TemporaryFile.h"
25
Elliott Hughesd0be7c82013-08-08 17:13:33 -070026TEST(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
48TEST(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 Hughes594b1a42013-10-22 10:54:11 -070057
58TEST(sys_stat, mkfifo) {
Christopher Ferris6c69afd2014-09-24 16:01:18 -070059 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 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070066
Christopher Ferris6c69afd2014-09-24 16:01:18 -070067 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 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070075}
Elliott Hughesdb1ea342014-01-17 18:42:49 -080076
77TEST(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}