From 1727595b0e06ba8d4cfe2f01e5ebde0313b0b4c1 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 13 May 2022 16:06:54 -0700 Subject: [PATCH] mntent_test: don't assume /proc isn't the first mount. This test tried to be lazy and test both getmntent() and getmntent_r() in the same test, but that led to an implicit assumption that /proc isn't the first mount returned. This new version is quite a bit more thorough than the old. It does assume that the mount list doesn't change while the test is running, but that seems like a reasonable assumption to make during CTS? Bug: https://issuetracker.google.com/230228681 Test: treehugger Change-Id: I5c5f0b86ae1c4df9a2ce69d48e1c3accb42c687b (cherry picked from commit 1e393b0699745d6120d2fd43f58dc3d5863e3b87) --- tests/mntent_test.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/mntent_test.cpp b/tests/mntent_test.cpp index b86af9fb6..4b8fc9a80 100644 --- a/tests/mntent_test.cpp +++ b/tests/mntent_test.cpp @@ -19,24 +19,40 @@ #include TEST(mntent, mntent_smoke) { + // Read all the entries with getmntent(). FILE* fp = setmntent("/proc/mounts", "r"); ASSERT_TRUE(fp != nullptr); - ASSERT_TRUE(getmntent(fp) != nullptr); + std::vector fsnames; + std::vector dirs; + mntent* me; + while ((me = getmntent(fp)) != nullptr) { + fsnames.push_back(me->mnt_fsname); + dirs.push_back(me->mnt_dir); + } - bool saw_proc = false; + ASSERT_EQ(1, endmntent(fp)); + + // Then again with getmntent_r(), checking they match. + fp = setmntent("/proc/mounts", "r"); + ASSERT_TRUE(fp != nullptr); struct mntent entry; char buf[BUFSIZ]; + size_t i = 0; while (getmntent_r(fp, &entry, buf, sizeof(buf)) != nullptr) { - if (strcmp(entry.mnt_fsname, "proc") == 0 && strcmp(entry.mnt_dir, "/proc") == 0) { - saw_proc = true; - } + ASSERT_EQ(fsnames[i], entry.mnt_fsname); + ASSERT_EQ(dirs[i], entry.mnt_dir); + i++; } - ASSERT_TRUE(saw_proc); - ASSERT_EQ(1, endmntent(fp)); + + // And just for good measure: we did see a /proc entry, right? + auto it = std::find(fsnames.begin(), fsnames.end(), "proc"); + ASSERT_TRUE(it != fsnames.end()); + size_t proc_index = it - fsnames.begin(); + ASSERT_EQ("/proc", dirs[proc_index]); } TEST(mntent, hasmntopt) {