From 343ae349c3645c8ad6f80215ceaf685a5b975cc7 Mon Sep 17 00:00:00 2001 From: Iftikhar Ahmad Date: Tue, 16 Oct 2012 14:36:11 +0500 Subject: Improving the test coverage for libpam-freerdp --- tests/mock_guest.c | 71 +++++++++++++++++++++++++++++++++++++++++++ tests/mock_guest.h | 2 ++ tests/test-freerdp-wrapper.cc | 2 ++ 3 files changed, 75 insertions(+) (limited to 'tests') diff --git a/tests/mock_guest.c b/tests/mock_guest.c index 2cf04b3..8bf2c3a 100644 --- a/tests/mock_guest.c +++ b/tests/mock_guest.c @@ -9,6 +9,11 @@ #include #include #include +#include +#include +#include +#include +#include static struct passwd guest = { "guest", "password", @@ -49,3 +54,69 @@ int chmod(const char *path, mode_t mode) int chown(const char *path, uid_t owner, gid_t group) { return 0; } +int execvp(const char *file, char *const argv[]) +{ + return 0; +} +/* wrap _exit, to make sure the gcov_exit function installed with atexit() + is really called to collect coverage statistics */ +void _exit (int exitcode) +{ + exit (exitcode); +} + + +#define BUFFER_SIZE 512 + +/*Borrowed this code form socket-sucker.c in lightdm-remote-session-freerdp*/ +int +socket_sucker () +{ + int socket_fd = 0; + int servlen = 0; + struct sockaddr_un serv_addr; + + bzero((char *)&serv_addr, sizeof(serv_addr)); + + const char * home = getenv("HOME"); + if (home == NULL) { + return -1; + } + + serv_addr.sun_family = AF_UNIX; + + int printsize = snprintf(serv_addr.sun_path, sizeof(serv_addr.sun_path) - 1, "%s/%s", home, ".freerdp-socket"); + if (printsize > sizeof(serv_addr.sun_path) - 1 || printsize < 0) { + return -1; + } + + servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family); + + if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + return -1; + } + + if (connect(socket_fd, (struct sockaddr *)&serv_addr, servlen) < 0) { + return -1; + } + + char buffer[BUFFER_SIZE + 2]; + int in = 0; + int out = 0; + + in = read(socket_fd, buffer, BUFFER_SIZE); + + if (in > 0) { + out = write(1, buffer, in); + } + + close(socket_fd); + + if (in > 0 && out > 0 && in == out) { + return 0; + } else { + return -1; + } +} + + diff --git a/tests/mock_guest.h b/tests/mock_guest.h index c4179b9..2c17536 100644 --- a/tests/mock_guest.h +++ b/tests/mock_guest.h @@ -20,5 +20,7 @@ int setegid(gid_t gid); int seteuid(uid_t uid); int chmod(const char *path, mode_t mode); int chown(const char *path, uid_t owner, gid_t group); +int execvp(const char *file, char *const argv[]); +int socket_sucker(); #endif diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index cfe86de..147682d 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -67,8 +67,10 @@ namespace { pam_sm_authenticate (pamh, 0, 0, argv)); EXPECT_EQ (PAM_SUCCESS, pam_sm_setcred (pamh, 0, 0, argv)); + EXPECT_EQ (PAM_SUCCESS, pam_sm_open_session (pamh, 0, 0, argv)); + EXPECT_EQ(0, socket_sucker()); EXPECT_EQ (PAM_SUCCESS, pam_sm_close_session (pamh, 0, 0, argv)); } -- cgit v1.2.3