2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek/*
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek Authors:
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek Copyright (C) 2014 Red Hat
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek SSSD tests: Server instantiation
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek This program is free software; you can redistribute it and/or modify
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek it under the terms of the GNU General Public License as published by
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek the Free Software Foundation; either version 3 of the License, or
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek (at your option) any later version.
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek This program is distributed in the hope that it will be useful,
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek GNU General Public License for more details.
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek You should have received a copy of the GNU General Public License
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek*/
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include <sys/types.h>
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include <sys/stat.h>
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include <fcntl.h>
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include <popt.h>
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include "util/util.h"
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina#include "util/strtonum.h"
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek#include "tests/cmocka/common_mock.h"
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekstatic void wait_for_fg_server(pid_t pid)
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid_t wpid;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int status;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_not_equal(pid, -1);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek wpid = waitpid(pid, &status, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(wpid, pid);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_true(WIFEXITED(status));
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(WEXITSTATUS(status), 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekstatic void wait_for_bg_server(const char *pidfile)
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int fd;
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina uint32_t tmp;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek char buf[16];
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid_t pid;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int ret;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int count;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek count = 0;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek do {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct stat sb;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek count++;
482914ed19e46146b3dd567fd0ce0faccd097e9dLukas Slebodnik if (count > 200) {
482914ed19e46146b3dd567fd0ce0faccd097e9dLukas Slebodnik fail();
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek break;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = stat(pidfile, &sb);
482914ed19e46146b3dd567fd0ce0faccd097e9dLukas Slebodnik usleep(50000);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek } while (ret != 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* read the pidfile */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek fd = open(pidfile, O_RDONLY);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_false(fd < 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = read(fd, buf, sizeof(buf));
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek close(fd);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_false(ret <= 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek buf[sizeof(buf) - 1] = '\0';
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina errno = 0;
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina tmp = strtouint32(buf, NULL, 10);
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina assert_int_not_equal(tmp, 0);
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina assert_int_equal(errno, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
fd52e9e51bdfe00e035e0ab08ce9a6a5d6b7a974Pavel Březina pid = (pid_t) (tmp);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Make sure the daemon goes away! */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = kill(pid, SIGTERM);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek fprintf(stderr, "killing %u\n", pid);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_true(ret == 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek unlink(pidfile);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekvoid test_run_as_root_fg(void **state)
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int ret;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct main_context *main_ctx;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid_t pid;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Must root as root, real or fake */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(geteuid(), 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid = fork();
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek if (pid == 0) {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = server_setup(__FUNCTION__, 0, 0, 0,
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek __FUNCTION__, &main_ctx);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(ret, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek exit(0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek wait_for_fg_server(pid);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekvoid test_run_as_sssd_fg(void **state)
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int ret;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct main_context *main_ctx;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct passwd *sssd;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid_t pid;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Must root as root, real or fake */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(geteuid(), 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek sssd = getpwnam("sssd");
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_non_null(sssd);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid = fork();
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek if (pid == 0) {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = server_setup(__FUNCTION__, 0, sssd->pw_uid, sssd->pw_gid,
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek __FUNCTION__, &main_ctx);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(ret, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek exit(0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek wait_for_fg_server(pid);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekvoid test_run_as_root_daemon(void **state)
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int ret;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct main_context *main_ctx;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid_t pid;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek char *pidfile;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Must root as root, real or fake */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(geteuid(), 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pidfile = talloc_asprintf(NULL, "%s/%s.pid", TEST_PID_PATH, __FUNCTION__);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Make sure there are no leftovers */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek unlink(pidfile);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pid = fork();
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek if (pid == 0) {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek ret = server_setup(__FUNCTION__, FLAGS_PID_FILE,
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek 0, 0, __FUNCTION__, &main_ctx);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek assert_int_equal(ret, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek server_loop(main_ctx);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek exit(0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek wait_for_bg_server(pidfile);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek talloc_free(pidfile);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozekint main(int argc, const char *argv[])
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek{
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek poptContext pc;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int opt;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek int rv;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek struct poptOption long_options[] = {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek POPT_AUTOHELP
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek SSSD_DEBUG_OPTS
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek POPT_TABLEEND
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek };
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek const struct CMUnitTest tests[] = {
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test(test_run_as_root_fg),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test(test_run_as_sssd_fg),
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test(test_run_as_root_daemon),
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek };
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Set debug level to invalid value so we can deside if -d 0 was used. */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek debug_level = SSSDBG_INVALID;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek while((opt = poptGetNextOpt(pc)) != -1) {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek switch(opt) {
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek default:
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek fprintf(stderr, "\nInvalid option %s: %s\n\n",
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek poptBadOption(pc, 0), poptStrerror(opt));
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek poptPrintUsage(pc, stderr, 0);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek return 1;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek poptFreeContext(pc);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek DEBUG_CLI_INIT(debug_level);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek /* Even though normally the tests should clean up after themselves
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek * they might not after a failed run. Remove the old db to be sure */
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek tests_set_cwd();
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek test_dom_suite_cleanup(TEST_DB_PATH, CONFDB_FILE, NULL);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek test_dom_suite_setup(TEST_DB_PATH);
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek rv = cmocka_run_group_tests(tests, NULL, NULL);
0da2cc8888ecc34c8630f01736a4a5dff0791551Lukas Slebodnik if (rv == 0) {
482914ed19e46146b3dd567fd0ce0faccd097e9dLukas Slebodnik test_dom_suite_cleanup(TEST_DB_PATH, CONFDB_FILE, NULL);
482914ed19e46146b3dd567fd0ce0faccd097e9dLukas Slebodnik }
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek return rv;
2689efa614826d45cab60ea1186d44b8bdd243adJakub Hrozek}