Add GetUserFromPid() to libpsl

And clean up some tests.
This commit is contained in:
Andrew Schwartzmeyer
2016-07-13 15:46:25 -07:00
parent 8ac64286e1
commit 900b46d281
7 changed files with 76 additions and 16 deletions
+1
View File
@@ -1,6 +1,7 @@
add_library(psl-native SHARED
getstat.cpp
getpwuid.cpp
getuserfrompid.cpp
getfileowner.cpp
getcurrentprocessorid.cpp
getusername.cpp
+47
View File
@@ -0,0 +1,47 @@
#include <string>
#include <sstream>
#include <errno.h>
#include <sys/sysctl.h>
#include "pal.h"
#include "getfileowner.h"
#include "getpwuid.h"
#include "getuserfrompid.h"
char* GetUserFromPid(pid_t pid)
{
#if defined(__linux__)
// Get effective owner of pid from procfs
std::stringstream ss;
ss << "/proc/" << pid;
std::string path;
ss >> path;
return GetFileOwner(path.c_str());
#elif defined(__APPLE__) && defined(__MACH__)
// Get effective owner of pid from sysctl
struct kinfo_proc oldp;
size_t oldlenp = sizeof(oldp);
int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
u_int namelen = sizeof(name)/sizeof(int);
// Read-only query
int ret = sysctl(name, namelen, &oldp, &oldlenp, NULL, 0);
if (ret != 0 || oldlenp == 0)
{
errno = ERROR_GEN_FAILURE;
return NULL;
}
return GetPwUid(oldp.kp_eproc.e_ucred.cr_uid);
#else
return NULL;
#endif
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "pal.h"
PAL_BEGIN_EXTERNC
char* GetUserFromPid(pid_t pid);
PAL_END_EXTERNC
+1
View File
@@ -3,6 +3,7 @@ add_subdirectory(googletest)
add_executable(psl-native-test
test-getfileowner.cpp
test-locale.cpp
test-getuserfrompid.cpp
test-getcurrentprocessid.cpp
test-getusername.cpp
test-getcomputername.cpp
+4 -11
View File
@@ -7,25 +7,18 @@
#include <unistd.h>
#include "getfileowner.h"
using namespace std;
//! Test fixture for GetFileOwner
class GetFileOwnerTest : public ::testing::Test
TEST(GetFileOwnerTest, CanGetOwnerOfRoot)
{
};
TEST_F(GetFileOwnerTest, CanGetOwnerOfRoot)
{
ASSERT_STREQ(GetFileOwner("/"), "root");
EXPECT_STREQ(GetFileOwner("/"), "root");
}
TEST_F(GetFileOwnerTest, CannotGetOwnerOfFakeFile)
TEST(GetFileOwnerTest, CannotGetOwnerOfFakeFile)
{
EXPECT_STREQ(GetFileOwner("SomeMadeUpFileNameThatDoesNotExist"), NULL);
EXPECT_EQ(errno, ERROR_FILE_NOT_FOUND);
}
TEST_F(GetFileOwnerTest, ReturnsNullForNullInput)
TEST(GetFileOwnerTest, ReturnsNullForNullInput)
{
EXPECT_STREQ(GetFileOwner(NULL), NULL);
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
@@ -0,0 +1,13 @@
//! @file test-getuserfrompid.cpp
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
//! @brief Unit tests for GetUserFromPid
#include <gtest/gtest.h>
#include <pwd.h>
#include "getuserfrompid.h"
TEST(GetUserFromPid, Success)
{
char* expected = getpwuid(geteuid())->pw_name;
EXPECT_STREQ(GetUserFromPid(getpid()), expected);
}
+1 -5
View File
@@ -2,9 +2,6 @@
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
//! @brief Unit tests for GetUserName
#include <string>
#include <vector>
#include <unistd.h>
#include <gtest/gtest.h>
#include <pwd.h>
#include "getusername.h"
@@ -12,6 +9,5 @@
TEST(GetUserName, Success)
{
char* expected = getpwuid(geteuid())->pw_name;
ASSERT_TRUE(expected != NULL);
ASSERT_EQ(GetUserName(), std::string(expected));
EXPECT_STREQ(GetUserName(), expected);
}