sys_stat_test.cpp revision 594b1a4af204aa9de2b4913182f4556e38d71648
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 <stdlib.h>
21#include <sys/stat.h>
22
23#include "TemporaryFile.h"
24
25TEST(sys_stat, futimens) {
26  FILE* fp = tmpfile();
27  ASSERT_TRUE(fp != NULL);
28
29  int fd = fileno(fp);
30  ASSERT_NE(fd, -1);
31
32  timespec times[2];
33  times[0].tv_sec = 123;
34  times[0].tv_nsec = 0;
35  times[1].tv_sec = 456;
36  times[1].tv_nsec = 0;
37  ASSERT_EQ(0, futimens(fd, times)) << strerror(errno);
38
39  struct stat sb;
40  ASSERT_EQ(0, fstat(fd, &sb));
41  ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime));
42  ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime));
43
44  fclose(fp);
45}
46
47TEST(sys_stat, futimens_EBADF) {
48  timespec times[2];
49  times[0].tv_sec = 123;
50  times[0].tv_nsec = 0;
51  times[1].tv_sec = 456;
52  times[1].tv_nsec = 0;
53  ASSERT_EQ(-1, futimens(-1, times));
54  ASSERT_EQ(EBADF, errno);
55}
56
57TEST(sys_stat, mkfifo) {
58  // Racy but probably sufficient way to get a suitable filename.
59  std::string path;
60  {
61    TemporaryFile tf;
62    path = tf.filename;
63  }
64
65  ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
66  struct stat sb;
67  ASSERT_EQ(0, stat(path.c_str(), &sb));
68  ASSERT_TRUE(S_ISFIFO(sb.st_mode));
69  unlink(path.c_str());
70}
71