sysdb-tests.c revision 89caf5edcc99f5731e89bd51e6ffaad3ec11c304
/*
SSSD
System Database
Copyright (C) Stephen Gallagher <sgallagh@redhat.com> 2009
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <check.h>
#include <talloc.h>
#include <tevent.h>
#include <popt.h>
#include "confdb/confdb_setup.h"
#include "db/sysdb_private.h"
#define TESTS_PATH "tests_sysdb"
#define TEST_CONF_FILE "tests_conf.ldb"
#define TEST_ATTR_NAME "test_attr_name"
#define TEST_ATTR_VALUE "test_attr_value"
#define TEST_ATTR_UPDATE_VALUE "test_attr_update_value"
#define TEST_ATTR_ADD_NAME "test_attr_add_name"
#define TEST_ATTR_ADD_VALUE "test_attr_add_value"
#define CUSTOM_TEST_CONTAINER "custom_test_container"
#define CUSTOM_TEST_OBJECT "custom_test_object"
#define ASQ_TEST_USER "testuser27010"
#define ASQ_TEST_USER_UID 27010
#define MBO_USER_BASE 27500
#define MBO_GROUP_BASE 28500
struct sysdb_test_ctx {
struct confdb_ctx *confdb;
struct tevent_context *ev;
struct sss_domain_info *domain;
};
{
struct sysdb_test_ctx *test_ctx;
char *conf_db;
int ret;
const char *val[2];
/* Create tests directory if it doesn't exist */
/* (relative to current dir) */
return EFAULT;
}
fail("Could not allocate memory for test context");
return ENOMEM;
}
/* Create an event context
* It will not be used except in confdb_init and sysdb_init
*/
fail("Could not create event context");
return EIO;
}
fail("Out of memory, aborting!");
return ENOMEM;
}
/* Connect to the conf db */
fail("Could not initialize connection to the confdb");
return ret;
}
val[0] = "LOCAL";
fail("Could not initialize domains placeholder");
return ret;
}
val[0] = "local";
fail("Could not initialize provider");
return ret;
}
val[0] = "TRUE";
fail("Could not initialize LOCAL domain");
return ret;
}
val[0] = "TRUE";
fail("Could not initialize LOCAL domain");
return ret;
}
fail("Could not retrieve LOCAL domain");
return ret;
}
return ret;
}
return EOK;
}
struct test_data {
struct tevent_context *ev;
struct sysdb_test_ctx *ctx;
const char *username;
const char *groupname;
const char *netgrname;
const char *shell;
bool finished;
int error;
struct sysdb_attrs *attrs;
const char **attrlist;
struct ldb_message *msg;
struct ldb_message **msgs;
};
{
char *homedir;
char *gecos;
int ret;
NULL, 0);
return ret;
}
{
char *homedir;
char *gecos;
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
}
return ret;
}
{
int ret;
return ret;
}
{
const char *username;
int ret;
return ENOMEM;
}
return ret;
}
{
const char *username;
int ret;
return ENOMEM;
}
return ret;
}
{
char *object_name;
int ret;
if (!object_name) {
return ENOMEM;
}
return ret;
}
{
int ret;
return ret;
}
{
int ret;
"LOCAL");
return ENOMEM;
}
LDB_SCOPE_SUBTREE, "objectClass=user",
return ret;
}
{
int ret;
"LOCAL");
if (!dn) {
return ENOMEM;
}
return ret;
}
{
int ret;
char *member;
int i;
if (!attrs) {
return ENOMEM;
}
if (!member) {
return ENOMEM;
}
return ret;
}
}
return ret;
}
{
const char *description;
int ret;
return ret;
}
{
struct ldb_dn *netgroup_dn;
int ret;
if (!netgroup_dn) return ENOMEM;
return ret;
}
{
int ret;
return ret;
}
{
int ret;
const char *description;
if (!attrs) {
return ENOMEM;
}
if (ret) {
return ret;
}
return ret;
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
const char *username;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (ret) {
fail("sysdb_getpwnam failed for username %s (%d: %s)",
goto done;
}
goto done;
}
/* Search for the user with the wrong case */
if (ret) {
fail("sysdb_getpwnam failed for username %s (%d: %s)",
goto done;
}
fail("The upper-case username search should fail.");
}
done:
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
const char *groupname;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (ret) {
fail("sysdb_getgrnam failed for groupname %s (%d: %s)",
goto done;
}
goto done;
}
"Did not find the expected GID (found %d expected %d)",
/* Search for the group with the wrong case */
if (ret) {
fail("sysdb_getgrnam failed for groupname %s (%d: %s)",
goto done;
}
fail("The upper-case groupname search should fail.");
}
done:
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
const char *e_groupname;
const char *groupname;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (ret) {
fail("sysdb_getgrgid failed for gid %d (%d: %s)",
goto done;
}
if (e_groupname == NULL) {
fail("Cannot allocate memory");
goto done;
}
"Did not find the expected groupname (found %s expected %s)",
done:
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
const char *e_username;
const char *username;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (ret) {
fail("sysdb_getpwuid failed for uid %d (%d: %s)",
goto done;
}
fail("Cannot allocate memory");
goto done;
}
"Did not find the expected username (found %s expected %s)",
done:
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
&res);
"sysdb_enumgrent failed (%d: %s)",
/* 10 groups + 10 users (we're MPG) */
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
&res);
"sysdb_enumpwent failed (%d: %s)",
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
fail("Could not create the changeset");
return;
}
fail("Could not create the changeset");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
const char *attrval;
char *username;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (ret) {
goto done;
}
"Got bad attribute value for user %s", username);
done:
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
fail("Could not create attribute list");
return;
}
fail("Could not add attribute");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
char *object_name;
/* Setup */
fail("Could not set up the test");
return;
}
&data->msgs_count,
"Wrong number of objects, exptected [1] got [%d]",
data->msgs_count);
"Wrong number of results, expected [1] got [%d]",
"Wrong attribute name");
"Wrong number of attribute values");
"Wrong attribute value");
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
fail("Could not create attribute list");
return;
}
fail("Could not add attribute");
return;
}
fail("Could not add attribute");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
char *object_name;
struct ldb_message_element *el;
/* Setup */
fail("Could not set up the test");
return;
}
&data->msgs_count,
"Wrong number of objects, exptected [1] got [%d]",
data->msgs_count);
"Wrong number of results, expected [2] got [%d]",
"Wrong attribute value");
"Wrong attribute value");
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *filter = "(distinguishedName=*)";
/* Setup */
fail("Could not set up the test");
return;
}
&data->msgs_count,
"Wrong number of objects, exptected [10] got [%d]",
data->msgs_count);
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
}
static void cached_authentication_without_expiration(const char *username,
const char *password,
int expected_result)
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *val[2];
/* Setup */
val[0] = "0";
fail("Could not initialize provider");
return;
}
"return expected result [%d].",
0, expire_date);
-1, delayed_until);
}
static void cached_authentication_with_expiration(const char *username,
const char *password,
int expected_result)
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *val[2];
/* Setup */
val[0] = "1";
fail("Could not initialize provider");
return;
}
"sysdb_cache_auth request does not return expected "
"Wrong expire date, expected [%d], got [%d]",
-1, delayed_until);
}
{
char *username;
}
{
char *username;
}
{
char *username;
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
struct ldb_message **msgs;
int i;
char *gid_str;
/* Setup */
fail("Could not set up the test");
return;
}
"found [%d] expected [10]", msgs_count);
for (i = 0; i < msgs_count; i++) {
"found [%d] expected [1]",
msgs[i]->num_elements);
"wrong number of values, found [%d] expected [1]",
"wrong value, found [%.*s] expected [%s]",
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
int i;
char *uid_str;
/* Setup */
fail("Could not set up the test");
return;
}
"wrong number of results, found [%d] expected [10]",
data->msgs_count);
for (i = 0; i < data->msgs_count; i++) {
"wrong number of elements, found [%d] expected [1]",
"wrong number of values, found [%d] expected [1]",
"wrong value, found [%.*s] expected [%s]",
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_attrs *attrs;
struct ldb_message_element *el;
int ret;
"sysdb_attrs_replace overwrites existing attribute");
"Wrong number of values for attribute oof, "
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
if (_i == 0) {
} else {
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
_i + MBO_GROUP_BASE,
if (_i == 5) {
"sysdb_search_group_by_gid found "
"already deleted group");
} else {
"Wrong number of results, expected [1] got [%d]",
"Wrong attribute name");
"Wrong number of attribute values, "
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
_i + MBO_GROUP_BASE,
"Wrong number of results, expected [1] got [%d]",
"Wrong attribute name");
"Wrong number of attribute values, expected [%d] got [%d]",
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
_i + MBO_GROUP_BASE,
"Wrong number of results, expected [1] got [%d]",
"Wrong attribute name");
"Wrong number of attribute values, expected [%d] got [%d]",
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
_i + MBO_GROUP_BASE,
if (_i == 5) {
"sysdb_search_group_by_gid_send found "
"already deleted group");
} else {
"Wrong number of results, expected [1] got [%d]",
"Wrong attribute name");
"Wrong number of attribute values, expected [%d] got [%d]",
}
}
{
char **list;
"test_attr", &list);
list[0]);
list[1]);
}
{
struct sysdb_test_ctx *test_ctx;
char **add_groups;
char **del_groups;
const char *user = "testuser27000";
/* Setup */
/* Add a user to two groups */
(const char *const *)add_groups, NULL);
/* Remove a user from one group and add to another */
(const char *const *)add_groups,
(const char *const *)del_groups);
/* Remove a user from two groups */
NULL, (const char *const *)del_groups);
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *groupname;
char *parsed;
/* Setup */
fail("Could not set up the test");
return;
}
fail("Out of memory");
return;
}
"Names don't match (got %s)", parsed);
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *netgrname;
struct ldb_message *msg;
struct ldb_dn *netgroup_dn;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
int ret;
const char *description;
const char *netgrname;
struct ldb_result *res;
const char *attrval;
/* Setup */
fail("Could not set up the test");
return;
}
"Got bad attribute value for netgroup %s", netgrname);
}
{
struct sysdb_test_ctx *test_ctx;
const char *netgrname;
const char *hostname;
const char *username;
const char *domainname;
struct ldb_result *res;
struct sysdb_netgroup_ctx **entries;
/* Setup */
fail("Could not set up the test");
return;
}
"Got more than one triple back");
"Got [%s], expected [%s] for hostname",
"Got [%s], expected [%s] for username",
"Got [%s], expected [%s] for domainname",
}
{
struct sysdb_test_ctx *test_ctx;
const char *netgrname;
const char *hostname;
const char *username;
const char *domainname;
struct ldb_result *res;
struct sysdb_netgroup_ctx **entries;
/* Setup */
fail("Could not set up the test");
return;
}
}
{
struct sysdb_test_ctx *test_ctx;
const char *netgrname;
const char *membername;
struct ldb_result *res;
struct sysdb_netgroup_ctx **entries;
char *hostname1;
char *username1;
char *domainname1;
char *hostname2;
char *username2;
char *domainname2;
/* Setup */
fail("Could not set up the test");
return;
}
"Did not get exactly two responses");
"Got [%s], expected [%s] for hostname",
"Got [%s], expected [%s] for username",
"Got [%s], expected [%s] for domainname",
"Got [%s], expected [%s] for hostname",
"Got [%s], expected [%s] for username",
"Got [%s], expected [%s] for domainname",
}
{
struct sysdb_test_ctx *test_ctx;
const char *netgrname;
const char *membername;
struct ldb_result *res;
struct sysdb_netgroup_ctx **entries;
char *hostname;
char *username;
char *domainname;
/* Setup */
fail("Could not set up the test");
return;
}
"Did not get exactly one response");
"Got [%s], expected [%s] for hostname",
"Got [%s], expected [%s] for username",
"Got [%s], expected [%s] for domainname",
}
{
struct sysdb_test_ctx *test_ctx;
struct ldb_result *res;
struct ldb_message *msg;
const char odd_username[] = "*(odd)\\user,name";
const char odd_groupname[] = "*(odd\\*)\\group,name";
const char odd_netgroupname[] = "*(odd\\*)\\netgroup,name";
const char *received_user;
const char *received_group;
static const char *user_attrs[] = SYSDB_PW_ATTRS;
static const char *netgr_attrs[] = SYSDB_NETGR_ATTRS;
/* Setup */
fail("Could not set up the test");
return;
}
/* ===== Groups ===== */
/* Add */
/* Retrieve */
"Expected [%s], got [%s]",
/* ===== Users ===== */
/* Add */
10000, 10000,
"","","");
/* Retrieve */
"Expected [%s] got [%s]\n",
/* Add to the group */
"Expected [%s], got [%s]",
/* Attributes */
/* Delete User */
/* Delete Group */
/* ===== Netgroups ===== */
/* Add */
odd_netgroupname, "No description",
NULL, 30);
/* Retrieve */
odd_netgroupname, &res);
/* ===== Arbitrary Entries ===== */
}
{
struct sysdb_test_ctx *test_ctx;
bool enumerated;
/* Setup */
true);
/* Recheck enumeration status */
&enumerated);
}
{
struct sysdb_test_ctx *test_ctx;
const char *filter;
struct ldb_message **msgs;
/* Setup */
"case_sensitive_group1", 29000,
"cn=case_sensitive_group1,cn=example,cn=com",
true);
"case_sensitive_group2", 29001,
"cn=CASE_SENSITIVE_GROUP1,cn=EXAMPLE,cn=COM",
true);
/* Search by originalDN should yield 2 entries */
"cn=case_sensitive_group1,cn=example,cn=com");
"case insensitive originalDN search");
}
Suite *create_sysdb_suite(void)
{
/* Create a new user */
/* Verify the users were added */
/* Create a new group */
/* Verify the groups were added */
/* sysdb_group_dn_name returns the name of the group in question */
/* sysdb_store_user allows setting attributes for existing users */
/* test the change */
/* Add and remove users in a group with sysdb_update_members */
/* Remove the other half by gid */
/* Remove the other half by uid */
/* Create a new user */
/* Verify the users were added */
/* Verify the users can be queried by UID */
/* Enumerate the users */
/* Change their attribute */
/* Verify the change */
/* Create a new group */
/* Verify the groups were added */
/* Verify the groups can be queried by GID */
/* Enumerate the groups */
/* Add some members to the groups */
/* Authenticate with missing cached password */
27010, 27011);
/* Add a cached password */
/* Authenticate against cached password */
27010, 27011);
/* ASQ search test */
/* Test search with more than one result */
/* Remove the members from the groups */
/* Remove the users by name */
/* Remove the groups by name */
/* test the ignore_not_found parameter for users */
/* test the ignore_not_found parameter for groups */
/* Create incomplete groups - remove will fail if the LDB objects don't exist */
/* test custom operations */
/* test recursive delete */
/* Test unusual characters */
/* Test sysdb enumerated flag */
/* Test originalDN searches */
/* ===== NETGROUP TESTS ===== */
/* Create a new netgroup */
/* Verify the netgroups were added */
/* Test setting attributes */
/* Verify they have been changed */
/* Add some tuples */
/* Add a nested netgroup */
/* Remove the nested netgroup */
/* Remove the tuples */
/* Remove half of them by name */
/* Remove the other half by DN */
/* Add all test cases to the test suite */
suite_add_tcase(s, tc_sysdb);
0, 10);
0, 10);
0, 10);
0, 10);
0, 10);
0, 10);
return s;
}
int opt;
int ret;
int failure_count;
int no_cleanup = 0;
struct poptOption long_options[] = {
_("Do not delete the test database after a test run"), NULL },
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
switch(opt) {
default:
return 1;
}
}
return EXIT_FAILURE;
}
/* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
if (failure_count == 0 && !no_cleanup) {
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}