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 | |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 17 | #include <sys/resource.h> |
| 18 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | TEST(sys_resource, rlimit_struct_size) { |
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 |
Elie Kheirallah | 0565497 | 2022-12-08 19:20:08 +0000 | [diff] [blame] | 29 | ASSERT_EQ(8U, sizeof(rlim64_t)); |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 30 | } |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 31 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 32 | class SysResourceTest : public ::testing::Test { |
| 33 | protected: |
Yi Kong | 358603a | 2019-03-29 14:25:16 -0700 | [diff] [blame] | 34 | void SetUp() override { |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 35 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_)); |
| 36 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_)); |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 37 | ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, nullptr, &pr_l32_)); |
| 38 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, nullptr, &pr_l64_)); |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 41 | void CheckResourceLimits(); |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 42 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 43 | protected: |
| 44 | rlimit l32_; |
| 45 | rlimit64 l64_; |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 46 | rlimit pr_l32_; |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 47 | rlimit64 pr_l64_; |
| 48 | }; |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 49 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 50 | void SysResourceTest::CheckResourceLimits() { |
| 51 | ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_)); |
| 52 | ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_)); |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 53 | ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, nullptr, &pr_l32_)); |
| 54 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, nullptr, &pr_l64_)); |
| 55 | |
| 56 | ASSERT_EQ(l32_.rlim_cur, pr_l32_.rlim_cur); |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 57 | ASSERT_EQ(l64_.rlim_cur, pr_l64_.rlim_cur); |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 58 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 59 | if (l64_.rlim_cur == RLIM64_INFINITY) { |
| 60 | ASSERT_EQ(RLIM_INFINITY, l32_.rlim_cur); |
| 61 | } else { |
| 62 | ASSERT_EQ(l64_.rlim_cur, l32_.rlim_cur); |
| 63 | } |
| 64 | |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 65 | ASSERT_EQ(l32_.rlim_max, pr_l32_.rlim_max); |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 66 | ASSERT_EQ(l64_.rlim_max, pr_l64_.rlim_max); |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 67 | |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 68 | if (l64_.rlim_max == RLIM64_INFINITY) { |
| 69 | ASSERT_EQ(RLIM_INFINITY, l32_.rlim_max); |
| 70 | } else { |
| 71 | ASSERT_EQ(l64_.rlim_max, l32_.rlim_max); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Force rlim_max to be bigger than a constant so we can continue following test. |
| 76 | // Change resource limit setting with "ulimit -Hc" in the shell if this test fails. |
| 77 | TEST_F(SysResourceTest, RLIMIT_CORE_rlim_max_not_zero) { |
| 78 | ASSERT_TRUE(l32_.rlim_max == RLIM_INFINITY || l32_.rlim_max >= 456U) << |
| 79 | "RLIMIT_CORE rlim_max = " << l32_.rlim_max; |
| 80 | } |
| 81 | |
| 82 | TEST_F(SysResourceTest, get_resource_limit_equal) { |
| 83 | CheckResourceLimits(); |
| 84 | } |
| 85 | |
| 86 | TEST_F(SysResourceTest, setrlimit) { |
| 87 | l32_.rlim_cur = 123U; |
| 88 | ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32_)); |
| 89 | CheckResourceLimits(); |
| 90 | ASSERT_EQ(123U, l32_.rlim_cur); |
| 91 | } |
| 92 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 93 | TEST_F(SysResourceTest, setrlimit64_smoke) { |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 94 | l64_.rlim_cur = 456U; |
| 95 | ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64_)); |
| 96 | CheckResourceLimits(); |
| 97 | ASSERT_EQ(456U, l64_.rlim_cur); |
| 98 | } |
| 99 | |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 100 | TEST_F(SysResourceTest, prlimit) { |
| 101 | pr_l32_.rlim_cur = pr_l32_.rlim_max; |
| 102 | ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, &pr_l32_, nullptr)); |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 103 | CheckResourceLimits(); |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 104 | ASSERT_EQ(pr_l32_.rlim_max, pr_l32_.rlim_cur); |
Yabin Cui | 4853f40 | 2015-01-05 11:06:30 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 107 | TEST_F(SysResourceTest, prlimit64_smoke) { |
Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 108 | pr_l64_.rlim_cur = pr_l64_.rlim_max; |
| 109 | ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &pr_l64_, nullptr)); |
| 110 | CheckResourceLimits(); |
| 111 | ASSERT_EQ(pr_l64_.rlim_max, pr_l64_.rlim_cur); |
Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 112 | } |