blob: f7a5afdab8f5b153c97ddc18d76b8e1d15af4f8c [file] [log] [blame]
Elliott Hughesa55f6302013-01-02 14:23:43 -08001/*
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>
Christopher Ferris13613132013-10-28 15:24:04 -070018#include "ScopedSignalHandler.h"
Elliott Hughesb4f76162013-09-19 16:27:24 -070019#include "TemporaryFile.h"
Elliott Hughesa55f6302013-01-02 14:23:43 -080020
Colin Cross3d19a832014-02-14 18:56:23 -080021#include <fcntl.h>
Elliott Hughes428f5562013-02-05 16:10:59 -080022#include <stdint.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080023#include <unistd.h>
24
25TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
26 ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
27}
Elliott Hughes428f5562013-02-05 16:10:59 -080028
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}
Elliott Hughesb4f76162013-09-19 16:27:24 -070038
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}
Elliott Hughes11952072013-10-24 15:15:14 -070078
79static bool gPauseTestFlag = false;
80static void PauseTestSignalHandler(int) {
81 gPauseTestFlag = true;
82}
83
84TEST(unistd, pause) {
Christopher Ferris13613132013-10-28 15:24:04 -070085 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
86
Elliott Hughes11952072013-10-24 15:15:14 -070087 alarm(1);
88 ASSERT_FALSE(gPauseTestFlag);
89 ASSERT_EQ(-1, pause());
90 ASSERT_TRUE(gPauseTestFlag);
91}
Colin Cross3d19a832014-02-14 18:56:23 -080092
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}