2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose/*
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose SSSD - Test for routine to check to access to responder sockets
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose Authors:
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose Sumit Bose <sbose@redhat.com>
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose Copyright (C) 2012 Red Hat
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose This program is free software; you can redistribute it and/or modify
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose it under the terms of the GNU General Public License as published by
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose the Free Software Foundation; either version 3 of the License, or
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose (at your option) any later version.
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose This program is distributed in the hope that it will be useful,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose GNU General Public License for more details.
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose You should have received a copy of the GNU General Public License
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose*/
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose#include <popt.h>
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose#include <check.h>
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose#include <string.h>
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose#include "tests/common.h"
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose#include "responder/common/responder.h"
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bosestruct cli_protocol_version *register_cli_protocol_version(void)
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose static struct cli_protocol_version responder_test_cli_protocol_version[] = {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {0, NULL, NULL}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose };
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose return responder_test_cli_protocol_version;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bosestruct s2a_data {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose const char *inp;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int exp_ret;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t exp_count;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_t *exp_uids;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose};
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bosestruct s2a_data s2a_data[] = {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1,2,3", 0, 3, (uid_t []){1, 2, 3}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1,2,3, 4,5 , 6 , 7 ", 0, 7, (uid_t []){1, 2, 3, 4, 5, 6, 7}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1", 0, 1, (uid_t []){1}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, +2,3", 0, 3, (uid_t []){1, 2, 3}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, -2,3", ERANGE, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, 2ab, 3, 4", EINVAL, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1,", EINVAL, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"", EINVAL, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, 2, 4294967295", 0, 3, (uid_t []){1, 2, 4294967295U}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, 2, 4294967296", ERANGE, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {"1, 2, root, 4, 5", 0, 5, (uid_t []){1, 2, 0, 4, 5}},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {NULL, EINVAL, 0, NULL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {NULL, -1, 0, NULL}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose};
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit BoseSTART_TEST(resp_str_to_array_test)
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int ret;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t uid_count;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_t *uids = NULL;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t c;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t d;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose for (c = 0; s2a_data[c].exp_ret != -1; c++) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose ret = csv_string_to_uid_array(global_talloc_context, s2a_data[c].inp,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose true, &uid_count, &uids);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose fail_unless(ret == s2a_data[c].exp_ret,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose "csv_string_to_uid_array failed [%d][%s].", ret,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose strerror(ret));
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose if (ret == 0) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose fail_unless(uid_count == s2a_data[c].exp_count,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose "Wrong number of values, expected [%d], got [%d].",
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose s2a_data[c].exp_count, uid_count);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose for (d = 0; d < s2a_data[c].exp_count; d++) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose fail_unless(uids[d] == s2a_data[c].exp_uids[d],
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose "Wrong value, expected [%d], got [%d].\n",
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose s2a_data[c].exp_uids[d], uids[d]);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose talloc_free(uids);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uids = NULL;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit BoseEND_TEST
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bosestruct uid_check_data {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_t uid;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t allowed_uids_count;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_t *allowed_uids;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int exp_ret;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose};
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bosestruct uid_check_data uid_check_data[] = {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {1, 3, (uid_t []){1, 2, 3}, 0},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {2, 3, (uid_t []){1, 2, 3}, 0},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {3, 3, (uid_t []){1, 2, 3}, 0},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {4, 3, (uid_t []){1, 2, 3}, EACCES},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {4, 0, NULL, EINVAL},
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose {0, 0, NULL, -1}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose};
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit BoseSTART_TEST(check_allowed_uids_test)
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int ret;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose size_t c;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
e6bf08648e4b808da75d220dfab8d17a2352ca02Lukas Slebodnik for (c = 0; uid_check_data[c].exp_ret != -1; c++) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose ret = check_allowed_uids(uid_check_data[c].uid,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_check_data[c].allowed_uids_count,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose uid_check_data[c].allowed_uids);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose fail_unless(ret == uid_check_data[c].exp_ret,
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose "check_allowed_uids failed [%d][%s].", ret, strerror(ret));
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit BoseEND_TEST
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit BoseSuite *responder_test_suite(void)
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose Suite *s = suite_create ("Responder socket access");
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose TCase *tc_utils = tcase_create("Utility test");
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose tcase_add_test(tc_utils, resp_str_to_array_test);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose tcase_add_test(tc_utils, check_allowed_uids_test);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose suite_add_tcase(s, tc_utils);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose return s;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose}
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Boseint main(int argc, const char *argv[])
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose{
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int opt;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose int number_failed;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose poptContext pc;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose struct poptOption long_options[] = {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose POPT_AUTOHELP
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose SSSD_MAIN_OPTS
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose POPT_TABLEEND
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose };
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose debug_level = SSSDBG_INVALID;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose pc = poptGetContext(argv[0], argc, argv, long_options, 0);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose while((opt = poptGetNextOpt(pc)) != -1) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose switch(opt) {
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose default:
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose fprintf(stderr, "\nInvalid option %s: %s\n\n",
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose poptBadOption(pc, 0), poptStrerror(opt));
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose poptPrintUsage(pc, stderr, 0);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose return 1;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose }
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose poptFreeContext(pc);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
6b57784f0f175275fd900eca21c77415e3a5ea52Jakub Hrozek DEBUG_CLI_INIT(debug_level);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose tests_set_cwd();
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose Suite *s = responder_test_suite();
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose SRunner *sr = srunner_create(s);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose srunner_run_all(sr, CK_ENV);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose number_failed = srunner_ntests_failed (sr);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose srunner_free (sr);
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2d257ccf620ce1b611f89cec8f0a94c88c2f2881Sumit Bose}