unistd_test.cpp revision 3d19a8319b9c27af8aa5cfbf495da0fe7fa62d3e
1/*
2 * Copyright (C) 2012 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#include "ScopedSignalHandler.h"
19#include "TemporaryFile.h"
20
21#include <fcntl.h>
22#include <stdint.h>
23#include <unistd.h>
24
25TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
26  ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
27}
28
29TEST(unistd, sbrk) {
30  void* initial_break = sbrk(0);
31
32  void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000);
33  ASSERT_EQ(0, brk(new_break));
34
35  void* final_break = sbrk(0);
36  ASSERT_EQ(final_break, new_break);
37}
38
39TEST(unistd, truncate) {
40  TemporaryFile tf;
41  ASSERT_EQ(0, close(tf.fd));
42  ASSERT_EQ(0, truncate(tf.filename, 123));
43
44  struct stat sb;
45  ASSERT_EQ(0, stat(tf.filename, &sb));
46  ASSERT_EQ(123, sb.st_size);
47}
48
49TEST(unistd, truncate64) {
50  TemporaryFile tf;
51  ASSERT_EQ(0, close(tf.fd));
52  ASSERT_EQ(0, truncate64(tf.filename, 123));
53
54  struct stat sb;
55  ASSERT_EQ(0, stat(tf.filename, &sb));
56  ASSERT_EQ(123, sb.st_size);
57}
58
59TEST(unistd, ftruncate) {
60  TemporaryFile tf;
61  ASSERT_EQ(0, ftruncate(tf.fd, 123));
62  ASSERT_EQ(0, close(tf.fd));
63
64  struct stat sb;
65  ASSERT_EQ(0, stat(tf.filename, &sb));
66  ASSERT_EQ(123, sb.st_size);
67}
68
69TEST(unistd, ftruncate64) {
70  TemporaryFile tf;
71  ASSERT_EQ(0, ftruncate64(tf.fd, 123));
72  ASSERT_EQ(0, close(tf.fd));
73
74  struct stat sb;
75  ASSERT_EQ(0, stat(tf.filename, &sb));
76  ASSERT_EQ(123, sb.st_size);
77}
78
79static bool gPauseTestFlag = false;
80static void PauseTestSignalHandler(int) {
81  gPauseTestFlag = true;
82}
83
84TEST(unistd, pause) {
85  ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
86
87  alarm(1);
88  ASSERT_FALSE(gPauseTestFlag);
89  ASSERT_EQ(-1, pause());
90  ASSERT_TRUE(gPauseTestFlag);
91}
92
93TEST(unistd, read) {
94  int fd = open("/proc/version", O_RDONLY);
95  ASSERT_TRUE(fd != -1);
96
97  char buf[5];
98  ASSERT_EQ(5, read(fd, buf, 5));
99  ASSERT_EQ(buf[0], 'L');
100  ASSERT_EQ(buf[1], 'i');
101  ASSERT_EQ(buf[2], 'n');
102  ASSERT_EQ(buf[3], 'u');
103  ASSERT_EQ(buf[4], 'x');
104  close(fd);
105}
106
107TEST(unistd, read_EBADF) {
108  // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
109  // our syscall stubs correctly return a 64-bit -1.
110  char buf[1];
111  ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
112  ASSERT_EQ(EBADF, errno);
113}
114