blob: 0b6b6ef6b086802ff35e818172c038074fdc58ee [file] [log] [blame]
Elliott Hughes0f461e32014-01-09 10:17:03 -08001/*
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 Hughes0f461e32014-01-09 10:17:03 -080017#include <sys/resource.h>
18
Yabin Cui4853f402015-01-05 11:06:30 -080019#include <gtest/gtest.h>
20
21TEST(sys_resource, rlimit_struct_size) {
Elliott Hughes063525c2014-05-13 11:19:57 -070022#if defined(__LP64__) || defined(__GLIBC__)
Elliott Hughes0f461e32014-01-09 10:17:03 -080023 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
Yabin Cui4853f402015-01-05 11:06:30 -080029}
Elliott Hughes0f461e32014-01-09 10:17:03 -080030
Yabin Cui4853f402015-01-05 11:06:30 -080031class SysResourceTest : public ::testing::Test {
32 protected:
33 virtual void SetUp() {
34 ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_));
35 ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_));
Elliott Hughes4151db52015-10-28 17:14:48 -070036 ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, nullptr, &pr_l32_));
37 ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, nullptr, &pr_l64_));
Elliott Hughes0f461e32014-01-09 10:17:03 -080038 }
39
Yabin Cui4853f402015-01-05 11:06:30 -080040 void CheckResourceLimits();
Elliott Hughes0f461e32014-01-09 10:17:03 -080041
Yabin Cui4853f402015-01-05 11:06:30 -080042 protected:
43 rlimit l32_;
44 rlimit64 l64_;
Elliott Hughes4151db52015-10-28 17:14:48 -070045 rlimit pr_l32_;
Yabin Cui4853f402015-01-05 11:06:30 -080046 rlimit64 pr_l64_;
47};
Elliott Hughes0f461e32014-01-09 10:17:03 -080048
Yabin Cui4853f402015-01-05 11:06:30 -080049void SysResourceTest::CheckResourceLimits() {
50 ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_));
51 ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_));
Elliott Hughes4151db52015-10-28 17:14:48 -070052 ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, nullptr, &pr_l32_));
53 ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, nullptr, &pr_l64_));
54
55 ASSERT_EQ(l32_.rlim_cur, pr_l32_.rlim_cur);
Yabin Cui4853f402015-01-05 11:06:30 -080056 ASSERT_EQ(l64_.rlim_cur, pr_l64_.rlim_cur);
Elliott Hughes4151db52015-10-28 17:14:48 -070057
Yabin Cui4853f402015-01-05 11:06:30 -080058 if (l64_.rlim_cur == RLIM64_INFINITY) {
59 ASSERT_EQ(RLIM_INFINITY, l32_.rlim_cur);
60 } else {
61 ASSERT_EQ(l64_.rlim_cur, l32_.rlim_cur);
62 }
63
Elliott Hughes4151db52015-10-28 17:14:48 -070064 ASSERT_EQ(l32_.rlim_max, pr_l32_.rlim_max);
Yabin Cui4853f402015-01-05 11:06:30 -080065 ASSERT_EQ(l64_.rlim_max, pr_l64_.rlim_max);
Elliott Hughes4151db52015-10-28 17:14:48 -070066
Yabin Cui4853f402015-01-05 11:06:30 -080067 if (l64_.rlim_max == RLIM64_INFINITY) {
68 ASSERT_EQ(RLIM_INFINITY, l32_.rlim_max);
69 } else {
70 ASSERT_EQ(l64_.rlim_max, l32_.rlim_max);
71 }
72}
73
74// Force rlim_max to be bigger than a constant so we can continue following test.
75// Change resource limit setting with "ulimit -Hc" in the shell if this test fails.
76TEST_F(SysResourceTest, RLIMIT_CORE_rlim_max_not_zero) {
77 ASSERT_TRUE(l32_.rlim_max == RLIM_INFINITY || l32_.rlim_max >= 456U) <<
78 "RLIMIT_CORE rlim_max = " << l32_.rlim_max;
79}
80
81TEST_F(SysResourceTest, get_resource_limit_equal) {
82 CheckResourceLimits();
83}
84
85TEST_F(SysResourceTest, setrlimit) {
86 l32_.rlim_cur = 123U;
87 ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32_));
88 CheckResourceLimits();
89 ASSERT_EQ(123U, l32_.rlim_cur);
90}
91
92TEST_F(SysResourceTest, setrlimit64) {
93 l64_.rlim_cur = 456U;
94 ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64_));
95 CheckResourceLimits();
96 ASSERT_EQ(456U, l64_.rlim_cur);
97}
98
Elliott Hughes4151db52015-10-28 17:14:48 -070099TEST_F(SysResourceTest, prlimit) {
100 pr_l32_.rlim_cur = pr_l32_.rlim_max;
101 ASSERT_EQ(0, prlimit(0, RLIMIT_CORE, &pr_l32_, nullptr));
Yabin Cui4853f402015-01-05 11:06:30 -0800102 CheckResourceLimits();
Elliott Hughes4151db52015-10-28 17:14:48 -0700103 ASSERT_EQ(pr_l32_.rlim_max, pr_l32_.rlim_cur);
Yabin Cui4853f402015-01-05 11:06:30 -0800104}
105
Elliott Hughes4151db52015-10-28 17:14:48 -0700106TEST_F(SysResourceTest, prlimit64) {
107 pr_l64_.rlim_cur = pr_l64_.rlim_max;
108 ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &pr_l64_, nullptr));
109 CheckResourceLimits();
110 ASSERT_EQ(pr_l64_.rlim_max, pr_l64_.rlim_cur);
Elliott Hughes0f461e32014-01-09 10:17:03 -0800111}