Add ASSERT_ERRNO and EXPECT_ERRNO (and use them).
We've talked about this many times in the past, but partners struggle to
understand "expected 38, got 22" in these contexts, and I always have to
go and check the header files just to be sure I'm sure.
I actually think the glibc geterrorname_np() function (which would
return "ENOSYS" rather than "Function not implemented") would be more
helpful, but I'll have to go and implement that first, and then come
back.
Being forced to go through all our errno assertions did also make me
want to use a more consistent style for our ENOSYS assertions in
particular --- there's a particularly readable idiom, and I'll also come
back and move more of those checks to the most readable idiom.
I've added a few missing `errno = 0`s before tests, and removed a few
stray `errno = 0`s from tests that don't actually make assertions about
errno, since I had to look at every single reference to errno anyway.
Test: treehugger
Change-Id: Iba7c56f2adc30288c3e00ade106635e515e88179
diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp
index 65a54a6..d3acf03 100644
--- a/tests/grp_pwd_test.cpp
+++ b/tests/grp_pwd_test.cpp
@@ -41,6 +41,8 @@
// Generated android_ids array
#include "generated_android_ids.h"
+#include "utils.h"
+
using android::base::Join;
using android::base::ReadFileToString;
using android::base::Split;
@@ -86,7 +88,7 @@
bool check_username) {
errno = 0;
passwd* pwd = getpwuid(uid);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getpwuid");
check_passwd(pwd, username, uid, uid_type, check_username);
}
@@ -95,7 +97,7 @@
bool check_username) {
errno = 0;
passwd* pwd = getpwnam(username);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getpwnam");
check_passwd(pwd, username, uid, uid_type, check_username);
}
@@ -110,7 +112,7 @@
passwd* pwd = nullptr;
result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
ASSERT_EQ(0, result);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getpwuid_r");
check_passwd(pwd, username, uid, uid_type, check_username);
}
@@ -125,7 +127,7 @@
passwd* pwd = nullptr;
result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
ASSERT_EQ(0, result);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getpwnam_r");
check_passwd(pwd, username, uid, uid_type, check_username);
}
@@ -145,7 +147,7 @@
passwd* passwd = nullptr;
passwd = getpwuid(uid);
EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
- EXPECT_EQ(ENOENT, errno);
+ EXPECT_ERRNO(ENOENT);
struct passwd passwd_storage;
char buf[512];
@@ -159,7 +161,7 @@
passwd* passwd = nullptr;
passwd = getpwnam(username);
EXPECT_EQ(nullptr, passwd) << "name = '" << passwd->pw_name << "'";
- EXPECT_EQ(ENOENT, errno);
+ EXPECT_ERRNO(ENOENT);
struct passwd passwd_storage;
char buf[512];
@@ -507,7 +509,7 @@
static void check_getgrgid(const char* group_name, gid_t gid, bool check_groupname) {
errno = 0;
group* grp = getgrgid(gid);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getgrgid");
check_group(grp, group_name, gid, check_groupname);
}
@@ -515,7 +517,7 @@
static void check_getgrnam(const char* group_name, gid_t gid, bool check_groupname) {
errno = 0;
group* grp = getgrnam(group_name);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getgrnam");
check_group(grp, group_name, gid, check_groupname);
}
@@ -528,7 +530,7 @@
errno = 0;
int result = getgrgid_r(gid, &grp_storage, buf, sizeof(buf), &grp);
ASSERT_EQ(0, result);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getgrgid_r");
check_group(grp, group_name, gid, check_groupname);
}
@@ -541,7 +543,7 @@
errno = 0;
int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
ASSERT_EQ(0, result);
- ASSERT_EQ(0, errno);
+ ASSERT_ERRNO(0);
SCOPED_TRACE("getgrnam_r");
check_group(grp, group_name, gid, check_groupname);
}
@@ -560,7 +562,7 @@
group* group = nullptr;
group = getgrgid(gid);
EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
- EXPECT_EQ(ENOENT, errno);
+ EXPECT_ERRNO(ENOENT);
struct group group_storage;
char buf[512];
@@ -574,7 +576,7 @@
group* group = nullptr;
group = getgrnam(groupname);
EXPECT_EQ(nullptr, group) << "name = '" << group->gr_name << "'";
- EXPECT_EQ(ENOENT, errno);
+ EXPECT_ERRNO(ENOENT);
struct group group_storage;
char buf[512];