unistd_test.cpp revision 3d19a8319b9c27af8aa5cfbf495da0fe7fa62d3e
1a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes/*
2a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes *
4a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * you may not use this file except in compliance with the License.
6a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * You may obtain a copy of the License at
7a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes *
8a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes *
10a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * See the License for the specific language governing permissions and
14a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes * limitations under the License.
15a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes */
16a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes
17a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes#include <gtest/gtest.h>
1813613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris#include "ScopedSignalHandler.h"
19b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes#include "TemporaryFile.h"
20a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes
213d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross#include <fcntl.h>
22428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes#include <stdint.h>
23a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes#include <unistd.h>
24a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes
25a55f63083fb16b2595f517a3260083e5f8cddd02Elliott HughesTEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
26a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes  ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
27a55f63083fb16b2595f517a3260083e5f8cddd02Elliott Hughes}
28428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes
29428f5567be25b8090e3dd72e2d3d337c305b514eElliott HughesTEST(unistd, sbrk) {
30428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes  void* initial_break = sbrk(0);
31428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes
32428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes  void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000);
33428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes  ASSERT_EQ(0, brk(new_break));
34428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes
35428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes  void* final_break = sbrk(0);
36428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes  ASSERT_EQ(final_break, new_break);
37428f5567be25b8090e3dd72e2d3d337c305b514eElliott Hughes}
38b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
39b4f7616fd618875768b8fffc122b58bdb84a9969Elliott HughesTEST(unistd, truncate) {
40b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  TemporaryFile tf;
41b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, close(tf.fd));
42b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, truncate(tf.filename, 123));
43b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
44b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  struct stat sb;
45b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, stat(tf.filename, &sb));
46b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(123, sb.st_size);
47b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes}
48b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
49b4f7616fd618875768b8fffc122b58bdb84a9969Elliott HughesTEST(unistd, truncate64) {
50b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  TemporaryFile tf;
51b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, close(tf.fd));
52b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, truncate64(tf.filename, 123));
53b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
54b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  struct stat sb;
55b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, stat(tf.filename, &sb));
56b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(123, sb.st_size);
57b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes}
58b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
59b4f7616fd618875768b8fffc122b58bdb84a9969Elliott HughesTEST(unistd, ftruncate) {
60b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  TemporaryFile tf;
61b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, ftruncate(tf.fd, 123));
62b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, close(tf.fd));
63b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
64b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  struct stat sb;
65b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, stat(tf.filename, &sb));
66b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(123, sb.st_size);
67b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes}
68b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
69b4f7616fd618875768b8fffc122b58bdb84a9969Elliott HughesTEST(unistd, ftruncate64) {
70b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  TemporaryFile tf;
71b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, ftruncate64(tf.fd, 123));
72b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, close(tf.fd));
73b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes
74b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  struct stat sb;
75b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(0, stat(tf.filename, &sb));
76b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes  ASSERT_EQ(123, sb.st_size);
77b4f7616fd618875768b8fffc122b58bdb84a9969Elliott Hughes}
7811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
7911952073af22568bba0b661f7a9d4402c443a888Elliott Hughesstatic bool gPauseTestFlag = false;
8011952073af22568bba0b661f7a9d4402c443a888Elliott Hughesstatic void PauseTestSignalHandler(int) {
8111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  gPauseTestFlag = true;
8211952073af22568bba0b661f7a9d4402c443a888Elliott Hughes}
8311952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
8411952073af22568bba0b661f7a9d4402c443a888Elliott HughesTEST(unistd, pause) {
8513613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris  ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
8613613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris
8711952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  alarm(1);
8811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_FALSE(gPauseTestFlag);
8911952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_EQ(-1, pause());
9011952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_TRUE(gPauseTestFlag);
9111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes}
923d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross
933d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin CrossTEST(unistd, read) {
943d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  int fd = open("/proc/version", O_RDONLY);
953d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_TRUE(fd != -1);
963d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross
973d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  char buf[5];
983d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(5, read(fd, buf, 5));
993d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(buf[0], 'L');
1003d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(buf[1], 'i');
1013d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(buf[2], 'n');
1023d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(buf[3], 'u');
1033d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(buf[4], 'x');
1043d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  close(fd);
1053d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross}
1063d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross
1073d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin CrossTEST(unistd, read_EBADF) {
1083d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
1093d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  // our syscall stubs correctly return a 64-bit -1.
1103d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  char buf[1];
1113d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
1123d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross  ASSERT_EQ(EBADF, errno);
1133d19a8319b9c27af8aa5cfbf495da0fe7fa62d3eColin Cross}
114