summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-01-05 11:06:30 -0800
committerYabin Cui <yabinc@google.com>2015-06-02 14:40:26 -0700
commite7ece90b5036a56241c268861bb0dea87aa856b7 (patch)
tree86219aa2942573705bb16017b54703627d93a5dd /tests
parentc891e24073830e07ba7373dee554ff2c70e1d313 (diff)
downloadbionic-e7ece90b5036a56241c268861bb0dea87aa856b7.zip
bionic-e7ece90b5036a56241c268861bb0dea87aa856b7.tar.gz
bionic-e7ece90b5036a56241c268861bb0dea87aa856b7.tar.bz2
Make sys_resource test more robust.
Bug: 19482626 "ulimit -c xxx" command may run before bionic-unit-tests. Make sure sys_resource test fails gently in that case. Change-Id: Ic3b5ed8b20acba56df8c5ef082c88e5050e761aa (cherry picked from commit 4853f40f3fb6664cb1b7af97b6b3e795717026b1)
Diffstat (limited to 'tests')
-rw-r--r--tests/sys_resource_test.cpp112
1 files changed, 68 insertions, 44 deletions
diff --git a/tests/sys_resource_test.cpp b/tests/sys_resource_test.cpp
index d6d99a0..4e2d0ba 100644
--- a/tests/sys_resource_test.cpp
+++ b/tests/sys_resource_test.cpp
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-#include <gtest/gtest.h>
-
#include <sys/resource.h>
+#include <gtest/gtest.h>
+
#if defined(__GLIBC__)
/* The host glibc we're currently building with doesn't have prlimit64 yet. */
static int prlimit64(pid_t, int resource, const struct rlimit64* new_limit, struct rlimit64* old_limit) {
@@ -29,7 +29,7 @@ static int prlimit64(pid_t, int resource, const struct rlimit64* new_limit, stru
}
#endif
-TEST(sys_resource, smoke) {
+TEST(sys_resource, rlimit_struct_size) {
#if defined(__LP64__) || defined(__GLIBC__)
ASSERT_EQ(sizeof(rlimit), sizeof(rlimit64));
ASSERT_EQ(8U, sizeof(rlim_t));
@@ -37,51 +37,75 @@ TEST(sys_resource, smoke) {
ASSERT_NE(sizeof(rlimit), sizeof(rlimit64));
ASSERT_EQ(4U, sizeof(rlim_t));
#endif
+}
- // Read with getrlimit, getrlimit64, and prlimit64.
- // (prlimit is prlimit64 on LP64 and unimplemented on 32-bit.)
- rlimit l32;
- rlimit64 l64;
- rlimit64 pr_l64;
- ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
- ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
- ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
- ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
- ASSERT_EQ(l64.rlim_max, pr_l64.rlim_max);
- if (l64.rlim_max == RLIM64_INFINITY) {
- ASSERT_EQ(RLIM_INFINITY, l32.rlim_max);
+class SysResourceTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_));
+ ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_));
+ ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64_));
+ }
+
+ void CheckResourceLimits();
+
+ protected:
+ rlimit l32_;
+ rlimit64 l64_;
+ rlimit64 pr_l64_;
+};
+
+void SysResourceTest::CheckResourceLimits() {
+ ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32_));
+ ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64_));
+ ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64_));
+ ASSERT_EQ(l64_.rlim_cur, pr_l64_.rlim_cur);
+ if (l64_.rlim_cur == RLIM64_INFINITY) {
+ ASSERT_EQ(RLIM_INFINITY, l32_.rlim_cur);
} else {
- ASSERT_EQ(l64.rlim_max, l32.rlim_max);
+ ASSERT_EQ(l64_.rlim_cur, l32_.rlim_cur);
}
- // Write with setrlimit and read back with everything.
- l32.rlim_cur = 123;
- ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32));
- ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
- ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
- ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
- ASSERT_EQ(123U, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
+ ASSERT_EQ(l64_.rlim_max, pr_l64_.rlim_max);
+ if (l64_.rlim_max == RLIM64_INFINITY) {
+ ASSERT_EQ(RLIM_INFINITY, l32_.rlim_max);
+ } else {
+ ASSERT_EQ(l64_.rlim_max, l32_.rlim_max);
+ }
+}
+
+// Force rlim_max to be bigger than a constant so we can continue following test.
+// Change resource limit setting with "ulimit -Hc" in the shell if this test fails.
+TEST_F(SysResourceTest, RLIMIT_CORE_rlim_max_not_zero) {
+ ASSERT_TRUE(l32_.rlim_max == RLIM_INFINITY || l32_.rlim_max >= 456U) <<
+ "RLIMIT_CORE rlim_max = " << l32_.rlim_max;
+}
+
+TEST_F(SysResourceTest, get_resource_limit_equal) {
+ CheckResourceLimits();
+}
- // Write with setrlimit64 and read back with everything.
- l64.rlim_cur = 456;
- ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64));
- ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
- ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
- ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
- ASSERT_EQ(456U, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
+TEST_F(SysResourceTest, setrlimit) {
+ l32_.rlim_cur = 123U;
+ ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &l32_));
+ CheckResourceLimits();
+ ASSERT_EQ(123U, l32_.rlim_cur);
+}
+
+TEST_F(SysResourceTest, setrlimit64) {
+ l64_.rlim_cur = 456U;
+ ASSERT_EQ(0, setrlimit64(RLIMIT_CORE, &l64_));
+ CheckResourceLimits();
+ ASSERT_EQ(456U, l64_.rlim_cur);
+}
+
+TEST_F(SysResourceTest, prlimit64) {
+ pr_l64_.rlim_cur = pr_l64_.rlim_max;
+ ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &pr_l64_, NULL));
+ CheckResourceLimits();
+ ASSERT_EQ(pr_l64_.rlim_max, pr_l64_.rlim_cur);
+}
- // Write with prlimit64 and read back with everything.
- l64.rlim_cur = 789;
- ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, &l64, NULL));
- ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &l32));
- ASSERT_EQ(0, getrlimit64(RLIMIT_CORE, &l64));
- ASSERT_EQ(0, prlimit64(0, RLIMIT_CORE, NULL, &pr_l64));
- ASSERT_EQ(789U, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, l32.rlim_cur);
- ASSERT_EQ(l64.rlim_cur, pr_l64.rlim_cur);
+TEST_F(SysResourceTest, prlimit) {
+ // prlimit is prlimit64 on LP64 and unimplemented on 32-bit. So we only test prlimit64.
}