[libc-commits] [libc] [libc] Add getgrnam and getgrgid entrypoints (PR #224858)

Victor Campos via libc-commits libc-commits at lists.llvm.org
Mon Sep 21 03:24:34 PDT 2026


================
@@ -0,0 +1,197 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for getgrnam.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/signal_macros.h"
+#include "hdr/types/gid_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_group.h"
+#include "src/__support/ctype_utils.h"
+#include "src/__support/libc_errno.h"
+#include "src/grp/endgrent.h"
+#include "src/grp/getgrent.h"
+#include "src/grp/getgrnam.h"
+#include "src/grp/grp_utils.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "test/src/grp/grp_test_utils.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LlvmLibcGetgrnamTest = LlvmLibcGrpTest;
+
+TEST_F(LlvmLibcGetgrnamTest, Success) {
+  const char *content = "root:x:0:\n"
+                        "wheel:x:10:alice,bob\n"
+                        "users:x:100:carol\n";
+  ScopedGroupFile test_file(libc_make_test_file_path("getgrnam_success.test"),
+                            content);
+
+  struct group *grp = LIBC_NAMESPACE::getgrnam("wheel");
+  ASSERT_NE(grp, nullptr);
+  EXPECT_STREQ(grp->gr_name, "wheel");
+  EXPECT_STREQ(grp->gr_passwd, "x");
+  EXPECT_EQ(grp->gr_gid, static_cast<gid_t>(10));
+  ASSERT_NE(grp->gr_mem, nullptr);
+  EXPECT_STREQ(grp->gr_mem[0], "alice");
+  EXPECT_STREQ(grp->gr_mem[1], "bob");
+  EXPECT_EQ(grp->gr_mem[2], nullptr);
+}
+
+TEST_F(LlvmLibcGetgrnamTest, FirstAndLastEntries) {
+  const char *content = "first:x:100:\n"
+                        "middle:x:101:\n"
+                        "last:x:102:zed\n";
+  ScopedGroupFile test_file(libc_make_test_file_path("getgrnam_boundary.test"),
+                            content);
+
+  struct group *grp = LIBC_NAMESPACE::getgrnam("first");
+  ASSERT_NE(grp, nullptr);
+  EXPECT_STREQ(grp->gr_name, "first");
+  EXPECT_EQ(grp->gr_gid, static_cast<gid_t>(100));
----------------
vhscampos wrote:

Assert that grp->gr_mem is null

https://github.com/llvm/llvm-project/pull/224858


More information about the libc-commits mailing list