Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <sys/resource.h> |
| 20 | |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 21 | TEST(sys_resource, smoke) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 22 | #if defined(__LP64__) || defined(__GLIBC__) |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 23 | ASSERT_EQ(sizeof(rlimit), sizeof(rlimit64)); |
| 24 | ASSERT_EQ(8U, sizeof(rlim_t)); |
| 25 | #else |
| 26 | ASSERT_NE(sizeof(rlimit), sizeof(rlimit64)); |
| 27 | ASSERT_EQ(4U, sizeof(rlim_t)); |
| 28 | #endif |
| 29 | |
| 30 | // Read with getrlimit, getrlimit64, and prlimit64. |
| 31 | // (prlimit is prlimit64 on LP64 and unimplemented on 32-bit.) |
| 32 | rlimit l32; |
| 33 | rlimit64 l64; |
| 34 | rlimit64 pr_l64; |
| 35 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32)); |
| 36 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64)); |
| 37 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64)); |
| 38 | ASSERT_EQ(l64.rlim_cur, l32.rlim_cur); |
| 39 | ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur); |
| 40 | ASSERT_EQ(l64.rlim_max, pr_l64.rlim_max); |
| 41 | if (l64.rlim_max == RLIM64_INFINITY) { |
| 42 | ASSERT_EQ(RLIM_INFINITY, l32.rlim_max); |
| 43 | } else { |
| 44 | ASSERT_EQ(l64.rlim_max, l32.rlim_max); |
| 45 | } |
| 46 | |
| 47 | // Write with setrlimit and read back with everything. |
| 48 | l32.rlim_cur = 123; |
| 49 | ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32)); |
| 50 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32)); |
| 51 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64)); |
| 52 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64)); |
| 53 | ASSERT_EQ(123U, l32.rlim_cur); |
| 54 | ASSERT_EQ(l64.rlim_cur, l32.rlim_cur); |
| 55 | ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur); |
| 56 | |
| 57 | // Write with setrlimit64 and read back with everything. |
| 58 | l64.rlim_cur = 456; |
| 59 | ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64)); |
| 60 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32)); |
| 61 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64)); |
| 62 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64)); |
| 63 | ASSERT_EQ(456U, l32.rlim_cur); |
| 64 | ASSERT_EQ(l64.rlim_cur, l32.rlim_cur); |
| 65 | ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur); |
| 66 | |
| 67 | // Write with prlimit64 and read back with everything. |
| 68 | l64.rlim_cur = 789; |
| 69 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &l64, NULL)); |
| 70 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32)); |
| 71 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64)); |
| 72 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64)); |
| 73 | ASSERT_EQ(789U, l32.rlim_cur); |
| 74 | ASSERT_EQ(l64.rlim_cur, l32.rlim_cur); |
| 75 | ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur); |
| 76 | } |