From 2704e3ffc83bba8f7aedd39799468c35402584e9 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 14:35:36 -0500 Subject: Steal a bunch of test infrastructure from libpam-icaclient --- tests/Makefile.am | 65 +++++++++++++++++++++++++ tests/mock_guest.c | 51 ++++++++++++++++++++ tests/mock_guest.h | 24 +++++++++ tests/mock_pam.c | 110 ++++++++++++++++++++++++++++++++++++++++++ tests/mock_pam.h | 23 +++++++++ tests/test-freerdp-wrapper.cc | 67 +++++++++++++++++++++++++ 6 files changed, 340 insertions(+) create mode 100644 tests/Makefile.am create mode 100644 tests/mock_guest.c create mode 100644 tests/mock_guest.h create mode 100644 tests/mock_pam.c create mode 100644 tests/mock_pam.h create mode 100644 tests/test-freerdp-wrapper.cc (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..e687799 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,65 @@ +CLEANFILES = +DISTCLEANFILES = +EXTRA_DIST = + +TESTS = \ + test-auth-check \ + test-icaclient-wrapper + +check_PROGRAMS = $(TESTS) + +########################## +# Google Test Test Suite # +########################## +check_LIBRARIES = libgtest.a + +AM_CPPFLAGS = $(GTEST_CPPFLAGS) \ + $(REMOTE_APPS_MANAGER_CFLAGS) \ + -I${top_srcdir}/src -Wall -Werror +AM_CXXFLAGS = $(GTEST_CXXFLAGS) \ + $(REMOTE_APPS_MANAGER_CFLAGS) + +AM_CFLAGS = \ + -Wall \ + -g + +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) + +test_auth_check_SOURCES = \ + test-auth-check.cc + +test_auth_check_LDADD = \ + $(top_builddir)/src/pam_ica.la \ + libgtest.a + +test_auth_check_CXXFLAGS = \ + $(AM_CXXFLAGS) \ + -I${top_srcdir}/src + +test_auth_check_LDFLAGS = \ + -pthread + +test_icaclient_wrapper_SOURCES = \ + mock_pam.c \ + mock_guest.c \ + test-icaclient-wrapper.cc + +test_icaclient_wrapper_LDADD = \ + $(top_builddir)/src/pam_ica.la \ + libgtest.a + +test_icaclient_wrapper_CXXFLAGS = \ + $(AM_CXXFLAGS) \ + -I${top_srcdir}/src + +test_icaclient_wrapper_LDFLAGS = \ + -pthread + diff --git a/tests/mock_guest.c b/tests/mock_guest.c new file mode 100644 index 0000000..2cf04b3 --- /dev/null +++ b/tests/mock_guest.c @@ -0,0 +1,51 @@ +/* + * Copyright © 2012 Canonical Ltd. All rights reserved. + * + * Author(s): David Barth + * + */ + +#include +#include +#include +#include + +static struct passwd guest = { "guest", + "password", + 500, 500, + "M. Guest", + "/tmp", + "/bin/true" }; +struct passwd * +getpwnam (const char *username) +{ return &guest; } + +int +setgroups(size_t size, const gid_t *list) +{ + errno = EPERM; + return -1; +} + +int +setgid(gid_t gid) +{ return 0; } + +int +setuid(uid_t uid) +{ return 0; } + +int +setegid(gid_t gid) +{ return 0; } + +int +seteuid(uid_t uid) +{ return 0; } + +int chmod(const char *path, mode_t mode) +{ return 0; } + +int chown(const char *path, uid_t owner, gid_t group) +{ return 0; } + diff --git a/tests/mock_guest.h b/tests/mock_guest.h new file mode 100644 index 0000000..c4179b9 --- /dev/null +++ b/tests/mock_guest.h @@ -0,0 +1,24 @@ +/* + * Copyright © 2012 Canonical Ltd. All rights reserved. + * + * Author(s): David Barth + * + */ + +#ifndef __MOCK_GUEST_H__ +#define __MOCK_GUEST_H__ + +#include +#include +#include + +struct passwd *getpwnam (const char *username); +int setgroups(size_t size, const gid_t *list); +int setgid(gid_t gid); +int setuid(uid_t uid); +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); + +#endif diff --git a/tests/mock_pam.c b/tests/mock_pam.c new file mode 100644 index 0000000..6368b84 --- /dev/null +++ b/tests/mock_pam.c @@ -0,0 +1,110 @@ +/* + * Copyright © 2012 Canonical Ltd. All rights reserved. + * + * Author(s): David Barth + * + */ + +#include +#include + +#include "mock_pam.h" + +struct pam_handle { + void *item[PAM_NUM_ITEMS]; + + struct pam_conv *conv; + + /* note: the other fields have been omitted */ +}; + +int fake_conv (int num_msg, const struct pam_message **msg, + struct pam_response **resp, void *appdata_ptr) +{ + struct pam_response *response = NULL; + response = malloc (sizeof (struct pam_response)); + + if (response == NULL) + return PAM_BUF_ERR; + + response->resp_retcode = 0; + + if (strcmp((*msg)->msg, "login:") == 0) + response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */ + else if (strcmp((*msg)->msg, "remote login:") == 0) + response->resp = strdup ("ruser"); + else if (strcmp((*msg)->msg, "remote host:") == 0) + response->resp = strdup ("protocol://rhost/dummy"); + else if (strcmp((*msg)->msg, "password:") == 0) + response->resp = strdup ("password"); + else if (strcmp((*msg)->msg, "domain:") == 0) + response->resp = strdup ("domain"); + else + return PAM_SYMBOL_ERR; /* leaks... */ + + *resp = response; + + return PAM_SUCCESS; +} + +struct pam_conv static_conv = { &fake_conv, (void *)NULL }; + +pam_handle_t *pam_handle_new (void) +{ + pam_handle_t *newh = malloc (sizeof (pam_handle_t)); + + if (newh != NULL) { + newh->conv = &static_conv; + memset(newh->item, 0, sizeof(void *) * PAM_NUM_ITEMS); + } + + return newh; +} + +int pam_get_item (const pam_handle_t *pamh, int type, const void **value) +{ + if (pamh == NULL) + return PAM_SYSTEM_ERR; + + if (type == PAM_CONV) + *value = pamh->conv; + else if (pamh->item[type] != NULL) + *value = pamh->item[type]; + else + *value = NULL; /* will result in a prompt conversation */ + + return PAM_SUCCESS; +} + +int pam_set_item (pam_handle_t *pamh, int type, const void *value) +{ + if (pamh == NULL) + return PAM_SYSTEM_ERR; + + void **slot, *tmp; + size_t nsize, osize; + + slot = &pamh->item[type]; + osize = nsize = 0; + + if (*slot != NULL) + osize = strlen((const char *)*slot) + 1; + if (value != NULL) + nsize = strlen((const char *)value) + 1; + + if (*slot != NULL) { + memset(*slot, 0xd0, osize); + free(*slot); + } + + if (value != NULL) { + if ((tmp = malloc(nsize)) == NULL) + return PAM_BUF_ERR; + memcpy(tmp, value, nsize); + } else { + tmp = NULL; + } + *slot = tmp; + + return PAM_SUCCESS; +} diff --git a/tests/mock_pam.h b/tests/mock_pam.h new file mode 100644 index 0000000..eb88a2e --- /dev/null +++ b/tests/mock_pam.h @@ -0,0 +1,23 @@ +/* + * Copyright © 2012 Canonical Ltd. All rights reserved. + * + * Author(s): David Barth + * + */ + +#ifndef __MOCK_PAM_H__ +#define __MOCK_PAM_H__ + +#include +#include +#include + +#define PAM_NUM_ITEMS PAM_AUTHTOK_TYPE + +typedef struct pam_handle pam_handle_t; + +pam_handle_t *pam_handle_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); + +#endif diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc new file mode 100644 index 0000000..889aedb --- /dev/null +++ b/tests/test-freerdp-wrapper.cc @@ -0,0 +1,67 @@ +/* + * 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[]); +} + +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/.freerdp-socket"); + } + + virtual void TearDown() { + // Code here will be called immediately after each test (right + // before the destructor). + unlink("/tmp/.freerdp-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, canCallPamOpenSession) { + const char *argv[] = { NULL }; + + pam_handle_t *pamh = pam_handle_new (); + + EXPECT_EQ (PAM_SUCCESS, + pam_sm_open_session (pamh, 0, 0, argv)); + } + +} -- cgit v1.2.3 From 99a318c4cfee27117c8b81f58ce4b8a4e55e27ed Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 15:33:38 -0500 Subject: Clean up Makefile and add proper files --- tests/Makefile.am | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index e687799..49f1701 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,16 +3,10 @@ DISTCLEANFILES = EXTRA_DIST = TESTS = \ - test-auth-check \ - test-icaclient-wrapper + test-freerdpclient-wrapper check_PROGRAMS = $(TESTS) -########################## -# Google Test Test Suite # -########################## -check_LIBRARIES = libgtest.a - AM_CPPFLAGS = $(GTEST_CPPFLAGS) \ $(REMOTE_APPS_MANAGER_CFLAGS) \ -I${top_srcdir}/src -Wall -Werror @@ -23,6 +17,12 @@ AM_CFLAGS = \ -Wall \ -g +########################## +# Google Test Test Suite # +########################## + +check_LIBRARIES = libgtest.a + nodist_libgtest_a_SOURCES = \ $(GTEST_SOURCE)/src/gtest-all.cc \ $(GTEST_SOURCE)/src/gtest_main.cc @@ -33,33 +33,23 @@ libgtest_a_CPPFLAGS = \ libgtest_a_CXXFLAGS = \ $(AM_CXXFLAGS) -test_auth_check_SOURCES = \ - test-auth-check.cc - -test_auth_check_LDADD = \ - $(top_builddir)/src/pam_ica.la \ - libgtest.a - -test_auth_check_CXXFLAGS = \ - $(AM_CXXFLAGS) \ - -I${top_srcdir}/src - -test_auth_check_LDFLAGS = \ - -pthread +########################## +# Wrapper +########################## -test_icaclient_wrapper_SOURCES = \ +test_freerdpclient_wrapper_SOURCES = \ mock_pam.c \ mock_guest.c \ - test-icaclient-wrapper.cc + test-freerdp-wrapper.cc -test_icaclient_wrapper_LDADD = \ - $(top_builddir)/src/pam_ica.la \ +test_freerdpclient_wrapper_LDADD = \ + $(top_builddir)/src/pam_freerdp.la \ libgtest.a -test_icaclient_wrapper_CXXFLAGS = \ +test_freerdpclient_wrapper_CXXFLAGS = \ $(AM_CXXFLAGS) \ -I${top_srcdir}/src -test_icaclient_wrapper_LDFLAGS = \ +test_freerdpclient_wrapper_LDFLAGS = \ -pthread -- cgit v1.2.3 From 939c909aa9f1eca060f1c7d0c1ba89739f54f958 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 15:34:58 -0500 Subject: Make sure to distribute the header files --- tests/Makefile.am | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 49f1701..c5da385 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -38,8 +38,8 @@ libgtest_a_CXXFLAGS = \ ########################## test_freerdpclient_wrapper_SOURCES = \ - mock_pam.c \ - mock_guest.c \ + mock_pam.c mock_pam.h \ + mock_guest.c mock_guest.h \ test-freerdp-wrapper.cc test_freerdpclient_wrapper_LDADD = \ -- cgit v1.2.3 From 0b4324a891f4c96ec21aef3bf02d961f72c91709 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 15:39:09 -0500 Subject: Make sure to close so we don't leave processes around --- tests/test-freerdp-wrapper.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index 889aedb..e53d94b 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -62,6 +62,8 @@ namespace { EXPECT_EQ (PAM_SUCCESS, pam_sm_open_session (pamh, 0, 0, argv)); + EXPECT_EQ (PAM_SUCCESS, + pam_sm_close_session (pamh, 0, 0, argv)); } } -- cgit v1.2.3 From 2c7ecf16c1df5de36e1dad477489a5d30133f1b0 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 16:42:55 -0500 Subject: Set everything up so that the auth check binary can be different in the tests --- tests/Makefile.am | 2 +- tests/test-freerdp-wrapper.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index c5da385..4e51e62 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -43,7 +43,7 @@ test_freerdpclient_wrapper_SOURCES = \ test-freerdp-wrapper.cc test_freerdpclient_wrapper_LDADD = \ - $(top_builddir)/src/pam_freerdp.la \ + $(top_builddir)/src/libfreerdpcore.la \ libgtest.a test_freerdpclient_wrapper_CXXFLAGS = \ diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index e53d94b..3effc2a 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -13,6 +13,9 @@ extern "C" { #include "mock_guest.h" int freerdpclient_wrapper (int argc, char * argv[]); + +const char * auth_check_path = ""; + } namespace { -- cgit v1.2.3 From 3d176dc378174863d78ed928d8deaef981cc6c00 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 16:59:53 -0500 Subject: Adding a small auth check utility --- tests/Makefile.am | 15 +++++++++++-- tests/test-freerdp-auth.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/test-freerdp-auth.c (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 4e51e62..b7c39f0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,9 +3,11 @@ DISTCLEANFILES = EXTRA_DIST = TESTS = \ - test-freerdpclient-wrapper + test-freerdpclient-wrapper -check_PROGRAMS = $(TESTS) +check_PROGRAMS = \ + test-freerdp-auth \ + $(TESTS) AM_CPPFLAGS = $(GTEST_CPPFLAGS) \ $(REMOTE_APPS_MANAGER_CFLAGS) \ @@ -37,6 +39,8 @@ libgtest_a_CXXFLAGS = \ # Wrapper ########################## +test_freerdp_wrapper: test-freerdp-auth + test_freerdpclient_wrapper_SOURCES = \ mock_pam.c mock_pam.h \ mock_guest.c mock_guest.h \ @@ -53,3 +57,10 @@ test_freerdpclient_wrapper_CXXFLAGS = \ test_freerdpclient_wrapper_LDFLAGS = \ -pthread +########################## +# Auth tool +########################## + +test_freerdp_auth_SOURCES = \ + test-freerdp-auth.c + diff --git a/tests/test-freerdp-auth.c b/tests/test-freerdp-auth.c new file mode 100644 index 0000000..a83885e --- /dev/null +++ b/tests/test-freerdp-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; +} -- cgit v1.2.3 From 7ee22c853b651808fa61226bad3d7dd5a0338379 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 17:01:11 -0500 Subject: Make the auth use the auth check utility --- tests/Makefile.am | 1 + tests/test-freerdp-wrapper.cc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index b7c39f0..56fe7b9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -52,6 +52,7 @@ test_freerdpclient_wrapper_LDADD = \ test_freerdpclient_wrapper_CXXFLAGS = \ $(AM_CXXFLAGS) \ + -DAUTH_CHECK="\"$(builddir)/test-freerdp-auth\"" \ -I${top_srcdir}/src test_freerdpclient_wrapper_LDFLAGS = \ diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index 3effc2a..2063c5c 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -14,7 +14,7 @@ extern "C" { int freerdpclient_wrapper (int argc, char * argv[]); -const char * auth_check_path = ""; +const char * auth_check_path = AUTH_CHECK; } -- cgit v1.2.3 From 88ec3eac3541793636048bab693145a0b4268c32 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 23:29:18 -0500 Subject: Make sure we have the full path to execute the auth check --- tests/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 56fe7b9..c257ac9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -52,7 +52,7 @@ test_freerdpclient_wrapper_LDADD = \ test_freerdpclient_wrapper_CXXFLAGS = \ $(AM_CXXFLAGS) \ - -DAUTH_CHECK="\"$(builddir)/test-freerdp-auth\"" \ + -DAUTH_CHECK="\"$(abs_builddir)/test-freerdp-auth\"" \ -I${top_srcdir}/src test_freerdpclient_wrapper_LDFLAGS = \ -- cgit v1.2.3 From 65bdd89ef0ede483dc7edf844314efde1aa85545 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 23:29:28 -0500 Subject: Adding an authenticate --- tests/test-freerdp-wrapper.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index 2063c5c..c9d221e 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -63,6 +63,8 @@ namespace { pam_handle_t *pamh = pam_handle_new (); + EXPECT_EQ (PAM_SUCCESS, + pam_sm_authenticate (pamh, 0, 0, argv)); EXPECT_EQ (PAM_SUCCESS, pam_sm_open_session (pamh, 0, 0, argv)); EXPECT_EQ (PAM_SUCCESS, -- cgit v1.2.3 From 19c01abdd2c3a3d772cf8b7f54b819231ac4dcba Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 19 Sep 2012 23:32:08 -0500 Subject: Add a set cred call like LightDM does it --- tests/test-freerdp-wrapper.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test-freerdp-wrapper.cc b/tests/test-freerdp-wrapper.cc index c9d221e..cfe86de 100644 --- a/tests/test-freerdp-wrapper.cc +++ b/tests/test-freerdp-wrapper.cc @@ -65,6 +65,8 @@ namespace { 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 (PAM_SUCCESS, -- cgit v1.2.3