Home | History | Annotate | Download | only in tests
      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 <sys/resource.h>
     18 
     19 #include <gtest/gtest.h>
     20 
     21 TEST(sys_resource, rlimit_struct_size) {
     22 #if defined(__LP64__) || defined(__GLIBC__)
     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 
     31 class 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_));
     36     ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64_));
     37   }
     38 
     39   void CheckResourceLimits();
     40 
     41  protected:
     42   rlimit l32_;
     43   rlimit64 l64_;
     44   rlimit64 pr_l64_;
     45 };
     46 
     47 void SysResourceTest::CheckResourceLimits() {
     48   ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_));
     49   ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_));
     50   ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64_));
     51   ASSERT_EQ(l64_.rlim_cur, pr_l64_.rlim_cur);
     52   if (l64_.rlim_cur == RLIM64_INFINITY) {
     53     ASSERT_EQ(RLIM_INFINITY, l32_.rlim_cur);
     54   } else {
     55     ASSERT_EQ(l64_.rlim_cur, l32_.rlim_cur);
     56   }
     57 
     58   ASSERT_EQ(l64_.rlim_max, pr_l64_.rlim_max);
     59   if (l64_.rlim_max == RLIM64_INFINITY) {
     60     ASSERT_EQ(RLIM_INFINITY, l32_.rlim_max);
     61   } else {
     62     ASSERT_EQ(l64_.rlim_max, l32_.rlim_max);
     63   }
     64 }
     65 
     66 // Force rlim_max to be bigger than a constant so we can continue following test.
     67 // Change resource limit setting with "ulimit -Hc" in the shell if this test fails.
     68 TEST_F(SysResourceTest, RLIMIT_CORE_rlim_max_not_zero) {
     69   ASSERT_TRUE(l32_.rlim_max == RLIM_INFINITY || l32_.rlim_max >= 456U) <<
     70     "RLIMIT_CORE rlim_max = " << l32_.rlim_max;
     71 }
     72 
     73 TEST_F(SysResourceTest, get_resource_limit_equal) {
     74   CheckResourceLimits();
     75 }
     76 
     77 TEST_F(SysResourceTest, setrlimit) {
     78   l32_.rlim_cur = 123U;
     79   ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32_));
     80   CheckResourceLimits();
     81   ASSERT_EQ(123U, l32_.rlim_cur);
     82 }
     83 
     84 TEST_F(SysResourceTest, setrlimit64) {
     85   l64_.rlim_cur = 456U;
     86   ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64_));
     87   CheckResourceLimits();
     88   ASSERT_EQ(456U, l64_.rlim_cur);
     89 }
     90 
     91 TEST_F(SysResourceTest, prlimit64) {
     92   pr_l64_.rlim_cur = pr_l64_.rlim_max;
     93   ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &pr_l64_, NULL));
     94   CheckResourceLimits();
     95   ASSERT_EQ(pr_l64_.rlim_max, pr_l64_.rlim_cur);
     96 }
     97 
     98 TEST_F(SysResourceTest, prlimit) {
     99   // prlimit is prlimit64 on LP64 and unimplemented on 32-bit. So we only test prlimit64.
    100 }
    101