From fbf3b363999971489e67dc51741022e7f9cb4dba Mon Sep 17 00:00:00 2001 From: Federico Aponte Date: Thu, 13 Jun 2024 18:20:03 +0200 Subject: [PATCH] Add unit test for NSS users/groups Signed-off-by: Federico Aponte --- userspace/libsinsp/test/user.ut.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/userspace/libsinsp/test/user.ut.cpp b/userspace/libsinsp/test/user.ut.cpp index 34ef20c010..c5a06153a8 100644 --- a/userspace/libsinsp/test/user.ut.cpp +++ b/userspace/libsinsp/test/user.ut.cpp @@ -169,13 +169,13 @@ class usergroup_manager_host_root_test : public sinsp_with_test_input { std::ofstream ofs(etc + "/passwd"); - ofs << "toor:x:0:0:toor:/toor:/bin/ash" << std::endl; - ofs.close(); + ofs << "toor:x:0:0:toor:/toor:/bin/ash\n" + << "+testuser::::::\n"; } { std::ofstream ofs(etc + "/group"); - ofs << "toor:x:0:toor" << std::endl; - ofs.close(); + ofs << "toor:x:0:toor\n" + << "+testgroup::::::\n"; } } @@ -211,4 +211,19 @@ TEST_F(usergroup_manager_host_root_test, host_root_lookup) ASSERT_EQ(group->gid, 0); ASSERT_STREQ(group->name, "toor"); } + +TEST_F(usergroup_manager_host_root_test, nss_user_lookup) +{ + std::string container_id; // empty container_id means host + + sinsp_usergroup_manager mgr(&m_inspector); + mgr.add_user(container_id, -1, 0, 0, {}, {}, {}); + mgr.add_user(container_id, -1, 65534, 0, {}, {}, {}); + + auto* usr = mgr.add_user(container_id, -1, 0, 0, "+test_user", "", ""); + ASSERT_EQ(usr, nullptr); + + auto* grp = mgr.add_group(container_id, -1, 0, "+test_group"); + ASSERT_EQ(grp, nullptr); +} #endif