diff --git a/kernel_lite/process_posix/src/UidGidTest.cpp b/kernel_lite/process_posix/src/UidGidTest.cpp index cde44231371673ac2c52011b983bfc303b2a7f5b..00b916d165e620575bb268490c04d95099667d4d 100755 --- a/kernel_lite/process_posix/src/UidGidTest.cpp +++ b/kernel_lite/process_posix/src/UidGidTest.cpp @@ -640,11 +640,15 @@ HWTEST_F(UidGidTest, testGetgroups, Function | MediumTest | Level1) const int testSize = 10; gid_t gidList[testSize] = {0}; int n = getgroups(0, gidList); - ASSERT_EQ(n, 1); - ASSERT_EQ(gidList[0], 0); + ASSERT_EQ(n, 2); + int rt = getgroups(n, gidList); + ASSERT_EQ(gidList[0], SHELL_GID); + ASSERT_EQ(gidList[1], 0); + ASSERT_EQ(rt, n); n = getgroups(testSize, gidList); - ASSERT_EQ(n, 1); + ASSERT_EQ(n, 2); ASSERT_EQ(gidList[0], SHELL_GID); + ASSERT_EQ(gidList[1], 0); } /** @@ -705,12 +709,15 @@ HWTEST_F(UidGidTest, testSetgroups2, Function | MediumTest | Level1) gid_t gidListOut[4] = {0}; LOG("Init: make sure groups not changed by other test."); - int n = getgroups(1, gidListOut); - EXPECT_EQ(n, 1); + int n = getgroups(0, gidListOut); + EXPECT_EQ(n, 2); + int rt = getgroups(n, gidListOut); EXPECT_EQ(gidListOut[0], SHELL_GID); + EXPECT_EQ(gidListOut[1], 0); + EXPECT_EQ(rt, n); LOG("add 2 groups"); - int rt = setgroups(2, gidListIn); + rt = setgroups(2, gidListIn); EXPECT_EQ(rt, 0); n = getgroups(4, gidListOut); EXPECT_EQ(n, 3); @@ -721,8 +728,10 @@ HWTEST_F(UidGidTest, testSetgroups2, Function | MediumTest | Level1) LOG("clear groups"); rt = setgroups(0, NULL); EXPECT_EQ(rt, 0); - n = getgroups(2, gidListOut); + n = getgroups(0, gidListOut); EXPECT_EQ(n, 1); + rt = getgroups(n, gidListOut); + EXPECT_EQ(rt, n); EXPECT_EQ(gidListOut[0], SHELL_GID); } diff --git a/kernel_lite/process_posix/src/UidGidTest.h b/kernel_lite/process_posix/src/UidGidTest.h index fc3a1131322bc4814e583a39ee7dc4952f294c32..57f9d9a18e40d5ecd3f47fa944f89082594383ae 100755 --- a/kernel_lite/process_posix/src/UidGidTest.h +++ b/kernel_lite/process_posix/src/UidGidTest.h @@ -23,6 +23,9 @@ // max test number of uid/gid, not actual max number const int MAX_UGID = 100000; +static int groupNum = -1; +const int ARRAY_SIZE = 10; +static gid_t groupsArray[ARRAY_SIZE] = {0}; // Assert all uid == expectUid #define AssertAllUid(expectUid) do { \ @@ -54,14 +57,30 @@ public: return id; } protected: + static void SetUpTestCase() + { + for (int i = 0; i < ARRAY_SIZE; i++) { + groupsArray[i] = -1; + } + groupNum = getgroups(0, groupsArray); + EXPECT_NE(groupNum, -1); + int rt = getgroups(groupNum, groupsArray); + EXPECT_EQ(rt, groupNum); + } void TearDown() { LOG("TearDown: reset uid and gid"); + gid_t groupIds[groupNum]; + LOG("TearDown: reset uid and gid %d", sizeof(groupIds) / sizeof(gid_t)); + for (int i = 0; i < groupNum; i++) { + groupIds[i] = groupsArray[i]; + } setuid(SHELL_UID); setgid(SHELL_GID); AssertAllUid(SHELL_UID); AssertAllGid(SHELL_GID); - int rt = setgroups(0, NULL); + int rt = setgroups(groupNum, groupIds); + EXPECT_EQ(rt, 0); } };