From 99d168a532894cafd6648547c1ac30dff2a62c43 Mon Sep 17 00:00:00 2001 From: Mike Gabriel Date: Sat, 5 May 2018 17:18:22 +0200 Subject: unit tests: Fix build and re-enable them. --- debian/rules | 2 +- src/pam-freerdp2-private.h | 32 +++++++++ tests/Makefile.am | 154 +++++++++++++++++++++++++++-------------- tests/mock_guest.c | 2 - tests/mock_pam.c | 25 +++---- tests/mock_pam.h | 11 ++- tests/test-freerdp-auth.c | 57 --------------- tests/test-freerdp-wrapper.cc | 88 ----------------------- tests/test-freerdp2-auth.c | 57 +++++++++++++++ tests/test-freerdp2-wrapper.cc | 90 ++++++++++++++++++++++++ 10 files changed, 303 insertions(+), 215 deletions(-) create mode 100644 src/pam-freerdp2-private.h delete mode 100644 tests/test-freerdp-auth.c delete mode 100644 tests/test-freerdp-wrapper.cc create mode 100644 tests/test-freerdp2-auth.c create mode 100644 tests/test-freerdp2-wrapper.cc diff --git a/debian/rules b/debian/rules index 19ce1e8..0969a92 100755 --- a/debian/rules +++ b/debian/rules @@ -9,7 +9,7 @@ include /usr/share/cdbs/1/rules/autoreconf.mk include /usr/share/cdbs/1/class/autotools.mk include /usr/share/cdbs/1/rules/utils.mk -#DEB_MAKE_CHECK_TARGET = check +DEB_MAKE_CHECK_TARGET = check # Remove .a and .la files from /lib/security remove-cruft:: diff --git a/src/pam-freerdp2-private.h b/src/pam-freerdp2-private.h new file mode 100644 index 0000000..74663ef --- /dev/null +++ b/src/pam-freerdp2-private.h @@ -0,0 +1,32 @@ +/* + * Copyright © 2018 Mike Gabriel + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Author: Mike Gabriel + * Author libpam-freerdp (where we forked from): Ted Gould + */ + +#ifndef _PAM_FREERDP2_PRIVATE_H_ +#define _PAM_FREERDP2_PRIVATE_H_ + +PAM_EXTERN int +pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc, const char **argv); +PAM_EXTERN int +pam_sm_open_session (pam_handle_t *pamh, int flags, int argc, const char ** argv); +PAM_EXTERN int +pam_sm_close_session (pam_handle_t *pamh, int flags, int argc, const char **argv); +PAM_EXTERN int +pam_sm_setcred (pam_handle_t *pamh, int flags, int argc, const char ** argv); + +#endif //_PAM_FREERDP2_PRIVATE_H_ diff --git a/tests/Makefile.am b/tests/Makefile.am index e69b0ab..29b0a98 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,74 +1,124 @@ +NULL = + CLEANFILES = DISTCLEANFILES = EXTRA_DIST = -TESTS = \ - test-freerdpclient-wrapper +TESTS = \ + test-freerdp2-wrapper \ + $(NULL) + +check_PROGRAMS = \ + test-freerdp2-auth \ + $(TESTS) \ + $(NULL) + +CLEANFILES += \ + test-freerdp2-auth \ + $(TESTS) \ + $(NULL) -check_PROGRAMS = \ - test-freerdp-auth \ - $(TESTS) +AM_CPPFLAGS = $(GTEST_CPPFLAGS) \ + $(REMOTE_APPS_MANAGER_CFLAGS) \ + -I${top_srcdir}/src -Wall -Werror \ + $(NULL) -AM_CPPFLAGS = $(GTEST_CPPFLAGS) \ - $(REMOTE_APPS_MANAGER_CFLAGS) \ - -I${top_srcdir}/src -Wall -Werror -AM_CXXFLAGS = $(GTEST_CXXFLAGS) \ - $(REMOTE_APPS_MANAGER_CFLAGS) +AM_CXXFLAGS = $(GTEST_CXXFLAGS) \ + $(REMOTE_APPS_MANAGER_CFLAGS) \ + $(NULL) -AM_CFLAGS = \ - -Wall \ - -g +AM_CFLAGS = \ + -Wall \ + -g \ + $(NULL) ########################## # Google Test Test Suite # ########################## -check_LIBRARIES = libgtest.a - -nodist_libgtest_a_SOURCES = \ - $(GTEST_SOURCE)/src/gtest-all.cc \ - $(GTEST_SOURCE)/src/gtest_main.cc - -libgtest_a_CPPFLAGS = \ - $(GTEST_CPPFLAGS) -w \ - $(AM_CPPFLAGS) -libgtest_a_CXXFLAGS = \ - $(AM_CXXFLAGS) +check_LIBRARIES = libgtest.a libgtest_main.a + +gtest-all.cc: + cp $(GTEST_SOURCE)/src/gtest-all.cc . + +gtest_main.cc: + cp $(GTEST_SOURCE)/src/gtest_main.cc . + +CLEANFILES += \ + gtest-all.cc \ + gtest_main.cc \ + $(NULL) + +nodist_libgtest_a_SOURCES = \ + gtest-all.cc \ + $(NULL) +nodist_libgtest_main_a_SOURCES = \ + gtest_main.cc \ + $(NULL) + +libgtest_a_CPPFLAGS = \ + $(GTEST_CPPFLAGS) -w \ + $(AM_CPPFLAGS) \ + $(NULL) +libgtest_main_a_CPPFLAGS = \ + $(GTEST_CPPFLAGS) -w \ + $(AM_CPPFLAGS) \ + $(NULL) + +libgtest_a_CXXFLAGS = \ + $(AM_CXXFLAGS) \ + $(NULL) +libgtest_main_a_CXXFLAGS = \ + $(AM_CXXFLAGS) \ + $(NULL) ########################## # Wrapper ########################## -test_freerdp_wrapper: test-freerdp-auth - -test_freerdpclient_wrapper_SOURCES = \ - mock_pam.c mock_pam.h \ - mock_guest.c mock_guest.h \ - test-freerdp-wrapper.cc - -test_freerdpclient_wrapper_LDADD = \ - $(top_builddir)/src/libfreerdp2core.la \ - libgtest.a - -test_freerdpclient_wrapper_CFLAGS = \ - $(AM_CXXFLAGS) \ - -I${top_srcdir}/src - -test_freerdpclient_wrapper_CXXFLAGS = \ - $(AM_CXXFLAGS) \ - -I${top_srcdir}/src - -test_freerdpclient_wrapper_CPPFLAGS = \ - $(AM_CPPFLAGS) \ - -DAUTH_CHECK="\"$(abs_builddir)/test-freerdp-auth\"" \ - -DLIBPAM_COMPILE - -test_freerdpclient_wrapper_LDFLAGS = \ - -pthread +test_freerdp2_wrapper: test-freerdp2-auth + +test_freerdp2_wrapper_SOURCES = \ + mock_pam.c mock_pam.h \ + mock_guest.c mock_guest.h \ + test-freerdp2-wrapper.cc \ + $(NULL) + +test_freerdp2_wrapper_LDADD = \ + libgtest.a libgtest_main.a \ + ${top_srcdir}/src/pam_freerdp2.la \ + $(FREERDP2_LIBS) \ + $(WINPR2_LIBS) \ + $(NULL) + +test_freerdp2_wrapper_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I${top_srcdir}/include \ + -I${top_srcdir}/src \ + $(GTEST_CPPFLAGS) \ + $(FREERDP2_CPPFLAGS) \ + $(WINPR2_CPPFLAGS) \ + $(NULL) + +test_freerdp2_wrapper_CXXFLAGS = \ + $(AM_CXXFLAGS) \ + -DAUTH_CHECK="\"$(abs_builddir)/test-freerdp2-auth\"" \ + $(GTEST_CXXFLAGS) \ + $(FREERDP2_CXXFLAGS) \ + $(WINPR2_CXXFLAGS) \ + $(NULL) + +test_freerdp2_wrapper_LDFLAGS = \ + $(GTEST_LDFLAGS) \ + $(FREERDP2_LDFLAGS) \ + $(WINPR2_LDFLAGS) \ + -pthread \ + $(NULL) ########################## # Auth tool ########################## -test_freerdp_auth_SOURCES = \ - test-freerdp-auth.c +test_freerdp2_auth_SOURCES = \ + test-freerdp2-auth.c \ + $(NULL) diff --git a/tests/mock_guest.c b/tests/mock_guest.c index f52a2d6..7505eec 100644 --- a/tests/mock_guest.c +++ b/tests/mock_guest.c @@ -118,5 +118,3 @@ socket_sucker () return -1; } } - - diff --git a/tests/mock_pam.c b/tests/mock_pam.c index 7aef55a..ce57973 100644 --- a/tests/mock_pam.c +++ b/tests/mock_pam.c @@ -9,6 +9,7 @@ #include #include "mock_pam.h" +#include "pam-freerdp2.h" struct pam_handle { void *item[PAM_NUM_ITEMS]; @@ -29,15 +30,15 @@ int fake_conv (int num_msg, const struct pam_message **msg, response->resp_retcode = 0; - if (strcmp((*msg)->msg, "login:") == 0) + if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_GUESTLOGIN) == 0) response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */ - else if (strcmp((*msg)->msg, "remote login:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_USER) == 0) response->resp = strdup ("ruser"); - else if (strcmp((*msg)->msg, "remote host:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_HOST) == 0) response->resp = strdup ("protocol://rhost/dummy"); - else if (strcmp((*msg)->msg, "password:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_PASSWORD) == 0) response->resp = strdup ("password"); - else if (strcmp((*msg)->msg, "domain:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_DOMAIN) == 0) response->resp = strdup ("domain"); else return PAM_SYMBOL_ERR; /* leaks... */ @@ -58,15 +59,15 @@ int fake_conv_empty_password (int num_msg, const struct pam_message **msg, response->resp_retcode = 0; - if (strcmp((*msg)->msg, "login:") == 0) + if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_GUESTLOGIN) == 0) response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */ - else if (strcmp((*msg)->msg, "remote login:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_USER) == 0) response->resp = strdup ("ruser"); - else if (strcmp((*msg)->msg, "remote host:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_HOST) == 0) response->resp = strdup ("protocol://rhost/dummy"); - else if (strcmp((*msg)->msg, "password:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_PASSWORD) == 0) response->resp = strdup (""); - else if (strcmp((*msg)->msg, "domain:") == 0) + else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_DOMAIN) == 0) response->resp = strdup ("domain"); else return PAM_SYMBOL_ERR; /* leaks... */ @@ -103,7 +104,7 @@ pam_handle_t *pam_handle_empty_pswd_new (void) return newh; } -int pam_get_item (const pam_handle_t *pamh, int type, const void **value) +int PAM_NONNULL((1)) pam_get_item (const pam_handle_t *pamh, int type, const void **value) { if (pamh == NULL) return PAM_SYSTEM_ERR; @@ -118,7 +119,7 @@ int pam_get_item (const pam_handle_t *pamh, int type, const void **value) return PAM_SUCCESS; } -int pam_set_item (pam_handle_t *pamh, int type, const void *value) +int PAM_NONNULL((1)) pam_set_item (pam_handle_t *pamh, int type, const void *value) { if (pamh == NULL) return PAM_SYSTEM_ERR; diff --git a/tests/mock_pam.h b/tests/mock_pam.h index c9c4f36..7fe0467 100644 --- a/tests/mock_pam.h +++ b/tests/mock_pam.h @@ -1,13 +1,16 @@ /* + * Copyright © 2012-2013 Mike Gabriel . * Copyright © 2012 Canonical Ltd. All rights reserved. * - * Author(s): David Barth + * Author(s): Mike Gabriel + * David Barth * */ #ifndef __MOCK_PAM_H__ #define __MOCK_PAM_H__ +#define LIBPAM_COMPILE #include #include #include @@ -18,7 +21,9 @@ typedef struct pam_handle pam_handle_t; pam_handle_t *pam_handle_new (void); pam_handle_t *pam_handle_empty_pswd_new (void); -int pam_get_item (const pam_handle_t *pamh, int type, const void **value); -int pam_set_item (pam_handle_t *pamh, int type, const void *value); +int PAM_NONNULL((1)) +pam_get_item (const pam_handle_t *pamh, int type, const void **value); +int PAM_NONNULL((1)) +pam_set_item (pam_handle_t *pamh, int type, const void *value); #endif diff --git a/tests/test-freerdp-auth.c b/tests/test-freerdp-auth.c deleted file mode 100644 index a83885e..0000000 --- a/tests/test-freerdp-auth.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright © 2012 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Author: Ted Gould - */ - -#include -#include -#include - -int -main (int argc, char * argv[]) -{ - char password[512]; - if (argc != 4) { - printf("Not enough params"); - return -1; - } - - if (scanf("%511s", password) != 1) { - return -1; - } - - /* Check username */ - if (strcmp(argv[2], "ruser")) { - return -1; - } - - /* Check password */ - if (strcmp(password, "password")) { - return -1; - } - - /* Check domain */ - if (strcmp(argv[3], "domain")) { - return -1; - } - - /* Check hostname */ - if (strcmp(argv[1], "rhost")) { - return -1; - } - - return 0; -} diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc deleted file mode 100644 index 3ce9ea6..0000000 --- a/tests/test-freerdp-wrapper.cc +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright © 2012 Canonical Ltd. All rights reserved. - * - * Author(s): David Barth - * - */ - -#include - -extern "C" { - -#include "mock_pam.h" -#include "mock_guest.h" - - int freerdpclient_wrapper (int argc, char * argv[]); - -const char * auth_check_path = AUTH_CHECK; - -} - -namespace { - - // The fixture for testing class Foo. - class FreerdpclientWrapperTest : public ::testing::Test { - protected: - // You can remove any or all of the following functions if its body - // is empty. - - FreerdpclientWrapperTest() { - // You can do set-up work for each test here. - setenv("HOME", "/tmp", 1 /* overwrite */); - } - - virtual ~FreerdpclientWrapperTest() { - // You can do clean-up work that doesn't throw exceptions here. - } - - // If the constructor and destructor are not enough for setting up - // and cleaning up each test, you can define the following methods: - - virtual void SetUp() { - // Code here will be called immediately after the constructor (right - // before each test). - unlink("/tmp/.freerdp2-socket"); - } - - virtual void TearDown() { - // Code here will be called immediately after each test (right - // before the destructor). - unlink("/tmp/.freerdp2-socket"); - } - - // Objects declared here can be used by all tests in the test case for Foo. - }; - - TEST_F(FreerdpclientWrapperTest, canLinkTheWholeGang) { - EXPECT_EQ (1, 1); // right, that's trivial, but that means - // that I got all of the wrapper and pam to link there - } - - TEST_F(FreerdpclientWrapperTest, canHandleEmptyPassword) { - const char *argv[] = { NULL }; - - pam_handle_t *pamh = pam_handle_empty_pswd_new (); - - EXPECT_EQ (PAM_AUTH_ERR, - pam_sm_authenticate (pamh, 0, 0, argv)); - - } - - TEST_F(FreerdpclientWrapperTest, canCallPamOpenSession) { - const char *argv[] = { NULL }; - - pam_handle_t *pamh = pam_handle_new (); - - EXPECT_EQ (PAM_SUCCESS, - 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)); - } - -} diff --git a/tests/test-freerdp2-auth.c b/tests/test-freerdp2-auth.c new file mode 100644 index 0000000..a83885e --- /dev/null +++ b/tests/test-freerdp2-auth.c @@ -0,0 +1,57 @@ +/* + * Copyright © 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Author: Ted Gould + */ + +#include +#include +#include + +int +main (int argc, char * argv[]) +{ + char password[512]; + if (argc != 4) { + printf("Not enough params"); + return -1; + } + + if (scanf("%511s", password) != 1) { + return -1; + } + + /* Check username */ + if (strcmp(argv[2], "ruser")) { + return -1; + } + + /* Check password */ + if (strcmp(password, "password")) { + return -1; + } + + /* Check domain */ + if (strcmp(argv[3], "domain")) { + return -1; + } + + /* Check hostname */ + if (strcmp(argv[1], "rhost")) { + return -1; + } + + return 0; +} diff --git a/tests/test-freerdp2-wrapper.cc b/tests/test-freerdp2-wrapper.cc new file mode 100644 index 0000000..33b4168 --- /dev/null +++ b/tests/test-freerdp2-wrapper.cc @@ -0,0 +1,90 @@ +/* + * Copyright © 2012 Canonical Ltd. All rights reserved. + * + * Author(s): David Barth + * + */ + +#include + +extern "C" { + +#include "mock_pam.h" +#include "mock_guest.h" + +#include "pam-freerdp2-private.h" + + int freerdpclient_wrapper (int argc, char * argv[]); + +const char * auth_check_path = AUTH_CHECK; + +} + +namespace { + + // The fixture for testing class Foo. + class FreerdpclientWrapperTest : public ::testing::Test { + protected: + // You can remove any or all of the following functions if its body + // is empty. + + FreerdpclientWrapperTest() { + // You can do set-up work for each test here. + setenv("HOME", "/tmp", 1 /* overwrite */); + } + + virtual ~FreerdpclientWrapperTest() { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + virtual void SetUp() { + // Code here will be called immediately after the constructor (right + // before each test). + unlink("/tmp/.freerdp2-socket"); + } + + virtual void TearDown() { + // Code here will be called immediately after each test (right + // before the destructor). + unlink("/tmp/.freerdp2-socket"); + } + + // Objects declared here can be used by all tests in the test case for Foo. + }; + + TEST_F(FreerdpclientWrapperTest, canLinkTheWholeGang) { + EXPECT_EQ (1, 1); // right, that's trivial, but that means + // that I got all of the wrapper and pam to link there + } + + TEST_F(FreerdpclientWrapperTest, canHandleEmptyPassword) { + const char *argv[] = { NULL }; + + pam_handle_t *pamh = pam_handle_empty_pswd_new (); + + EXPECT_EQ (PAM_AUTH_ERR, + pam_sm_authenticate (pamh, 0, 0, argv)); + + } + + TEST_F(FreerdpclientWrapperTest, canCallPamOpenSession) { + const char *argv[] = { NULL }; + + pam_handle_t *pamh = pam_handle_new (); + + EXPECT_EQ (PAM_SUCCESS, + 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