| Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 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> | 
| Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 18 | #include "ScopedSignalHandler.h" | 
| Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 19 | #include "TemporaryFile.h" | 
| Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 20 |  | 
| Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 21 | #include <stdint.h> | 
| Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 22 | #include <unistd.h> | 
|  | 23 |  | 
|  | 24 | TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) { | 
|  | 25 | ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0); | 
|  | 26 | } | 
| Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 27 |  | 
|  | 28 | TEST(unistd, sbrk) { | 
|  | 29 | void* initial_break = sbrk(0); | 
|  | 30 |  | 
|  | 31 | void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000); | 
|  | 32 | ASSERT_EQ(0, brk(new_break)); | 
|  | 33 |  | 
|  | 34 | void* final_break = sbrk(0); | 
|  | 35 | ASSERT_EQ(final_break, new_break); | 
|  | 36 | } | 
| Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | TEST(unistd, truncate) { | 
|  | 39 | TemporaryFile tf; | 
|  | 40 | ASSERT_EQ(0, close(tf.fd)); | 
|  | 41 | ASSERT_EQ(0, truncate(tf.filename, 123)); | 
|  | 42 |  | 
|  | 43 | struct stat sb; | 
|  | 44 | ASSERT_EQ(0, stat(tf.filename, &sb)); | 
|  | 45 | ASSERT_EQ(123, sb.st_size); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | TEST(unistd, truncate64) { | 
|  | 49 | TemporaryFile tf; | 
|  | 50 | ASSERT_EQ(0, close(tf.fd)); | 
|  | 51 | ASSERT_EQ(0, truncate64(tf.filename, 123)); | 
|  | 52 |  | 
|  | 53 | struct stat sb; | 
|  | 54 | ASSERT_EQ(0, stat(tf.filename, &sb)); | 
|  | 55 | ASSERT_EQ(123, sb.st_size); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | TEST(unistd, ftruncate) { | 
|  | 59 | TemporaryFile tf; | 
|  | 60 | ASSERT_EQ(0, ftruncate(tf.fd, 123)); | 
|  | 61 | ASSERT_EQ(0, close(tf.fd)); | 
|  | 62 |  | 
|  | 63 | struct stat sb; | 
|  | 64 | ASSERT_EQ(0, stat(tf.filename, &sb)); | 
|  | 65 | ASSERT_EQ(123, sb.st_size); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | TEST(unistd, ftruncate64) { | 
|  | 69 | TemporaryFile tf; | 
|  | 70 | ASSERT_EQ(0, ftruncate64(tf.fd, 123)); | 
|  | 71 | ASSERT_EQ(0, close(tf.fd)); | 
|  | 72 |  | 
|  | 73 | struct stat sb; | 
|  | 74 | ASSERT_EQ(0, stat(tf.filename, &sb)); | 
|  | 75 | ASSERT_EQ(123, sb.st_size); | 
|  | 76 | } | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | static bool gPauseTestFlag = false; | 
|  | 79 | static void PauseTestSignalHandler(int) { | 
|  | 80 | gPauseTestFlag = true; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | TEST(unistd, pause) { | 
| Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 84 | ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler); | 
|  | 85 |  | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 86 | alarm(1); | 
|  | 87 | ASSERT_FALSE(gPauseTestFlag); | 
|  | 88 | ASSERT_EQ(-1, pause()); | 
|  | 89 | ASSERT_TRUE(gPauseTestFlag); | 
|  | 90 | } |