LCOV - code coverage report
Current view: top level - tests - sysdb-tests.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 2858 2998 95.3 %
Date: 2015-10-19 Functions: 154 154 100.0 %

          Line data    Source code
       1             : /*
       2             :    SSSD
       3             : 
       4             :    System Database
       5             : 
       6             :    Copyright (C) Stephen Gallagher <sgallagh@redhat.com>  2009
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include <stdlib.h>
      23             : #include <check.h>
      24             : #include <talloc.h>
      25             : #include <tevent.h>
      26             : #include <popt.h>
      27             : #include <sys/stat.h>
      28             : #include <sys/types.h>
      29             : #include "util/util.h"
      30             : #include "util/crypto/sss_crypto.h"
      31             : #include "confdb/confdb_setup.h"
      32             : #include "db/sysdb_private.h"
      33             : #include "db/sysdb_services.h"
      34             : #include "db/sysdb_autofs.h"
      35             : #include "tests/common.h"
      36             : 
      37             : #define TESTS_PATH "tp_" BASE_FILE_STEM
      38             : #define TEST_CONF_FILE "tests_conf.ldb"
      39             : 
      40             : #define TEST_ATTR_NAME "test_attr_name"
      41             : #define TEST_ATTR_VALUE "test_attr_value"
      42             : #define TEST_ATTR_UPDATE_VALUE "test_attr_update_value"
      43             : #define TEST_ATTR_ADD_NAME "test_attr_add_name"
      44             : #define TEST_ATTR_ADD_VALUE "test_attr_add_value"
      45             : #define CUSTOM_TEST_CONTAINER "custom_test_container"
      46             : #define CUSTOM_TEST_OBJECT "custom_test_object"
      47             : 
      48             : #define ASQ_TEST_USER "testuser27010"
      49             : #define ASQ_TEST_USER_UID 27010
      50             : 
      51             : #define MBO_USER_BASE 27500
      52             : #define MBO_GROUP_BASE 28500
      53             : #define NUM_GHOSTS 10
      54             : 
      55             : #define TEST_AUTOFS_MAP_BASE 29500
      56             : 
      57             : struct sysdb_test_ctx {
      58             :     struct sysdb_ctx *sysdb;
      59             :     struct confdb_ctx *confdb;
      60             :     struct tevent_context *ev;
      61             :     struct sss_domain_info *domain;
      62             : };
      63             : 
      64         824 : static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
      65             : {
      66             :     struct sysdb_test_ctx *test_ctx;
      67             :     char *conf_db;
      68             :     int ret;
      69             : 
      70             :     const char *val[2];
      71         824 :     val[1] = NULL;
      72             : 
      73             :     /* Create tests directory if it doesn't exist */
      74             :     /* (relative to current dir) */
      75         824 :     ret = mkdir(TESTS_PATH, 0775);
      76         824 :     if (ret == -1 && errno != EEXIST) {
      77           0 :         fail("Could not create %s directory", TESTS_PATH);
      78             :         return EFAULT;
      79             :     }
      80             : 
      81         824 :     test_ctx = talloc_zero(NULL, struct sysdb_test_ctx);
      82         824 :     if (test_ctx == NULL) {
      83           0 :         fail("Could not allocate memory for test context");
      84             :         return ENOMEM;
      85             :     }
      86             : 
      87             :     /* Create an event context
      88             :      * It will not be used except in confdb_init and sysdb_init
      89             :      */
      90         824 :     test_ctx->ev = tevent_context_init(test_ctx);
      91         824 :     if (test_ctx->ev == NULL) {
      92           0 :         fail("Could not create event context");
      93             :         talloc_free(test_ctx);
      94             :         return EIO;
      95             :     }
      96             : 
      97         824 :     conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
      98         824 :     if (conf_db == NULL) {
      99           0 :         fail("Out of memory, aborting!");
     100             :         talloc_free(test_ctx);
     101             :         return ENOMEM;
     102             :     }
     103         824 :     DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);
     104             : 
     105             :     /* Connect to the conf db */
     106         824 :     ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
     107         824 :     if (ret != EOK) {
     108           0 :         fail("Could not initialize connection to the confdb");
     109             :         talloc_free(test_ctx);
     110             :         return ret;
     111             :     }
     112             : 
     113         824 :     val[0] = "LOCAL";
     114         824 :     ret = confdb_add_param(test_ctx->confdb, true,
     115             :                            "config/sssd", "domains", val);
     116         824 :     if (ret != EOK) {
     117           0 :         fail("Could not initialize domains placeholder");
     118             :         talloc_free(test_ctx);
     119             :         return ret;
     120             :     }
     121             : 
     122         824 :     val[0] = "local";
     123         824 :     ret = confdb_add_param(test_ctx->confdb, true,
     124             :                            "config/domain/LOCAL", "id_provider", val);
     125         824 :     if (ret != EOK) {
     126           0 :         fail("Could not initialize provider");
     127             :         talloc_free(test_ctx);
     128             :         return ret;
     129             :     }
     130             : 
     131         824 :     val[0] = enumerate ? "TRUE" : "FALSE";
     132         824 :     ret = confdb_add_param(test_ctx->confdb, true,
     133             :                            "config/domain/LOCAL", "enumerate", val);
     134         824 :     if (ret != EOK) {
     135           0 :         fail("Could not initialize LOCAL domain");
     136             :         talloc_free(test_ctx);
     137             :         return ret;
     138             :     }
     139             : 
     140         824 :     val[0] = "TRUE";
     141         824 :     ret = confdb_add_param(test_ctx->confdb, true,
     142             :                            "config/domain/LOCAL", "cache_credentials", val);
     143         824 :     if (ret != EOK) {
     144           0 :         fail("Could not initialize LOCAL domain");
     145             :         talloc_free(test_ctx);
     146             :         return ret;
     147             :     }
     148             : 
     149         824 :     ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
     150             :                            TESTS_PATH, &test_ctx->domain);
     151         824 :     if (ret != EOK) {
     152           0 :         fail("Could not initialize connection to the sysdb (%d)", ret);
     153             :         talloc_free(test_ctx);
     154             :         return ret;
     155             :     }
     156         824 :     test_ctx->sysdb = test_ctx->domain->sysdb;
     157             : 
     158         824 :     *ctx = test_ctx;
     159         824 :     return EOK;
     160             : }
     161             : 
     162             : #define setup_sysdb_tests(ctx) _setup_sysdb_tests((ctx), false)
     163             : 
     164             : struct test_data {
     165             :     struct tevent_context *ev;
     166             :     struct sysdb_test_ctx *ctx;
     167             : 
     168             :     const char *username;
     169             :     const char *groupname;
     170             :     const char *netgrname;
     171             :     const char *autofsmapname;
     172             :     uid_t uid;
     173             :     gid_t gid;
     174             :     const char *shell;
     175             : 
     176             :     bool finished;
     177             :     int error;
     178             : 
     179             :     struct sysdb_attrs *attrs;
     180             :     const char **attrlist;
     181             :     char **memberlist;
     182             :     struct ldb_message *msg;
     183             : 
     184             :     size_t msgs_count;
     185             :     struct ldb_message **msgs;
     186             : };
     187             : 
     188          10 : static int test_add_user(struct test_data *data)
     189             : {
     190             :     char *homedir;
     191             :     char *gecos;
     192             :     int ret;
     193             : 
     194          10 :     homedir = talloc_asprintf(data, "/home/testuser%d", data->uid);
     195          10 :     gecos = talloc_asprintf(data, "Test User %d", data->uid);
     196             : 
     197          10 :     ret = sysdb_add_user(data->ctx->domain, data->username,
     198             :                          data->uid, 0, gecos, homedir, "/bin/bash",
     199             :                          NULL, NULL, 0, 0);
     200          10 :     return ret;
     201             : }
     202             : 
     203          45 : static int test_store_user(struct test_data *data)
     204             : {
     205             :     char *homedir;
     206             :     char *gecos;
     207             :     int ret;
     208             : 
     209          45 :     homedir = talloc_asprintf(data, "/home/testuser%d", data->uid);
     210          45 :     gecos = talloc_asprintf(data, "Test User %d", data->uid);
     211             : 
     212          45 :     ret = sysdb_store_user(data->ctx->domain,
     213             :                            data->username, "x",
     214             :                            data->uid, 0, gecos, homedir,
     215          45 :                            data->shell ? data->shell : "/bin/bash",
     216             :                            NULL, NULL, NULL, -1, 0);
     217          45 :     return ret;
     218             : }
     219             : 
     220          10 : static int test_remove_user(struct test_data *data)
     221             : {
     222             :     struct ldb_dn *user_dn;
     223             :     int ret;
     224             : 
     225          10 :     user_dn = sysdb_user_dn(data, data->ctx->domain, data->username);
     226          10 :     if (!user_dn) return ENOMEM;
     227             : 
     228          10 :     ret = sysdb_delete_entry(data->ctx->sysdb, user_dn, true);
     229          10 :     return ret;
     230             : }
     231             : 
     232          15 : static int test_remove_user_by_uid(struct test_data *data)
     233             : {
     234             :     int ret;
     235             : 
     236          15 :     ret = sysdb_delete_user(data->ctx->domain, NULL, data->uid);
     237          15 :     return ret;
     238             : }
     239             : 
     240           1 : static int test_remove_nonexistent_group(struct test_data *data)
     241             : {
     242             :     int ret;
     243             : 
     244           1 :     ret = sysdb_delete_group(data->ctx->domain, NULL, data->uid);
     245           1 :     return ret;
     246             : }
     247             : 
     248           1 : static int test_remove_nonexistent_user(struct test_data *data)
     249             : {
     250             :     int ret;
     251             : 
     252           1 :     ret = sysdb_delete_user(data->ctx->domain, NULL, data->uid);
     253           1 :     return ret;
     254             : }
     255             : 
     256          10 : static int test_add_group(struct test_data *data)
     257             : {
     258             :     int ret;
     259             : 
     260          10 :     ret = sysdb_add_group(data->ctx->domain, data->groupname, data->gid,
     261             :                           data->attrs, 0, 0);
     262          10 :     return ret;
     263             : }
     264             : 
     265          10 : static int test_add_incomplete_group(struct test_data *data)
     266             : {
     267             :     int ret;
     268             : 
     269          10 :     ret = sysdb_add_incomplete_group(data->ctx->domain, data->groupname,
     270             :                                      data->gid, NULL, NULL, NULL, true, 0);
     271          10 :     return ret;
     272             : }
     273             : 
     274          20 : static int test_store_group(struct test_data *data)
     275             : {
     276             :     int ret;
     277             : 
     278          20 :     ret = sysdb_store_group(data->ctx->domain,
     279             :                             data->groupname, data->gid, data->attrs, -1, 0);
     280          20 :     return ret;
     281             : }
     282             : 
     283          10 : static int test_remove_group(struct test_data *data)
     284             : {
     285             :     struct ldb_dn *group_dn;
     286             :     int ret;
     287             : 
     288          10 :     group_dn = sysdb_group_dn(data, data->ctx->domain, data->groupname);
     289          10 :     if (!group_dn) return ENOMEM;
     290             : 
     291          10 :     ret = sysdb_delete_entry(data->ctx->sysdb, group_dn, true);
     292          10 :     return ret;
     293             : }
     294             : 
     295          93 : static int test_remove_group_by_gid(struct test_data *data)
     296             : {
     297             :     int ret;
     298             : 
     299          93 :     ret = sysdb_delete_group(data->ctx->domain, NULL, data->gid);
     300          93 :     if (ret == ENOENT) {
     301           2 :         ret = EOK;
     302             :     }
     303          93 :     return ret;
     304             : }
     305             : 
     306          10 : static int test_set_user_attr(struct test_data *data)
     307             : {
     308             :     int ret;
     309             : 
     310          10 :     ret = sysdb_set_user_attr(data->ctx->domain, data->username,
     311             :                               data->attrs, SYSDB_MOD_REP);
     312          10 :     return ret;
     313             : }
     314             : 
     315          39 : static int test_add_group_member(struct test_data *data)
     316             : {
     317             :     const char *username;
     318             :     int ret;
     319             : 
     320          39 :     username = talloc_asprintf(data, "testuser%d", data->uid);
     321          39 :     if (username == NULL) {
     322           0 :         return ENOMEM;
     323             :     }
     324             : 
     325          39 :     ret = sysdb_add_group_member(data->ctx->domain,
     326             :                                  data->groupname, username,
     327             :                                  SYSDB_MEMBER_USER, false);
     328          39 :     return ret;
     329             : }
     330             : 
     331          10 : static int test_remove_group_member(struct test_data *data)
     332             : {
     333             :     const char *username;
     334             :     int ret;
     335             : 
     336          10 :     username = talloc_asprintf(data, "testuser%d", data->uid);
     337          10 :     if (username == NULL) {
     338           0 :         return ENOMEM;
     339             :     }
     340             : 
     341          10 :     ret = sysdb_remove_group_member(data->ctx->domain,
     342             :                                     data->groupname, username,
     343             :                                     SYSDB_MEMBER_USER, false);
     344          10 :     return ret;
     345             : }
     346             : 
     347          11 : static int test_store_custom(struct test_data *data)
     348             : {
     349             :     char *object_name;
     350             :     int ret;
     351             : 
     352          11 :     object_name = talloc_asprintf(data, "%s_%d", CUSTOM_TEST_OBJECT, data->uid);
     353          11 :     if (!object_name) {
     354           0 :         return ENOMEM;
     355             :     }
     356             : 
     357          11 :     ret = sysdb_store_custom(data->ctx->domain, object_name,
     358             :                              CUSTOM_TEST_CONTAINER, data->attrs);
     359          11 :     return ret;
     360             : }
     361             : 
     362           1 : static int test_delete_custom(struct test_data *data)
     363             : {
     364             :     int ret;
     365             : 
     366           1 :     ret = sysdb_delete_custom(data->ctx->domain, CUSTOM_TEST_OBJECT,
     367             :                               CUSTOM_TEST_CONTAINER);
     368           1 :     return ret;
     369             : }
     370             : 
     371           1 : static int test_search_all_users(struct test_data *data)
     372             : {
     373             :     struct ldb_dn *base_dn;
     374             :     int ret;
     375             : 
     376           1 :     base_dn = ldb_dn_new_fmt(data, data->ctx->sysdb->ldb, SYSDB_TMPL_USER_BASE,
     377             :                              "LOCAL");
     378           1 :     if (base_dn == NULL) {
     379           0 :         return ENOMEM;
     380             :     }
     381             : 
     382           1 :     ret = sysdb_search_entry(data, data->ctx->sysdb, base_dn,
     383             :                              LDB_SCOPE_SUBTREE, "objectClass=user",
     384             :                              data->attrlist, &data->msgs_count, &data->msgs);
     385           1 :     return ret;
     386             : }
     387             : 
     388           1 : static int test_delete_recursive(struct test_data *data)
     389             : {
     390             :     struct ldb_dn *dn;
     391             :     int ret;
     392             : 
     393           1 :     dn = ldb_dn_new_fmt(data, data->ctx->sysdb->ldb, SYSDB_DOM_BASE,
     394             :                         "LOCAL");
     395           1 :     if (!dn) {
     396           0 :         return ENOMEM;
     397             :     }
     398             : 
     399           1 :     ret = sysdb_delete_recursive(data->ctx->sysdb, dn, false);
     400           1 :     fail_unless(ret == EOK, "sysdb_delete_recursive returned [%d]", ret);
     401           1 :     return ret;
     402             : }
     403             : 
     404          31 : static int test_memberof_store_group(struct test_data *data)
     405             : {
     406             :     int ret;
     407          31 :     struct sysdb_attrs *attrs = NULL;
     408             :     char *member;
     409             :     int i;
     410             : 
     411          31 :     attrs = sysdb_new_attrs(data);
     412          31 :     if (!attrs) {
     413           0 :         return ENOMEM;
     414             :     }
     415          59 :     for (i = 0; data->attrlist && data->attrlist[i]; i++) {
     416          28 :         member = sysdb_group_strdn(data, data->ctx->domain->name,
     417          28 :                                    data->attrlist[i]);
     418          28 :         if (!member) {
     419           0 :             return ENOMEM;
     420             :         }
     421          28 :         ret = sysdb_attrs_steal_string(attrs, SYSDB_MEMBER, member);
     422          28 :         if (ret != EOK) {
     423           0 :             return ret;
     424             :         }
     425             :     }
     426             : 
     427          31 :     ret = sysdb_store_group(data->ctx->domain,
     428             :                             data->groupname, data->gid, attrs, -1, 0);
     429          31 :     return ret;
     430             : }
     431             : 
     432          40 : static int test_memberof_store_group_with_ghosts(struct test_data *data)
     433             : {
     434             :     int ret;
     435          40 :     struct sysdb_attrs *attrs = NULL;
     436             :     char *member;
     437             :     int i;
     438             : 
     439          40 :     attrs = sysdb_new_attrs(data);
     440          40 :     if (!attrs) {
     441           0 :         return ENOMEM;
     442             :     }
     443             : 
     444          80 :     for (i = 0; data->attrlist && data->attrlist[i]; i++) {
     445          40 :         member = sysdb_group_strdn(data, data->ctx->domain->name,
     446          40 :                                    data->attrlist[i]);
     447          40 :         if (!member) {
     448           0 :             return ENOMEM;
     449             :         }
     450          40 :         ret = sysdb_attrs_steal_string(attrs, SYSDB_MEMBER, member);
     451          40 :         if (ret != EOK) {
     452           0 :             return ret;
     453             :         }
     454             :     }
     455             : 
     456          90 :     for (i = 0; data->memberlist && data->memberlist[i]; i++) {
     457          50 :         ret = sysdb_attrs_steal_string(attrs, SYSDB_GHOST,
     458          50 :                                        data->memberlist[i]);
     459          50 :         if (ret != EOK) {
     460           0 :             return ret;
     461             :         }
     462             :     }
     463             : 
     464          40 :     ret = sysdb_store_group(data->ctx->domain,
     465             :                             data->groupname, data->gid, attrs, -1, 0);
     466          40 :     return ret;
     467             : }
     468             : 
     469          10 : static int test_add_basic_netgroup(struct test_data *data)
     470             : {
     471             :     const char *description;
     472             :     int ret;
     473             : 
     474          10 :     description = talloc_asprintf(data, "Test Netgroup %d", data->uid);
     475             : 
     476          10 :     ret = sysdb_add_basic_netgroup(data->ctx->domain, data->netgrname,
     477             :                                    description);
     478          10 :     return ret;
     479             : }
     480             : 
     481           5 : static int test_remove_netgroup_entry(struct test_data *data)
     482             : {
     483             :     struct ldb_dn *netgroup_dn;
     484             :     int ret;
     485             : 
     486           5 :     netgroup_dn = sysdb_netgroup_dn(data, data->ctx->domain, data->netgrname);
     487           5 :     if (!netgroup_dn) return ENOMEM;
     488             : 
     489           5 :     ret = sysdb_delete_entry(data->ctx->sysdb, netgroup_dn, true);
     490           5 :     return ret;
     491             : }
     492             : 
     493           5 : static int test_remove_netgroup_by_name(struct test_data *data)
     494             : {
     495             :     int ret;
     496             : 
     497           5 :     ret = sysdb_delete_netgroup(data->ctx->domain, data->netgrname);
     498           5 :     return ret;
     499             : }
     500             : 
     501          10 : static int test_set_netgroup_attr(struct test_data *data)
     502             : {
     503             :     int ret;
     504             :     const char *description;
     505          10 :     struct sysdb_attrs *attrs = NULL;
     506             : 
     507          10 :     description = talloc_asprintf(data, "Sysdb Netgroup %d", data->uid);
     508             : 
     509          10 :     attrs = sysdb_new_attrs(data);
     510          10 :     if (!attrs) {
     511           0 :         return ENOMEM;
     512             :     }
     513             : 
     514          10 :     ret = sysdb_attrs_add_string(attrs, SYSDB_DESCRIPTION, description);
     515          10 :     if (ret) {
     516           0 :         return ret;
     517             :     }
     518             : 
     519          10 :     ret = sysdb_set_netgroup_attr(data->ctx->domain, data->netgrname,
     520             :                                   attrs, SYSDB_MOD_REP);
     521          10 :     return ret;
     522             : }
     523             : 
     524           1 : START_TEST (test_sysdb_user_new_id)
     525             : {
     526             :     struct sysdb_test_ctx *test_ctx;
     527             :     int ret;
     528             :     const char *username;
     529           1 :     struct sysdb_attrs *attrs = NULL;
     530             :     struct ldb_message *msg;
     531           1 :     const char *get_attrs[] = { SYSDB_DESCRIPTION, NULL };
     532             :     const char *desc;
     533           1 :     const char *desc_in = "testuser_new_id_desc";
     534             : 
     535             :     /* Setup */
     536           1 :     ret = setup_sysdb_tests(&test_ctx);
     537           1 :     if (ret != EOK) {
     538           0 :         fail("Could not set up the test");
     539             :         return;
     540             :     }
     541             : 
     542           1 :     username = "testuser_newid";
     543             : 
     544           1 :     attrs = sysdb_new_attrs(test_ctx);
     545           1 :     fail_if(attrs == NULL);
     546             : 
     547           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_DESCRIPTION, desc_in);
     548           1 :     fail_if(ret != EOK);
     549             : 
     550           1 :     ret = sysdb_add_user(test_ctx->domain, username,
     551             :                          0, 0, username, "/", "/bin/bash",
     552             :                          NULL, attrs, 0, 0);
     553           1 :     fail_if(ret != EOK, "Could not store user %s", username);
     554             : 
     555           1 :     ret = sysdb_search_user_by_name(test_ctx,
     556           1 :                                     test_ctx->domain,
     557             :                                     username, get_attrs, &msg);
     558           1 :     fail_if(ret != EOK, "Could not retrieve user %s", username);
     559             : 
     560           1 :     desc = ldb_msg_find_attr_as_string(msg, SYSDB_DESCRIPTION, NULL);
     561           1 :     fail_unless(desc != NULL);
     562           1 :     ck_assert_str_eq(desc, desc_in);
     563             : 
     564           1 :     ret = sysdb_delete_user(test_ctx->domain, username, 0);
     565           1 :     fail_unless(ret == EOK, "sysdb_delete_user error [%d][%s]",
     566             :                             ret, strerror(ret));
     567             : 
     568           1 :     talloc_free(test_ctx);
     569             : }
     570           1 : END_TEST
     571             : 
     572          10 : START_TEST (test_sysdb_store_user)
     573             : {
     574             :     struct sysdb_test_ctx *test_ctx;
     575             :     struct test_data *data;
     576             :     int ret;
     577             : 
     578             :     /* Setup */
     579          10 :     ret = setup_sysdb_tests(&test_ctx);
     580          10 :     if (ret != EOK) {
     581           0 :         fail("Could not set up the test");
     582             :         return;
     583             :     }
     584             : 
     585          10 :     data = talloc_zero(test_ctx, struct test_data);
     586          10 :     data->ctx = test_ctx;
     587          10 :     data->ev = test_ctx->ev;
     588          10 :     data->uid = _i;
     589          10 :     data->gid = _i;
     590          10 :     data->username = talloc_asprintf(data, "testuser%d", _i);
     591             : 
     592          10 :     ret = test_store_user(data);
     593             : 
     594          10 :     fail_if(ret != EOK, "Could not store user %s", data->username);
     595          10 :     talloc_free(test_ctx);
     596             : }
     597          10 : END_TEST
     598             : 
     599          10 : START_TEST (test_sysdb_store_user_existing)
     600             : {
     601             :     struct sysdb_test_ctx *test_ctx;
     602             :     struct test_data *data;
     603             :     int ret;
     604             : 
     605             :     /* Setup */
     606          10 :     ret = setup_sysdb_tests(&test_ctx);
     607          10 :     if (ret != EOK) {
     608           0 :         fail("Could not set up the test");
     609             :         return;
     610             :     }
     611             : 
     612          10 :     data = talloc_zero(test_ctx, struct test_data);
     613          10 :     data->ctx = test_ctx;
     614          10 :     data->ev = test_ctx->ev;
     615          10 :     data->uid = _i;
     616          10 :     data->gid = _i;
     617          10 :     data->username = talloc_asprintf(data, "testuser%d", _i);
     618          10 :     data->shell = talloc_asprintf(data, "/bin/ksh");
     619             : 
     620          10 :     ret = test_store_user(data);
     621             : 
     622          10 :     fail_if(ret != EOK, "Could not store user %s", data->username);
     623          10 :     talloc_free(test_ctx);
     624             : }
     625          10 : END_TEST
     626             : 
     627          10 : START_TEST (test_sysdb_store_group)
     628             : {
     629             :     struct sysdb_test_ctx *test_ctx;
     630             :     struct test_data *data;
     631             :     int ret;
     632             : 
     633             :     /* Setup */
     634          10 :     ret = setup_sysdb_tests(&test_ctx);
     635          10 :     if (ret != EOK) {
     636           0 :         fail("Could not set up the test");
     637             :         return;
     638             :     }
     639             : 
     640          10 :     data = talloc_zero(test_ctx, struct test_data);
     641          10 :     data->ctx = test_ctx;
     642          10 :     data->ev = test_ctx->ev;
     643          10 :     data->gid = _i;
     644          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
     645             : 
     646          10 :     ret = test_store_group(data);
     647             : 
     648          10 :     fail_if(ret != EOK, "Could not store POSIX group #%d", _i);
     649          10 :     talloc_free(test_ctx);
     650             : }
     651          10 : END_TEST
     652             : 
     653          10 : START_TEST (test_sysdb_remove_local_user)
     654             : {
     655             :     struct sysdb_test_ctx *test_ctx;
     656             :     struct test_data *data;
     657             :     int ret;
     658             : 
     659             :     /* Setup */
     660          10 :     ret = setup_sysdb_tests(&test_ctx);
     661          10 :     if (ret != EOK) {
     662           0 :         fail("Could not set up the test");
     663             :         return;
     664             :     }
     665             : 
     666          10 :     data = talloc_zero(test_ctx, struct test_data);
     667          10 :     data->ctx = test_ctx;
     668          10 :     data->ev = test_ctx->ev;
     669          10 :     data->username = talloc_asprintf(data, "testuser%d", _i);
     670             : 
     671          10 :     ret = test_remove_user(data);
     672             : 
     673          10 :     fail_if(ret != EOK, "Could not remove user %s", data->username);
     674          10 :     talloc_free(test_ctx);
     675             : }
     676          10 : END_TEST
     677             : 
     678          10 : START_TEST (test_sysdb_remove_local_user_by_uid)
     679             : {
     680             :     struct sysdb_test_ctx *test_ctx;
     681             :     struct test_data *data;
     682             :     int ret;
     683             : 
     684             :     /* Setup */
     685          10 :     ret = setup_sysdb_tests(&test_ctx);
     686          10 :     if (ret != EOK) {
     687           0 :         fail("Could not set up the test");
     688             :         return;
     689             :     }
     690             : 
     691          10 :     data = talloc_zero(test_ctx, struct test_data);
     692          10 :     data->ctx = test_ctx;
     693          10 :     data->ev = test_ctx->ev;
     694          10 :     data->uid = _i;
     695             : 
     696          10 :     ret = test_remove_user_by_uid(data);
     697             : 
     698          10 :     fail_if(ret != EOK, "Could not remove user with uid %d", _i);
     699          10 :     talloc_free(test_ctx);
     700             : }
     701          10 : END_TEST
     702             : 
     703          10 : START_TEST (test_sysdb_remove_local_group)
     704             : {
     705             :     struct sysdb_test_ctx *test_ctx;
     706             :     struct test_data *data;
     707             :     int ret;
     708             : 
     709             :     /* Setup */
     710          10 :     ret = setup_sysdb_tests(&test_ctx);
     711          10 :     if (ret != EOK) {
     712           0 :         fail("Could not set up the test");
     713             :         return;
     714             :     }
     715             : 
     716          10 :     data = talloc_zero(test_ctx, struct test_data);
     717          10 :     data->ctx = test_ctx;
     718          10 :     data->ev = test_ctx->ev;
     719          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
     720             : 
     721          10 :     ret = test_remove_group(data);
     722             : 
     723          10 :     fail_if(ret != EOK, "Could not remove group %s", data->groupname);
     724          10 :     talloc_free(test_ctx);
     725             : }
     726          10 : END_TEST
     727             : 
     728          93 : START_TEST (test_sysdb_remove_local_group_by_gid)
     729             : {
     730             :     struct sysdb_test_ctx *test_ctx;
     731             :     struct test_data *data;
     732             :     int ret;
     733             : 
     734             :     /* Setup */
     735          93 :     ret = setup_sysdb_tests(&test_ctx);
     736          93 :     if (ret != EOK) {
     737           0 :         fail("Could not set up the test");
     738             :         return;
     739             :     }
     740             : 
     741          93 :     data = talloc_zero(test_ctx, struct test_data);
     742          93 :     data->ctx = test_ctx;
     743          93 :     data->ev = test_ctx->ev;
     744          93 :     data->gid = _i;
     745             : 
     746          93 :     ret = test_remove_group_by_gid(data);
     747             : 
     748          93 :     fail_if(ret != EOK, "Could not remove group with gid %d", _i);
     749          93 :     talloc_free(test_ctx);
     750             : }
     751          93 : END_TEST
     752             : 
     753          10 : START_TEST (test_sysdb_add_user)
     754             : {
     755             :     struct sysdb_test_ctx *test_ctx;
     756             :     struct test_data *data;
     757             :     int ret;
     758             : 
     759             :     /* Setup */
     760          10 :     ret = setup_sysdb_tests(&test_ctx);
     761          10 :     if (ret != EOK) {
     762           0 :         fail("Could not set up the test");
     763             :         return;
     764             :     }
     765             : 
     766          10 :     data = talloc_zero(test_ctx, struct test_data);
     767          10 :     data->ctx = test_ctx;
     768          10 :     data->ev = test_ctx->ev;
     769          10 :     data->uid = _i;
     770          10 :     data->gid = _i;
     771          10 :     data->username = talloc_asprintf(data, "testuser%d", _i);
     772             : 
     773          10 :     ret = test_add_user(data);
     774             : 
     775          10 :     fail_if(ret != EOK, "Could not add user %s", data->username);
     776          10 :     talloc_free(test_ctx);
     777             : }
     778          10 : END_TEST
     779             : 
     780          10 : START_TEST (test_sysdb_add_group)
     781             : {
     782             :     struct sysdb_test_ctx *test_ctx;
     783             :     struct test_data *data;
     784             :     int ret;
     785             : 
     786             :     /* Setup */
     787          10 :     ret = setup_sysdb_tests(&test_ctx);
     788          10 :     if (ret != EOK) {
     789           0 :         fail("Could not set up the test");
     790             :         return;
     791             :     }
     792             : 
     793          10 :     data = talloc_zero(test_ctx, struct test_data);
     794          10 :     data->ctx = test_ctx;
     795          10 :     data->ev = test_ctx->ev;
     796          10 :     data->uid = _i;
     797          10 :     data->gid = _i;
     798          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
     799             : 
     800          10 :     ret = test_add_group(data);
     801             : 
     802          10 :     fail_if(ret != EOK, "Could not add group %s", data->groupname);
     803          10 :     talloc_free(test_ctx);
     804             : }
     805          10 : END_TEST
     806             : 
     807          10 : START_TEST (test_sysdb_add_group_with_ghosts)
     808             : {
     809             :     struct sysdb_test_ctx *test_ctx;
     810             :     struct test_data *data;
     811             :     int ret;
     812             :     char *membername;
     813             :     int j;
     814             : 
     815             :     /* Setup */
     816          10 :     ret = setup_sysdb_tests(&test_ctx);
     817          10 :     if (ret != EOK) {
     818           0 :         fail("Could not set up the test");
     819             :         return;
     820             :     }
     821             : 
     822          10 :     data = talloc_zero(test_ctx, struct test_data);
     823          10 :     data->ctx = test_ctx;
     824          10 :     data->ev = test_ctx->ev;
     825          10 :     data->uid = _i;
     826          10 :     data->gid = _i;
     827          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
     828          10 :     fail_unless(data->groupname != NULL, "Out of memory\n");
     829             : 
     830          10 :     data->attrs = sysdb_new_attrs(data);
     831          10 :     if (ret != EOK) {
     832           0 :         fail("Could not create the changeset");
     833             :         return;
     834             :     }
     835             : 
     836          55 :     for (j = MBO_GROUP_BASE; j < _i; j++) {
     837          45 :         membername = talloc_asprintf(data, "testghost%d", j);
     838          45 :         fail_unless(membername != NULL, "Out of memory\n");
     839          45 :         ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, membername);
     840          45 :         if (ret != EOK) {
     841           0 :             fail_unless(ret == EOK, "Cannot add attr\n");
     842             :         }
     843             :     }
     844             : 
     845          10 :     ret = test_store_group(data);
     846             : 
     847          10 :     fail_if(ret != EOK, "Could not add group %s", data->groupname);
     848          10 :     talloc_free(test_ctx);
     849             : }
     850          10 : END_TEST
     851             : 
     852          10 : START_TEST (test_sysdb_add_incomplete_group)
     853             : {
     854             :     struct sysdb_test_ctx *test_ctx;
     855             :     struct test_data *data;
     856             :     int ret;
     857             : 
     858             :     /* Setup */
     859          10 :     ret = setup_sysdb_tests(&test_ctx);
     860          10 :     if (ret != EOK) {
     861           0 :         fail("Could not set up the test");
     862             :         return;
     863             :     }
     864             : 
     865          10 :     data = talloc_zero(test_ctx, struct test_data);
     866          10 :     data->ctx = test_ctx;
     867          10 :     data->ev = test_ctx->ev;
     868          10 :     data->uid = _i;
     869          10 :     data->gid = _i;
     870          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
     871             : 
     872          10 :     ret = test_add_incomplete_group(data);
     873             : 
     874          10 :     fail_if(ret != EOK, "Could not add incomplete group %s", data->groupname);
     875          10 :     talloc_free(test_ctx);
     876             : }
     877          10 : END_TEST
     878             : 
     879          20 : START_TEST (test_sysdb_getpwnam)
     880             : {
     881             :     struct sysdb_test_ctx *test_ctx;
     882             :     struct ldb_result *res;
     883             :     const char *username;
     884             :     uid_t uid;
     885             :     int ret;
     886             : 
     887             :     /* Setup */
     888          20 :     ret = setup_sysdb_tests(&test_ctx);
     889          20 :     if (ret != EOK) {
     890           0 :         fail("Could not set up the test");
     891             :         return;
     892             :     }
     893             : 
     894          20 :     username = talloc_asprintf(test_ctx, "testuser%d", _i);
     895             : 
     896          20 :     ret = sysdb_getpwnam(test_ctx,
     897          20 :                          test_ctx->domain,
     898             :                          username, &res);
     899          20 :     if (ret) {
     900           0 :         fail("sysdb_getpwnam failed for username %s (%d: %s)",
     901             :              username, ret, strerror(ret));
     902             :         goto done;
     903             :     }
     904             : 
     905          20 :     if (res->count != 1) {
     906           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
     907             :         goto done;
     908             :     }
     909             : 
     910          20 :     uid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_UIDNUM, 0);
     911          20 :     fail_unless(uid == _i, "Did not find the expected UID");
     912             : 
     913             :     /* Search for the user with the wrong case */
     914          20 :     username = talloc_asprintf(test_ctx, "TESTUSER%d", _i);
     915             : 
     916          20 :     ret = sysdb_getpwnam(test_ctx,
     917          20 :                          test_ctx->domain,
     918             :                          username, &res);
     919          20 :     if (ret) {
     920           0 :         fail("sysdb_getpwnam failed for username %s (%d: %s)",
     921             :              username, ret, strerror(ret));
     922             :         goto done;
     923             :     }
     924             : 
     925          20 :     if (res->count != 0) {
     926           0 :         fail("The upper-case username search should fail.");
     927             :     }
     928             : 
     929             : done:
     930          20 :     talloc_free(test_ctx);
     931             : }
     932          20 : END_TEST
     933             : 
     934          10 : START_TEST (test_sysdb_getgrnam)
     935             : {
     936             :     struct sysdb_test_ctx *test_ctx;
     937             :     struct ldb_result *res;
     938             :     const char *groupname;
     939             :     gid_t gid;
     940             :     int ret;
     941             : 
     942             :     /* Setup */
     943          10 :     ret = setup_sysdb_tests(&test_ctx);
     944          10 :     if (ret != EOK) {
     945           0 :         fail("Could not set up the test");
     946             :         return;
     947             :     }
     948             : 
     949          10 :     groupname = talloc_asprintf(test_ctx, "testgroup%d", _i);
     950             : 
     951          10 :     ret = sysdb_getgrnam(test_ctx,
     952          10 :                          test_ctx->domain,
     953             :                          groupname, &res);
     954          10 :     if (ret) {
     955           0 :         fail("sysdb_getgrnam failed for groupname %s (%d: %s)",
     956             :              groupname, ret, strerror(ret));
     957             :         goto done;
     958             :     }
     959             : 
     960          10 :     if (res->count != 1) {
     961           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
     962             :         goto done;
     963             :     }
     964             : 
     965          10 :     gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0);
     966          10 :     fail_unless(gid == _i,
     967             :                 "Did not find the expected GID (found %d expected %d)",
     968             :                 gid, _i);
     969             : 
     970             :     /* Search for the group with the wrong case */
     971          10 :     groupname = talloc_asprintf(test_ctx, "TESTGROUP%d", _i);
     972             : 
     973          10 :     ret = sysdb_getgrnam(test_ctx,
     974          10 :                          test_ctx->domain,
     975             :                          groupname, &res);
     976          10 :     if (ret) {
     977           0 :         fail("sysdb_getgrnam failed for groupname %s (%d: %s)",
     978             :              groupname, ret, strerror(ret));
     979             :         goto done;
     980             :     }
     981             : 
     982          10 :     if (res->count != 0) {
     983           0 :         fail("The upper-case groupname search should fail.");
     984             :     }
     985             : 
     986             : done:
     987          10 :     talloc_free(test_ctx);
     988             : }
     989          10 : END_TEST
     990             : 
     991          10 : START_TEST (test_sysdb_getgrgid)
     992             : {
     993             :     struct sysdb_test_ctx *test_ctx;
     994             :     struct ldb_result *res;
     995             :     const char *e_groupname;
     996             :     const char *groupname;
     997             :     int ret;
     998             : 
     999             :     /* Setup */
    1000          10 :     ret = setup_sysdb_tests(&test_ctx);
    1001          10 :     if (ret != EOK) {
    1002           0 :         fail("Could not set up the test");
    1003             :         return;
    1004             :     }
    1005             : 
    1006          20 :     ret = sysdb_getgrgid(test_ctx,
    1007          10 :                          test_ctx->domain,
    1008             :                          _i, &res);
    1009          10 :     if (ret) {
    1010           0 :         fail("sysdb_getgrgid failed for gid %d (%d: %s)",
    1011             :              _i, ret, strerror(ret));
    1012             :         goto done;
    1013             :     }
    1014             : 
    1015          10 :     groupname = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, 0);
    1016             : 
    1017          10 :     e_groupname = talloc_asprintf(test_ctx, "testgroup%d", _i);
    1018          10 :     if (e_groupname == NULL) {
    1019           0 :         fail("Cannot allocate memory");
    1020             :         goto done;
    1021             :     }
    1022             : 
    1023          10 :     fail_unless(strcmp(groupname, e_groupname) == 0,
    1024             :                 "Did not find the expected groupname (found %s expected %s)",
    1025             :                 groupname, e_groupname);
    1026             : done:
    1027          10 :     talloc_free(test_ctx);
    1028             : }
    1029          10 : END_TEST
    1030             : 
    1031          10 : START_TEST (test_sysdb_search_groups)
    1032             : {
    1033             :     struct sysdb_test_ctx *test_ctx;
    1034             :     int ret;
    1035          10 :     const char *attrs[] = { SYSDB_NAME, NULL };
    1036             :     char *filter;
    1037             :     size_t count;
    1038             :     struct ldb_message **msgs;
    1039             : 
    1040             :     /* Setup */
    1041          10 :     ret = setup_sysdb_tests(&test_ctx);
    1042          10 :     fail_if(ret != EOK, "Could not set up the test");
    1043             : 
    1044          10 :     filter = talloc_asprintf(test_ctx, "("SYSDB_GIDNUM"=%d)", _i);
    1045          10 :     fail_if(filter == NULL, "OOM");
    1046             : 
    1047          10 :     ret = sysdb_search_groups(test_ctx, test_ctx->domain,
    1048             :                              filter, attrs, &count, &msgs);
    1049          10 :     talloc_free(filter);
    1050          10 :     fail_if(ret != EOK, "Search failed: %d", ret);
    1051          10 :     fail_if(count != 1, "Did not find the expected group\n");
    1052             : 
    1053          10 :     talloc_free(test_ctx);
    1054             : }
    1055          10 : END_TEST
    1056             : 
    1057          10 : START_TEST (test_sysdb_getpwuid)
    1058             : {
    1059             :     struct sysdb_test_ctx *test_ctx;
    1060             :     struct ldb_result *res;
    1061             :     const char *e_username;
    1062             :     const char *username;
    1063             :     int ret;
    1064             : 
    1065             :     /* Setup */
    1066          10 :     ret = setup_sysdb_tests(&test_ctx);
    1067          10 :     if (ret != EOK) {
    1068           0 :         fail("Could not set up the test");
    1069             :         return;
    1070             :     }
    1071             : 
    1072          20 :     ret = sysdb_getpwuid(test_ctx,
    1073          10 :                          test_ctx->domain,
    1074             :                          _i, &res);
    1075          10 :     if (ret) {
    1076           0 :         fail("sysdb_getpwuid failed for uid %d (%d: %s)",
    1077             :              _i, ret, strerror(ret));
    1078             :         goto done;
    1079             :     }
    1080             : 
    1081          10 :     fail_unless(res->count == 1, "Expected 1 user entry, found %d\n",
    1082             :                 res->count);
    1083             : 
    1084          10 :     username = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, 0);
    1085             : 
    1086          10 :     e_username = talloc_asprintf(test_ctx, "testuser%d", _i);
    1087          10 :     if (username == NULL) {
    1088           0 :         fail("Cannot allocate memory");
    1089             :         goto done;
    1090             :     }
    1091             : 
    1092          10 :     fail_unless(strcmp(username, e_username) == 0,
    1093             :                 "Did not find the expected username (found %s expected %s)",
    1094             :                 username, e_username);
    1095             : done:
    1096          10 :     talloc_free(test_ctx);
    1097             : }
    1098          10 : END_TEST
    1099             : 
    1100           1 : START_TEST (test_sysdb_enumgrent)
    1101             : {
    1102             :     struct sysdb_test_ctx *test_ctx;
    1103             :     struct ldb_result *res;
    1104             :     int ret;
    1105             : 
    1106             :     /* Setup */
    1107           1 :     ret = setup_sysdb_tests(&test_ctx);
    1108           1 :     if (ret != EOK) {
    1109           0 :         fail("Could not set up the test");
    1110             :         return;
    1111             :     }
    1112             : 
    1113           1 :     ret = sysdb_enumgrent(test_ctx,
    1114           1 :                           test_ctx->domain,
    1115             :                           &res);
    1116           1 :     fail_unless(ret == EOK,
    1117             :                 "sysdb_enumgrent failed (%d: %s)",
    1118             :                 ret, strerror(ret));
    1119             : 
    1120             :     /* 10 groups + 10 users (we're MPG) */
    1121           1 :     fail_if(res->count != 20, "Expected 20 users, got %d", res->count);
    1122             : 
    1123           1 :     talloc_free(test_ctx);
    1124             : }
    1125           1 : END_TEST
    1126             : 
    1127           1 : START_TEST (test_sysdb_enumpwent)
    1128             : {
    1129             :     struct sysdb_test_ctx *test_ctx;
    1130             :     struct ldb_result *res;
    1131             :     int ret;
    1132             : 
    1133             :     /* Setup */
    1134           1 :     ret = setup_sysdb_tests(&test_ctx);
    1135           1 :     if (ret != EOK) {
    1136           0 :         fail("Could not set up the test");
    1137             :         return;
    1138             :     }
    1139             : 
    1140           1 :     ret = sysdb_enumpwent(test_ctx,
    1141           1 :                           test_ctx->domain,
    1142             :                           &res);
    1143           1 :     fail_unless(ret == EOK,
    1144             :                 "sysdb_enumpwent failed (%d: %s)",
    1145             :                 ret, strerror(ret));
    1146             : 
    1147           1 :     fail_if(res->count != 10, "Expected 10 users, got %d", res->count);
    1148             : 
    1149           1 :     talloc_free(test_ctx);
    1150             : }
    1151           1 : END_TEST
    1152             : 
    1153             : 
    1154          10 : START_TEST (test_sysdb_set_user_attr)
    1155             : {
    1156             :     struct sysdb_test_ctx *test_ctx;
    1157             :     struct test_data *data;
    1158             :     int ret;
    1159             : 
    1160             :     /* Setup */
    1161          10 :     ret = setup_sysdb_tests(&test_ctx);
    1162          10 :     if (ret != EOK) {
    1163           0 :         fail("Could not set up the test");
    1164             :         return;
    1165             :     }
    1166             : 
    1167          10 :     data = talloc_zero(test_ctx, struct test_data);
    1168          10 :     data->ctx = test_ctx;
    1169          10 :     data->ev = test_ctx->ev;
    1170          10 :     data->username = talloc_asprintf(data, "testuser%d", _i);
    1171             : 
    1172          10 :     data->attrs = sysdb_new_attrs(test_ctx);
    1173          10 :     if (ret != EOK) {
    1174           0 :         fail("Could not create the changeset");
    1175             :         return;
    1176             :     }
    1177             : 
    1178          10 :     ret = sysdb_attrs_add_string(data->attrs,
    1179             :                                  SYSDB_SHELL,
    1180             :                                  "/bin/ksh");
    1181          10 :     if (ret != EOK) {
    1182           0 :         fail("Could not create the changeset");
    1183             :         return;
    1184             :     }
    1185             : 
    1186          10 :     ret = test_set_user_attr(data);
    1187             : 
    1188          10 :     fail_if(ret != EOK, "Could not modify user %s", data->username);
    1189             : 
    1190          10 :     talloc_free(test_ctx);
    1191             : }
    1192          10 : END_TEST
    1193             : 
    1194          10 : START_TEST (test_sysdb_search_users)
    1195             : {
    1196             :     struct sysdb_test_ctx *test_ctx;
    1197             :     int ret;
    1198          10 :     const char *attrs[] = { SYSDB_NAME, NULL };
    1199             :     char *filter;
    1200             :     size_t count;
    1201             :     struct ldb_message **msgs;
    1202             : 
    1203             :     /* Setup */
    1204          10 :     ret = setup_sysdb_tests(&test_ctx);
    1205          10 :     fail_if(ret != EOK, "Could not set up the test");
    1206             : 
    1207          10 :     filter = talloc_asprintf(test_ctx,
    1208             :                              "(&("SYSDB_UIDNUM"=%d)("SYSDB_SHELL"=/bin/ksh))",
    1209             :                              _i);
    1210          10 :     fail_if(filter == NULL, "OOM");
    1211             : 
    1212          10 :     ret = sysdb_search_users(test_ctx, test_ctx->domain,
    1213             :                              filter, attrs, &count, &msgs);
    1214          10 :     talloc_free(filter);
    1215          10 :     fail_if(ret != EOK, "Search failed: %d", ret);
    1216          10 :     fail_if(count != 1, "Did not find the expected user\n");
    1217             : 
    1218          10 :     talloc_free(test_ctx);
    1219             : }
    1220          10 : END_TEST
    1221             : 
    1222          10 : START_TEST (test_sysdb_remove_attrs)
    1223             : {
    1224             :     struct sysdb_test_ctx *test_ctx;
    1225             :     int ret;
    1226             :     char *rmattrs[2];
    1227             :     char *username;
    1228             :     struct ldb_result *res;
    1229             :     const char *shell;
    1230             : 
    1231          10 :     ret = setup_sysdb_tests(&test_ctx);
    1232          10 :     fail_if(ret != EOK, "Could not set up the test");
    1233             : 
    1234          10 :     username = talloc_asprintf(test_ctx, "testuser%d", _i);
    1235          10 :     fail_if(username == NULL, "OOM");
    1236             : 
    1237          10 :     ret = sysdb_getpwnam(test_ctx,
    1238          10 :                          test_ctx->domain,
    1239             :                          username, &res);
    1240          10 :     fail_if(ret != EOK, "sysdb_getpwnam failed for username %s (%d: %s)",
    1241             :                          username, ret, strerror(ret));
    1242          10 :     shell = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SHELL, NULL);
    1243          10 :     fail_unless(shell != NULL, "Did not find user shell before removal");
    1244             : 
    1245          10 :     rmattrs[0] = discard_const(SYSDB_SHELL);
    1246          10 :     rmattrs[1] = NULL;
    1247             : 
    1248          10 :     ret = sysdb_remove_attrs(test_ctx->domain, username, SYSDB_MEMBER_USER,
    1249             :                              rmattrs);
    1250          10 :     fail_if(ret != EOK, "Removing attributes failed: %d", ret);
    1251             : 
    1252          10 :     ret = sysdb_getpwnam(test_ctx,
    1253          10 :                          test_ctx->domain,
    1254             :                          username, &res);
    1255          10 :     fail_if(ret != EOK, "sysdb_getpwnam failed for username %s (%d: %s)",
    1256             :                          username, ret, strerror(ret));
    1257          10 :     shell = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SHELL, NULL);
    1258          10 :     fail_unless(shell == NULL, "Found user shell after removal");
    1259             : }
    1260          10 : END_TEST
    1261             : 
    1262          20 : START_TEST (test_sysdb_get_user_attr)
    1263             : {
    1264             :     struct sysdb_test_ctx *test_ctx;
    1265          20 :     const char *attrs[] = { SYSDB_SHELL, NULL };
    1266             :     struct ldb_result *res;
    1267             :     const char *attrval;
    1268             :     char *username;
    1269             :     int ret;
    1270             : 
    1271             :     /* Setup */
    1272          20 :     ret = setup_sysdb_tests(&test_ctx);
    1273          20 :     if (ret != EOK) {
    1274           0 :         fail("Could not set up the test");
    1275             :         return;
    1276             :     }
    1277             : 
    1278          20 :     username = talloc_asprintf(test_ctx, "testuser%d", _i);
    1279             : 
    1280          20 :     ret = sysdb_get_user_attr(test_ctx, test_ctx->domain, username, attrs,
    1281             :                               &res);
    1282          20 :     if (ret) {
    1283           0 :         fail("Could not get attributes for user %s", username);
    1284             :         goto done;
    1285             :     }
    1286             : 
    1287          20 :     fail_if(res->count != 1,
    1288             :             "Invalid number of entries, expected 1, got %d", res->count);
    1289             : 
    1290          20 :     attrval = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SHELL, 0);
    1291          20 :     fail_if(strcmp(attrval, "/bin/ksh"),
    1292             :             "Got bad attribute value for user %s", username);
    1293             : done:
    1294          20 :     talloc_free(test_ctx);
    1295             : }
    1296          20 : END_TEST
    1297             : 
    1298           1 : START_TEST (test_sysdb_get_user_attr_subdomain)
    1299             : {
    1300             :     struct sysdb_test_ctx *test_ctx;
    1301           1 :     struct sss_domain_info *subdomain = NULL;
    1302           1 :     const char *attrs[] = { SYSDB_SHELL, NULL };
    1303             :     struct ldb_result *res;
    1304             :     const char *attrval;
    1305           1 :     const char *username = "test_sysdb_get_user_attr_subdomain";
    1306             :     const char *fq_name;
    1307             :     int ret;
    1308             : 
    1309             :     /* Setup */
    1310           1 :     ret = setup_sysdb_tests(&test_ctx);
    1311           1 :     fail_if(ret != EOK, "Could not set up the test");
    1312             : 
    1313             :     /* Create subdomain */
    1314           1 :     subdomain = new_subdomain(test_ctx, test_ctx->domain,
    1315             :                               "test.sub", "TEST.SUB", "test", "S-3",
    1316             :                               false, false, NULL, 0);
    1317           1 :     fail_if(subdomain == NULL, "Failed to create new subdomain.");
    1318             : 
    1319           1 :     ret = sss_names_init_from_args(test_ctx,
    1320             :                                    "(((?P<domain>[^\\\\]+)\\\\(?P<name>.+$))|" \
    1321             :                                    "((?P<name>[^@]+)@(?P<domain>.+$))|" \
    1322             :                                    "(^(?P<name>[^@\\\\]+)$))",
    1323             :                                    "%1$s@%2$s", &subdomain->names);
    1324           1 :     fail_if(ret != EOK, "Failed to init names.");
    1325             : 
    1326             :     /* Create user */
    1327           1 :     fq_name = sss_tc_fqname(test_ctx, subdomain->names, subdomain, username);
    1328           1 :     fail_if(fq_name == NULL, "Failed to create fq name.");
    1329             : 
    1330           1 :     ret = sysdb_store_user(subdomain, fq_name, NULL, 12345, 0, "Gecos",
    1331             :                            "/home/userhome", "/bin/bash", NULL, NULL, NULL,
    1332             :                            -1, 0);
    1333           1 :     fail_if(ret != EOK, "sysdb_store_user failed.");
    1334             : 
    1335             :     /* Test */
    1336           1 :     ret = sysdb_get_user_attr(test_ctx, subdomain, username,
    1337             :                               attrs, &res);
    1338           1 :     fail_if(ret != EOK, "Could not get user attributes.");
    1339           1 :     fail_if(res->count != 1, "Invalid number of entries, expected 1, got %d",
    1340             :             res->count);
    1341             : 
    1342           1 :     attrval = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SHELL, 0);
    1343           1 :     fail_if(strcmp(attrval, "/bin/bash") != 0, "Got bad attribute value.");
    1344             : 
    1345           1 :     talloc_free(test_ctx);
    1346             : }
    1347           1 : END_TEST
    1348             : 
    1349          10 : START_TEST (test_sysdb_add_group_member)
    1350             : {
    1351             :     struct sysdb_test_ctx *test_ctx;
    1352             :     struct test_data *data;
    1353             :     int ret;
    1354             : 
    1355             :     /* Setup */
    1356          10 :     ret = setup_sysdb_tests(&test_ctx);
    1357          10 :     if (ret != EOK) {
    1358           0 :         fail("Could not set up the test");
    1359             :         return;
    1360             :     }
    1361             : 
    1362          10 :     data = talloc_zero(test_ctx, struct test_data);
    1363          10 :     data->ctx = test_ctx;
    1364          10 :     data->ev = test_ctx->ev;
    1365          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
    1366          10 :     data->uid = _i - 1000; /* the UID of user to add */
    1367             : 
    1368          10 :     ret = test_add_group_member(data);
    1369             : 
    1370          10 :     fail_if(ret != EOK, "Could not modify group %s", data->groupname);
    1371          10 :     talloc_free(test_ctx);
    1372             : }
    1373          10 : END_TEST
    1374             : 
    1375          10 : START_TEST (test_sysdb_initgroups)
    1376             : {
    1377             :     struct sysdb_test_ctx *test_ctx;
    1378             :     int ret;
    1379             :     const char *username;
    1380             :     struct ldb_result *res;
    1381             :     struct ldb_message *user;
    1382             :     struct ldb_message *group;
    1383             :     gid_t gid;
    1384             :     uid_t uid;
    1385             : 
    1386             :     /* Setup */
    1387          10 :     ret = setup_sysdb_tests(&test_ctx);
    1388          10 :     if (ret != EOK) {
    1389           0 :         fail("Could not set up the test");
    1390             :         return;
    1391             :     }
    1392             : 
    1393          10 :     username = talloc_asprintf(test_ctx, "testuser%d", _i);
    1394             : 
    1395          10 :     ret = sysdb_initgroups(test_ctx,
    1396          10 :                            test_ctx->domain,
    1397             :                            username, &res);
    1398          10 :     fail_if(ret != EOK, "sysdb_initgroups failed\n");
    1399             : 
    1400             :     /* result should contain 2 messages - user and his group */
    1401          10 :     fail_if(res->count != 2, "expected 2 groups, got %d\n", res->count);
    1402             : 
    1403             :     /* check if it's the expected user and expected group */
    1404          10 :     user = res->msgs[0];
    1405          10 :     group = res->msgs[1];
    1406             : 
    1407          10 :     uid = ldb_msg_find_attr_as_uint(user, SYSDB_UIDNUM, 0);
    1408          10 :     fail_unless(uid == _i,
    1409             :                 "Did not find the expected UID (found %d expected %d)",
    1410             :                 uid, _i);
    1411             : 
    1412          10 :     fail_unless(strcmp(ldb_msg_find_attr_as_string(user, SYSDB_NAME, NULL),
    1413             :                        username) == 0,
    1414             :                 "Wrong username\n");
    1415             : 
    1416             : 
    1417          10 :     gid = ldb_msg_find_attr_as_uint(group, SYSDB_GIDNUM, 0);
    1418          10 :     fail_unless(gid == _i + 1000,
    1419             :                 "Did not find the expected GID (found %d expected %d)",
    1420             :                 gid, _i);
    1421             : }
    1422          10 : END_TEST
    1423             : 
    1424          10 : START_TEST (test_sysdb_remove_group_member)
    1425             : {
    1426             :     struct sysdb_test_ctx *test_ctx;
    1427             :     struct test_data *data;
    1428             :     int ret;
    1429             : 
    1430             :     /* Setup */
    1431          10 :     ret = setup_sysdb_tests(&test_ctx);
    1432          10 :     if (ret != EOK) {
    1433           0 :         fail("Could not set up the test");
    1434             :         return;
    1435             :     }
    1436             : 
    1437          10 :     data = talloc_zero(test_ctx, struct test_data);
    1438          10 :     data->ctx = test_ctx;
    1439          10 :     data->ev = test_ctx->ev;
    1440          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
    1441          10 :     data->uid = _i - 1000; /* the UID of user to add */
    1442             : 
    1443          10 :     ret = test_remove_group_member(data);
    1444          10 :     fail_if(ret != EOK, "Remove group member failed: %d", ret);
    1445             : 
    1446          10 :     talloc_free(test_ctx);
    1447             : }
    1448          10 : END_TEST
    1449             : 
    1450           1 : START_TEST (test_sysdb_remove_nonexistent_user)
    1451             : {
    1452             :     struct sysdb_test_ctx *test_ctx;
    1453             :     struct test_data *data;
    1454             :     int ret;
    1455             : 
    1456             :     /* Setup */
    1457           1 :     ret = setup_sysdb_tests(&test_ctx);
    1458           1 :     if (ret != EOK) {
    1459           0 :         fail("Could not set up the test");
    1460             :         return;
    1461             :     }
    1462             : 
    1463           1 :     data = talloc_zero(test_ctx, struct test_data);
    1464           1 :     data->ctx = test_ctx;
    1465           1 :     data->ev = test_ctx->ev;
    1466           1 :     data->uid = 12345;
    1467             : 
    1468           1 :     ret = test_remove_nonexistent_user(data);
    1469             : 
    1470           1 :     fail_if(ret != ENOENT, "Unexpected return code %d, expected ENOENT", ret);
    1471           1 :     talloc_free(test_ctx);
    1472             : }
    1473           1 : END_TEST
    1474             : 
    1475           1 : START_TEST (test_sysdb_remove_nonexistent_group)
    1476             : {
    1477             :     struct sysdb_test_ctx *test_ctx;
    1478             :     struct test_data *data;
    1479             :     int ret;
    1480             : 
    1481             :     /* Setup */
    1482           1 :     ret = setup_sysdb_tests(&test_ctx);
    1483           1 :     if (ret != EOK) {
    1484           0 :         fail("Could not set up the test");
    1485             :         return;
    1486             :     }
    1487             : 
    1488           1 :     data = talloc_zero(test_ctx, struct test_data);
    1489           1 :     data->ctx = test_ctx;
    1490           1 :     data->ev = test_ctx->ev;
    1491           1 :     data->uid = 12345;
    1492             : 
    1493           1 :     ret = test_remove_nonexistent_group(data);
    1494             : 
    1495           1 :     fail_if(ret != ENOENT, "Unexpected return code %d, expected ENOENT", ret);
    1496           1 :     talloc_free(test_ctx);
    1497             : }
    1498           1 : END_TEST
    1499             : 
    1500           1 : START_TEST (test_sysdb_get_new_id)
    1501             : {
    1502             :     struct sysdb_test_ctx *test_ctx;
    1503             :     int ret;
    1504             :     uint32_t id;
    1505             : 
    1506             :     /* Setup */
    1507           1 :     ret = setup_sysdb_tests(&test_ctx);
    1508           1 :     fail_if(ret != EOK, "Cannot setup sysdb tests\n");
    1509             : 
    1510           1 :     ret = sysdb_get_new_id(test_ctx->domain, &id);
    1511           1 :     fail_if(ret != EOK, "Cannot get new ID\n");
    1512           1 :     fail_if(id != test_ctx->domain->id_min);
    1513             : }
    1514           1 : END_TEST
    1515             : 
    1516          10 : START_TEST (test_sysdb_store_custom)
    1517             : {
    1518             :     struct sysdb_test_ctx *test_ctx;
    1519             :     struct test_data *data;
    1520             :     int ret;
    1521             : 
    1522             :     /* Setup */
    1523          10 :     ret = setup_sysdb_tests(&test_ctx);
    1524          10 :     if (ret != EOK) {
    1525           0 :         fail("Could not set up the test");
    1526             :         return;
    1527             :     }
    1528             : 
    1529          10 :     data = talloc_zero(test_ctx, struct test_data);
    1530          10 :     data->ctx = test_ctx;
    1531          10 :     data->ev = test_ctx->ev;
    1532          10 :     data->uid = _i;
    1533          10 :     data->attrs = sysdb_new_attrs(test_ctx);
    1534          10 :     if (ret != EOK) {
    1535           0 :         fail("Could not create attribute list");
    1536             :         return;
    1537             :     }
    1538             : 
    1539          10 :     ret = sysdb_attrs_add_string(data->attrs,
    1540             :                                  TEST_ATTR_NAME,
    1541             :                                  TEST_ATTR_VALUE);
    1542          10 :     if (ret != EOK) {
    1543           0 :         fail("Could not add attribute");
    1544             :         return;
    1545             :     }
    1546             : 
    1547          10 :     ret = test_store_custom(data);
    1548             : 
    1549          10 :     fail_if(ret != EOK, "Could not add custom object");
    1550          10 :     talloc_free(test_ctx);
    1551             : }
    1552          10 : END_TEST
    1553             : 
    1554           1 : START_TEST (test_sysdb_search_custom_by_name)
    1555             : {
    1556             :     struct sysdb_test_ctx *test_ctx;
    1557             :     struct test_data *data;
    1558             :     int ret;
    1559             :     char *object_name;
    1560             : 
    1561             :     /* Setup */
    1562           1 :     ret = setup_sysdb_tests(&test_ctx);
    1563           1 :     if (ret != EOK) {
    1564           0 :         fail("Could not set up the test");
    1565             :         return;
    1566             :     }
    1567             : 
    1568           1 :     data = talloc_zero(test_ctx, struct test_data);
    1569           1 :     fail_unless(data != NULL, "talloc_zero failed");
    1570           1 :     data->ctx = test_ctx;
    1571           1 :     data->ev = test_ctx->ev;
    1572           1 :     data->attrlist = talloc_array(test_ctx, const char *, 2);
    1573           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed");
    1574           1 :     data->attrlist[0] = TEST_ATTR_NAME;
    1575           1 :     data->attrlist[1] = NULL;
    1576             : 
    1577           1 :     object_name = talloc_asprintf(data, "%s_%d", CUSTOM_TEST_OBJECT, 29010);
    1578           1 :     fail_unless(object_name != NULL, "talloc_asprintf failed");
    1579             : 
    1580           2 :     ret = sysdb_search_custom_by_name(data,
    1581           1 :                                       data->ctx->domain,
    1582             :                                       object_name,
    1583             :                                       CUSTOM_TEST_CONTAINER,
    1584             :                                       data->attrlist,
    1585             :                                       &data->msgs_count,
    1586             :                                       &data->msgs);
    1587             : 
    1588           1 :     fail_if(ret != EOK, "Could not search custom object");
    1589             : 
    1590           1 :     fail_unless(data->msgs_count == 1,
    1591             :                 "Wrong number of objects, exptected [1] got [%d]",
    1592             :                 data->msgs_count);
    1593           1 :     fail_unless(data->msgs[0]->num_elements == 1,
    1594             :                 "Wrong number of results, expected [1] got [%d]",
    1595             :                 data->msgs[0]->num_elements);
    1596           1 :     fail_unless(strcmp(data->msgs[0]->elements[0].name, TEST_ATTR_NAME) == 0,
    1597             :                 "Wrong attribute name");
    1598           1 :     fail_unless(data->msgs[0]->elements[0].num_values == 1,
    1599             :                 "Wrong number of attribute values");
    1600           1 :     fail_unless(strncmp((const char *)data->msgs[0]->elements[0].values[0].data,
    1601             :                         TEST_ATTR_VALUE,
    1602             :                         data->msgs[0]->elements[0].values[0].length) == 0,
    1603             :                 "Wrong attribute value");
    1604             : 
    1605           1 :     talloc_free(test_ctx);
    1606             : }
    1607           1 : END_TEST
    1608             : 
    1609           1 : START_TEST (test_sysdb_update_custom)
    1610             : {
    1611             :     struct sysdb_test_ctx *test_ctx;
    1612             :     struct test_data *data;
    1613             :     int ret;
    1614             : 
    1615             :     /* Setup */
    1616           1 :     ret = setup_sysdb_tests(&test_ctx);
    1617           1 :     if (ret != EOK) {
    1618           0 :         fail("Could not set up the test");
    1619             :         return;
    1620             :     }
    1621             : 
    1622           1 :     data = talloc_zero(test_ctx, struct test_data);
    1623           1 :     data->ctx = test_ctx;
    1624           1 :     data->ev = test_ctx->ev;
    1625           1 :     data->uid = 29010;
    1626           1 :     data->attrs = sysdb_new_attrs(test_ctx);
    1627           1 :     if (ret != EOK) {
    1628           0 :         fail("Could not create attribute list");
    1629             :         return;
    1630             :     }
    1631             : 
    1632           1 :     ret = sysdb_attrs_add_string(data->attrs,
    1633             :                                  TEST_ATTR_NAME,
    1634             :                                  TEST_ATTR_UPDATE_VALUE);
    1635           1 :     if (ret != EOK) {
    1636           0 :         fail("Could not add attribute");
    1637             :         return;
    1638             :     }
    1639             : 
    1640           1 :     ret = sysdb_attrs_add_string(data->attrs,
    1641             :                                  TEST_ATTR_ADD_NAME,
    1642             :                                  TEST_ATTR_ADD_VALUE);
    1643           1 :     if (ret != EOK) {
    1644           0 :         fail("Could not add attribute");
    1645             :         return;
    1646             :     }
    1647             : 
    1648           1 :     ret = test_store_custom(data);
    1649             : 
    1650           1 :     fail_if(ret != EOK, "Could not add custom object");
    1651           1 :     talloc_free(test_ctx);
    1652             : }
    1653           1 : END_TEST
    1654             : 
    1655           1 : START_TEST (test_sysdb_search_custom_update)
    1656             : {
    1657             :     struct sysdb_test_ctx *test_ctx;
    1658             :     struct test_data *data;
    1659             :     int ret;
    1660             :     char *object_name;
    1661             :     struct ldb_message_element *el;
    1662             : 
    1663             :     /* Setup */
    1664           1 :     ret = setup_sysdb_tests(&test_ctx);
    1665           1 :     if (ret != EOK) {
    1666           0 :         fail("Could not set up the test");
    1667             :         return;
    1668             :     }
    1669             : 
    1670           1 :     data = talloc_zero(test_ctx, struct test_data);
    1671           1 :     fail_unless(data != NULL, "talloc_zero failed");
    1672           1 :     data->ctx = test_ctx;
    1673           1 :     data->ev = test_ctx->ev;
    1674           1 :     data->attrlist = talloc_array(test_ctx, const char *, 3);
    1675           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed");
    1676           1 :     data->attrlist[0] = TEST_ATTR_NAME;
    1677           1 :     data->attrlist[1] = TEST_ATTR_ADD_NAME;
    1678           1 :     data->attrlist[2] = NULL;
    1679             : 
    1680           1 :     object_name = talloc_asprintf(data, "%s_%d", CUSTOM_TEST_OBJECT, 29010);
    1681           1 :     fail_unless(object_name != NULL, "talloc_asprintf failed");
    1682             : 
    1683           2 :     ret = sysdb_search_custom_by_name(data,
    1684           1 :                                       data->ctx->domain,
    1685             :                                       object_name,
    1686             :                                       CUSTOM_TEST_CONTAINER,
    1687             :                                       data->attrlist,
    1688             :                                       &data->msgs_count,
    1689             :                                       &data->msgs);
    1690             : 
    1691           1 :     fail_if(ret != EOK, "Could not search custom object");
    1692             : 
    1693           1 :     fail_unless(data->msgs_count == 1,
    1694             :                 "Wrong number of objects, exptected [1] got [%d]",
    1695             :                 data->msgs_count);
    1696           1 :     fail_unless(data->msgs[0]->num_elements == 2,
    1697             :                 "Wrong number of results, expected [2] got [%d]",
    1698             :                 data->msgs[0]->num_elements);
    1699             : 
    1700           1 :     el = ldb_msg_find_element(data->msgs[0], TEST_ATTR_NAME);
    1701           1 :     fail_unless(el != NULL, "Attribute [%s] not found", TEST_ATTR_NAME);
    1702           1 :     fail_unless(el->num_values == 1, "Wrong number ([%d] instead of 1) "
    1703             :                 "of attribute values for [%s]", el->num_values,
    1704             :                 TEST_ATTR_NAME);
    1705           1 :     fail_unless(strncmp((const char *) el->values[0].data,
    1706             :                 TEST_ATTR_UPDATE_VALUE,
    1707             :                 el->values[0].length) == 0,
    1708             :                 "Wrong attribute value");
    1709             : 
    1710           1 :     el = ldb_msg_find_element(data->msgs[0], TEST_ATTR_ADD_NAME);
    1711           1 :     fail_unless(el != NULL, "Attribute [%s] not found", TEST_ATTR_ADD_NAME);
    1712           1 :     fail_unless(el->num_values == 1, "Wrong number ([%d] instead of 1) "
    1713             :                 "of attribute values for [%s]", el->num_values,
    1714             :                 TEST_ATTR_ADD_NAME);
    1715           1 :     fail_unless(strncmp((const char *) el->values[0].data,
    1716             :                 TEST_ATTR_ADD_VALUE,
    1717             :                 el->values[0].length) == 0,
    1718             :                 "Wrong attribute value");
    1719             : 
    1720             : 
    1721           1 :     talloc_free(test_ctx);
    1722             : }
    1723           1 : END_TEST
    1724             : 
    1725           1 : START_TEST (test_sysdb_search_custom)
    1726             : {
    1727             :     struct sysdb_test_ctx *test_ctx;
    1728             :     struct test_data *data;
    1729             :     int ret;
    1730           1 :     const char *filter = "(distinguishedName=*)";
    1731             : 
    1732             :     /* Setup */
    1733           1 :     ret = setup_sysdb_tests(&test_ctx);
    1734           1 :     if (ret != EOK) {
    1735           0 :         fail("Could not set up the test");
    1736             :         return;
    1737             :     }
    1738             : 
    1739           1 :     data = talloc_zero(test_ctx, struct test_data);
    1740           1 :     fail_unless(data != NULL, "talloc_zero failed");
    1741           1 :     data->ctx = test_ctx;
    1742           1 :     data->ev = test_ctx->ev;
    1743           1 :     data->attrlist = talloc_array(test_ctx, const char *, 3);
    1744           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed");
    1745           1 :     data->attrlist[0] = TEST_ATTR_NAME;
    1746           1 :     data->attrlist[1] = TEST_ATTR_ADD_NAME;
    1747           1 :     data->attrlist[2] = NULL;
    1748             : 
    1749           1 :     ret = sysdb_search_custom(data, data->ctx->domain, filter,
    1750             :                               CUSTOM_TEST_CONTAINER,
    1751             :                               data->attrlist,
    1752             :                               &data->msgs_count,
    1753             :                               &data->msgs);
    1754             : 
    1755           1 :     fail_if(ret != EOK, "Could not search custom object");
    1756             : 
    1757           1 :     fail_unless(data->msgs_count == 10,
    1758             :                 "Wrong number of objects, exptected [10] got [%d]",
    1759             :                 data->msgs_count);
    1760             : 
    1761           1 :     talloc_free(test_ctx);
    1762             : }
    1763           1 : END_TEST
    1764             : 
    1765           1 : START_TEST (test_sysdb_delete_custom)
    1766             : {
    1767             :     struct sysdb_test_ctx *test_ctx;
    1768             :     struct test_data *data;
    1769             :     int ret;
    1770             : 
    1771             :     /* Setup */
    1772           1 :     ret = setup_sysdb_tests(&test_ctx);
    1773           1 :     if (ret != EOK) {
    1774           0 :         fail("Could not set up the test");
    1775             :         return;
    1776             :     }
    1777             : 
    1778           1 :     data = talloc_zero(test_ctx, struct test_data);
    1779           1 :     data->ctx = test_ctx;
    1780           1 :     data->ev = test_ctx->ev;
    1781             : 
    1782           1 :     ret = test_delete_custom(data);
    1783             : 
    1784           1 :     fail_if(ret != EOK, "Could not delete custom object");
    1785           1 :     talloc_free(test_ctx);
    1786             : }
    1787           1 : END_TEST
    1788             : 
    1789           1 : START_TEST (test_sysdb_cache_password)
    1790             : {
    1791             :     struct sysdb_test_ctx *test_ctx;
    1792             :     struct test_data *data;
    1793             :     int ret;
    1794             : 
    1795             :     /* Setup */
    1796           1 :     ret = setup_sysdb_tests(&test_ctx);
    1797           1 :     fail_unless(ret == EOK, "Could not set up the test");
    1798             : 
    1799           1 :     data = talloc_zero(test_ctx, struct test_data);
    1800           1 :     data->ctx = test_ctx;
    1801           1 :     data->ev = test_ctx->ev;
    1802           1 :     data->username = talloc_asprintf(data, "testuser%d", _i);
    1803             : 
    1804           1 :     ret = sysdb_cache_password(test_ctx->domain, data->username,
    1805             :                                data->username);
    1806             : 
    1807           1 :     fail_unless(ret == EOK, "sysdb_cache_password request failed [%d].", ret);
    1808             : 
    1809           1 :     talloc_free(test_ctx);
    1810             : }
    1811           1 : END_TEST
    1812             : 
    1813           1 : START_TEST (test_sysdb_cache_password_ex)
    1814             : {
    1815             :     struct sysdb_test_ctx *test_ctx;
    1816             :     struct test_data *data;
    1817             :     int ret;
    1818             :     struct ldb_result *res;
    1819           1 :     const char *attrs[] = { SYSDB_CACHEDPWD_TYPE, SYSDB_CACHEDPWD_FA2_LEN,
    1820             :                             NULL };
    1821             :     int val;
    1822             : 
    1823             :     /* Setup */
    1824           1 :     ret = setup_sysdb_tests(&test_ctx);
    1825           1 :     fail_unless(ret == EOK, "Could not set up the test");
    1826             : 
    1827           1 :     data = talloc_zero(test_ctx, struct test_data);
    1828           1 :     data->ctx = test_ctx;
    1829           1 :     data->ev = test_ctx->ev;
    1830           1 :     data->username = talloc_asprintf(data, "testuser%d", _i);
    1831             : 
    1832           1 :     ret = sysdb_get_user_attr(test_ctx, test_ctx->domain, data->username,
    1833             :                               attrs, &res);
    1834           1 :     fail_unless(ret == EOK, "sysdb_get_user_attr request failed [%d].", ret);
    1835             : 
    1836           1 :     val = ldb_msg_find_attr_as_int(res->msgs[0], SYSDB_CACHEDPWD_TYPE, 0);
    1837           1 :     fail_unless(val == SSS_AUTHTOK_TYPE_PASSWORD,
    1838             :                 "Unexpected authtok type, found [%d], expected [%d].",
    1839             :                 val, SSS_AUTHTOK_TYPE_PASSWORD);
    1840             : 
    1841           1 :     ret = sysdb_cache_password_ex(test_ctx->domain, data->username,
    1842             :                                   data->username, SSS_AUTHTOK_TYPE_2FA, 12);
    1843             : 
    1844           1 :     fail_unless(ret == EOK, "sysdb_cache_password request failed [%d].", ret);
    1845             : 
    1846           1 :     ret = sysdb_get_user_attr(test_ctx, test_ctx->domain, data->username,
    1847             :                               attrs, &res);
    1848           1 :     fail_unless(ret == EOK, "sysdb_get_user_attr request failed [%d].", ret);
    1849             : 
    1850           1 :     val = ldb_msg_find_attr_as_int(res->msgs[0], SYSDB_CACHEDPWD_TYPE, 0);
    1851           1 :     fail_unless(val == SSS_AUTHTOK_TYPE_2FA,
    1852             :                 "Unexpected authtok type, found [%d], expected [%d].",
    1853             :                 val, SSS_AUTHTOK_TYPE_2FA);
    1854             : 
    1855           1 :     val = ldb_msg_find_attr_as_int(res->msgs[0], SYSDB_CACHEDPWD_FA2_LEN, 0);
    1856           1 :     fail_unless(val == 12,
    1857             :                 "Unexpected second factor length, found [%d], expected [%d].",
    1858             :                 val, 12);
    1859             : 
    1860           1 :     talloc_free(test_ctx);
    1861             : }
    1862           1 : END_TEST
    1863             : 
    1864           3 : static void cached_authentication_without_expiration(const char *username,
    1865             :                                                      const char *password,
    1866             :                                                      int expected_result)
    1867             : {
    1868             :     struct sysdb_test_ctx *test_ctx;
    1869             :     struct test_data *data;
    1870             :     int ret;
    1871           3 :     time_t expire_date = -1;
    1872           3 :     time_t delayed_until = -1;
    1873             :     const char *val[2];
    1874           3 :     val[1] = NULL;
    1875             : 
    1876             :     /* Setup */
    1877           3 :     ret = setup_sysdb_tests(&test_ctx);
    1878           3 :     fail_unless(ret == EOK, "Could not set up the test");
    1879             : 
    1880           3 :     data = talloc_zero(test_ctx, struct test_data);
    1881           3 :     data->ctx = test_ctx;
    1882           3 :     data->ev = test_ctx->ev;
    1883           3 :     data->username = username;
    1884             : 
    1885           3 :     val[0] = "0";
    1886           3 :     ret = confdb_add_param(test_ctx->confdb, true, CONFDB_PAM_CONF_ENTRY,
    1887             :                            CONFDB_PAM_CRED_TIMEOUT, val);
    1888           3 :     if (ret != EOK) {
    1889           0 :         fail("Could not initialize provider");
    1890             :         talloc_free(test_ctx);
    1891             :         return;
    1892             :     }
    1893             : 
    1894           3 :     ret = sysdb_cache_auth(test_ctx->domain, data->username,
    1895           3 :                            password, test_ctx->confdb, false,
    1896             :                            &expire_date, &delayed_until);
    1897             : 
    1898           3 :     fail_unless(ret == expected_result, "sysdb_cache_auth request does not "
    1899             :                                         "return expected result [%d].",
    1900             :                                         expected_result);
    1901             : 
    1902           3 :     fail_unless(expire_date == 0, "Wrong expire date, expected [%d], got [%d]",
    1903             :                                   0, expire_date);
    1904             : 
    1905           3 :     fail_unless(delayed_until == -1, "Wrong delay, expected [%d], got [%d]",
    1906             :                                   -1, delayed_until);
    1907             : 
    1908           3 :     talloc_free(test_ctx);
    1909             : }
    1910             : 
    1911           3 : static void cached_authentication_with_expiration(const char *username,
    1912             :                                                   const char *password,
    1913             :                                                   int expected_result)
    1914             : {
    1915             :     struct sysdb_test_ctx *test_ctx;
    1916             :     struct test_data *data;
    1917             :     int ret;
    1918           3 :     time_t expire_date = -1;
    1919             :     const char *val[2];
    1920           3 :     val[1] = NULL;
    1921             :     time_t now;
    1922             :     time_t expected_expire_date;
    1923           3 :     time_t delayed_until = -1;
    1924             : 
    1925             :     /* Setup */
    1926           3 :     ret = setup_sysdb_tests(&test_ctx);
    1927           3 :     fail_unless(ret == EOK, "Could not set up the test");
    1928             : 
    1929           3 :     data = talloc_zero(test_ctx, struct test_data);
    1930           3 :     data->ctx = test_ctx;
    1931           3 :     data->ev = test_ctx->ev;
    1932           3 :     data->username = username;
    1933             : 
    1934           3 :     val[0] = "1";
    1935           3 :     ret = confdb_add_param(test_ctx->confdb, true, CONFDB_PAM_CONF_ENTRY,
    1936             :                            CONFDB_PAM_CRED_TIMEOUT, val);
    1937           3 :     if (ret != EOK) {
    1938           0 :         fail("Could not initialize provider");
    1939             :         talloc_free(test_ctx);
    1940             :         return;
    1941             :     }
    1942             : 
    1943           3 :     now = time(NULL);
    1944           3 :     expected_expire_date = now + (24 * 60 * 60);
    1945           3 :     DEBUG(SSSDBG_TRACE_ALL,
    1946             :           "Setting SYSDB_LAST_ONLINE_AUTH to [%lld].\n", (long long) now);
    1947             : 
    1948           3 :     data->attrs = sysdb_new_attrs(data);
    1949           3 :     ret = sysdb_attrs_add_time_t(data->attrs, SYSDB_LAST_ONLINE_AUTH, now);
    1950           3 :     fail_unless(ret == EOK, "Could not mdd attribute "SYSDB_LAST_ONLINE_AUTH
    1951             :                             ": %s", data->username);
    1952             : 
    1953           3 :     ret = sysdb_set_user_attr(data->ctx->domain, data->username, data->attrs,
    1954             :                               SYSDB_MOD_REP);
    1955           3 :     fail_unless(ret == EOK, "Could not modify user %s", data->username);
    1956             : 
    1957           3 :     ret = sysdb_cache_auth(data->ctx->domain, data->username,
    1958           3 :                            password, test_ctx->confdb, false,
    1959             :                            &expire_date, &delayed_until);
    1960             : 
    1961           3 :     fail_unless(ret == expected_result,
    1962             :                 "sysdb_cache_auth request does not return expected "
    1963             :                 "result [%d], got [%d].", expected_result, ret);
    1964             : 
    1965           3 :     fail_unless(expire_date == expected_expire_date,
    1966             :                 "Wrong expire date, expected [%d], got [%d]",
    1967             :                 expected_expire_date, expire_date);
    1968             : 
    1969           3 :     fail_unless(delayed_until == -1, "Wrong delay, expected [%d], got [%d]",
    1970             :                                   -1, delayed_until);
    1971             : 
    1972           3 :     talloc_free(test_ctx);
    1973             : }
    1974             : 
    1975           1 : START_TEST (test_sysdb_cached_authentication_missing_password)
    1976             : {
    1977             :     TALLOC_CTX *tmp_ctx;
    1978             :     char *username;
    1979             : 
    1980           1 :     tmp_ctx = talloc_new(NULL);
    1981           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed.");
    1982             : 
    1983           1 :     username = talloc_asprintf(tmp_ctx, "testuser%d", _i);
    1984           1 :     fail_unless(username != NULL, "talloc_asprintf failed.");
    1985             : 
    1986           1 :     cached_authentication_without_expiration(username, "abc",
    1987             :                                              ERR_NO_CACHED_CREDS);
    1988           1 :     cached_authentication_with_expiration(username, "abc",
    1989             :                                              ERR_NO_CACHED_CREDS);
    1990             : 
    1991           1 :     talloc_free(tmp_ctx);
    1992             : 
    1993             : }
    1994           1 : END_TEST
    1995             : 
    1996           1 : START_TEST (test_sysdb_cached_authentication_wrong_password)
    1997             : {
    1998             :     TALLOC_CTX *tmp_ctx;
    1999             :     char *username;
    2000             : 
    2001           1 :     tmp_ctx = talloc_new(NULL);
    2002           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed.");
    2003             : 
    2004           1 :     username = talloc_asprintf(tmp_ctx, "testuser%d", _i);
    2005           1 :     fail_unless(username != NULL, "talloc_asprintf failed.");
    2006             : 
    2007           1 :     cached_authentication_without_expiration(username, "abc",
    2008             :                                              ERR_AUTH_FAILED);
    2009           1 :     cached_authentication_with_expiration(username, "abc",
    2010             :                                              ERR_AUTH_FAILED);
    2011             : 
    2012           1 :     talloc_free(tmp_ctx);
    2013             : 
    2014             : }
    2015           1 : END_TEST
    2016             : 
    2017           1 : START_TEST (test_sysdb_cached_authentication)
    2018             : {
    2019             :     TALLOC_CTX *tmp_ctx;
    2020             :     char *username;
    2021             : 
    2022           1 :     tmp_ctx = talloc_new(NULL);
    2023           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed.");
    2024             : 
    2025           1 :     username = talloc_asprintf(tmp_ctx, "testuser%d", _i);
    2026           1 :     fail_unless(username != NULL, "talloc_asprintf failed.");
    2027             : 
    2028           1 :     cached_authentication_without_expiration(username, username, EOK);
    2029           1 :     cached_authentication_with_expiration(username, username, EOK);
    2030             : 
    2031           1 :     talloc_free(tmp_ctx);
    2032             : 
    2033             : }
    2034           1 : END_TEST
    2035             : 
    2036           9 : START_TEST (test_sysdb_prepare_asq_test_user)
    2037             : {
    2038             :     struct sysdb_test_ctx *test_ctx;
    2039             :     struct test_data *data;
    2040             :     int ret;
    2041             : 
    2042             :     /* Setup */
    2043           9 :     ret = setup_sysdb_tests(&test_ctx);
    2044           9 :     if (ret != EOK) {
    2045           0 :         fail("Could not set up the test");
    2046             :         return;
    2047             :     }
    2048             : 
    2049           9 :     data = talloc_zero(test_ctx, struct test_data);
    2050           9 :     data->ctx = test_ctx;
    2051           9 :     data->ev = test_ctx->ev;
    2052           9 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i);
    2053           9 :     data->uid = ASQ_TEST_USER_UID;
    2054             : 
    2055           9 :     ret = test_add_group_member(data);
    2056             : 
    2057           9 :     fail_if(ret != EOK, "Could not modify group %s", data->groupname);
    2058           9 :     talloc_free(test_ctx);
    2059             : }
    2060           9 : END_TEST
    2061             : 
    2062           1 : START_TEST (test_sysdb_asq_search)
    2063             : {
    2064             :     struct sysdb_test_ctx *test_ctx;
    2065             :     struct test_data *data;
    2066             :     struct ldb_dn *user_dn;
    2067             :     int ret;
    2068             :     size_t msgs_count;
    2069             :     struct ldb_message **msgs;
    2070             :     int i;
    2071             :     char *gid_str;
    2072             : 
    2073             :     /* Setup */
    2074           1 :     ret = setup_sysdb_tests(&test_ctx);
    2075           1 :     if (ret != EOK) {
    2076           0 :         fail("Could not set up the test");
    2077             :         return;
    2078             :     }
    2079             : 
    2080           1 :     data = talloc_zero(test_ctx, struct test_data);
    2081           1 :     data->ctx = test_ctx;
    2082           1 :     data->ev = test_ctx->ev;
    2083           1 :     data->attrlist = talloc_array(data, const char *, 2);
    2084           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed");
    2085             : 
    2086           1 :     data->attrlist[0] = "gidNumber";
    2087           1 :     data->attrlist[1] = NULL;
    2088             : 
    2089           1 :     user_dn = sysdb_user_dn(data, data->ctx->domain, ASQ_TEST_USER);
    2090           1 :     fail_unless(user_dn != NULL, "sysdb_user_dn failed");
    2091             : 
    2092           1 :     ret = sysdb_asq_search(data, test_ctx->domain,
    2093             :                            user_dn, NULL, "memberof",
    2094             :                            data->attrlist, &msgs_count, &msgs);
    2095             : 
    2096           1 :     fail_if(ret != EOK, "Failed to send ASQ search request.\n");
    2097             : 
    2098           1 :     fail_unless(msgs_count == 10, "wrong number of results, "
    2099             :                                   "found [%d] expected [10]", msgs_count);
    2100             : 
    2101          11 :     for (i = 0; i < msgs_count; i++) {
    2102          10 :         fail_unless(msgs[i]->num_elements == 1, "wrong number of elements, "
    2103             :                                      "found [%d] expected [1]",
    2104             :                                      msgs[i]->num_elements);
    2105             : 
    2106          10 :         fail_unless(msgs[i]->elements[0].num_values == 1,
    2107             :                     "wrong number of values, found [%d] expected [1]",
    2108             :                     msgs[i]->elements[0].num_values);
    2109             : 
    2110          10 :         gid_str = talloc_asprintf(data, "%d", 28010 + i);
    2111          10 :         fail_unless(gid_str != NULL, "talloc_asprintf failed.");
    2112          10 :         fail_unless(strncmp(gid_str,
    2113             :                             (const char *) msgs[i]->elements[0].values[0].data,
    2114             :                             msgs[i]->elements[0].values[0].length)  == 0,
    2115             :                             "wrong value, found [%.*s] expected [%s]",
    2116             :                             msgs[i]->elements[0].values[0].length,
    2117             :                             msgs[i]->elements[0].values[0].data, gid_str);
    2118             :     }
    2119             : 
    2120           1 :     talloc_free(test_ctx);
    2121             : }
    2122           1 : END_TEST
    2123             : 
    2124           1 : START_TEST (test_sysdb_search_all_users)
    2125             : {
    2126             :     struct sysdb_test_ctx *test_ctx;
    2127             :     struct test_data *data;
    2128             :     int ret;
    2129             :     int i;
    2130             :     char *uid_str;
    2131             : 
    2132             :     /* Setup */
    2133           1 :     ret = setup_sysdb_tests(&test_ctx);
    2134           1 :     if (ret != EOK) {
    2135           0 :         fail("Could not set up the test");
    2136             :         return;
    2137             :     }
    2138             : 
    2139           1 :     data = talloc_zero(test_ctx, struct test_data);
    2140           1 :     data->ctx = test_ctx;
    2141           1 :     data->ev = test_ctx->ev;
    2142           1 :     data->attrlist = talloc_array(data, const char *, 2);
    2143           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed");
    2144             : 
    2145           1 :     data->attrlist[0] = "uidNumber";
    2146           1 :     data->attrlist[1] = NULL;
    2147             : 
    2148           1 :     ret = test_search_all_users(data);
    2149             : 
    2150           1 :     fail_if(ret != EOK, "Search failed");
    2151             : 
    2152           1 :     fail_unless(data->msgs_count == 10,
    2153             :                 "wrong number of results, found [%d] expected [10]",
    2154             :                 data->msgs_count);
    2155             : 
    2156          11 :     for (i = 0; i < data->msgs_count; i++) {
    2157          10 :         fail_unless(data->msgs[i]->num_elements == 1,
    2158             :                     "wrong number of elements, found [%d] expected [1]",
    2159             :                     data->msgs[i]->num_elements);
    2160             : 
    2161          10 :         fail_unless(data->msgs[i]->elements[0].num_values == 1,
    2162             :                     "wrong number of values, found [%d] expected [1]",
    2163             :                     data->msgs[i]->elements[0].num_values);
    2164             : 
    2165          10 :         uid_str = talloc_asprintf(data, "%d", 27010 + i);
    2166          10 :         fail_unless(uid_str != NULL, "talloc_asprintf failed.");
    2167          10 :         fail_unless(strncmp(uid_str,
    2168             :                             (char *) data->msgs[i]->elements[0].values[0].data,
    2169             :                             data->msgs[i]->elements[0].values[0].length)  == 0,
    2170             :                             "wrong value, found [%.*s] expected [%s]",
    2171             :                             data->msgs[i]->elements[0].values[0].length,
    2172             :                             data->msgs[i]->elements[0].values[0].data, uid_str);
    2173             :     }
    2174             : 
    2175           1 :     talloc_free(test_ctx);
    2176             : }
    2177           1 : END_TEST
    2178             : 
    2179           1 : START_TEST (test_sysdb_delete_recursive)
    2180             : {
    2181             :     struct sysdb_test_ctx *test_ctx;
    2182             :     struct test_data *data;
    2183             :     int ret;
    2184             : 
    2185             :     /* Setup */
    2186           1 :     ret = setup_sysdb_tests(&test_ctx);
    2187           1 :     if (ret != EOK) {
    2188           0 :         fail("Could not set up the test");
    2189             :         return;
    2190             :     }
    2191             : 
    2192           1 :     data = talloc_zero(test_ctx, struct test_data);
    2193           1 :     data->ctx = test_ctx;
    2194           1 :     data->ev = test_ctx->ev;
    2195             : 
    2196           1 :     ret = test_delete_recursive(data);
    2197             : 
    2198           1 :     fail_if(ret != EOK, "Recursive delete failed");
    2199           1 :     talloc_free(test_ctx);
    2200             : }
    2201           1 : END_TEST
    2202             : 
    2203           1 : START_TEST (test_sysdb_attrs_replace_name)
    2204             : {
    2205             :     struct sysdb_attrs *attrs;
    2206             :     struct ldb_message_element *el;
    2207             :     int ret;
    2208             : 
    2209           1 :     attrs = sysdb_new_attrs(NULL);
    2210           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    2211             : 
    2212           1 :     ret = sysdb_attrs_add_string(attrs, "foo", "bar");
    2213           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
    2214             : 
    2215           1 :     ret = sysdb_attrs_add_string(attrs, "fool", "bool");
    2216           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
    2217             : 
    2218           1 :     ret = sysdb_attrs_add_string(attrs, "foot", "boot");
    2219           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
    2220             : 
    2221           1 :     ret = sysdb_attrs_replace_name(attrs, "foo", "foot");
    2222           1 :     fail_unless(ret == EEXIST,
    2223             :                 "sysdb_attrs_replace overwrites existing attribute");
    2224             : 
    2225           1 :     ret = sysdb_attrs_replace_name(attrs, "foo", "oof");
    2226           1 :     fail_unless(ret == EOK, "sysdb_attrs_replace failed");
    2227             : 
    2228           1 :     ret = sysdb_attrs_get_el(attrs, "foo", &el);
    2229           1 :     fail_unless(ret == EOK, "sysdb_attrs_get_el failed");
    2230           1 :     fail_unless(el->num_values == 0, "Attribute foo is not empty.");
    2231             : 
    2232           1 :     ret = sysdb_attrs_get_el(attrs, "oof", &el);
    2233           1 :     fail_unless(ret == EOK, "sysdb_attrs_get_el failed");
    2234           1 :     fail_unless(el->num_values == 1,
    2235             :                 "Wrong number of values for attribute oof, "
    2236             :                 "expected [1] got [%d].", el->num_values);
    2237           1 :     fail_unless(strncmp("bar", (char *) el->values[0].data,
    2238             :                         el->values[0].length) == 0,
    2239             :                 "Wrong value, expected [bar] got [%.*s]", el->values[0].length,
    2240             :                                                           el->values[0].data);
    2241             : 
    2242           1 :     talloc_free(attrs);
    2243             : }
    2244           1 : END_TEST
    2245             : 
    2246          30 : START_TEST (test_sysdb_memberof_store_group)
    2247             : {
    2248             :     struct sysdb_test_ctx *test_ctx;
    2249             :     struct test_data *data;
    2250             :     int ret;
    2251             : 
    2252             :     /* Setup */
    2253          30 :     ret = setup_sysdb_tests(&test_ctx);
    2254          30 :     if (ret != EOK) {
    2255           0 :         fail("Could not set up the test");
    2256             :         return;
    2257             :     }
    2258             : 
    2259          30 :     data = talloc_zero(test_ctx, struct test_data);
    2260          30 :     data->ctx = test_ctx;
    2261          30 :     data->ev = test_ctx->ev;
    2262          30 :     data->gid = MBO_GROUP_BASE + _i;
    2263          30 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2264             : 
    2265          30 :     if (_i == 0) {
    2266           3 :         data->attrlist = NULL;
    2267             :     } else {
    2268          27 :         data->attrlist = talloc_array(data, const char *, 2);
    2269          27 :         fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2270          27 :         data->attrlist[0] = talloc_asprintf(data, "testgroup%d", data->gid - 1);
    2271          27 :         data->attrlist[1] = NULL;
    2272             :     }
    2273             : 
    2274          30 :     ret = test_memberof_store_group(data);
    2275             : 
    2276          30 :     fail_if(ret != EOK, "Could not store POSIX group #%d", data->gid);
    2277          30 :     talloc_free(test_ctx);
    2278             : }
    2279          30 : END_TEST
    2280             : 
    2281          30 : START_TEST (test_sysdb_memberof_store_group_with_ghosts)
    2282             : {
    2283             :     struct sysdb_test_ctx *test_ctx;
    2284             :     struct test_data *data;
    2285             :     int ret;
    2286             : 
    2287             :     /* Setup */
    2288          30 :     ret = setup_sysdb_tests(&test_ctx);
    2289          30 :     if (ret != EOK) {
    2290           0 :         fail("Could not set up the test");
    2291             :         return;
    2292             :     }
    2293             : 
    2294          30 :     data = talloc_zero(test_ctx, struct test_data);
    2295          30 :     data->ctx = test_ctx;
    2296          30 :     data->ev = test_ctx->ev;
    2297          30 :     data->gid = _i;
    2298          30 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2299             : 
    2300          30 :     if (_i == 0) {
    2301           0 :         data->attrlist = NULL;
    2302             :     } else {
    2303          30 :         data->attrlist = talloc_array(data, const char *, 2);
    2304          30 :         fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2305          30 :         data->attrlist[0] = talloc_asprintf(data, "testgroup%d", data->gid - 1);
    2306          30 :         data->attrlist[1] = NULL;
    2307             :     }
    2308             : 
    2309          30 :     data->memberlist = talloc_array(data, char *, 2);
    2310          30 :     fail_unless(data->memberlist != NULL, "talloc_array failed.");
    2311          30 :     data->memberlist[0] = talloc_asprintf(data, "testuser%d", data->gid);
    2312          30 :     data->memberlist[1] = NULL;
    2313             : 
    2314          30 :     ret = test_memberof_store_group_with_ghosts(data);
    2315             : 
    2316          30 :     fail_if(ret != EOK, "Could not store POSIX group #%d", data->gid);
    2317          30 :     talloc_free(test_ctx);
    2318             : }
    2319          30 : END_TEST
    2320             : 
    2321          10 : START_TEST (test_sysdb_memberof_store_group_with_double_ghosts)
    2322             : {
    2323             :     struct sysdb_test_ctx *test_ctx;
    2324             :     struct test_data *data;
    2325             :     int ret;
    2326             : 
    2327             :     /* Setup */
    2328          10 :     ret = setup_sysdb_tests(&test_ctx);
    2329          10 :     if (ret != EOK) {
    2330           0 :         fail("Could not set up the test");
    2331             :         return;
    2332             :     }
    2333             : 
    2334          10 :     data = talloc_zero(test_ctx, struct test_data);
    2335          10 :     data->ctx = test_ctx;
    2336          10 :     data->ev = test_ctx->ev;
    2337          10 :     data->gid = _i;
    2338          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2339             : 
    2340          10 :     if (_i == 0) {
    2341           0 :         data->attrlist = NULL;
    2342             :     } else {
    2343          10 :         data->attrlist = talloc_array(data, const char *, 2);
    2344          10 :         fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2345          10 :         data->attrlist[0] = talloc_asprintf(data, "testgroup%d", data->gid - 1);
    2346          10 :         data->attrlist[1] = NULL;
    2347             :     }
    2348             : 
    2349          10 :     data->memberlist = talloc_array(data, char *, 3);
    2350          10 :     fail_unless(data->memberlist != NULL, "talloc_array failed.");
    2351          10 :     data->memberlist[0] = talloc_asprintf(data, "testusera%d", data->gid);
    2352          10 :     data->memberlist[1] = talloc_asprintf(data, "testuserb%d", data->gid);
    2353          10 :     data->memberlist[2] = NULL;
    2354             : 
    2355          10 :     ret = test_memberof_store_group_with_ghosts(data);
    2356             : 
    2357          10 :     fail_if(ret != EOK, "Could not store POSIX group #%d", data->gid);
    2358          10 :     talloc_free(test_ctx);
    2359             : }
    2360          10 : END_TEST
    2361             : 
    2362          10 : START_TEST (test_sysdb_memberof_mod_add)
    2363             : {
    2364             :     struct sysdb_test_ctx *test_ctx;
    2365             :     struct test_data *data;
    2366             :     char *ghostname;
    2367             :     int ret;
    2368             :     struct ldb_message_element *el;
    2369             :     struct ldb_val gv, *test_gv;
    2370             :     gid_t itergid;
    2371             : 
    2372             :     /* Setup */
    2373          10 :     ret = setup_sysdb_tests(&test_ctx);
    2374          10 :     if (ret != EOK) {
    2375           0 :         fail("Could not set up the test");
    2376             :         return;
    2377             :     }
    2378             : 
    2379          10 :     data = talloc_zero(test_ctx, struct test_data);
    2380          10 :     data->ctx = test_ctx;
    2381          10 :     data->ev = test_ctx->ev;
    2382          10 :     data->gid = _i;
    2383          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2384             : 
    2385          10 :     data->attrs = sysdb_new_attrs(data);
    2386          10 :     if (ret != EOK) {
    2387           0 :         fail("Could not create the changeset");
    2388             :         return;
    2389             :     }
    2390             : 
    2391          10 :     ghostname = talloc_asprintf(data, "testghost%d", _i);
    2392          10 :     fail_unless(ghostname != NULL, "Out of memory\n");
    2393          10 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname);
    2394          10 :     fail_unless(ret == EOK, "Cannot add attr\n");
    2395             : 
    2396          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2397          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2398          10 :     data->attrlist[0] = SYSDB_GHOST;
    2399          10 :     data->attrlist[1] = NULL;
    2400             : 
    2401             :     /* Before the add, the groups should not contain the ghost attribute */
    2402          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2403          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2404             :                                         data->attrlist, &data->msg);
    2405          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2406             :                 (unsigned long long) data->gid);
    2407             : 
    2408          55 :         gv.data = (uint8_t *) ghostname;
    2409          55 :         gv.length = strlen(ghostname);
    2410             : 
    2411          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2412          55 :         if (data->gid > MBO_GROUP_BASE) {
    2413             :             /* The first group would have the ghost attribute gone completely */
    2414          45 :             fail_if(el == NULL, "Cannot find ghost element\n");
    2415          45 :             test_gv = ldb_msg_find_val(el, &gv);
    2416          45 :             fail_unless(test_gv == NULL,
    2417             :                         "Ghost user %s unexpectedly found\n", ghostname);
    2418             :         } else {
    2419          10 :             fail_unless(el == NULL, "Stray values in ghost element?\n");
    2420             :         }
    2421             :     }
    2422             : 
    2423             :     /* Perform the add operation */
    2424          10 :     ret =  sysdb_set_group_attr(test_ctx->domain,
    2425             :                                 data->groupname, data->attrs, SYSDB_MOD_ADD);
    2426          10 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    2427             : 
    2428             :     /* Before the delete, all groups with gid >= _i have the testuser%_i
    2429             :      * as a member
    2430             :      */
    2431          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2432          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2433             :                                         data->attrlist, &data->msg);
    2434          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2435             :                 (unsigned long long) data->gid);
    2436             : 
    2437          55 :         gv.data = (uint8_t *) ghostname;
    2438          55 :         gv.length = strlen(ghostname);
    2439             : 
    2440          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2441          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    2442             : 
    2443          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2444          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname);
    2445             :     }
    2446          10 :     talloc_free(test_ctx);
    2447             : }
    2448          10 : END_TEST
    2449             : 
    2450          10 : START_TEST (test_sysdb_memberof_mod_replace)
    2451             : {
    2452             :     struct sysdb_test_ctx *test_ctx;
    2453             :     struct test_data *data;
    2454             :     char *ghostname_del;
    2455             :     char *ghostname_add;
    2456             :     int ret;
    2457             :     struct ldb_message_element *el;
    2458             :     struct ldb_val gv, *test_gv;
    2459             :     gid_t itergid;
    2460             : 
    2461             :     /* Setup */
    2462          10 :     ret = setup_sysdb_tests(&test_ctx);
    2463          10 :     if (ret != EOK) {
    2464           0 :         fail("Could not set up the test");
    2465             :         return;
    2466             :     }
    2467             : 
    2468          10 :     data = talloc_zero(test_ctx, struct test_data);
    2469          10 :     data->ctx = test_ctx;
    2470          10 :     data->ev = test_ctx->ev;
    2471          10 :     data->gid = _i;
    2472          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2473             : 
    2474          10 :     data->attrs = sysdb_new_attrs(data);
    2475          10 :     if (ret != EOK) {
    2476           0 :         fail("Could not create the changeset");
    2477             :         return;
    2478             :     }
    2479             : 
    2480             :     /* The test replaces the testuser%i attribute with testghost%i */
    2481          10 :     ghostname_del = talloc_asprintf(data, "testuser%d", _i);
    2482          10 :     fail_unless(ghostname_del != NULL, "Out of memory\n");
    2483             : 
    2484          10 :     ghostname_add = talloc_asprintf(data, "testghost%d", _i);
    2485          10 :     fail_unless(ghostname_add != NULL, "Out of memory\n");
    2486          10 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname_add);
    2487          10 :     fail_unless(ret == EOK, "Cannot add attr\n");
    2488             : 
    2489          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2490          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2491          10 :     data->attrlist[0] = SYSDB_GHOST;
    2492          10 :     data->attrlist[1] = NULL;
    2493             : 
    2494             :     /* Before the replace, all groups with gid >= _i have the testuser%_i
    2495             :      * as a member
    2496             :      */
    2497          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2498          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2499             :                                         data->attrlist, &data->msg);
    2500          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2501             :                 (unsigned long long) data->gid);
    2502             : 
    2503          55 :         gv.data = (uint8_t *) ghostname_del;
    2504          55 :         gv.length = strlen(ghostname_del);
    2505             : 
    2506          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2507          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    2508             : 
    2509          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2510          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_del);
    2511             :     }
    2512             : 
    2513             :     /* Perform the replace operation */
    2514          10 :     ret =  sysdb_set_group_attr(test_ctx->domain,
    2515             :                                 data->groupname, data->attrs, SYSDB_MOD_REP);
    2516          10 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    2517             : 
    2518             :     /* After the replace, all groups with gid >= _i have the testghost%_i
    2519             :      * as a member
    2520             :      */
    2521          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2522          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2523             :                                         data->attrlist, &data->msg);
    2524          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2525             :                 (unsigned long long) data->gid);
    2526             : 
    2527          55 :         gv.data = (uint8_t *) ghostname_add;
    2528          55 :         gv.length = strlen(ghostname_add);
    2529             : 
    2530          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2531          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    2532             : 
    2533          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2534          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_add);
    2535             :     }
    2536             : 
    2537          10 :     talloc_free(test_ctx);
    2538             : }
    2539          10 : END_TEST
    2540             : 
    2541          10 : START_TEST (test_sysdb_memberof_mod_replace_keep)
    2542             : {
    2543             :     struct sysdb_test_ctx *test_ctx;
    2544             :     struct test_data *data;
    2545             :     char *ghostname_rep;
    2546             :     char *ghostname_del;
    2547             :     char *ghostname_check;
    2548             :     int ret;
    2549             :     struct ldb_message_element *el;
    2550             :     struct ldb_val gv, *test_gv;
    2551             :     gid_t itergid;
    2552             :     uid_t iteruid;
    2553             : 
    2554             :     /* Setup */
    2555          10 :     ret = setup_sysdb_tests(&test_ctx);
    2556          10 :     if (ret != EOK) {
    2557           0 :         fail("Could not set up the test");
    2558             :         return;
    2559             :     }
    2560             : 
    2561          10 :     data = talloc_zero(test_ctx, struct test_data);
    2562          10 :     data->ctx = test_ctx;
    2563          10 :     data->ev = test_ctx->ev;
    2564          10 :     data->gid = MBO_GROUP_BASE + 10 - _i;
    2565          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2566             : 
    2567          10 :     data->attrs = sysdb_new_attrs(data);
    2568          10 :     if (ret != EOK) {
    2569           0 :         fail("Could not create the changeset");
    2570             :         return;
    2571             :     }
    2572             : 
    2573             :     /* The test replaces the attributes (testusera$gid, testuserb$gid) with
    2574             :      * just testusera$gid. The result should be not only testusera, but also
    2575             :      * all ghost users inherited from child groups
    2576             :      */
    2577          10 :     ghostname_rep = talloc_asprintf(data, "testusera%d", data->gid);
    2578          10 :     fail_unless(ghostname_rep != NULL, "Out of memory\n");
    2579          10 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname_rep);
    2580          10 :     fail_unless(ret == EOK, "Cannot add attr\n");
    2581             : 
    2582          10 :     ghostname_del = talloc_asprintf(data, "testuserb%d", data->gid);
    2583          10 :     fail_unless(ghostname_del != NULL, "Out of memory\n");
    2584             : 
    2585          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2586          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2587          10 :     data->attrlist[0] = SYSDB_GHOST;
    2588          10 :     data->attrlist[1] = NULL;
    2589             : 
    2590             :     /* Before the replace, all groups with gid >= _i have both testuser a
    2591             :      * and testuserb as a member
    2592             :      */
    2593          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2594          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2595             :                                         data->attrlist, &data->msg);
    2596          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2597             :                 (unsigned long long) data->gid);
    2598             : 
    2599          55 :         gv.data = (uint8_t *) ghostname_rep;
    2600          55 :         gv.length = strlen(ghostname_rep);
    2601             : 
    2602          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2603          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    2604             : 
    2605          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2606          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_rep);
    2607             : 
    2608          55 :         gv.data = (uint8_t *) ghostname_del;
    2609          55 :         gv.length = strlen(ghostname_rep);
    2610             : 
    2611          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2612          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_del);
    2613             : 
    2614             :         /* inherited users must be there */
    2615         385 :         for (iteruid = MBO_GROUP_BASE ; iteruid < itergid ; iteruid++) {
    2616         330 :             ghostname_check = talloc_asprintf(data, "testusera%d", iteruid);
    2617         330 :             gv.data = (uint8_t *) ghostname_check;
    2618         330 :             gv.length = strlen(ghostname_check);
    2619             : 
    2620         330 :             test_gv = ldb_msg_find_val(el, &gv);
    2621         330 :             fail_if(test_gv == NULL, "Cannot find inherited ghost user %s\n",
    2622             :                     ghostname_check);
    2623             : 
    2624         330 :             if (iteruid < data->gid) {
    2625             :                 /* Also check the B user if it hasn't been deleted yet */
    2626         165 :                 ghostname_check = talloc_asprintf(data, "testuserb%d", iteruid);
    2627         165 :                 gv.data = (uint8_t *) ghostname_check;
    2628         165 :                 gv.length = strlen(ghostname_check);
    2629             : 
    2630         165 :                 test_gv = ldb_msg_find_val(el, &gv);
    2631         165 :                 fail_if(test_gv == NULL, "Cannot find inherited ghost user %s\n",
    2632             :                         ghostname_check);
    2633             :             }
    2634         330 :             talloc_zfree(ghostname_check);
    2635             :         }
    2636             :     }
    2637             : 
    2638             :     /* Perform the replace operation */
    2639          10 :     ret =  sysdb_set_group_attr(test_ctx->domain,
    2640             :                                 data->groupname, data->attrs, SYSDB_MOD_REP);
    2641          10 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    2642             : 
    2643             :     /* After the replace, testusera should still be there, but we also need
    2644             :      * to keep ghost users inherited from other groups
    2645             :      */
    2646          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    2647          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    2648             :                                         data->attrlist, &data->msg);
    2649          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    2650             :                 (unsigned long long) data->gid);
    2651             : 
    2652          55 :         gv.data = (uint8_t *) ghostname_rep;
    2653          55 :         gv.length = strlen(ghostname_rep);
    2654             : 
    2655             :         /* testusera must still be there */
    2656          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    2657          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    2658             : 
    2659          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2660          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_rep);
    2661             : 
    2662             :         /* testuserb must be gone */
    2663          55 :         gv.data = (uint8_t *) ghostname_del;
    2664          55 :         gv.length = strlen(ghostname_rep);
    2665             : 
    2666          55 :         test_gv = ldb_msg_find_val(el, &gv);
    2667          55 :         fail_unless(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_del);
    2668             : 
    2669             :         /* inherited users must still be there */
    2670         385 :         for (iteruid = MBO_GROUP_BASE ; iteruid < itergid ; iteruid++) {
    2671         330 :             ghostname_check = talloc_asprintf(data, "testusera%d", iteruid);
    2672         330 :             gv.data = (uint8_t *) ghostname_check;
    2673         330 :             gv.length = strlen(ghostname_check);
    2674             : 
    2675         330 :             test_gv = ldb_msg_find_val(el, &gv);
    2676         330 :             fail_if(test_gv == NULL, "Cannot find inherited ghost user %s\n",
    2677             :                     ghostname_check);
    2678             : 
    2679         330 :             if (iteruid < data->gid) {
    2680             :                 /* Also check the B user if it hasn't been deleted yet */
    2681         165 :                 ghostname_check = talloc_asprintf(data, "testuserb%d", iteruid);
    2682         165 :                 gv.data = (uint8_t *) ghostname_check;
    2683         165 :                 gv.length = strlen(ghostname_check);
    2684             : 
    2685         165 :                 test_gv = ldb_msg_find_val(el, &gv);
    2686         165 :                 fail_if(test_gv == NULL, "Cannot find inherited ghost user %s\n",
    2687             :                         ghostname_check);
    2688             :             }
    2689         330 :             talloc_zfree(ghostname_check);
    2690             :         }
    2691             :     }
    2692             : 
    2693          10 :     talloc_free(test_ctx);
    2694             : }
    2695          10 : END_TEST
    2696             : 
    2697           1 : START_TEST (test_sysdb_memberof_close_loop)
    2698             : {
    2699             :     struct sysdb_test_ctx *test_ctx;
    2700             :     struct test_data *data;
    2701             :     int ret;
    2702             : 
    2703             :     /* Setup */
    2704           1 :     ret = setup_sysdb_tests(&test_ctx);
    2705           1 :     if (ret != EOK) {
    2706           0 :         fail("Could not set up the test");
    2707             :         return;
    2708             :     }
    2709             : 
    2710           1 :     data = talloc_zero(test_ctx, struct test_data);
    2711           1 :     data->ctx = test_ctx;
    2712           1 :     data->ev = test_ctx->ev;
    2713           1 :     data->gid = MBO_GROUP_BASE;
    2714           1 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    2715             : 
    2716           1 :     data->attrlist = talloc_array(data, const char *, 2);
    2717           1 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2718           1 :     data->attrlist[0] = talloc_asprintf(data, "testgroup%d", data->gid + 9);
    2719           1 :     data->attrlist[1] = NULL;
    2720             : 
    2721           1 :     ret = test_memberof_store_group(data);
    2722             : 
    2723           1 :     fail_if(ret != EOK, "Could not store POSIX group #%d", data->gid);
    2724           1 :     talloc_free(test_ctx);
    2725             : }
    2726           1 : END_TEST
    2727             : 
    2728          20 : START_TEST (test_sysdb_memberof_store_user)
    2729             : {
    2730             :     struct sysdb_test_ctx *test_ctx;
    2731             :     struct test_data *data;
    2732             :     int ret;
    2733             : 
    2734             :     /* Setup */
    2735          20 :     ret = setup_sysdb_tests(&test_ctx);
    2736          20 :     if (ret != EOK) {
    2737           0 :         fail("Could not set up the test");
    2738             :         return;
    2739             :     }
    2740             : 
    2741          20 :     data = talloc_zero(test_ctx, struct test_data);
    2742          20 :     data->ctx = test_ctx;
    2743          20 :     data->ev = test_ctx->ev;
    2744          20 :     data->uid = MBO_USER_BASE + _i;
    2745          20 :     data->gid = 0; /* MPG domain */
    2746          20 :     data->username = talloc_asprintf(data, "testuser%d", data->uid);
    2747             : 
    2748          20 :     ret = test_store_user(data);
    2749             : 
    2750          20 :     fail_if(ret != EOK, "Could not store user %s", data->username);
    2751          20 :     talloc_free(test_ctx);
    2752             : }
    2753          20 : END_TEST
    2754             : 
    2755          20 : START_TEST (test_sysdb_memberof_add_group_member)
    2756             : {
    2757             :     struct sysdb_test_ctx *test_ctx;
    2758             :     struct test_data *data;
    2759             :     int ret;
    2760             : 
    2761             :     /* Setup */
    2762          20 :     ret = setup_sysdb_tests(&test_ctx);
    2763          20 :     if (ret != EOK) {
    2764           0 :         fail("Could not set up the test");
    2765             :         return;
    2766             :     }
    2767             : 
    2768          20 :     data = talloc_zero(test_ctx, struct test_data);
    2769          20 :     data->ctx = test_ctx;
    2770          20 :     data->ev = test_ctx->ev;
    2771          20 :     data->groupname = talloc_asprintf(data, "testgroup%d", _i + MBO_GROUP_BASE);
    2772          20 :     data->uid = MBO_USER_BASE + _i;
    2773             : 
    2774          20 :     ret = test_add_group_member(data);
    2775             : 
    2776          20 :     fail_if(ret != EOK, "Could not modify group %s", data->groupname);
    2777             : 
    2778          20 :     talloc_free(test_ctx);
    2779             : }
    2780          20 : END_TEST
    2781             : 
    2782          10 : START_TEST (test_sysdb_memberof_check_memberuid_without_group_5)
    2783             : {
    2784             :     struct sysdb_test_ctx *test_ctx;
    2785             :     struct test_data *data;
    2786             :     int ret;
    2787             : 
    2788             :     /* Setup */
    2789          10 :     ret = setup_sysdb_tests(&test_ctx);
    2790          10 :     if (ret != EOK) {
    2791           0 :         fail("Could not set up the test");
    2792             :         return;
    2793             :     }
    2794             : 
    2795          10 :     data = talloc_zero(test_ctx, struct test_data);
    2796          10 :     data->ctx = test_ctx;
    2797          10 :     data->ev = test_ctx->ev;
    2798          10 :     data->gid = _i + MBO_GROUP_BASE;
    2799             : 
    2800          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2801          10 :     fail_unless(data->attrlist != NULL, "tallo_array failed.");
    2802          10 :     data->attrlist[0] = "memberuid";
    2803          10 :     data->attrlist[1] = NULL;
    2804             : 
    2805          20 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain,
    2806          10 :                                     _i + MBO_GROUP_BASE, data->attrlist,
    2807             :                                     &data->msg);
    2808          10 :     if (_i == 5) {
    2809           1 :         fail_unless(ret == ENOENT,
    2810             :                     "sysdb_search_group_by_gid found "
    2811             :                     "already deleted group");
    2812           1 :         if (ret == ENOENT) ret = EOK;
    2813             : 
    2814           1 :         fail_if(ret != EOK, "Could not check group %d", data->gid);
    2815             :     } else {
    2816           9 :         fail_if(ret != EOK, "Could not check group %d", data->gid);
    2817             : 
    2818           9 :         fail_unless(data->msg->num_elements == 1,
    2819             :                     "Wrong number of results, expected [1] got [%d]",
    2820             :                     data->msg->num_elements);
    2821           9 :         fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0,
    2822             :                     "Wrong attribute name");
    2823           9 :         fail_unless(data->msg->elements[0].num_values == ((_i + 1) % 6),
    2824             :                     "Wrong number of attribute values, "
    2825             :                     "expected [%d] got [%d]", ((_i + 1) % 6),
    2826             :                     data->msg->elements[0].num_values);
    2827             :     }
    2828             : 
    2829          10 :     talloc_free(test_ctx);
    2830             : }
    2831          10 : END_TEST
    2832             : 
    2833          10 : START_TEST (test_sysdb_memberof_check_memberuid)
    2834             : {
    2835             :     struct sysdb_test_ctx *test_ctx;
    2836             :     struct test_data *data;
    2837             :     int ret;
    2838             : 
    2839             :     /* Setup */
    2840          10 :     ret = setup_sysdb_tests(&test_ctx);
    2841          10 :     if (ret != EOK) {
    2842           0 :         fail("Could not set up the test");
    2843             :         return;
    2844             :     }
    2845             : 
    2846          10 :     data = talloc_zero(test_ctx, struct test_data);
    2847          10 :     data->ctx = test_ctx;
    2848          10 :     data->ev = test_ctx->ev;
    2849          10 :     data->gid = _i + MBO_GROUP_BASE;
    2850             : 
    2851          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2852          10 :     fail_unless(data->attrlist != NULL, "tallo_array failed.");
    2853          10 :     data->attrlist[0] = "memberuid";
    2854          10 :     data->attrlist[1] = NULL;
    2855             : 
    2856          20 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain,
    2857          10 :                                     _i + MBO_GROUP_BASE, data->attrlist,
    2858             :                                     &data->msg);
    2859             : 
    2860          10 :     fail_if(ret != EOK, "Could not check group %d", data->gid);
    2861             : 
    2862          10 :     fail_unless(data->msg->num_elements == 1,
    2863             :                 "Wrong number of results, expected [1] got [%d]",
    2864             :                 data->msg->num_elements);
    2865          10 :     fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0,
    2866             :                 "Wrong attribute name");
    2867          10 :     fail_unless(data->msg->elements[0].num_values == _i + 1,
    2868             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    2869             :                 _i + 1, data->msg->elements[0].num_values);
    2870             : 
    2871          10 :     talloc_free(test_ctx);
    2872             : }
    2873          10 : END_TEST
    2874             : 
    2875          10 : START_TEST (test_sysdb_memberof_check_memberuid_loop)
    2876             : {
    2877             :     struct sysdb_test_ctx *test_ctx;
    2878             :     struct test_data *data;
    2879             :     int ret;
    2880             : 
    2881             :     /* Setup */
    2882          10 :     ret = setup_sysdb_tests(&test_ctx);
    2883          10 :     if (ret != EOK) {
    2884           0 :         fail("Could not set up the test");
    2885             :         return;
    2886             :     }
    2887             : 
    2888          10 :     data = talloc_zero(test_ctx, struct test_data);
    2889          10 :     data->ctx = test_ctx;
    2890          10 :     data->ev = test_ctx->ev;
    2891          10 :     data->gid = _i + MBO_GROUP_BASE;
    2892             : 
    2893          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2894          10 :     fail_unless(data->attrlist != NULL, "tallo_array failed.");
    2895          10 :     data->attrlist[0] = "memberuid";
    2896          10 :     data->attrlist[1] = NULL;
    2897             : 
    2898          20 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain,
    2899          10 :                                     _i + MBO_GROUP_BASE, data->attrlist,
    2900             :                                     &data->msg);
    2901             : 
    2902          10 :     fail_if(ret != EOK, "Could not check group %d", data->gid);
    2903             : 
    2904          10 :     fail_unless(data->msg->num_elements == 1,
    2905             :                 "Wrong number of results, expected [1] got [%d]",
    2906             :                 data->msg->num_elements);
    2907          10 :     fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0,
    2908             :                 "Wrong attribute name");
    2909          10 :     fail_unless(data->msg->elements[0].num_values == 10,
    2910             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    2911             :                 10, data->msg->elements[0].num_values);
    2912             : 
    2913          10 :     talloc_free(test_ctx);
    2914             : }
    2915          10 : END_TEST
    2916             : 
    2917          10 : START_TEST (test_sysdb_memberof_check_memberuid_loop_without_group_5)
    2918             : {
    2919             :     struct sysdb_test_ctx *test_ctx;
    2920             :     struct test_data *data;
    2921             :     int ret;
    2922             : 
    2923             :     /* Setup */
    2924          10 :     ret = setup_sysdb_tests(&test_ctx);
    2925          10 :     if (ret != EOK) {
    2926           0 :         fail("Could not set up the test");
    2927             :         return;
    2928             :     }
    2929             : 
    2930          10 :     data = talloc_zero(test_ctx, struct test_data);
    2931          10 :     data->ctx = test_ctx;
    2932          10 :     data->ev = test_ctx->ev;
    2933          10 :     data->gid = _i + MBO_GROUP_BASE;
    2934             : 
    2935          10 :     data->attrlist = talloc_array(data, const char *, 2);
    2936          10 :     fail_unless(data->attrlist != NULL, "tallo_array failed.");
    2937          10 :     data->attrlist[0] = "memberuid";
    2938          10 :     data->attrlist[1] = NULL;
    2939             : 
    2940          20 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain,
    2941          10 :                                     _i + MBO_GROUP_BASE, data->attrlist,
    2942             :                                     &data->msg);
    2943             : 
    2944          10 :     if (_i == 5) {
    2945           1 :         fail_unless(ret == ENOENT,
    2946             :                     "sysdb_search_group_by_gid_send found "
    2947             :                     "already deleted group");
    2948           1 :         if (ret == ENOENT) ret = EOK;
    2949             : 
    2950           1 :         fail_if(ret != EOK, "Could not check group %d", data->gid);
    2951             :     } else {
    2952           9 :         fail_if(ret != EOK, "Could not check group %d", data->gid);
    2953             : 
    2954           9 :         fail_unless(data->msg->num_elements == 1,
    2955             :                     "Wrong number of results, expected [1] got [%d]",
    2956             :                     data->msg->num_elements);
    2957           9 :         fail_unless(strcmp(data->msg->elements[0].name, "memberuid") == 0,
    2958             :                     "Wrong attribute name");
    2959           9 :         fail_unless(data->msg->elements[0].num_values == ((_i + 5) % 10),
    2960             :                     "Wrong number of attribute values, expected [%d] got [%d]",
    2961             :                     ((_i + 5) % 10), data->msg->elements[0].num_values);
    2962             :     }
    2963             : 
    2964          10 :     talloc_free(test_ctx);
    2965             : }
    2966          10 : END_TEST
    2967             : 
    2968          40 : START_TEST (test_sysdb_memberof_check_nested_ghosts)
    2969             : {
    2970             :     struct sysdb_test_ctx *test_ctx;
    2971             :     struct test_data *data;
    2972             :     int ret;
    2973             : 
    2974             :     /* Setup */
    2975          40 :     ret = setup_sysdb_tests(&test_ctx);
    2976          40 :     if (ret != EOK) {
    2977           0 :         fail("Could not set up the test");
    2978             :         return;
    2979             :     }
    2980             : 
    2981          40 :     data = talloc_zero(test_ctx, struct test_data);
    2982          40 :     data->ctx = test_ctx;
    2983          40 :     data->ev = test_ctx->ev;
    2984          40 :     data->gid = _i;
    2985             : 
    2986          40 :     data->attrlist = talloc_array(data, const char *, 2);
    2987          40 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    2988          40 :     data->attrlist[0] = SYSDB_GHOST;
    2989          40 :     data->attrlist[1] = NULL;
    2990             : 
    2991          40 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    2992             :                                     data->attrlist, &data->msg);
    2993          40 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n", (unsigned long long) data->gid);
    2994             : 
    2995          40 :     fail_unless(strcmp(data->msg->elements[0].name, SYSDB_GHOST) == 0,
    2996             :                 "Wrong attribute name");
    2997          40 :     fail_unless(data->msg->elements[0].num_values == _i - MBO_GROUP_BASE + 1,
    2998             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    2999             :                 _i + 1, data->msg->elements[0].num_values);
    3000             : 
    3001          40 :     talloc_free(test_ctx);
    3002             : }
    3003          40 : END_TEST
    3004             : 
    3005          10 : START_TEST (test_sysdb_memberof_check_nested_double_ghosts)
    3006             : {
    3007             :     struct sysdb_test_ctx *test_ctx;
    3008             :     struct test_data *data;
    3009             :     int ret;
    3010             : 
    3011             :     /* Setup */
    3012          10 :     ret = setup_sysdb_tests(&test_ctx);
    3013          10 :     if (ret != EOK) {
    3014           0 :         fail("Could not set up the test");
    3015             :         return;
    3016             :     }
    3017             : 
    3018          10 :     data = talloc_zero(test_ctx, struct test_data);
    3019          10 :     data->ctx = test_ctx;
    3020          10 :     data->ev = test_ctx->ev;
    3021          10 :     data->gid = _i;
    3022             : 
    3023          10 :     data->attrlist = talloc_array(data, const char *, 2);
    3024          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3025          10 :     data->attrlist[0] = SYSDB_GHOST;
    3026          10 :     data->attrlist[1] = NULL;
    3027             : 
    3028          10 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3029             :                                     data->attrlist, &data->msg);
    3030          10 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n", (unsigned long long) data->gid);
    3031             : 
    3032          10 :     fail_unless(strcmp(data->msg->elements[0].name, SYSDB_GHOST) == 0,
    3033             :                 "Wrong attribute name");
    3034          10 :     fail_unless(data->msg->elements[0].num_values == (_i - MBO_GROUP_BASE + 1)*2,
    3035             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    3036             :                 (_i - MBO_GROUP_BASE + 1)*2,
    3037             :                 data->msg->elements[0].num_values);
    3038             : 
    3039          10 :     talloc_free(test_ctx);
    3040             : }
    3041          10 : END_TEST
    3042             : 
    3043           9 : START_TEST (test_sysdb_memberof_remove_child_group_and_check_ghost)
    3044             : {
    3045             :     struct sysdb_test_ctx *test_ctx;
    3046             :     struct test_data *data;
    3047             :     int ret;
    3048             :     gid_t delgid;
    3049             : 
    3050             :     /* Setup */
    3051           9 :     ret = setup_sysdb_tests(&test_ctx);
    3052           9 :     if (ret != EOK) {
    3053           0 :         fail("Could not set up the test");
    3054             :         return;
    3055             :     }
    3056             : 
    3057           9 :     data = talloc_zero(test_ctx, struct test_data);
    3058           9 :     data->ctx = test_ctx;
    3059           9 :     data->ev = test_ctx->ev;
    3060           9 :     data->gid = _i;
    3061           9 :     delgid = data->gid - 1;
    3062             : 
    3063           9 :     data->attrlist = talloc_array(data, const char *, 2);
    3064           9 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3065           9 :     data->attrlist[0] = SYSDB_GHOST;
    3066           9 :     data->attrlist[1] = NULL;
    3067             : 
    3068           9 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3069             :                                     data->attrlist, &data->msg);
    3070           9 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n", (unsigned long long) data->gid);
    3071             : 
    3072           9 :     fail_unless(strcmp(data->msg->elements[0].name, SYSDB_GHOST) == 0,
    3073             :                 "Wrong attribute name");
    3074             : 
    3075             :     /* Expect our own and our parent's */
    3076           9 :     fail_unless(data->msg->elements[0].num_values == 2,
    3077             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    3078             :                 2, data->msg->elements[0].num_values);
    3079             : 
    3080             :     /* Remove the parent */
    3081           9 :     ret = sysdb_delete_group(data->ctx->domain, NULL, delgid);
    3082           9 :     fail_if(ret != EOK, "Cannot delete group %llu [%d]: %s\n",
    3083             :             (unsigned long long) data->gid, ret, strerror(ret));
    3084             : 
    3085           9 :     talloc_free(data->msg);
    3086             : 
    3087             :     /* Check the parent again. The inherited ghost user should be gone. */
    3088           9 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3089             :                                     data->attrlist, &data->msg);
    3090           9 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n", (unsigned long long) data->gid);
    3091             : 
    3092           9 :     fail_unless(strcmp(data->msg->elements[0].name, SYSDB_GHOST) == 0,
    3093             :                 "Wrong attribute name");
    3094             : 
    3095             :     /* Expect our own now only */
    3096           9 :     fail_unless(data->msg->elements[0].num_values == 1,
    3097             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    3098             :                 1, data->msg->elements[0].num_values);
    3099             : 
    3100           9 :     talloc_free(test_ctx);
    3101             : }
    3102           9 : END_TEST
    3103             : 
    3104          10 : START_TEST (test_sysdb_memberof_mod_del)
    3105             : {
    3106             :     struct sysdb_test_ctx *test_ctx;
    3107             :     struct test_data *data;
    3108             :     char *ghostname;
    3109             :     int ret;
    3110             :     struct ldb_message_element *el;
    3111             :     struct ldb_val gv, *test_gv;
    3112             :     gid_t itergid;
    3113             : 
    3114             :     /* Setup */
    3115          10 :     ret = setup_sysdb_tests(&test_ctx);
    3116          10 :     if (ret != EOK) {
    3117           0 :         fail("Could not set up the test");
    3118             :         return;
    3119             :     }
    3120             : 
    3121          10 :     data = talloc_zero(test_ctx, struct test_data);
    3122          10 :     data->ctx = test_ctx;
    3123          10 :     data->ev = test_ctx->ev;
    3124          10 :     data->gid = _i;
    3125          10 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    3126             : 
    3127          10 :     data->attrs = sysdb_new_attrs(data);
    3128          10 :     if (ret != EOK) {
    3129           0 :         fail("Could not create the changeset");
    3130             :         return;
    3131             :     }
    3132             : 
    3133          10 :     ghostname = talloc_asprintf(data, "testuser%d", _i);
    3134          10 :     fail_unless(ghostname != NULL, "Out of memory\n");
    3135          10 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname);
    3136          10 :     fail_unless(ret == EOK, "Cannot add attr\n");
    3137             : 
    3138          10 :     data->attrlist = talloc_array(data, const char *, 2);
    3139          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3140          10 :     data->attrlist[0] = SYSDB_GHOST;
    3141          10 :     data->attrlist[1] = NULL;
    3142             : 
    3143             :     /* Before the delete, all groups with gid >= _i have the testuser%_i
    3144             :      * as a member
    3145             :      */
    3146          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    3147          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    3148             :                                         data->attrlist, &data->msg);
    3149          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3150             :                 (unsigned long long) data->gid);
    3151             : 
    3152          55 :         gv.data = (uint8_t *) ghostname;
    3153          55 :         gv.length = strlen(ghostname);
    3154             : 
    3155          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3156          55 :         fail_if(el == NULL, "Cannot find ghost element\n");
    3157             : 
    3158          55 :         test_gv = ldb_msg_find_val(el, &gv);
    3159          55 :         fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname);
    3160             :     }
    3161             : 
    3162             :     /* Delete the attribute */
    3163          10 :     ret = sysdb_set_group_attr(test_ctx->domain,
    3164             :                                data->groupname, data->attrs, SYSDB_MOD_DEL);
    3165          10 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    3166             : 
    3167             :     /* After the delete, we shouldn't be able to find the ghost attribute */
    3168          65 :     for (itergid = data->gid ; itergid < MBO_GROUP_BASE + NUM_GHOSTS; itergid++) {
    3169          55 :         ret = sysdb_search_group_by_gid(data, test_ctx->domain, itergid,
    3170             :                                         data->attrlist, &data->msg);
    3171          55 :         fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3172             :                 (unsigned long long) data->gid);
    3173             : 
    3174          55 :         gv.data = (uint8_t *) ghostname;
    3175          55 :         gv.length = strlen(ghostname);
    3176             : 
    3177          55 :         el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3178          55 :         if (itergid > data->gid) {
    3179             :             /* The first group would have the ghost attribute gone completely */
    3180          45 :             fail_if(el == NULL, "Cannot find ghost element\n");
    3181          45 :             test_gv = ldb_msg_find_val(el, &gv);
    3182          45 :             fail_unless(test_gv == NULL,
    3183             :                         "Ghost user %s unexpectedly found\n", ghostname);
    3184             :         } else {
    3185          10 :             fail_unless(el == NULL, "Stray values in ghost element?\n");
    3186             :         }
    3187             :     }
    3188             : 
    3189          10 :     talloc_free(test_ctx);
    3190             : }
    3191          10 : END_TEST
    3192             : 
    3193          10 : START_TEST (test_sysdb_memberof_check_ghost)
    3194             : {
    3195             :     struct sysdb_test_ctx *test_ctx;
    3196             :     struct test_data *data;
    3197             :     int ret, j;
    3198             :     char *expected;
    3199             : 
    3200             :     /* Setup */
    3201          10 :     ret = setup_sysdb_tests(&test_ctx);
    3202          10 :     if (ret != EOK) {
    3203           0 :         fail("Could not set up the test");
    3204             :         return;
    3205             :     }
    3206             : 
    3207          10 :     data = talloc_zero(test_ctx, struct test_data);
    3208          10 :     data->ctx = test_ctx;
    3209          10 :     data->ev = test_ctx->ev;
    3210          10 :     data->gid = _i;
    3211             : 
    3212          10 :     data->attrlist = talloc_array(data, const char *, 2);
    3213          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3214          10 :     data->attrlist[0] = SYSDB_GHOST;
    3215          10 :     data->attrlist[1] = NULL;
    3216             : 
    3217          10 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3218             :                                     data->attrlist, &data->msg);
    3219             : 
    3220          10 :     fail_if(ret != EOK, "Could not check group %d", data->gid);
    3221             : 
    3222          10 :     if (_i > MBO_GROUP_BASE) {
    3223             :         /* After the previous test, the first group (gid == MBO_GROUP_BASE)
    3224             :          * has no ghost users. That's a legitimate test case we need to account
    3225             :          * for now.
    3226             :          */
    3227           9 :         fail_unless(data->msg->num_elements == 1,
    3228             :                     "Wrong number of results, expected [1] got [%d] for %d",
    3229             :                     data->msg->num_elements, data->gid);
    3230             :     }
    3231             : 
    3232          10 :     if (data->msg->num_elements == 0) {
    3233           1 :         talloc_free(test_ctx);
    3234           1 :         return;
    3235             :     }
    3236             : 
    3237           9 :     fail_unless(strcmp(data->msg->elements[0].name, SYSDB_GHOST) == 0,
    3238             :                 "Wrong attribute name");
    3239           9 :     fail_unless(data->msg->elements[0].num_values == _i - MBO_GROUP_BASE,
    3240             :                 "Wrong number of attribute values, expected [%d] got [%d]",
    3241             :                 _i + 1, data->msg->elements[0].num_values);
    3242             : 
    3243          54 :     for (j = MBO_GROUP_BASE; j < _i; j++) {
    3244          45 :         expected = talloc_asprintf(data, "testghost%d", j);
    3245          45 :         fail_if(expected == NULL, "OOM\n");
    3246          45 :         fail_unless(strcmp(expected,
    3247             :                            (const char *) data->msg->elements[0].values[j-MBO_GROUP_BASE].data) == 0);
    3248          45 :         talloc_free(expected);
    3249             :     }
    3250             : 
    3251           9 :     talloc_free(test_ctx);
    3252             : }
    3253             : END_TEST
    3254             : 
    3255           5 : START_TEST (test_sysdb_memberof_convert_to_real_users)
    3256             : {
    3257             :     struct sysdb_test_ctx *test_ctx;
    3258             :     struct test_data *data;
    3259             :     int ret;
    3260             : 
    3261             :     /* Setup */
    3262           5 :     ret = setup_sysdb_tests(&test_ctx);
    3263           5 :     if (ret != EOK) {
    3264           0 :         fail("Could not set up the test");
    3265             :         return;
    3266             :     }
    3267             : 
    3268           5 :     data = talloc_zero(test_ctx, struct test_data);
    3269           5 :     data->ctx = test_ctx;
    3270           5 :     data->ev = test_ctx->ev;
    3271           5 :     data->uid = _i * 2;
    3272           5 :     data->gid = _i * 2;
    3273           5 :     data->username = talloc_asprintf(data, "testghost%d", _i);
    3274             : 
    3275           5 :     ret = test_store_user(data);
    3276           5 :     fail_if(ret != EOK, "Cannot add user %s\n", data->username);
    3277             : }
    3278           5 : END_TEST
    3279             : 
    3280          10 : START_TEST (test_sysdb_memberof_check_convert)
    3281             : {
    3282             :     struct sysdb_test_ctx *test_ctx;
    3283             :     struct test_data *data;
    3284             :     int ret;
    3285             :     struct ldb_message_element *ghosts;
    3286             :     struct ldb_message_element *members;
    3287             :     int exp_mem, exp_gh;
    3288             : 
    3289             :     /* Eplicitly disable enumeration during setup as converting the ghost
    3290             :      * users into real ones work only when enumeration is disabled
    3291             :      */
    3292          10 :     ret = _setup_sysdb_tests(&test_ctx, false);
    3293          10 :     if (ret != EOK) {
    3294           0 :         fail("Could not set up the test");
    3295             :         return;
    3296             :     }
    3297             : 
    3298          10 :     data = talloc_zero(test_ctx, struct test_data);
    3299          10 :     data->ctx = test_ctx;
    3300          10 :     data->ev = test_ctx->ev;
    3301          10 :     data->gid = _i;
    3302             : 
    3303          10 :     data->attrlist = talloc_array(data, const char *, 3);
    3304          10 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3305          10 :     data->attrlist[0] = SYSDB_GHOST;
    3306          10 :     data->attrlist[1] = SYSDB_MEMBER;
    3307          10 :     data->attrlist[2] = NULL;
    3308             : 
    3309          10 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3310             :                                     data->attrlist, &data->msg);
    3311             : 
    3312          10 :     fail_if(ret != EOK, "Could not check group %d", data->gid);
    3313             : 
    3314          10 :     fail_unless(data->msg->num_elements == (_i == MBO_GROUP_BASE) ? 0 : 1,
    3315             :                 "Wrong number of results, expected [1] got [%d] for %d",
    3316             :                 data->msg->num_elements, data->gid);
    3317             : 
    3318          10 :     if (data->msg->num_elements == 0) {
    3319           1 :         talloc_free(test_ctx);
    3320           1 :         return;
    3321             :     }
    3322             : 
    3323             : 
    3324           9 :     members = ldb_msg_find_element(data->msg, SYSDB_MEMBER);
    3325           9 :     exp_mem = _i - MBO_GROUP_BASE;
    3326           9 :     if (exp_mem > NUM_GHOSTS/2) {
    3327           4 :         exp_mem = NUM_GHOSTS/2;
    3328             :     }
    3329             : 
    3330           9 :     ghosts = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3331           9 :     exp_gh = _i - MBO_GROUP_BASE - 5;
    3332           9 :     if (exp_gh < 0) {
    3333           4 :         exp_gh = 0;
    3334             :     }
    3335             : 
    3336           9 :     fail_if(exp_mem != members->num_values,
    3337             :             "Expected %d members, found %d\n", exp_mem, members->num_values);
    3338           9 :     if (exp_gh) {
    3339           4 :         fail_if(exp_gh != ghosts->num_values,
    3340             :                 "Expected %d members, found %d\n", exp_gh, ghosts->num_values);
    3341             :     }
    3342             : 
    3343           9 :     talloc_free(test_ctx);
    3344             : }
    3345             : END_TEST
    3346             : 
    3347           4 : START_TEST (test_sysdb_memberof_ghost_replace)
    3348             : {
    3349             :     struct sysdb_test_ctx *test_ctx;
    3350             :     struct test_data *data;
    3351             :     char *ghostname_del;
    3352             :     char *ghostname_add;
    3353             :     int ret;
    3354             :     struct ldb_message_element *el;
    3355             :     struct ldb_val gv, *test_gv;
    3356             : 
    3357             :     /* Setup */
    3358           4 :     ret = setup_sysdb_tests(&test_ctx);
    3359           4 :     if (ret != EOK) {
    3360           0 :         fail("Could not set up the test");
    3361             :         return;
    3362             :     }
    3363             : 
    3364           4 :     data = talloc_zero(test_ctx, struct test_data);
    3365           4 :     data->ctx = test_ctx;
    3366           4 :     data->ev = test_ctx->ev;
    3367           4 :     data->gid = _i;
    3368           4 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    3369             : 
    3370           4 :     data->attrs = sysdb_new_attrs(data);
    3371           4 :     if (ret != EOK) {
    3372           0 :         fail("Could not create the changeset");
    3373             :         return;
    3374             :     }
    3375             : 
    3376             :     /* The test replaces the testghost%i attribute with testuser%i */
    3377           4 :     ghostname_del = talloc_asprintf(data, "testghost%d", _i - 1);
    3378           4 :     fail_unless(ghostname_del != NULL, "Out of memory\n");
    3379             : 
    3380           4 :     ghostname_add = talloc_asprintf(data, "testuser%d", _i - 1);
    3381           4 :     fail_unless(ghostname_add != NULL, "Out of memory\n");
    3382           4 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname_add);
    3383           4 :     fail_unless(ret == EOK, "Cannot add attr\n");
    3384             : 
    3385           4 :     data->attrlist = talloc_array(data, const char *, 2);
    3386           4 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3387           4 :     data->attrlist[0] = SYSDB_GHOST;
    3388           4 :     data->attrlist[1] = NULL;
    3389             : 
    3390             :     /* Before the replace, the group has the testghost%_i as a member */
    3391           4 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3392             :                                     data->attrlist, &data->msg);
    3393           4 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3394             :             (unsigned long long) data->gid);
    3395             : 
    3396           4 :     gv.data = (uint8_t *) ghostname_del;
    3397           4 :     gv.length = strlen(ghostname_del);
    3398             : 
    3399           4 :     el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3400           4 :     fail_if(el == NULL, "Cannot find ghost element\n");
    3401             : 
    3402           4 :     test_gv = ldb_msg_find_val(el, &gv);
    3403           4 :     fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_del);
    3404             : 
    3405             :     /* Perform the replace operation */
    3406           4 :     ret =  sysdb_set_group_attr(test_ctx->domain,
    3407             :                                 data->groupname, data->attrs, SYSDB_MOD_REP);
    3408           4 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    3409             : 
    3410             :     /* After the replace, the group has the testghost%_i as a member */
    3411           4 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3412             :                                     data->attrlist, &data->msg);
    3413           4 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3414             :             (unsigned long long) data->gid);
    3415             : 
    3416           4 :     gv.data = (uint8_t *) ghostname_add;
    3417           4 :     gv.length = strlen(ghostname_add);
    3418             : 
    3419           4 :     el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3420           4 :     fail_if(el == NULL, "Cannot find ghost element\n");
    3421             : 
    3422           4 :     test_gv = ldb_msg_find_val(el, &gv);
    3423           4 :     fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_add);
    3424             : }
    3425           4 : END_TEST
    3426             : 
    3427           4 : START_TEST (test_sysdb_memberof_ghost_replace_noop)
    3428             : {
    3429             :     struct sysdb_test_ctx *test_ctx;
    3430             :     struct test_data *data;
    3431             :     char *ghostname_del;
    3432             :     char *ghostname_add;
    3433             :     int ret;
    3434             :     struct ldb_message_element *el;
    3435             :     struct ldb_val gv, *test_gv;
    3436             : 
    3437             :     /* Setup */
    3438           4 :     ret = setup_sysdb_tests(&test_ctx);
    3439           4 :     if (ret != EOK) {
    3440           0 :         fail("Could not set up the test");
    3441             :         return;
    3442             :     }
    3443             : 
    3444           4 :     data = talloc_zero(test_ctx, struct test_data);
    3445           4 :     data->ctx = test_ctx;
    3446           4 :     data->ev = test_ctx->ev;
    3447           4 :     data->gid = _i;
    3448           4 :     data->groupname = talloc_asprintf(data, "testgroup%d", data->gid);
    3449             : 
    3450           4 :     data->attrs = sysdb_new_attrs(data);
    3451           4 :     if (ret != EOK) {
    3452           0 :         fail("Could not create the changeset");
    3453             :         return;
    3454             :     }
    3455             : 
    3456             :     /* The test replaces the testghost%i attribute with testuser%i */
    3457           4 :     ghostname_del = talloc_asprintf(data, "testuser%d", _i - 1);
    3458           4 :     fail_unless(ghostname_del != NULL, "Out of memory\n");
    3459             : 
    3460           4 :     ghostname_add = talloc_asprintf(data, "testuser%d", _i - 1);
    3461           4 :     fail_unless(ghostname_add != NULL, "Out of memory\n");
    3462           4 :     ret = sysdb_attrs_steal_string(data->attrs, SYSDB_GHOST, ghostname_add);
    3463           4 :     fail_unless(ret == EOK, "Cannot add attr\n");
    3464             : 
    3465           4 :     data->attrlist = talloc_array(data, const char *, 2);
    3466           4 :     fail_unless(data->attrlist != NULL, "talloc_array failed.");
    3467           4 :     data->attrlist[0] = SYSDB_GHOST;
    3468           4 :     data->attrlist[1] = NULL;
    3469             : 
    3470             :     /* Before the replace, the group has the testghost%_i as a member */
    3471           4 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3472             :                                     data->attrlist, &data->msg);
    3473           4 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3474             :             (unsigned long long) data->gid);
    3475             : 
    3476           4 :     gv.data = (uint8_t *) ghostname_del;
    3477           4 :     gv.length = strlen(ghostname_del);
    3478             : 
    3479           4 :     el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3480           4 :     fail_if(el == NULL, "Cannot find ghost element\n");
    3481             : 
    3482           4 :     test_gv = ldb_msg_find_val(el, &gv);
    3483           4 :     fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_del);
    3484             : 
    3485             :     /* Perform the replace operation */
    3486           4 :     ret =  sysdb_set_group_attr(test_ctx->domain,
    3487             :                                 data->groupname, data->attrs, SYSDB_MOD_REP);
    3488           4 :     fail_unless(ret == EOK, "Cannot set group attrs\n");
    3489             : 
    3490             :     /* After the replace, the group has the testghost%_i as a member */
    3491           4 :     ret = sysdb_search_group_by_gid(data, test_ctx->domain, data->gid,
    3492             :                                     data->attrlist, &data->msg);
    3493           4 :     fail_if(ret != EOK, "Cannot retrieve group %llu\n",
    3494             :             (unsigned long long) data->gid);
    3495             : 
    3496           4 :     gv.data = (uint8_t *) ghostname_add;
    3497           4 :     gv.length = strlen(ghostname_add);
    3498             : 
    3499           4 :     el = ldb_msg_find_element(data->msg, SYSDB_GHOST);
    3500           4 :     fail_if(el == NULL, "Cannot find ghost element\n");
    3501             : 
    3502           4 :     test_gv = ldb_msg_find_val(el, &gv);
    3503           4 :     fail_if(test_gv == NULL, "Cannot find ghost user %s\n", ghostname_add);
    3504             : }
    3505           4 : END_TEST
    3506             : 
    3507           5 : START_TEST (test_sysdb_memberof_user_cleanup)
    3508             : {
    3509             :     struct sysdb_test_ctx *test_ctx;
    3510             :     struct test_data *data;
    3511             :     int ret;
    3512             : 
    3513             :     /* Setup */
    3514           5 :     ret = setup_sysdb_tests(&test_ctx);
    3515           5 :     if (ret != EOK) {
    3516           0 :         fail("Could not set up the test");
    3517             :         return;
    3518             :     }
    3519             : 
    3520           5 :     data = talloc_zero(test_ctx, struct test_data);
    3521           5 :     data->ctx = test_ctx;
    3522           5 :     data->ev = test_ctx->ev;
    3523           5 :     data->uid = _i * 2;
    3524             : 
    3525           5 :     ret = test_remove_user_by_uid(data);
    3526             : 
    3527           5 :     fail_if(ret != EOK, "Could not remove user with uid %d", _i);
    3528           5 :     talloc_free(test_ctx);
    3529             : }
    3530           5 : END_TEST
    3531             : 
    3532           1 : START_TEST (test_sysdb_set_get_bool)
    3533             : {
    3534             :     struct sysdb_test_ctx *test_ctx;
    3535             :     struct ldb_dn *dn, *ne_dn;
    3536             :     bool value;
    3537             :     int ret;
    3538           1 :     const char *attr_val = "BOOL_VALUE";
    3539             : 
    3540             :     /* Setup */
    3541           1 :     ret = setup_sysdb_tests(&test_ctx);
    3542           1 :     if (ret != EOK) {
    3543           0 :         fail("Could not set up the test");
    3544             :         return;
    3545             :     }
    3546             : 
    3547           1 :     dn = sysdb_domain_dn(test_ctx, test_ctx->domain);
    3548           1 :     fail_unless(dn != NULL);
    3549             : 
    3550             :     /* attribute is not created yet */
    3551           1 :     ret = sysdb_get_bool(test_ctx->sysdb, dn, attr_val,
    3552             :                          &value);
    3553           1 :     fail_unless(ret == ENOENT,
    3554             :                 "sysdb_get_bool returned %d:[%s], but ENOENT is expected",
    3555             :                 ret, sss_strerror(ret));
    3556             : 
    3557             :     /* add attribute */
    3558           1 :     ret = sysdb_set_bool(test_ctx->sysdb, dn, test_ctx->domain->name,
    3559             :                          attr_val, true);
    3560           1 :     fail_unless(ret == EOK);
    3561             : 
    3562             :     /* successfully obtain attribute */
    3563           1 :     ret = sysdb_get_bool(test_ctx->sysdb, dn, attr_val,
    3564             :                          &value);
    3565           1 :     fail_unless(ret == EOK, "sysdb_get_bool failed %d:[%s]",
    3566             :                 ret, sss_strerror(ret));
    3567           1 :     fail_unless(value == true);
    3568             : 
    3569             :     /* use non-existing DN */
    3570           1 :     ne_dn = ldb_dn_new_fmt(test_ctx, test_ctx->sysdb->ldb, SYSDB_DOM_BASE,
    3571             :                         "non-existing domain");
    3572           1 :     fail_unless(ne_dn != NULL);
    3573           1 :     ret = sysdb_get_bool(test_ctx->sysdb, ne_dn, attr_val,
    3574             :                          &value);
    3575           1 :     fail_unless(ret == ENOENT,
    3576             :                 "sysdb_get_bool returned %d:[%s], but ENOENT is expected",
    3577             :                 ret, sss_strerror(ret));
    3578             : 
    3579             :     /* free ctx */
    3580           1 :     talloc_free(test_ctx);
    3581             : }
    3582           1 : END_TEST
    3583             : 
    3584           1 : START_TEST (test_sysdb_attrs_to_list)
    3585             : {
    3586             :     struct sysdb_attrs *attrs_list[3];
    3587             :     char **list;
    3588             :     errno_t ret;
    3589             : 
    3590           1 :     TALLOC_CTX *test_ctx = talloc_new(NULL);
    3591             : 
    3592           1 :     attrs_list[0] = sysdb_new_attrs(test_ctx);
    3593           1 :     ret = sysdb_attrs_add_string(attrs_list[0], "test_attr", "attr1");
    3594           1 :     fail_if(ret, "Add string failed");
    3595           1 :     attrs_list[1] = sysdb_new_attrs(test_ctx);
    3596           1 :     ret = sysdb_attrs_add_string(attrs_list[1], "test_attr", "attr2");
    3597           1 :     fail_if(ret, "Add string failed");
    3598           1 :     attrs_list[2] = sysdb_new_attrs(test_ctx);
    3599           1 :     ret = sysdb_attrs_add_string(attrs_list[2], "nottest_attr", "attr3");
    3600           1 :     fail_if(ret, "Add string failed");
    3601             : 
    3602           1 :     ret = sysdb_attrs_to_list(test_ctx, attrs_list, 3,
    3603             :                               "test_attr", &list);
    3604           1 :     fail_unless(ret == EOK, "sysdb_attrs_to_list failed with code %d", ret);
    3605             : 
    3606           1 :     fail_unless(strcmp(list[0],"attr1") == 0, "Expected [attr1], got [%s]",
    3607             :                                               list[0]);
    3608           1 :     fail_unless(strcmp(list[1],"attr2") == 0, "Expected [attr2], got [%s]",
    3609             :                                               list[1]);
    3610           1 :     fail_unless(list[2] == NULL, "List should be NULL-terminated");
    3611             : 
    3612           1 :     talloc_free(test_ctx);
    3613             : }
    3614           1 : END_TEST
    3615             : 
    3616           1 : START_TEST(test_sysdb_get_real_name)
    3617             : {
    3618             :     errno_t ret;
    3619             :     struct sysdb_test_ctx *test_ctx;
    3620             :     struct sysdb_attrs *user_attrs;
    3621             :     const char *str;
    3622             : 
    3623           1 :     ret = setup_sysdb_tests(&test_ctx);
    3624           1 :     fail_if(ret != EOK, "Could not set up the test");
    3625             : 
    3626           1 :     user_attrs = sysdb_new_attrs(test_ctx);
    3627           1 :     fail_unless(user_attrs != NULL, "sysdb_new_attrs failed");
    3628             : 
    3629           1 :     ret = sysdb_attrs_add_string(user_attrs, SYSDB_NAME_ALIAS, "alias");
    3630           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    3631             : 
    3632           1 :     ret = sysdb_attrs_add_string(user_attrs, SYSDB_UPN, "foo@bar");
    3633           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    3634             : 
    3635           1 :     ret = sysdb_attrs_add_string(user_attrs, SYSDB_SID_STR,
    3636             :                                  "S-1-5-21-123-456-789-111");
    3637           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    3638             : 
    3639           1 :     ret = sysdb_attrs_add_string(user_attrs, SYSDB_UUID,
    3640             :                                  "12345678-9012-3456-7890-123456789012");
    3641           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    3642             : 
    3643           1 :     ret = sysdb_store_user(test_ctx->domain, "RealName",
    3644             :                            NULL, 22345, 0, "gecos",
    3645             :                            "/home/realname", "/bin/bash",
    3646             :                            NULL, user_attrs, NULL, -1, 0);
    3647           1 :     fail_unless(ret == EOK, "sysdb_store_user failed.");
    3648             : 
    3649             :     /* Get real, uncanonicalized name as string */
    3650           1 :     ret = sysdb_get_real_name(test_ctx, test_ctx->domain, "alias", &str);
    3651           1 :     fail_unless(ret == EOK, "sysdb_get_real_name failed.");
    3652           1 :     fail_unless(strcmp(str, "RealName") == 0, "Expected [%s], got [%s].",
    3653             :                                               "RealName", str);
    3654             : 
    3655           1 :     ret = sysdb_get_real_name(test_ctx, test_ctx->domain, "foo@bar", &str);
    3656           1 :     fail_unless(ret == EOK, "sysdb_get_real_name failed.");
    3657           1 :     fail_unless(strcmp(str, "RealName") == 0, "Expected [%s], got [%s].",
    3658             :                                               "RealName", str);
    3659             : 
    3660           1 :     ret = sysdb_get_real_name(test_ctx, test_ctx->domain,
    3661             :                               "S-1-5-21-123-456-789-111", &str);
    3662           1 :     fail_unless(ret == EOK, "sysdb_get_real_name failed.");
    3663           1 :     fail_unless(strcmp(str, "RealName") == 0, "Expected [%s], got [%s].",
    3664             :                                               "RealName", str);
    3665             : 
    3666           1 :     ret = sysdb_get_real_name(test_ctx, test_ctx->domain,
    3667             :                               "12345678-9012-3456-7890-123456789012", &str);
    3668           1 :     fail_unless(ret == EOK, "sysdb_get_real_name failed.");
    3669           1 :     fail_unless(strcmp(str, "RealName") == 0, "Expected [%s], got [%s].",
    3670             :                                               "RealName", str);
    3671             : }
    3672           1 : END_TEST
    3673             : 
    3674           1 : START_TEST(test_group_rename)
    3675             : {
    3676             :     struct sysdb_test_ctx *test_ctx;
    3677             :     errno_t ret;
    3678             :     gid_t gid;
    3679           1 :     const gid_t grgid = 38001;
    3680             :     const char *name;
    3681           1 :     const char *fromname = "fromgroup";
    3682           1 :     const char *toname = "togroup";
    3683             :     struct ldb_result *res;
    3684             : 
    3685             :     /* Setup */
    3686           1 :     ret = setup_sysdb_tests(&test_ctx);
    3687           1 :     fail_unless(ret == EOK, "Could not set up the test");
    3688             : 
    3689             :     /* Store and verify the first group */
    3690           1 :     ret = sysdb_store_group(test_ctx->domain,
    3691             :                             fromname, grgid, NULL, 0, 0);
    3692           1 :     fail_unless(ret == EOK, "Could not add first group");
    3693             : 
    3694           1 :     ret = sysdb_getgrnam(test_ctx, test_ctx->domain, fromname, &res);
    3695           1 :     fail_unless(ret == EOK, "Could not retrieve the group from cache\n");
    3696           1 :     if (res->count != 1) {
    3697           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
    3698             :         goto done;
    3699             :     }
    3700             : 
    3701           1 :     gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0);
    3702           1 :     fail_unless(gid == grgid,
    3703             :                 "Did not find the expected GID (found %llu expected %llu)",
    3704             :                 (unsigned long long) gid, (unsigned long long) grgid);
    3705           1 :     name = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    3706           1 :     fail_unless(strcmp(fromname, name) == 0,
    3707             :                 "Did not find the expected name (found %s expected %s)",
    3708             :                 name, fromname);
    3709             : 
    3710             :     /* Perform rename and check that GID is the same, but name changed */
    3711           1 :     ret = sysdb_add_group(test_ctx->domain, toname, grgid, NULL, 0, 0);
    3712           1 :     fail_unless(ret == EEXIST, "Group renamed with a low level call?");
    3713             : 
    3714           1 :     ret = sysdb_store_group(test_ctx->domain,
    3715             :                             toname, grgid, NULL, 0, 0);
    3716           1 :     fail_unless(ret == EOK, "Could not add first group");
    3717             : 
    3718           1 :     ret = sysdb_getgrnam(test_ctx, test_ctx->domain, toname, &res);
    3719           1 :     fail_unless(ret == EOK, "Could not retrieve the group from cache\n");
    3720           1 :     if (res->count != 1) {
    3721           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
    3722             :         goto done;
    3723             :     }
    3724             : 
    3725           1 :     gid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_GIDNUM, 0);
    3726           1 :     fail_unless(gid == grgid,
    3727             :                 "Did not find the expected GID (found %llu expected %llu)",
    3728             :                 (unsigned long long) gid, (unsigned long long) grgid);
    3729           1 :     name = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    3730           1 :     fail_unless(strcmp(toname, name) == 0,
    3731             :                 "Did not find the expected GID (found %s expected %s)",
    3732             :                 name, toname);
    3733             : 
    3734             :     /* Verify the first name is gone */
    3735           1 :     ret = sysdb_getgrnam(test_ctx, test_ctx->domain, fromname, &res);
    3736           1 :     fail_unless(ret == EOK, "Could not retrieve the group from cache\n");
    3737           1 :     fail_unless(res->count == 0, "Unexpectedly found the original user\n");
    3738             : 
    3739             : done:
    3740           1 :     talloc_free(test_ctx);
    3741             : }
    3742           1 : END_TEST
    3743             : 
    3744           1 : START_TEST(test_user_rename)
    3745             : {
    3746             :     struct sysdb_test_ctx *test_ctx;
    3747             :     errno_t ret;
    3748             :     uid_t uid;
    3749           1 :     const uid_t userid = 38002;
    3750             :     const char *name;
    3751           1 :     const char *fromname = "fromuser";
    3752           1 :     const char *toname = "touser";
    3753             :     struct ldb_result *res;
    3754             : 
    3755             :     /* Setup */
    3756           1 :     ret = setup_sysdb_tests(&test_ctx);
    3757           1 :     fail_unless(ret == EOK, "Could not set up the test");
    3758             : 
    3759             :     /* Store and verify the first user */
    3760           1 :     ret = sysdb_store_user(test_ctx->domain,
    3761             :                            fromname, NULL, userid, 0,
    3762             :                            fromname, "/", "/bin/sh",
    3763             :                            NULL, NULL, NULL, 0, 0);
    3764           1 :     fail_unless(ret == EOK, "Could not add first user");
    3765             : 
    3766           1 :     ret = sysdb_getpwnam(test_ctx, test_ctx->domain, fromname, &res);
    3767           1 :     fail_unless(ret == EOK, "Could not retrieve the user from cache\n");
    3768           1 :     if (res->count != 1) {
    3769           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
    3770             :         goto done;
    3771             :     }
    3772             : 
    3773           1 :     uid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_UIDNUM, 0);
    3774           1 :     fail_unless(uid == userid,
    3775             :                 "Did not find the expected UID (found %llu expected %llu)",
    3776             :                 (unsigned long long) uid, (unsigned long long) userid);
    3777           1 :     name = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    3778           1 :     fail_unless(strcmp(fromname, name) == 0,
    3779             :                 "Did not find the expected name (found %s expected %s)",
    3780             :                 name, fromname);
    3781             : 
    3782             :     /* Perform rename and check that GID is the same, but name changed */
    3783           1 :     ret = sysdb_add_user(test_ctx->domain, toname, userid, 0,
    3784             :                          fromname, "/", "/bin/sh", NULL, NULL, 0, 0);
    3785           1 :     fail_unless(ret == EEXIST, "A second user added with low level call?");
    3786             : 
    3787           1 :     ret = sysdb_store_user(test_ctx->domain, toname, NULL,
    3788             :                            userid, 0, fromname, "/", "/bin/sh",
    3789             :                            NULL, NULL, NULL, 0, 0);
    3790           1 :     fail_unless(ret == EOK, "Could not add second user");
    3791             : 
    3792           1 :     ret = sysdb_getpwnam(test_ctx, test_ctx->domain, toname, &res);
    3793           1 :     fail_unless(ret == EOK, "Could not retrieve the user from cache\n");
    3794           1 :     if (res->count != 1) {
    3795           0 :         fail("Invalid number of replies. Expected 1, got %d", res->count);
    3796             :         goto done;
    3797             :     }
    3798             : 
    3799           1 :     uid = ldb_msg_find_attr_as_uint(res->msgs[0], SYSDB_UIDNUM, 0);
    3800           1 :     fail_unless(uid == userid,
    3801             :                 "Did not find the expected UID (found %llu expected %llu)",
    3802             :                 (unsigned long long) uid, (unsigned long long) userid);
    3803           1 :     name = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    3804           1 :     fail_unless(strcmp(toname, name) == 0,
    3805             :                 "Did not find the expected name (found %s expected %s)",
    3806             :                 name, fromname);
    3807             : 
    3808             :     /* Verify the first name is gone */
    3809           1 :     ret = sysdb_getpwnam(test_ctx, test_ctx->domain, fromname, &res);
    3810           1 :     fail_unless(ret == EOK, "Could not retrieve the user from cache\n");
    3811           1 :     fail_unless(res->count == 0, "Unexpectedly found the original user\n");
    3812             : 
    3813             : done:
    3814           1 :     talloc_free(test_ctx);
    3815             : }
    3816           1 : END_TEST
    3817             : 
    3818           1 : START_TEST (test_sysdb_update_members)
    3819             : {
    3820             :     struct sysdb_test_ctx *test_ctx;
    3821             :     char **add_groups;
    3822             :     char **del_groups;
    3823           1 :     const char *user = "testuser27000";
    3824             :     errno_t ret;
    3825             : 
    3826             :     /* Setup */
    3827           1 :     ret = setup_sysdb_tests(&test_ctx);
    3828           1 :     fail_unless(ret == EOK, "Could not set up the test");
    3829             : 
    3830             :     /* Add a user to two groups */
    3831           1 :     add_groups = talloc_array(test_ctx, char *, 3);
    3832           1 :     add_groups[0] = talloc_strdup(add_groups, "testgroup28001");
    3833           1 :     add_groups[1] = talloc_strdup(add_groups, "testgroup28002");
    3834           1 :     add_groups[2] = NULL;
    3835             : 
    3836           1 :     ret = sysdb_update_members(test_ctx->domain, user, SYSDB_MEMBER_USER,
    3837             :                                (const char *const *)add_groups, NULL);
    3838           1 :     fail_unless(ret == EOK, "Could not add groups");
    3839           1 :     talloc_zfree(add_groups);
    3840             : 
    3841             :     /* Remove a user from one group and add to another */
    3842           1 :     del_groups = talloc_array(test_ctx, char *, 2);
    3843           1 :     del_groups[0] = talloc_strdup(del_groups, "testgroup28001");
    3844           1 :     del_groups[1] = NULL;
    3845           1 :     add_groups = talloc_array(test_ctx, char *, 2);
    3846           1 :     add_groups[0] = talloc_strdup(add_groups, "testgroup28003");
    3847           1 :     add_groups[1] = NULL;
    3848             : 
    3849           1 :     ret = sysdb_update_members(test_ctx->domain, user, SYSDB_MEMBER_USER,
    3850             :                                (const char *const *)add_groups,
    3851             :                                (const char *const *)del_groups);
    3852           1 :     fail_unless(ret == EOK, "Group replace failed");
    3853           1 :     talloc_zfree(add_groups);
    3854           1 :     talloc_zfree(del_groups);
    3855             : 
    3856             :     /* Remove a user from two groups */
    3857           1 :     del_groups = talloc_array(test_ctx, char *, 3);
    3858           1 :     del_groups[0] = talloc_strdup(del_groups, "testgroup28002");
    3859           1 :     del_groups[1] = talloc_strdup(del_groups, "testgroup28003");
    3860           1 :     del_groups[2] = NULL;
    3861             : 
    3862           1 :     ret = sysdb_update_members(test_ctx->domain, user, SYSDB_MEMBER_USER,
    3863             :                                NULL, (const char *const *)del_groups);
    3864           1 :     fail_unless(ret == EOK, "Could not remove groups");
    3865             : 
    3866           1 :     talloc_zfree(test_ctx);
    3867             : }
    3868           1 : END_TEST
    3869             : 
    3870             : 
    3871          10 : START_TEST (test_sysdb_group_dn_name)
    3872             : {
    3873             :     struct sysdb_test_ctx *test_ctx;
    3874             :     int ret;
    3875             :     struct ldb_dn *group_dn;
    3876             :     const char *groupname;
    3877             :     char *parsed;
    3878             : 
    3879             :     /* Setup */
    3880          10 :     ret = setup_sysdb_tests(&test_ctx);
    3881          10 :     if (ret != EOK) {
    3882           0 :         fail("Could not set up the test");
    3883             :         return;
    3884             :     }
    3885             : 
    3886          10 :     groupname = talloc_asprintf(test_ctx, "testgroup%d", _i);
    3887          10 :     group_dn = sysdb_group_dn(test_ctx, test_ctx->domain, groupname);
    3888          10 :     if (!group_dn || !groupname) {
    3889           0 :         fail("Out of memory");
    3890             :         return;
    3891             :     }
    3892             : 
    3893          10 :     ret = sysdb_group_dn_name(test_ctx->sysdb, test_ctx,
    3894             :                               ldb_dn_get_linearized(group_dn), &parsed);
    3895          10 :     fail_if(ret != EOK, "Cannot get the group name from DN");
    3896             : 
    3897          10 :     fail_if(strcmp(groupname, parsed) != 0,
    3898             :             "Names don't match (got %s)", parsed);
    3899          10 :     talloc_free(test_ctx);
    3900             : }
    3901          10 : END_TEST
    3902             : 
    3903          10 : START_TEST (test_sysdb_add_basic_netgroup)
    3904             : {
    3905             :     struct sysdb_test_ctx *test_ctx;
    3906             :     struct test_data *data;
    3907             :     int ret;
    3908             : 
    3909             :     /* Setup */
    3910          10 :     ret = setup_sysdb_tests(&test_ctx);
    3911          10 :     if (ret != EOK) {
    3912           0 :         fail("Could not set up the test");
    3913             :         return;
    3914             :     }
    3915             : 
    3916          10 :     data = talloc_zero(test_ctx, struct test_data);
    3917          10 :     data->ctx = test_ctx;
    3918          10 :     data->ev = test_ctx->ev;
    3919          10 :     data->uid = _i;         /* This is kinda abuse of uid, though */
    3920          10 :     data->netgrname = talloc_asprintf(data, "testnetgr%d", _i);
    3921             : 
    3922          10 :     ret = test_add_basic_netgroup(data);
    3923             : 
    3924          10 :     fail_if(ret != EOK, "Could not add netgroup %s", data->netgrname);
    3925          10 :     talloc_free(test_ctx);
    3926             : }
    3927          10 : END_TEST
    3928             : 
    3929          10 : START_TEST (test_sysdb_search_netgroup_by_name)
    3930             : {
    3931             :     struct sysdb_test_ctx *test_ctx;
    3932             :     int ret;
    3933             :     const char *netgrname;
    3934             :     struct ldb_message *msg;
    3935             :     struct ldb_dn *netgroup_dn;
    3936             : 
    3937             :     /* Setup */
    3938          10 :     ret = setup_sysdb_tests(&test_ctx);
    3939          10 :     if (ret != EOK) {
    3940           0 :         fail("Could not set up the test");
    3941             :         return;
    3942             :     }
    3943             : 
    3944          10 :     netgrname = talloc_asprintf(test_ctx, "testnetgr%d", _i);
    3945             : 
    3946          10 :     ret = sysdb_search_netgroup_by_name(test_ctx, test_ctx->domain,
    3947             :                                         netgrname, NULL, &msg);
    3948          10 :     fail_if(ret != EOK, "Could not find netgroup with name %s", netgrname);
    3949             : 
    3950          10 :     netgroup_dn = sysdb_netgroup_dn(test_ctx, test_ctx->domain, netgrname);
    3951          10 :     fail_if(netgroup_dn == NULL);
    3952          10 :     fail_if(ldb_dn_compare(msg->dn, netgroup_dn) != 0, "Found wrong netgroup!\n");
    3953          10 :     talloc_free(test_ctx);
    3954             : }
    3955          10 : END_TEST
    3956             : 
    3957           5 : START_TEST (test_sysdb_remove_netgroup_entry)
    3958             : {
    3959             :     struct sysdb_test_ctx *test_ctx;
    3960             :     struct test_data *data;
    3961             :     int ret;
    3962             : 
    3963             :     /* Setup */
    3964           5 :     ret = setup_sysdb_tests(&test_ctx);
    3965           5 :     if (ret != EOK) {
    3966           0 :         fail("Could not set up the test");
    3967             :         return;
    3968             :     }
    3969             : 
    3970           5 :     data = talloc_zero(test_ctx, struct test_data);
    3971           5 :     data->ctx = test_ctx;
    3972           5 :     data->ev = test_ctx->ev;
    3973           5 :     data->netgrname = talloc_asprintf(data, "testnetgr%d", _i);
    3974             : 
    3975           5 :     ret = test_remove_netgroup_entry(data);
    3976             : 
    3977           5 :     fail_if(ret != EOK, "Could not remove netgroup %s", data->netgrname);
    3978           5 :     talloc_free(test_ctx);
    3979             : }
    3980           5 : END_TEST
    3981             : 
    3982           5 : START_TEST (test_sysdb_remove_netgroup_by_name)
    3983             : {
    3984             :     struct sysdb_test_ctx *test_ctx;
    3985             :     struct test_data *data;
    3986             :     int ret;
    3987             : 
    3988             :     /* Setup */
    3989           5 :     ret = setup_sysdb_tests(&test_ctx);
    3990           5 :     if (ret != EOK) {
    3991           0 :         fail("Could not set up the test");
    3992             :         return;
    3993             :     }
    3994             : 
    3995           5 :     data = talloc_zero(test_ctx, struct test_data);
    3996           5 :     data->ctx = test_ctx;
    3997           5 :     data->ev = test_ctx->ev;
    3998           5 :     data->netgrname = talloc_asprintf(data, "testnetgr%d", _i);
    3999             : 
    4000           5 :     ret = test_remove_netgroup_by_name(data);
    4001             : 
    4002           5 :     fail_if(ret != EOK, "Could not remove netgroup with name %s", data->netgrname);
    4003           5 :     talloc_free(test_ctx);
    4004             : }
    4005           5 : END_TEST
    4006             : 
    4007          10 : START_TEST (test_sysdb_set_netgroup_attr)
    4008             : {
    4009             :     struct sysdb_test_ctx *test_ctx;
    4010             :     struct test_data *data;
    4011             :     int ret;
    4012             : 
    4013             :     /* Setup */
    4014          10 :     ret = setup_sysdb_tests(&test_ctx);
    4015          10 :     if (ret != EOK) {
    4016           0 :         fail("Could not set up the test");
    4017             :         return;
    4018             :     }
    4019             : 
    4020          10 :     data = talloc_zero(test_ctx, struct test_data);
    4021          10 :     data->ctx = test_ctx;
    4022          10 :     data->ev = test_ctx->ev;
    4023          10 :     data->uid = _i;         /* This is kinda abuse of uid, though */
    4024          10 :     data->netgrname = talloc_asprintf(data, "testnetgr%d", _i);
    4025             : 
    4026          10 :     ret = test_set_netgroup_attr(data);
    4027             : 
    4028          10 :     fail_if(ret != EOK, "Could not set netgroup attribute %s", data->netgrname);
    4029          10 :     talloc_free(test_ctx);
    4030             : }
    4031          10 : END_TEST
    4032             : 
    4033          10 : START_TEST (test_sysdb_get_netgroup_attr)
    4034             : {
    4035             :     struct sysdb_test_ctx *test_ctx;
    4036             :     int ret;
    4037             :     const char *description;
    4038             :     const char *netgrname;
    4039             :     struct ldb_result *res;
    4040          10 :     const char *attrs[] = { SYSDB_DESCRIPTION, NULL };
    4041             :     const char *attrval;
    4042             : 
    4043             :     /* Setup */
    4044          10 :     ret = setup_sysdb_tests(&test_ctx);
    4045          10 :     if (ret != EOK) {
    4046           0 :         fail("Could not set up the test");
    4047             :         return;
    4048             :     }
    4049             : 
    4050          10 :     description = talloc_asprintf(test_ctx, "Sysdb Netgroup %d", _i);
    4051          10 :     netgrname = talloc_asprintf(test_ctx, "testnetgr%d", _i);
    4052             : 
    4053          10 :     ret = sysdb_get_netgroup_attr(test_ctx, test_ctx->domain, netgrname,
    4054             :                                   attrs, &res);
    4055             : 
    4056          10 :     fail_if(ret != EOK, "Could not get netgroup attributes");
    4057          10 :     fail_if(res->count != 1,
    4058             :             "Invalid number of entries, expected 1, got %d", res->count);
    4059             : 
    4060          10 :     attrval = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_DESCRIPTION, 0);
    4061          10 :     fail_if(strcmp(attrval, description),
    4062             :             "Got bad attribute value for netgroup %s", netgrname);
    4063          10 :     talloc_free(test_ctx);
    4064             : }
    4065          10 : END_TEST
    4066             : 
    4067           1 : START_TEST (test_netgroup_base_dn)
    4068             : {
    4069             :     errno_t ret;
    4070             :     struct sysdb_test_ctx *test_ctx;
    4071             :     struct ldb_dn *base_dn;
    4072             :     const char *strdn;
    4073             : 
    4074           1 :     ret = setup_sysdb_tests(&test_ctx);
    4075           1 :     fail_if(ret != EOK, "Could not set up the test");
    4076             : 
    4077           1 :     base_dn = sysdb_netgroup_base_dn(test_ctx, test_ctx->domain);
    4078           1 :     fail_if(base_dn == NULL, "Could not get netgroup base DN");
    4079             : 
    4080           1 :     strdn = ldb_dn_get_linearized(base_dn);
    4081           1 :     fail_if(strdn == NULL, "Could not get string netgroup base DN");
    4082             : 
    4083           1 :     fail_if(strstr(strdn, SYSDB_NETGROUP_CONTAINER) != strdn,
    4084             :             "Malformed netgroup baseDN");
    4085             : }
    4086           1 : END_TEST
    4087             : 
    4088           1 : START_TEST(test_odd_characters)
    4089             : {
    4090             :     errno_t ret;
    4091             :     struct sysdb_test_ctx *test_ctx;
    4092             :     struct ldb_result *res;
    4093             :     struct ldb_message *msg;
    4094             :     const struct ldb_val *val;
    4095           1 :     const char odd_username[] = "*(odd)\\user,name";
    4096           1 :     const char odd_username_orig_dn[] =
    4097             :         "\\2a\\28odd\\29\\5cuser,name,cn=users,dc=example,dc=com";
    4098           1 :     const char odd_groupname[] = "*(odd\\*)\\group,name";
    4099           1 :     const char odd_netgroupname[] = "*(odd\\*)\\netgroup,name";
    4100             :     const char *received_user;
    4101             :     const char *received_group;
    4102             :     static const char *user_attrs[] = SYSDB_PW_ATTRS;
    4103             :     static const char *netgr_attrs[] = SYSDB_NETGR_ATTRS;
    4104             : 
    4105             :     /* Setup */
    4106           1 :     ret = setup_sysdb_tests(&test_ctx);
    4107           1 :     if (ret != EOK) {
    4108           0 :         fail("Could not set up the test");
    4109             :         return;
    4110             :     }
    4111             : 
    4112             :     /* ===== Groups ===== */
    4113             : 
    4114             :     /* Add */
    4115           1 :     ret = sysdb_add_incomplete_group(test_ctx->domain, odd_groupname,
    4116             :                                      20000, NULL, NULL, NULL, true, 0);
    4117           1 :     fail_unless(ret == EOK, "sysdb_add_incomplete_group error [%d][%s]",
    4118             :                             ret, strerror(ret));
    4119             : 
    4120             :     /* Retrieve */
    4121           1 :     ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
    4122             :                                      odd_groupname, NULL, &msg);
    4123           1 :     fail_unless(ret == EOK, "sysdb_search_group_by_name error [%d][%s]",
    4124             :                             ret, strerror(ret));
    4125           1 :     talloc_zfree(msg);
    4126             : 
    4127           1 :     ret = sysdb_getgrnam(test_ctx, test_ctx->domain, odd_groupname, &res);
    4128           1 :     fail_unless(ret == EOK, "sysdb_getgrnam error [%d][%s]",
    4129             :                             ret, strerror(ret));
    4130           1 :     fail_unless(res->count == 1, "Received [%d] responses",
    4131             :                                  res->count);
    4132           1 :     received_group = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    4133           1 :     fail_unless(strcmp(received_group, odd_groupname) == 0,
    4134             :                 "Expected [%s], got [%s]",
    4135             :                 odd_groupname, received_group);
    4136           1 :     talloc_free(res);
    4137             : 
    4138             : 
    4139             :     /* ===== Users ===== */
    4140             : 
    4141             :     /* Add */
    4142           1 :     ret = sysdb_add_basic_user(test_ctx->domain,
    4143             :                                odd_username,
    4144             :                                10000, 10000,
    4145             :                                "","","");
    4146           1 :     fail_unless(ret == EOK, "sysdb_add_basic_user error [%d][%s]",
    4147             :                             ret, strerror(ret));
    4148             : 
    4149             :     /* Retrieve */
    4150           1 :     ret = sysdb_search_user_by_name(test_ctx,
    4151           1 :                                     test_ctx->domain,
    4152             :                                     odd_username, NULL, &msg);
    4153           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_name error [%d][%s]",
    4154             :                             ret, strerror(ret));
    4155           1 :     val = ldb_dn_get_component_val(msg->dn, 0);
    4156           1 :     fail_unless(strcmp((char *)val->data, odd_username)==0,
    4157             :                 "Expected [%s] got [%s]\n",
    4158             :                 odd_username, (char *)val->data);
    4159           1 :     talloc_zfree(msg);
    4160             : 
    4161             :     /* Add to the group */
    4162           1 :     ret = sysdb_add_group_member(test_ctx->domain,
    4163             :                                  odd_groupname, odd_username,
    4164             :                                  SYSDB_MEMBER_USER, false);
    4165           1 :     fail_unless(ret == EOK, "sysdb_add_group_member error [%d][%s]",
    4166             :                             ret, strerror(ret));
    4167             : 
    4168           1 :     ret = sysdb_getpwnam(test_ctx, test_ctx->domain, odd_username, &res);
    4169           1 :     fail_unless(ret == EOK, "sysdb_getpwnam error [%d][%s]",
    4170             :                             ret, strerror(ret));
    4171           1 :     fail_unless(res->count == 1, "Received [%d] responses",
    4172             :                                  res->count);
    4173           1 :     received_user = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL);
    4174           1 :     fail_unless(strcmp(received_user, odd_username) == 0,
    4175             :                 "Expected [%s], got [%s]",
    4176             :                 odd_username, received_user);
    4177           1 :     talloc_zfree(res);
    4178             : 
    4179             :     /* Attributes */
    4180           1 :     ret = sysdb_get_user_attr(test_ctx, test_ctx->domain, odd_username,
    4181             :                               user_attrs, &res);
    4182           1 :     fail_unless(ret == EOK, "sysdb_get_user_attr error [%d][%s]",
    4183             :                             ret, strerror(ret));
    4184           1 :     talloc_free(res);
    4185             : 
    4186             :     /* Delete User */
    4187           1 :     ret = sysdb_delete_user(test_ctx->domain, odd_username, 10000);
    4188           1 :     fail_unless(ret == EOK, "sysdb_delete_user error [%d][%s]",
    4189             :                             ret, strerror(ret));
    4190             : 
    4191             :     /* Delete non existing User */
    4192           1 :     ret = sysdb_delete_user(test_ctx->domain, odd_username, 10000);
    4193           1 :     fail_unless(ret == ENOENT, "sysdb_delete_user error [%d][%s]",
    4194             :                                ret, strerror(ret));
    4195             : 
    4196             :     /* Delete Group */
    4197           1 :     ret = sysdb_delete_group(test_ctx->domain, odd_groupname, 20000);
    4198           1 :     fail_unless(ret == EOK, "sysdb_delete_group error [%d][%s]",
    4199             :                             ret, strerror(ret));
    4200             : 
    4201             :     /* Add */
    4202           1 :     ret = sysdb_add_user(test_ctx->domain,
    4203             :                          odd_username,
    4204             :                          10000, 0,
    4205             :                          "","","",
    4206             :                          odd_username_orig_dn,
    4207             :                          NULL, 5400, 0);
    4208           1 :     fail_unless(ret == EOK, "sysdb_add_user error [%d][%s]",
    4209             :                             ret, strerror(ret));
    4210             : 
    4211             :     /* Delete User */
    4212           1 :     ret = sysdb_delete_user(test_ctx->domain, odd_username, 10000);
    4213           1 :     fail_unless(ret == EOK, "sysdb_delete_user error [%d][%s]",
    4214             :                             ret, strerror(ret));
    4215             : 
    4216             :     /* ===== Netgroups ===== */
    4217             :     /* Add */
    4218           1 :     ret = sysdb_add_netgroup(test_ctx->domain,
    4219             :                              odd_netgroupname, "No description",
    4220             :                              NULL, NULL, 30, 0);
    4221           1 :     fail_unless(ret == EOK, "sysdb_add_netgroup error [%d][%s]",
    4222             :                             ret, strerror(ret));
    4223             : 
    4224             :     /* Retrieve */
    4225           1 :     ret = sysdb_getnetgr(test_ctx, test_ctx->domain, odd_netgroupname, &res);
    4226           1 :     fail_unless(ret == EOK, "sysdb_getnetgr error [%d][%s]",
    4227             :                             ret, strerror(ret));
    4228           1 :     fail_unless(res->count == 1, "Received [%d] responses",
    4229             :                                  res->count);
    4230           1 :     talloc_zfree(res);
    4231             : 
    4232           1 :     ret = sysdb_get_netgroup_attr(test_ctx, test_ctx->domain,
    4233             :                                   odd_netgroupname, netgr_attrs, &res);
    4234           1 :     fail_unless(ret == EOK, "sysdb_get_netgroup_attr error [%d][%s]",
    4235             :                             ret, strerror(ret));
    4236           1 :     fail_unless(res->count == 1, "Received [%d] responses",
    4237             :                                  res->count);
    4238           1 :     talloc_zfree(res);
    4239             : 
    4240             :     /* ===== Arbitrary Entries ===== */
    4241             : 
    4242           1 :     talloc_free(test_ctx);
    4243             : }
    4244           1 : END_TEST
    4245             : 
    4246           1 : START_TEST(test_SSS_LDB_SEARCH)
    4247             : {
    4248             :     errno_t ret;
    4249             :     struct sysdb_test_ctx *test_ctx;
    4250             :     struct ldb_dn *group_dn, *nonexist_dn;
    4251             :     struct ldb_result *res;
    4252           1 :     const char groupname[] = "test_group";
    4253             :     const char *received_group;
    4254             : 
    4255             :     /* Setup */
    4256           1 :     ret = setup_sysdb_tests(&test_ctx);
    4257           1 :     if (ret != EOK) {
    4258           0 :         fail("Could not set up the test");
    4259             :         return;
    4260             :     }
    4261             : 
    4262           1 :     check_leaks_push(test_ctx);
    4263             : 
    4264           1 :     group_dn = sysdb_group_dn(test_ctx, test_ctx->domain, groupname);
    4265           1 :     fail_if(group_dn == NULL, "sysdb_group_dn failed");
    4266             : 
    4267           1 :     nonexist_dn = sysdb_group_dn(test_ctx, test_ctx->domain,
    4268             :                                  "non-existing-group");
    4269           1 :     fail_if(nonexist_dn == NULL, "sysdb_group_dn failed");
    4270             : 
    4271             :     /* Add */
    4272           1 :     ret = sysdb_add_incomplete_group(test_ctx->domain, groupname,
    4273             :                                      20000, NULL, NULL, NULL, true, 0);
    4274           1 :     fail_unless(ret == EOK, "sysdb_add_incomplete_group error [%d][%s]",
    4275             :                 ret, strerror(ret));
    4276             : 
    4277             :     /* Retrieve */
    4278             : 
    4279             :     /* Empty filter */
    4280           1 :     SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
    4281             :                    LDB_SCOPE_BASE, NULL, NULL);
    4282             : 
    4283           1 :     fail_unless(ret == EOK, "SSS_LDB_SEARCH error [%d][%s]",
    4284             :                 ret, strerror(ret));
    4285             : 
    4286           1 :     fail_unless(res->count == 1, "Received [%d] responses",
    4287             :                                  res->count);
    4288             : 
    4289           1 :     received_group = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME,
    4290             :                                                  NULL);
    4291           1 :     fail_unless(strcmp(received_group, groupname) == 0,
    4292             :                 "Expected [%s], got [%s]", groupname, received_group);
    4293             : 
    4294           1 :     talloc_zfree(res);
    4295             : 
    4296             :     /* Non-empty filter */
    4297           1 :     SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
    4298             :                    LDB_SCOPE_BASE, NULL, "objectClass=group");
    4299             : 
    4300           1 :     fail_unless(ret == EOK, "SSS_LDB_SEARCH error [%d][%s]",
    4301             :                 ret, strerror(ret));
    4302           1 :     talloc_zfree(res);
    4303             : 
    4304             :     /* Filter yeilding no results */
    4305           1 :     SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, group_dn,
    4306             :                    LDB_SCOPE_BASE, NULL,
    4307             :                    "objectClass=nonExistingObjectClass");
    4308             : 
    4309           1 :     fail_unless(ret == ENOENT, "sss_ldb_search error [%d][%s]",
    4310             :                 ret, strerror(ret));
    4311           1 :     talloc_zfree(res);
    4312             : 
    4313             :     /* Non-existing dn */
    4314           1 :     SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, nonexist_dn,
    4315             :                    LDB_SCOPE_BASE, NULL, NULL);
    4316             : 
    4317           1 :     fail_unless(ret == ENOENT, "SSS_LDB_SEARCH error [%d][%s]",
    4318             :                 ret, strerror(ret));
    4319           1 :     talloc_zfree(res);
    4320             : 
    4321           1 :     talloc_zfree(nonexist_dn);
    4322           1 :     talloc_zfree(group_dn);
    4323           1 :     fail_unless(check_leaks_pop(test_ctx) == true, "Memory leak");
    4324             : }
    4325           1 : END_TEST
    4326             : 
    4327             : /* == SERVICE TESTS == */
    4328          12 : void services_check_match(struct sysdb_test_ctx *test_ctx,
    4329             :                           bool by_name,
    4330             :                           const char *primary_name,
    4331             :                           int port,
    4332             :                           const char **aliases,
    4333             :                           const char **protocols)
    4334             : {
    4335             :     errno_t ret;
    4336             :     unsigned int i, j;
    4337             :     bool matched;
    4338             :     const char *ret_name;
    4339             :     int ret_port;
    4340             :     struct ldb_result *res;
    4341             :     struct ldb_message *msg;
    4342             :     struct ldb_message_element *el;
    4343             : 
    4344          12 :     if (by_name) {
    4345             :         /* Look up the service by name */
    4346           6 :         ret = sysdb_getservbyname(test_ctx, test_ctx->domain, primary_name,
    4347             :                                   NULL, &res);
    4348           6 :         fail_if(ret != EOK, "sysdb_getservbyname error [%s]\n",
    4349             :                              strerror(ret));
    4350             :     } else {
    4351             :         /* Look up the newly-added service by port */
    4352           6 :         ret = sysdb_getservbyport(test_ctx, test_ctx->domain, port, NULL,
    4353             :                                   &res);
    4354           6 :         fail_if(ret != EOK, "sysdb_getservbyport error [%s]\n",
    4355             :                              strerror(ret));
    4356             :     }
    4357          12 :     fail_if(res == NULL, "ENOMEM");
    4358          12 :     fail_if(res->count != 1);
    4359             : 
    4360             :     /* Make sure the returned entry matches */
    4361          12 :     msg = res->msgs[0];
    4362          12 :     ret_name = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    4363          12 :     fail_if(ret_name == NULL);
    4364          12 :     fail_unless(strcmp(ret_name, primary_name) == 0);
    4365             : 
    4366          12 :     ret_port = ldb_msg_find_attr_as_int(msg, SYSDB_SVC_PORT, 0);
    4367          12 :     fail_if (ret_port != port);
    4368             : 
    4369          12 :     el = ldb_msg_find_element(msg, SYSDB_NAME_ALIAS);
    4370          34 :     for (i = 0; i < el->num_values; i++) {
    4371          22 :         matched = false;
    4372          64 :         for (j = 0; aliases[j]; j++) {
    4373          42 :             if (strcmp(aliases[j], (const char *)el->values[i].data) == 0) {
    4374          22 :                 matched = true;
    4375             :             }
    4376             :         }
    4377          22 :         fail_if(!matched, "Unexpected value in LDB entry: [%s]",
    4378             :                 (const char *)el->values[i].data);
    4379             :     }
    4380             : 
    4381          12 :     el = ldb_msg_find_element(msg, SYSDB_SVC_PROTO);
    4382          36 :     for (i = 0; i < el->num_values; i++) {
    4383          24 :         matched = false;
    4384          72 :         for (j = 0; protocols[j]; j++) {
    4385          48 :             if (strcmp(protocols[j], (const char *)el->values[i].data) == 0) {
    4386          24 :                 matched = true;
    4387             :             }
    4388             :         }
    4389          24 :         fail_if(!matched, "Unexpected value in LDB entry: [%s]",
    4390             :                 (const char *)el->values[i].data);
    4391             :     }
    4392          12 : }
    4393             : 
    4394             : #define services_check_match_name(test_ctx, primary_name, port, aliases, protocols) \
    4395             :     do { \
    4396             :         services_check_match(test_ctx, true, primary_name, port, aliases, protocols); \
    4397             :     } while(0);
    4398             : 
    4399             : #define services_check_match_port(test_ctx, primary_name, port, aliases, protocols) \
    4400             :     do { \
    4401             :         services_check_match(test_ctx, false, primary_name, port, aliases, protocols); \
    4402             :     } while(0);
    4403             : 
    4404           1 : START_TEST(test_sysdb_add_services)
    4405             : {
    4406             :     errno_t ret;
    4407             :     struct sysdb_test_ctx *test_ctx;
    4408             :     char *primary_name;
    4409             :     const char **aliases;
    4410             :     const char **protocols;
    4411           1 :     int port = 3890;
    4412             : 
    4413             :     /* Setup */
    4414           1 :     ret = setup_sysdb_tests(&test_ctx);
    4415           1 :     fail_if(ret != EOK, "Could not set up the test");
    4416             : 
    4417           1 :     primary_name = talloc_asprintf(test_ctx, "test_service");
    4418           1 :     fail_if(primary_name == NULL);
    4419             : 
    4420           1 :     aliases = talloc_array(test_ctx, const char *, 3);
    4421           1 :     fail_if(aliases == NULL);
    4422             : 
    4423           1 :     aliases[0] = talloc_asprintf(aliases, "test_service_alias1");
    4424           1 :     fail_if(aliases[0] == NULL);
    4425             : 
    4426           1 :     aliases[1] = talloc_asprintf(aliases, "test_service_alias2");
    4427           1 :     fail_if(aliases[1] == NULL);
    4428             : 
    4429           1 :     aliases[2] = NULL;
    4430             : 
    4431           1 :     protocols = talloc_array(test_ctx, const char *, 3);
    4432           1 :     fail_if(protocols == NULL);
    4433             : 
    4434           1 :     protocols[0] = talloc_asprintf(protocols, "tcp");
    4435           1 :     fail_if(protocols[0] == NULL);
    4436             : 
    4437           1 :     protocols[1] = talloc_asprintf(protocols, "udp");
    4438           1 :     fail_if(protocols[1] == NULL);
    4439             : 
    4440           1 :     protocols[2] = NULL;
    4441             : 
    4442           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    4443           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4444             : 
    4445           1 :     ret = sysdb_svc_add(NULL, test_ctx->domain,
    4446             :                         primary_name, port,
    4447             :                         aliases, protocols,
    4448             :                         NULL);
    4449           1 :     fail_unless(ret == EOK, "sysdb_svc_add error [%s]\n", strerror(ret));
    4450             : 
    4451             :     /* Search by name and make sure the results match */
    4452           1 :     services_check_match_name(test_ctx,
    4453             :                               primary_name, port,
    4454             :                               aliases, protocols);
    4455             : 
    4456             :     /* Search by port and make sure the results match */
    4457           1 :     services_check_match_port(test_ctx,
    4458             :                               primary_name, port,
    4459             :                               aliases, protocols);
    4460             : 
    4461           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    4462           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4463             : 
    4464             :     /* Clean up after ourselves (and test deleting by name)
    4465             :      *
    4466             :      * We have to do this after the transaction, because LDB
    4467             :      * doesn't like adding and deleting the same entry in a
    4468             :      * single transaction.
    4469             :      */
    4470           1 :     ret = sysdb_svc_delete(test_ctx->domain, primary_name, 0, NULL);
    4471           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4472             : 
    4473           1 :     talloc_free(test_ctx);
    4474             : }
    4475           1 : END_TEST
    4476             : 
    4477           1 : START_TEST(test_sysdb_store_services)
    4478             : {
    4479             :     errno_t ret;
    4480             :     struct sysdb_test_ctx *test_ctx;
    4481           1 :     const char *primary_name = "test_store_service";
    4482           1 :     const char *alt_primary_name = "alt_test_store_service";
    4483             :     const char **aliases;
    4484             :     const char **protocols;
    4485           1 :     int port = 3890;
    4486           1 :     int altport = 3891;
    4487             : 
    4488             :     /* Setup */
    4489           1 :     ret = setup_sysdb_tests(&test_ctx);
    4490           1 :     fail_if(ret != EOK, "Could not set up the test");
    4491             : 
    4492           1 :     aliases = talloc_array(test_ctx, const char *, 3);
    4493           1 :     fail_if(aliases == NULL);
    4494             : 
    4495           1 :     aliases[0] = talloc_asprintf(aliases, "test_service_alias1");
    4496           1 :     fail_if(aliases[0] == NULL);
    4497             : 
    4498           1 :     aliases[1] = talloc_asprintf(aliases, "test_service_alias2");
    4499           1 :     fail_if(aliases[1] == NULL);
    4500             : 
    4501           1 :     aliases[2] = NULL;
    4502             : 
    4503           1 :     protocols = talloc_array(test_ctx, const char *, 3);
    4504           1 :     fail_if(protocols == NULL);
    4505             : 
    4506           1 :     protocols[0] = talloc_asprintf(protocols, "tcp");
    4507           1 :     fail_if(protocols[0] == NULL);
    4508             : 
    4509           1 :     protocols[1] = talloc_asprintf(protocols, "udp");
    4510           1 :     fail_if(protocols[1] == NULL);
    4511             : 
    4512           1 :     protocols[2] = NULL;
    4513             : 
    4514           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    4515           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4516             : 
    4517             :     /* Store this group (which will add it) */
    4518           1 :     ret = sysdb_store_service(test_ctx->domain,
    4519             :                               primary_name, port,
    4520             :                               aliases, protocols,
    4521             :                               NULL, NULL, 1, 1);
    4522           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4523             : 
    4524             :     /* Search by name and make sure the results match */
    4525           1 :     services_check_match_name(test_ctx,
    4526             :                               primary_name, port,
    4527             :                               aliases, protocols);
    4528             : 
    4529             :     /* Search by port and make sure the results match */
    4530           1 :     services_check_match_port(test_ctx,
    4531             :                               primary_name, port,
    4532             :                               aliases, protocols);
    4533             : 
    4534             :     /* Change the service name */
    4535           1 :     ret = sysdb_store_service(test_ctx->domain,
    4536             :                               alt_primary_name, port,
    4537             :                               aliases, protocols,
    4538             :                               NULL, NULL, 1, 1);
    4539           1 :     fail_if (ret != EOK, "[%s]", strerror(ret));
    4540             : 
    4541           1 :     services_check_match_name(test_ctx,
    4542             :                               alt_primary_name, port,
    4543             :                               aliases, protocols);
    4544             : 
    4545             :     /* Search by port and make sure the results match */
    4546           1 :     services_check_match_port(test_ctx,
    4547             :                               alt_primary_name, port,
    4548             :                               aliases, protocols);
    4549             : 
    4550             : 
    4551             :     /* Change it back */
    4552           1 :     ret = sysdb_store_service(test_ctx->domain,
    4553             :                               primary_name, port,
    4554             :                               aliases, protocols,
    4555             :                               NULL, NULL, 1, 1);
    4556           1 :     fail_if (ret != EOK, "[%s]", strerror(ret));
    4557             : 
    4558             :     /* Change the port number */
    4559           1 :     ret = sysdb_store_service(test_ctx->domain,
    4560             :                               primary_name, altport,
    4561             :                               aliases, protocols,
    4562             :                               NULL, NULL, 1, 1);
    4563           1 :     fail_if (ret != EOK, "[%s]", strerror(ret));
    4564             : 
    4565             :     /* Search by name and make sure the results match */
    4566           1 :     services_check_match_name(test_ctx,
    4567             :                               primary_name, altport,
    4568             :                               aliases, protocols);
    4569             : 
    4570             :     /* Search by port and make sure the results match */
    4571           1 :     services_check_match_port(test_ctx,
    4572             :                               primary_name, altport,
    4573             :                               aliases, protocols);
    4574             : 
    4575             :     /* TODO: Test changing aliases and protocols */
    4576             : 
    4577           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    4578           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4579             : 
    4580             :     /* Clean up after ourselves (and test deleting by port)
    4581             :      *
    4582             :      * We have to do this after the transaction, because LDB
    4583             :      * doesn't like adding and deleting the same entry in a
    4584             :      * single transaction.
    4585             :      */
    4586           1 :     ret = sysdb_svc_delete(test_ctx->domain, NULL, altport, NULL);
    4587           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4588             : 
    4589           1 :     talloc_free(test_ctx);
    4590             : }
    4591           1 : END_TEST
    4592             : 
    4593             : errno_t
    4594             : sysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
    4595             :                        struct ldb_dn *dn,
    4596             :                        const char *alias);
    4597             : 
    4598           1 : START_TEST(test_sysdb_svc_remove_alias)
    4599             : {
    4600             :     errno_t ret;
    4601             :     struct sysdb_test_ctx *test_ctx;
    4602           1 :     const char *primary_name = "remove_alias_test";
    4603             :     const char **aliases;
    4604             :     const char **protocols;
    4605           1 :     int port = 3990;
    4606             :     struct ldb_dn *dn;
    4607             : 
    4608             :     /* Setup */
    4609           1 :     ret = setup_sysdb_tests(&test_ctx);
    4610           1 :     fail_if(ret != EOK, "Could not set up the test");
    4611             : 
    4612           1 :     aliases = talloc_array(test_ctx, const char *, 3);
    4613           1 :     fail_if(aliases == NULL);
    4614             : 
    4615           1 :     aliases[0] = talloc_asprintf(aliases, "remove_alias_alias1");
    4616           1 :     fail_if(aliases[0] == NULL);
    4617             : 
    4618           1 :     aliases[1] = talloc_asprintf(aliases, "remove_alias_alias2");
    4619           1 :     fail_if(aliases[1] == NULL);
    4620             : 
    4621           1 :     aliases[2] = NULL;
    4622             : 
    4623           1 :     protocols = talloc_array(test_ctx, const char *, 3);
    4624           1 :     fail_if(protocols == NULL);
    4625             : 
    4626           1 :     protocols[0] = talloc_asprintf(protocols, "tcp");
    4627           1 :     fail_if(protocols[0] == NULL);
    4628             : 
    4629           1 :     protocols[1] = talloc_asprintf(protocols, "udp");
    4630           1 :     fail_if(protocols[1] == NULL);
    4631             : 
    4632           1 :     protocols[2] = NULL;
    4633             : 
    4634           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    4635           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4636             : 
    4637           1 :     ret = sysdb_svc_add(NULL, test_ctx->domain,
    4638             :                         primary_name, port,
    4639             :                         aliases, protocols,
    4640             :                         NULL);
    4641           1 :     fail_unless(ret == EOK, "sysdb_svc_add error [%s]\n", strerror(ret));
    4642             : 
    4643             :     /* Search by name and make sure the results match */
    4644           1 :     services_check_match_name(test_ctx,
    4645             :                               primary_name, port,
    4646             :                               aliases, protocols);
    4647             : 
    4648             :     /* Search by port and make sure the results match */
    4649           1 :     services_check_match_port(test_ctx,
    4650             :                               primary_name, port,
    4651             :                               aliases, protocols);
    4652             : 
    4653             :     /* Now remove an alias */
    4654           1 :     dn = sysdb_svc_dn(test_ctx->sysdb, test_ctx, test_ctx->domain->name, primary_name);
    4655           1 :     fail_if (dn == NULL);
    4656             : 
    4657           1 :     ret = sysdb_svc_remove_alias(test_ctx->sysdb, dn, aliases[1]);
    4658           1 :     fail_if (ret != EOK, "[%s]", strerror(ret));
    4659             : 
    4660           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    4661           1 :     fail_if(ret != EOK);
    4662             : 
    4663           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    4664           1 :     fail_if(ret != EOK);
    4665             : 
    4666             :     /* Set aliases[1] to NULL to perform validation checks */
    4667           1 :     aliases[1] = NULL;
    4668             : 
    4669             :     /* Search by name and make sure the results match */
    4670           1 :     services_check_match_name(test_ctx,
    4671             :                               primary_name, port,
    4672             :                               aliases, protocols);
    4673             : 
    4674             :     /* Search by port and make sure the results match */
    4675           1 :     services_check_match_port(test_ctx,
    4676             :                               primary_name, port,
    4677             :                               aliases, protocols);
    4678             : 
    4679           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    4680           1 :     fail_if(ret != EOK, "[%s]", strerror(ret));
    4681             : 
    4682           1 :     talloc_free(test_ctx);
    4683             : }
    4684           1 : END_TEST
    4685             : 
    4686             : #define LC_NAME_ALIAS_TEST_VAL "TeSt VaLuE"
    4687             : #define LC_NAME_ALIAS_CHECK_VAL "test value"
    4688           1 : START_TEST(test_sysdb_attrs_add_lc_name_alias)
    4689             : {
    4690             :     int ret;
    4691             :     struct sysdb_attrs *attrs;
    4692             :     const char *str;
    4693             : 
    4694           1 :     ret = sysdb_attrs_add_lc_name_alias(NULL, NULL);
    4695           1 :     fail_unless(ret == EINVAL, "EINVAL not returned for NULL input");
    4696             : 
    4697           1 :     attrs = sysdb_new_attrs(NULL);
    4698           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    4699             : 
    4700           1 :     ret = sysdb_attrs_add_lc_name_alias(attrs, LC_NAME_ALIAS_TEST_VAL);
    4701           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_lc_name_alias failed");
    4702             : 
    4703           1 :     ret = sysdb_attrs_get_string(attrs, SYSDB_NAME_ALIAS, &str);
    4704           1 :     fail_unless(ret == EOK, "sysdb_attrs_get_string failed");
    4705           1 :     fail_unless(strcmp(str, LC_NAME_ALIAS_CHECK_VAL) == 0,
    4706             :                 "Unexpected value, expected [%s], got [%s]",
    4707             :                 LC_NAME_ALIAS_CHECK_VAL, str);
    4708             : 
    4709           1 :     talloc_free(attrs);
    4710             : }
    4711           1 : END_TEST
    4712             : 
    4713           1 : START_TEST(test_sysdb_attrs_get_string_array)
    4714             : {
    4715             :     int ret;
    4716             :     struct sysdb_attrs *attrs;
    4717             :     const char **list;
    4718           1 :     const char *attrname = "test_attr";
    4719             :     TALLOC_CTX *tmp_ctx;
    4720           1 :     struct ldb_message_element *el = NULL;
    4721             : 
    4722           1 :     tmp_ctx = talloc_new(NULL);
    4723           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed");
    4724             : 
    4725           1 :     attrs = sysdb_new_attrs(NULL);
    4726           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    4727             : 
    4728           1 :     ret = sysdb_attrs_add_string(attrs, attrname, "val1");
    4729           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
    4730           1 :     ret = sysdb_attrs_add_string(attrs, attrname, "val2");
    4731           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
    4732             : 
    4733           1 :     ret = sysdb_attrs_get_el_ext(attrs, attrname, false, &el);
    4734           1 :     fail_unless(ret == EOK, "sysdb_attrs_get_el_ext failed");
    4735             : 
    4736           1 :     list = sss_ldb_el_to_string_list(tmp_ctx, el);
    4737           1 :     fail_if(list == NULL, ("sss_ldb_el_to_string_list failed\n"));
    4738             : 
    4739           1 :     ck_assert_str_eq(list[0], "val1");
    4740           1 :     ck_assert_str_eq(list[1], "val2");
    4741           1 :     fail_unless(list[2] == NULL, "Expected terminated list");
    4742             : 
    4743           1 :     talloc_free(list);
    4744             : 
    4745           1 :     ret = sysdb_attrs_get_string_array(attrs, attrname, tmp_ctx, &list);
    4746           1 :     fail_unless(ret == EOK, "sysdb_attrs_get_string_array failed");
    4747             : 
    4748             :     /* This test relies on values keeping the same order. It is the case
    4749             :      * with LDB, but if we ever switch from LDB, we need to amend the test
    4750             :      */
    4751           1 :     ck_assert_str_eq(list[0], "val1");
    4752           1 :     ck_assert_str_eq(list[1], "val2");
    4753           1 :     fail_unless(list[2] == NULL, "Expected terminated list");
    4754             : 
    4755           1 :     talloc_free(tmp_ctx);
    4756             : }
    4757           1 : END_TEST
    4758             : 
    4759           1 : START_TEST(test_sysdb_attrs_add_val)
    4760             : {
    4761             :     int ret;
    4762             :     struct sysdb_attrs *attrs;
    4763             :     TALLOC_CTX *tmp_ctx;
    4764           1 :     struct ldb_val val = {discard_const(TEST_ATTR_VALUE),
    4765             :                           sizeof(TEST_ATTR_VALUE) - 1};
    4766             : 
    4767           1 :     tmp_ctx = talloc_new(NULL);
    4768           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed");
    4769             : 
    4770           1 :     attrs = sysdb_new_attrs(NULL);
    4771           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    4772             : 
    4773           1 :     ret = sysdb_attrs_add_val(attrs, TEST_ATTR_NAME, &val);
    4774           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4775             : 
    4776           1 :     ret = sysdb_attrs_add_val(attrs, TEST_ATTR_NAME, &val);
    4777           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4778             : 
    4779           1 :     fail_unless(attrs->num == 1, "Unexpected number of attributes.");
    4780           1 :     fail_unless(strcmp(attrs->a[0].name, TEST_ATTR_NAME) == 0,
    4781             :                 "Unexpected attribute name.");
    4782           1 :     fail_unless(attrs->a[0].num_values == 2,
    4783             :                 "Unexpected number of attribute values.");
    4784           1 :     fail_unless(ldb_val_string_cmp(&attrs->a[0].values[0],
    4785             :                                    TEST_ATTR_VALUE) == 0,
    4786             :                 "Unexpected attribute value.");
    4787           1 :     fail_unless(ldb_val_string_cmp(&attrs->a[0].values[1],
    4788             :                                    TEST_ATTR_VALUE) == 0,
    4789             :                 "Unexpected attribute value.");
    4790             : 
    4791           1 :     talloc_free(tmp_ctx);
    4792             : }
    4793           1 : END_TEST
    4794             : 
    4795           1 : START_TEST(test_sysdb_attrs_add_val_safe)
    4796             : {
    4797             :     int ret;
    4798             :     struct sysdb_attrs *attrs;
    4799             :     TALLOC_CTX *tmp_ctx;
    4800           1 :     struct ldb_val val = {discard_const(TEST_ATTR_VALUE),
    4801             :                           sizeof(TEST_ATTR_VALUE) - 1};
    4802             : 
    4803           1 :     tmp_ctx = talloc_new(NULL);
    4804           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed");
    4805             : 
    4806           1 :     attrs = sysdb_new_attrs(NULL);
    4807           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    4808             : 
    4809           1 :     ret = sysdb_attrs_add_val(attrs, TEST_ATTR_NAME, &val);
    4810           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4811             : 
    4812           1 :     ret = sysdb_attrs_add_val_safe(attrs, TEST_ATTR_NAME, &val);
    4813           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4814             : 
    4815           1 :     fail_unless(attrs->num == 1, "Unexpected number of attributes.");
    4816           1 :     fail_unless(strcmp(attrs->a[0].name, TEST_ATTR_NAME) == 0,
    4817             :                 "Unexpected attribute name.");
    4818           1 :     fail_unless(attrs->a[0].num_values == 1,
    4819             :                 "Unexpected number of attribute values.");
    4820           1 :     fail_unless(ldb_val_string_cmp(&attrs->a[0].values[0],
    4821             :                                    TEST_ATTR_VALUE) == 0,
    4822             :                 "Unexpected attribute value.");
    4823             : 
    4824           1 :     talloc_free(tmp_ctx);
    4825             : }
    4826           1 : END_TEST
    4827             : 
    4828           1 : START_TEST(test_sysdb_attrs_add_string_safe)
    4829             : {
    4830             :     int ret;
    4831             :     struct sysdb_attrs *attrs;
    4832             :     TALLOC_CTX *tmp_ctx;
    4833             : 
    4834           1 :     tmp_ctx = talloc_new(NULL);
    4835           1 :     fail_unless(tmp_ctx != NULL, "talloc_new failed");
    4836             : 
    4837           1 :     attrs = sysdb_new_attrs(NULL);
    4838           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    4839             : 
    4840           1 :     ret = sysdb_attrs_add_string(attrs, TEST_ATTR_NAME, TEST_ATTR_VALUE);
    4841           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4842             : 
    4843           1 :     ret = sysdb_attrs_add_string_safe(attrs, TEST_ATTR_NAME, TEST_ATTR_VALUE);
    4844           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
    4845             : 
    4846           1 :     fail_unless(attrs->num == 1, "Unexpected number of attributes.");
    4847           1 :     fail_unless(strcmp(attrs->a[0].name, TEST_ATTR_NAME) == 0,
    4848             :                 "Unexpected attribute name.");
    4849           1 :     fail_unless(attrs->a[0].num_values == 1,
    4850             :                 "Unexpected number of attribute values.");
    4851           1 :     fail_unless(ldb_val_string_cmp(&attrs->a[0].values[0],
    4852             :                                    TEST_ATTR_VALUE) == 0,
    4853             :                 "Unexpected attribute value.");
    4854             : 
    4855           1 :     talloc_free(tmp_ctx);
    4856             : }
    4857           1 : END_TEST
    4858             : 
    4859           1 : START_TEST (test_sysdb_search_return_ENOENT)
    4860             : {
    4861             :     struct sysdb_test_ctx *test_ctx;
    4862             :     int ret;
    4863           1 :     struct ldb_dn *user_dn = NULL;
    4864           1 :     struct ldb_message *msg = NULL;
    4865           1 :     struct ldb_message **msgs = NULL;
    4866           1 :     struct ldb_result *res = NULL;
    4867             :     size_t count;
    4868           1 :     const char *str = NULL;
    4869             : 
    4870             :     /* Setup */
    4871           1 :     ret = setup_sysdb_tests(&test_ctx);
    4872           1 :     fail_if(ret != EOK, "Could not set up the test");
    4873           1 :     check_leaks_push(test_ctx);
    4874             : 
    4875             :     /* id mapping */
    4876           1 :     ret = sysdb_idmap_get_mappings(test_ctx, test_ctx->domain, &res);
    4877           1 :     fail_unless(ret == ENOENT, "sysdb_idmap_get_mappings error [%d][%s].",
    4878             :                 ret, strerror(ret));
    4879           1 :     talloc_zfree(res);
    4880             : 
    4881             :     /* Search user */
    4882           1 :     ret = sysdb_search_user_by_name(test_ctx, test_ctx->domain,
    4883             :                                     "nonexisting_user", NULL, &msg);
    4884           1 :     fail_unless(ret == ENOENT, "sysdb_search_user_by_name error [%d][%s].",
    4885             :                                ret, strerror(ret));
    4886           1 :     talloc_zfree(msg);
    4887             : 
    4888           1 :     ret = sysdb_get_real_name(test_ctx, test_ctx->domain,
    4889             :                               "nonexisting_user", &str);
    4890           1 :     fail_unless(ret == ENOENT, "sysdb_get_real_name error [%d][%s].",
    4891             :                                ret, strerror(ret));
    4892           1 :     talloc_zfree(str);
    4893             : 
    4894           1 :     ret = sysdb_search_user_by_uid(test_ctx, test_ctx->domain,
    4895             :                                    1234, NULL, &msg);
    4896           1 :     fail_unless(ret == ENOENT, "sysdb_search_user_by_uid error [%d][%s].",
    4897             :                                ret, strerror(ret));
    4898           1 :     talloc_zfree(msg);
    4899             : 
    4900           1 :     ret = sysdb_search_user_by_sid_str(test_ctx, test_ctx->domain,
    4901             :                                        "S-5-4-3-2-1", NULL, &msg);
    4902           1 :     fail_unless(ret == ENOENT, "sysdb_search_user_by_sid_str failed with "
    4903             :                                "[%d][%s].", ret, strerror(ret));
    4904             : 
    4905             :     /* Search group */
    4906           1 :     ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
    4907             :                                      "nonexisting_group", NULL, &msg);
    4908           1 :     fail_unless(ret == ENOENT, "sysdb_search_group_by_name error [%d][%s].",
    4909             :                                ret, strerror(ret));
    4910           1 :     talloc_zfree(msg);
    4911             : 
    4912           1 :     ret = sysdb_search_group_by_gid(test_ctx, test_ctx->domain,
    4913             :                                     1234, NULL, &msg);
    4914           1 :     fail_unless(ret == ENOENT, "sysdb_search_group_by_gid error [%d][%s].",
    4915             :                                ret, strerror(ret));
    4916           1 :     talloc_zfree(msg);
    4917             : 
    4918           1 :     ret = sysdb_search_group_by_sid_str(test_ctx, test_ctx->domain,
    4919             :                                         "S-5-4-3-2-1", NULL, &msg);
    4920           1 :     fail_unless(ret == ENOENT, "sysdb_search_group_by_sid_str failed with "
    4921             :                                "[%d][%s].", ret, strerror(ret));
    4922           1 :     talloc_zfree(msg);
    4923             : 
    4924             :     /* Search netgroup */
    4925           1 :     ret = sysdb_search_netgroup_by_name(test_ctx, test_ctx->domain,
    4926             :                                         "nonexisting_netgroup", NULL, &msg);
    4927           1 :     fail_unless(ret == ENOENT, "sysdb_search_netgroup_by_name error [%d][%s].",
    4928             :                                ret, strerror(ret));
    4929           1 :     talloc_zfree(msg);
    4930             : 
    4931           1 :     ret = sysdb_getnetgr(test_ctx, test_ctx->domain, "nonexisting_netgroup",
    4932             :                          &res);
    4933           1 :     fail_unless(ret == ENOENT, "sysdb_getnetgr error [%d][%s]",
    4934             :                 ret, strerror(ret));
    4935           1 :     talloc_zfree(res);
    4936             : 
    4937             :     /* Search object */
    4938           1 :     ret = sysdb_search_object_by_sid(test_ctx, test_ctx->domain,
    4939             :                                      "S-5-4-3-2-1", NULL, &res);
    4940           1 :     fail_unless(ret == ENOENT, "sysdb_search_object_by_sid failed with "
    4941             :                                "[%d][%s].", ret, strerror(ret));
    4942           1 :     talloc_zfree(res);
    4943             : 
    4944             :     /* Search can return more results */
    4945           1 :     ret = sysdb_search_users(test_ctx, test_ctx->domain,
    4946             :                              "("SYSDB_SHELL"=/bin/nologin)", NULL,
    4947             :                              &count, &msgs);
    4948           1 :     fail_unless(ret == ENOENT, "sysdb_search_users failed: %d, %s",
    4949             :                                ret, strerror(ret));
    4950           1 :     talloc_zfree(msgs);
    4951             : 
    4952           1 :     ret = sysdb_search_groups(test_ctx, test_ctx->domain,
    4953             :                               "("SYSDB_GIDNUM"=1234)", NULL,
    4954             :                               &count, &msgs);
    4955           1 :     fail_unless(ret == ENOENT, "sysdb_search_groups failed: %d, %s",
    4956             :                                ret, strerror(ret));
    4957           1 :     talloc_zfree(msgs);
    4958             : 
    4959           1 :     ret = sysdb_search_netgroups(test_ctx, test_ctx->domain,
    4960             :                                  "("SYSDB_NAME"=nonexisting)", NULL,
    4961             :                                  &count, &msgs);
    4962           1 :     fail_unless(ret == ENOENT, "sysdb_search_netgroups failed: %d, %s",
    4963             :                                ret, strerror(ret));
    4964           1 :     talloc_zfree(msgs);
    4965             : 
    4966             :     /* Search custom */
    4967           1 :     ret = sysdb_search_custom(test_ctx, test_ctx->domain,
    4968             :                               "(distinguishedName=nonexisting)",
    4969             :                               CUSTOM_TEST_CONTAINER, NULL,
    4970             :                               &count, &msgs);
    4971           1 :     fail_unless(ret == ENOENT, "sysdb_search_custom failed: %d, %s",
    4972             :                                ret, strerror(ret));
    4973           1 :     talloc_zfree(msgs);
    4974             : 
    4975           1 :     ret = sysdb_search_custom_by_name(test_ctx, test_ctx->domain,
    4976             :                                      "nonexisting",
    4977             :                                      CUSTOM_TEST_CONTAINER, NULL,
    4978             :                                      &count, &msgs);
    4979           1 :     fail_unless(ret == ENOENT, "sysdb_search_custom_by_name failed: %d, %s",
    4980             :                                ret, strerror(ret));
    4981           1 :     talloc_zfree(msgs);
    4982             : 
    4983             :     /* General search */
    4984           1 :     user_dn = sysdb_user_dn(test_ctx, test_ctx->domain, "nonexisting_user");
    4985           1 :     fail_if(user_dn == NULL, "sysdb_user_dn failed");
    4986             : 
    4987           1 :     ret = sysdb_asq_search(test_ctx, test_ctx->domain,
    4988             :                            user_dn, NULL, "memberof", NULL,
    4989             :                            &count, &msgs);
    4990           1 :     fail_unless(ret == ENOENT, "sysdb_asq_search failed: %d, %s",
    4991             :                                ret, strerror(ret));
    4992           1 :     talloc_zfree(msgs);
    4993             : 
    4994           1 :     ret = sysdb_search_entry(test_ctx, test_ctx->sysdb,
    4995             :                              user_dn, LDB_SCOPE_SUBTREE,
    4996             :                              "objectClass=user", NULL,
    4997             :                              &count, &msgs);
    4998           1 :     fail_unless(ret == ENOENT, "sysdb_search_entry failed: %d, %s",
    4999             :                                ret, strerror(ret));
    5000           1 :     talloc_zfree(msgs);
    5001           1 :     talloc_zfree(user_dn);
    5002             : 
    5003             :     /* SSS_LDB_SEARCH */
    5004           1 :     user_dn = sysdb_user_dn(test_ctx, test_ctx->domain, "nonexisting_user");
    5005           1 :     fail_if(user_dn == NULL, "sysdb_user_dn failed");
    5006           1 :     SSS_LDB_SEARCH(ret, test_ctx->sysdb->ldb, test_ctx, &res, user_dn,
    5007             :                    LDB_SCOPE_BASE, NULL, "objectClass=user");
    5008             : 
    5009           1 :     fail_unless(ret == ENOENT, "SSS_LDB_SEARCH failed: %d, %s",
    5010             :                                ret, strerror(ret));
    5011             : 
    5012           1 :     talloc_zfree(res);
    5013           1 :     talloc_zfree(user_dn);
    5014             : 
    5015             :     /* TODO: test sysdb_search_selinux_config */
    5016             : 
    5017           1 :     fail_unless(check_leaks_pop(test_ctx) == true, "Memory leak");
    5018           1 :     talloc_free(test_ctx);
    5019             : }
    5020           1 : END_TEST
    5021             : 
    5022           1 : START_TEST(test_sysdb_has_enumerated)
    5023             : {
    5024             :     errno_t ret;
    5025             :     struct sysdb_test_ctx *test_ctx;
    5026             :     bool enumerated;
    5027             : 
    5028             :     /* Setup */
    5029           1 :     ret = setup_sysdb_tests(&test_ctx);
    5030           1 :     fail_if(ret != EOK, "Could not set up the test");
    5031             : 
    5032           1 :     ret = sysdb_has_enumerated(test_ctx->domain, &enumerated);
    5033           1 :     fail_if(ret != ENOENT,
    5034             :             "Error [%d][%s] checking enumeration ENOENT is expected",
    5035             :             ret, strerror(ret));
    5036             : 
    5037           1 :     ret = sysdb_set_enumerated(test_ctx->domain, true);
    5038           1 :     fail_if(ret != EOK, "Error [%d][%s] setting enumeration",
    5039             :                         ret, strerror(ret));
    5040             : 
    5041             :     /* Recheck enumeration status */
    5042           1 :     ret = sysdb_has_enumerated(test_ctx->domain, &enumerated);
    5043           1 :     fail_if(ret != EOK, "Error [%d][%s] checking enumeration",
    5044             :                         ret, strerror(ret));
    5045             : 
    5046           1 :     fail_unless(enumerated, "Enumeration should have been set to true");
    5047             : 
    5048           1 :     talloc_free(test_ctx);
    5049             : }
    5050           1 : END_TEST
    5051             : 
    5052           1 : START_TEST(test_sysdb_original_dn_case_insensitive)
    5053             : {
    5054             :     errno_t ret;
    5055             :     struct sysdb_test_ctx *test_ctx;
    5056             :     const char *filter;
    5057             :     struct ldb_dn *base_dn;
    5058           1 :     const char *no_attrs[] = { NULL };
    5059             :     struct ldb_message **msgs;
    5060             :     size_t num_msgs;
    5061             : 
    5062             :     /* Setup */
    5063           1 :     ret = setup_sysdb_tests(&test_ctx);
    5064           1 :     fail_if(ret != EOK, "Could not set up the test");
    5065             : 
    5066           1 :     ret = sysdb_add_incomplete_group(test_ctx->domain,
    5067             :                                      "case_sensitive_group1", 29000,
    5068             :                                      "cn=case_sensitive_group1,cn=example,cn=com",
    5069             :                                      NULL, NULL, true, 0);
    5070           1 :     fail_unless(ret == EOK, "sysdb_add_incomplete_group error [%d][%s]",
    5071             :                             ret, strerror(ret));
    5072             : 
    5073           1 :     ret = sysdb_add_incomplete_group(test_ctx->domain,
    5074             :                                      "case_sensitive_group2", 29001,
    5075             :                                      "cn=CASE_SENSITIVE_GROUP1,cn=EXAMPLE,cn=COM",
    5076             :                                      NULL, NULL, true, 0);
    5077           1 :     fail_unless(ret == EOK, "sysdb_add_incomplete_group error [%d][%s]",
    5078             :                             ret, strerror(ret));
    5079             : 
    5080             :     /* Search by originalDN should yield 2 entries */
    5081           1 :     filter = talloc_asprintf(test_ctx, "%s=%s", SYSDB_ORIG_DN,
    5082             :                              "cn=case_sensitive_group1,cn=example,cn=com");
    5083           1 :     fail_if(filter == NULL, "Cannot construct filter\n");
    5084             : 
    5085           1 :     base_dn = sysdb_domain_dn(test_ctx, test_ctx->domain);
    5086           1 :     fail_if(base_dn == NULL, "Cannot construct basedn\n");
    5087             : 
    5088           1 :     ret = sysdb_search_entry(test_ctx, test_ctx->sysdb,
    5089             :                              base_dn, LDB_SCOPE_SUBTREE, filter, no_attrs,
    5090             :                              &num_msgs, &msgs);
    5091           1 :     fail_unless(ret == EOK, "cache search error [%d][%s]",
    5092             :                             ret, strerror(ret));
    5093           1 :     fail_unless(num_msgs == 2, "Did not find the expected number of entries using "
    5094             :                                "case insensitive originalDN search");
    5095             : }
    5096           1 : END_TEST
    5097             : 
    5098           1 : START_TEST(test_sysdb_search_sid_str)
    5099             : {
    5100             :     errno_t ret;
    5101             :     struct sysdb_test_ctx *test_ctx;
    5102             :     struct ldb_message *msg;
    5103           1 :     struct sysdb_attrs *attrs = NULL;
    5104             : 
    5105             :     /* Setup */
    5106           1 :     ret = setup_sysdb_tests(&test_ctx);
    5107           1 :     fail_if(ret != EOK, "Could not set up the test");
    5108             : 
    5109           1 :     ret = sysdb_add_incomplete_group(test_ctx->domain,
    5110             :                                      "group", 29000,
    5111             :                                      "cn=group,cn=example,cn=com",
    5112             :                                      "S-1-2-3-4", NULL, true, 0);
    5113           1 :     fail_unless(ret == EOK, "sysdb_add_incomplete_group error [%d][%s]",
    5114             :                             ret, strerror(ret));
    5115             : 
    5116           1 :     ret = sysdb_search_group_by_sid_str(test_ctx, test_ctx->domain,
    5117             :                                         "S-1-2-3-4", NULL, &msg);
    5118           1 :     fail_unless(ret == EOK, "sysdb_search_group_by_sid_str failed with [%d][%s].",
    5119             :                 ret, strerror(ret));
    5120             : 
    5121             :     /* Delete the group by SID */
    5122           1 :     ret = sysdb_delete_by_sid(test_ctx->sysdb, test_ctx->domain, "S-1-2-3-4");
    5123           1 :     fail_unless(ret == EOK, "sysdb_delete_by_sid failed with [%d][%s].",
    5124             :                 ret, strerror(ret));
    5125             : 
    5126             :     /* Verify it's gone */
    5127           1 :     ret = sysdb_search_group_by_sid_str(test_ctx, test_ctx->domain, "S-1-2-3-4",
    5128             :                                         NULL, &msg);
    5129           1 :     fail_unless(ret == ENOENT, "sysdb_search_group_by_sid_str failed with [%d][%s].",
    5130             :                 ret, strerror(ret));
    5131             : 
    5132           1 :     talloc_free(msg);
    5133           1 :     msg = NULL;
    5134             : 
    5135           1 :     attrs = sysdb_new_attrs(test_ctx);
    5136           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    5137             : 
    5138           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_SID_STR, "S-1-2-3-4-5");
    5139           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed with [%d][%s].",
    5140             :                 ret, strerror(ret));
    5141             : 
    5142           1 :     ret = sysdb_add_user(test_ctx->domain, "SIDuser",
    5143             :                          12345, 0, "SID user", "/home/siduser", "/bin/bash",
    5144             :                          NULL, attrs, 0, 0);
    5145           1 :     fail_unless(ret == EOK, "sysdb_add_user failed with [%d][%s].",
    5146             :                 ret, strerror(ret));
    5147             : 
    5148           1 :     ret = sysdb_search_user_by_sid_str(test_ctx, test_ctx->domain,
    5149             :                                        "S-1-2-3-4-5", NULL, &msg);
    5150           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_sid_str failed with [%d][%s].",
    5151             :                 ret, strerror(ret));
    5152             : 
    5153           1 :     talloc_free(test_ctx);
    5154             : }
    5155           1 : END_TEST
    5156             : 
    5157           1 : START_TEST(test_sysdb_search_object_by_uuid)
    5158             : {
    5159             :     errno_t ret;
    5160             :     struct sysdb_test_ctx *test_ctx;
    5161             :     struct ldb_result *res;
    5162           1 :     struct sysdb_attrs *attrs = NULL;
    5163             : 
    5164             :     /* Setup */
    5165           1 :     ret = setup_sysdb_tests(&test_ctx);
    5166           1 :     fail_if(ret != EOK, "Could not set up the test");
    5167             : 
    5168           1 :     attrs = sysdb_new_attrs(test_ctx);
    5169           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    5170             : 
    5171           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_UUID,
    5172             :                                  "11111111-2222-3333-4444-555555555555");
    5173           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed with [%d][%s].",
    5174             :                 ret, strerror(ret));
    5175             : 
    5176           1 :     ret = sysdb_add_user(test_ctx->domain, "UUIDuser",
    5177             :                          123456, 0, "UUID user", "/home/uuiduser", "/bin/bash",
    5178             :                          NULL, attrs, 0, 0);
    5179           1 :     fail_unless(ret == EOK, "sysdb_add_user failed with [%d][%s].",
    5180             :                 ret, strerror(ret));
    5181             : 
    5182           1 :     ret = sysdb_search_object_by_uuid(test_ctx, test_ctx->domain,
    5183             :                                       "11111111-2222-3333-4444-555555555556",
    5184             :                                       NULL, &res);
    5185           1 :     fail_unless(ret == ENOENT,
    5186             :                 "Unexpected return code from sysdb_search_object_by_uuid for "
    5187             :                 "missing object, expected [%d], got [%d].", ENOENT, ret);
    5188             : 
    5189           1 :     ret = sysdb_search_object_by_uuid(test_ctx, test_ctx->domain,
    5190             :                                       "11111111-2222-3333-4444-555555555555",
    5191             :                                       NULL, &res);
    5192           1 :     fail_unless(ret == EOK, "sysdb_search_object_by_uuid failed with [%d][%s].",
    5193             :                 ret, strerror(ret));
    5194           1 :     fail_unless(res->count == 1, "Unexpected number of results, " \
    5195             :                                  "expected [%u], get [%u].", 1, res->count);
    5196           1 :     fail_unless(strcmp(ldb_msg_find_attr_as_string(res->msgs[0],
    5197             :                                                    SYSDB_NAME, ""),
    5198             :                       "UUIDuser") == 0, "Unexpected object found, " \
    5199             :                       "expected [%s], got [%s].", "UUIDuser",
    5200             :                       ldb_msg_find_attr_as_string(res->msgs[0],SYSDB_NAME, ""));
    5201           1 :     talloc_free(test_ctx);
    5202             : }
    5203           1 : END_TEST
    5204             : 
    5205             : /* For simple searches the content of the certificate does not matter */
    5206             : #define TEST_USER_CERT_DERB64 "gJznJT7L0aETU5CMk+n+1Q=="
    5207           1 : START_TEST(test_sysdb_search_user_by_cert)
    5208             : {
    5209             :     errno_t ret;
    5210             :     struct sysdb_test_ctx *test_ctx;
    5211             :     struct ldb_result *res;
    5212           1 :     struct sysdb_attrs *attrs = NULL;
    5213             :     struct ldb_val val;
    5214             : 
    5215             :     /* Setup */
    5216           1 :     ret = setup_sysdb_tests(&test_ctx);
    5217           1 :     fail_if(ret != EOK, "Could not set up the test");
    5218             : 
    5219           1 :     val.data = sss_base64_decode(test_ctx, TEST_USER_CERT_DERB64, &val.length);
    5220           1 :     fail_unless(val.data != NULL, "sss_base64_decode failed.");
    5221             : 
    5222           1 :     attrs = sysdb_new_attrs(test_ctx);
    5223           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed");
    5224             : 
    5225           1 :     ret = sysdb_attrs_add_val(attrs, SYSDB_USER_CERT, &val);
    5226           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_val failed with [%d][%s].",
    5227             :                 ret, strerror(ret));
    5228             : 
    5229           1 :     ret = sysdb_add_user(test_ctx->domain, "certuser",
    5230             :                          234567, 0, "cert user", "/home/certuser", "/bin/bash",
    5231             :                          NULL, attrs, 0, 0);
    5232           1 :     fail_unless(ret == EOK, "sysdb_add_user failed with [%d][%s].",
    5233             :                 ret, strerror(ret));
    5234             : 
    5235           1 :     ret = sysdb_search_user_by_cert(test_ctx, test_ctx->domain, "ABA=", &res);
    5236           1 :     fail_unless(ret == ENOENT,
    5237             :                 "Unexpected return code from sysdb_search_user_by_cert for "
    5238             :                 "missing object, expected [%d], got [%d].", ENOENT, ret);
    5239             : 
    5240           1 :     ret = sysdb_search_user_by_cert(test_ctx, test_ctx->domain,
    5241             :                                     TEST_USER_CERT_DERB64, &res);
    5242           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_cert failed with [%d][%s].",
    5243             :                 ret, strerror(ret));
    5244           1 :     fail_unless(res->count == 1, "Unexpected number of results, " \
    5245             :                                  "expected [%u], get [%u].", 1, res->count);
    5246           1 :     fail_unless(strcmp(ldb_msg_find_attr_as_string(res->msgs[0],
    5247             :                                                    SYSDB_NAME, ""),
    5248             :                       "certuser") == 0, "Unexpected object found, " \
    5249             :                       "expected [%s], got [%s].", "certuser",
    5250             :                       ldb_msg_find_attr_as_string(res->msgs[0],SYSDB_NAME, ""));
    5251           1 :     talloc_free(test_ctx);
    5252             : }
    5253           1 : END_TEST
    5254             : 
    5255           1 : START_TEST(test_sysdb_delete_by_sid)
    5256             : {
    5257             :     errno_t ret;
    5258             :     struct sysdb_test_ctx *test_ctx;
    5259             : 
    5260             :     /* Setup */
    5261           1 :     ret = setup_sysdb_tests(&test_ctx);
    5262           1 :     fail_if(ret != EOK, "Could not set up the test");
    5263             : 
    5264           1 :     check_leaks_push(test_ctx);
    5265             : 
    5266             :     /* Delete the group by SID */
    5267           1 :     ret = sysdb_delete_by_sid(test_ctx->sysdb, test_ctx->domain,
    5268             :                               "S-1-2-3-4-NON_EXISTING_SID");
    5269           1 :     fail_unless(ret == EOK, "sysdb_delete_by_sid failed with [%d][%s].",
    5270             :                 ret, strerror(ret));
    5271             : 
    5272           1 :     fail_unless(check_leaks_pop(test_ctx) == true, "Memory leak");
    5273           1 :     talloc_free(test_ctx);
    5274             : }
    5275           1 : END_TEST
    5276             : 
    5277             : const char *const testdom[4] = { "test.sub", "TEST.SUB", "test", "S-3" };
    5278             : 
    5279           1 : START_TEST(test_sysdb_subdomain_store_user)
    5280             : {
    5281             :     struct sysdb_test_ctx *test_ctx;
    5282             :     errno_t ret;
    5283           1 :     struct sss_domain_info *subdomain = NULL;
    5284           1 :     struct ldb_result *results = NULL;
    5285           1 :     struct ldb_dn *base_dn = NULL;
    5286           1 :     struct ldb_dn *check_dn = NULL;
    5287           1 :     const char *attrs[] = { SYSDB_NAME, SYSDB_NAME_ALIAS, NULL };
    5288             :     struct sysdb_attrs *user_attrs;
    5289             :     struct ldb_message *msg;
    5290             : 
    5291           1 :     ret = setup_sysdb_tests(&test_ctx);
    5292           1 :     fail_if(ret != EOK, "Could not set up the test");
    5293             : 
    5294           1 :     subdomain = new_subdomain(test_ctx, test_ctx->domain,
    5295             :                               testdom[0], testdom[1], testdom[2], testdom[3],
    5296             :                               false, false, NULL, 0);
    5297           1 :     fail_unless(subdomain != NULL, "Failed to create new subdomin.");
    5298           1 :     ret = sysdb_subdomain_store(test_ctx->sysdb,
    5299             :                                 testdom[0], testdom[1], testdom[2], testdom[3],
    5300             :                                 false, false, NULL, 0);
    5301           1 :     fail_if(ret != EOK, "Could not set up the test (test subdom)");
    5302             : 
    5303           1 :     ret = sysdb_update_subdomains(test_ctx->domain);
    5304           1 :     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
    5305             :                             ret, strerror(ret));
    5306             : 
    5307           1 :     user_attrs = sysdb_new_attrs(test_ctx);
    5308           1 :     fail_unless(user_attrs != NULL, "sysdb_new_attrs failed");
    5309             : 
    5310           1 :     ret = sysdb_attrs_add_string(user_attrs, SYSDB_NAME_ALIAS, "subdomuser");
    5311           1 :     fail_unless(ret == EOK, "sysdb_store_user failed.");
    5312             : 
    5313           1 :     ret = sysdb_store_user(subdomain, "SubDomUser",
    5314             :                            NULL, 12345, 0, "Sub Domain User",
    5315             :                            "/home/subdomuser", "/bin/bash",
    5316             :                            NULL, user_attrs, NULL, -1, 0);
    5317           1 :     fail_unless(ret == EOK, "sysdb_store_user failed.");
    5318             : 
    5319           1 :     base_dn =ldb_dn_new(test_ctx, test_ctx->sysdb->ldb, "cn=sysdb");
    5320           1 :     fail_unless(base_dn != NULL);
    5321             : 
    5322           1 :     check_dn = ldb_dn_new(test_ctx, test_ctx->sysdb->ldb,
    5323             :                           "name=SubDomUser,cn=users,cn=test.sub,cn=sysdb");
    5324           1 :     fail_unless(check_dn != NULL);
    5325             : 
    5326           1 :     ret = ldb_search(test_ctx->sysdb->ldb, test_ctx, &results, base_dn,
    5327             :                      LDB_SCOPE_SUBTREE, NULL, "name=SubDomUser");
    5328           1 :     fail_unless(ret == EOK, "ldb_search failed.");
    5329           1 :     fail_unless(results->count == 1, "Unexpected number of results, "
    5330             :                                      "expected [%d], got [%d]",
    5331             :                                      1, results->count);
    5332           1 :     fail_unless(ldb_dn_compare(results->msgs[0]->dn, check_dn) == 0,
    5333             :                 "Unexpedted DN returned");
    5334             : 
    5335             :     /* Subdomains are case-insensitive. Test that the lowercased name
    5336             :      * can be found, too */
    5337           1 :     ret = sysdb_search_user_by_name(test_ctx, subdomain, "subdomuser", attrs,
    5338             :                                     &msg);
    5339           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_name failed.");
    5340             : 
    5341           1 :     ret = sysdb_delete_user(subdomain, "subdomuser", 0);
    5342           1 :     fail_unless(ret == EOK, "sysdb_delete_user failed [%d][%s].",
    5343             :                             ret, strerror(ret));
    5344             : 
    5345           1 :     ret = ldb_search(test_ctx->sysdb->ldb, test_ctx, &results, base_dn,
    5346             :                      LDB_SCOPE_SUBTREE, NULL, "name=subdomuser");
    5347           1 :     fail_unless(ret == EOK, "ldb_search failed.");
    5348           1 :     fail_unless(results->count == 0, "Unexpected number of results, "
    5349             :                                      "expected [%d], got [%d]",
    5350             :                                      0, results->count);
    5351             : }
    5352           1 : END_TEST
    5353             : 
    5354           1 : START_TEST(test_sysdb_subdomain_user_ops)
    5355             : {
    5356             :     struct sysdb_test_ctx *test_ctx;
    5357             :     errno_t ret;
    5358           1 :     struct sss_domain_info *subdomain = NULL;
    5359           1 :     struct ldb_message *msg = NULL;
    5360           1 :     struct ldb_dn *check_dn = NULL;
    5361             : 
    5362           1 :     ret = setup_sysdb_tests(&test_ctx);
    5363           1 :     fail_if(ret != EOK, "Could not set up the test");
    5364             : 
    5365           1 :     subdomain = new_subdomain(test_ctx, test_ctx->domain,
    5366             :                               testdom[0], testdom[1], testdom[2], testdom[3],
    5367             :                               false, false, NULL, 0);
    5368           1 :     fail_unless(subdomain != NULL, "Failed to create new subdomin.");
    5369           1 :     ret = sysdb_subdomain_store(test_ctx->sysdb,
    5370             :                                 testdom[0], testdom[1], testdom[2], testdom[3],
    5371             :                                 false, false, NULL, 0);
    5372           1 :     fail_if(ret != EOK, "Could not set up the test (test subdom)");
    5373             : 
    5374           1 :     ret = sysdb_update_subdomains(test_ctx->domain);
    5375           1 :     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
    5376             :                             ret, strerror(ret));
    5377             : 
    5378           1 :     ret = sysdb_store_user(subdomain, "subdomuser",
    5379             :                            NULL, 12345, 0, "Sub Domain User",
    5380             :                            "/home/subdomuser", "/bin/bash",
    5381             :                            NULL, NULL, NULL, -1, 0);
    5382           1 :     fail_unless(ret == EOK, "sysdb_store_domuser failed.");
    5383             : 
    5384           1 :     check_dn = ldb_dn_new(test_ctx, test_ctx->sysdb->ldb,
    5385             :                           "name=subdomuser,cn=users,cn=test.sub,cn=sysdb");
    5386           1 :     fail_unless(check_dn != NULL);
    5387             : 
    5388           1 :     ret = sysdb_search_user_by_name(test_ctx, subdomain, "subdomuser", NULL,
    5389             :                                     &msg);
    5390           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_name failed with [%d][%s].",
    5391             :                             ret, strerror(ret));
    5392           1 :     fail_unless(ldb_dn_compare(msg->dn, check_dn) == 0,
    5393             :                 "Unexpedted DN returned");
    5394             : 
    5395           1 :     ret = sysdb_search_user_by_uid(test_ctx, subdomain, 12345, NULL, &msg);
    5396           1 :     fail_unless(ret == EOK, "sysdb_search_domuser_by_uid failed with [%d][%s].",
    5397             :                             ret, strerror(ret));
    5398           1 :     fail_unless(ldb_dn_compare(msg->dn, check_dn) == 0,
    5399             :                 "Unexpedted DN returned");
    5400             : 
    5401           1 :     ret = sysdb_delete_user(subdomain, "subdomuser", 12345);
    5402           1 :     fail_unless(ret == EOK, "sysdb_delete_domuser failed with [%d][%s].",
    5403             :                             ret, strerror(ret));
    5404             : 
    5405             : }
    5406           1 : END_TEST
    5407             : 
    5408           1 : START_TEST(test_sysdb_subdomain_group_ops)
    5409             : {
    5410             :     struct sysdb_test_ctx *test_ctx;
    5411             :     errno_t ret;
    5412           1 :     struct sss_domain_info *subdomain = NULL;
    5413           1 :     struct ldb_message *msg = NULL;
    5414           1 :     struct ldb_dn *check_dn = NULL;
    5415             :     struct sysdb_attrs *group_attrs;
    5416             : 
    5417           1 :     ret = setup_sysdb_tests(&test_ctx);
    5418           1 :     fail_if(ret != EOK, "Could not set up the test");
    5419             : 
    5420           1 :     subdomain = new_subdomain(test_ctx, test_ctx->domain,
    5421             :                               testdom[0], testdom[1], testdom[2], testdom[3],
    5422             :                               false, false, NULL, 0);
    5423           1 :     fail_unless(subdomain != NULL, "Failed to create new subdomin.");
    5424           1 :     ret = sysdb_subdomain_store(test_ctx->sysdb,
    5425             :                                 testdom[0], testdom[1], testdom[2], testdom[3],
    5426             :                                 false, false, NULL, 0);
    5427           1 :     fail_if(ret != EOK, "Could not set up the test (test subdom)");
    5428             : 
    5429           1 :     ret = sysdb_update_subdomains(test_ctx->domain);
    5430           1 :     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
    5431             :                             ret, strerror(ret));
    5432             : 
    5433           1 :     group_attrs = sysdb_new_attrs(test_ctx);
    5434           1 :     fail_unless(group_attrs != NULL, "sysdb_new_attrs failed");
    5435             : 
    5436           1 :     ret = sysdb_attrs_add_string(group_attrs, SYSDB_NAME_ALIAS, "subdomgroup");
    5437           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    5438             : 
    5439           1 :     ret = sysdb_store_group(subdomain,
    5440             :                             "subDomGroup", 12345, group_attrs, -1, 0);
    5441           1 :     fail_unless(ret == EOK, "sysdb_store_group failed.");
    5442             : 
    5443           1 :     check_dn = ldb_dn_new(test_ctx, test_ctx->sysdb->ldb,
    5444             :                           "name=subDomGroup,cn=groups,cn=test.sub,cn=sysdb");
    5445           1 :     fail_unless(check_dn != NULL);
    5446             : 
    5447           1 :     ret = sysdb_search_group_by_name(test_ctx, subdomain, "subDomGroup", NULL,
    5448             :                                      &msg);
    5449           1 :     fail_unless(ret == EOK, "sysdb_search_group_by_name failed with [%d][%s].",
    5450             :                             ret, strerror(ret));
    5451           1 :     fail_unless(ldb_dn_compare(msg->dn, check_dn) == 0,
    5452             :                 "Unexpected DN returned");
    5453             : 
    5454             :     /* subdomains are case insensitive, so it should be possible to search
    5455             :     the group with a lowercase name version, too */
    5456           1 :     ret = sysdb_search_group_by_name(test_ctx, subdomain, "subdomgroup", NULL,
    5457             :                                      &msg);
    5458           1 :     fail_unless(ret == EOK, "case-insensitive group search failed with [%d][%s].",
    5459             :                             ret, strerror(ret));
    5460           1 :     fail_unless(ldb_dn_compare(msg->dn, check_dn) == 0,
    5461             :                 "Unexpected DN returned");
    5462             : 
    5463             : 
    5464           1 :     ret = sysdb_search_group_by_gid(test_ctx, subdomain, 12345, NULL, &msg);
    5465           1 :     fail_unless(ret == EOK, "sysdb_search_group_by_gid failed with [%d][%s].",
    5466             :                             ret, strerror(ret));
    5467           1 :     fail_unless(ldb_dn_compare(msg->dn, check_dn) == 0,
    5468             :                 "Unexpedted DN returned");
    5469             : 
    5470           1 :     ret = sysdb_delete_group(subdomain, "subDomGroup", 12345);
    5471           1 :     fail_unless(ret == EOK, "sysdb_delete_group failed with [%d][%s].",
    5472             :                             ret, strerror(ret));
    5473             : 
    5474             : }
    5475           1 : END_TEST
    5476             : 
    5477             : #ifdef BUILD_AUTOFS
    5478          10 : START_TEST(test_autofs_create_map)
    5479             : {
    5480             :     struct sysdb_test_ctx *test_ctx;
    5481             :     const char *autofsmapname;
    5482             :     errno_t ret;
    5483             : 
    5484          10 :     ret = setup_sysdb_tests(&test_ctx);
    5485          10 :     fail_if(ret != EOK, "Could not set up the test");
    5486             : 
    5487          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5488          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5489             : 
    5490          10 :     ret = sysdb_save_autofsmap(test_ctx->domain, autofsmapname,
    5491             :                                autofsmapname, NULL, 0, 0);
    5492          10 :     fail_if(ret != EOK, "Could not store autofs map %s", autofsmapname);
    5493          10 :     talloc_free(test_ctx);
    5494             : }
    5495          10 : END_TEST
    5496             : 
    5497          10 : START_TEST(test_autofs_retrieve_map)
    5498             : {
    5499             :     struct sysdb_test_ctx *test_ctx;
    5500             :     const char *autofsmapname;
    5501             :     errno_t ret;
    5502          10 :     struct ldb_message *map = NULL;
    5503             : 
    5504          10 :     ret = setup_sysdb_tests(&test_ctx);
    5505          10 :     fail_if(ret != EOK, "Could not set up the test");
    5506             : 
    5507          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5508          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5509             : 
    5510          10 :     ret = sysdb_get_map_byname(test_ctx, test_ctx->domain,
    5511             :                                autofsmapname, &map);
    5512          10 :     fail_if(ret != EOK, "Could not retrieve autofs map %s", autofsmapname);
    5513          10 :     fail_if(map == NULL, "No map retrieved?\n");
    5514          10 :     talloc_free(test_ctx);
    5515             : }
    5516          10 : END_TEST
    5517             : 
    5518          10 : START_TEST(test_autofs_delete_map)
    5519             : {
    5520             :     struct sysdb_test_ctx *test_ctx;
    5521             :     const char *autofsmapname;
    5522             :     errno_t ret;
    5523             : 
    5524          10 :     ret = setup_sysdb_tests(&test_ctx);
    5525          10 :     fail_if(ret != EOK, "Could not set up the test");
    5526             : 
    5527          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5528          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5529             : 
    5530          10 :     ret = sysdb_delete_autofsmap(test_ctx->domain, autofsmapname);
    5531          10 :     fail_if(ret != EOK, "Could not retrieve autofs map %s", autofsmapname);
    5532          10 :     talloc_free(test_ctx);
    5533             : }
    5534          10 : END_TEST
    5535             : 
    5536          10 : START_TEST(test_autofs_retrieve_map_neg)
    5537             : {
    5538             :     struct sysdb_test_ctx *test_ctx;
    5539             :     const char *autofsmapname;
    5540             :     errno_t ret;
    5541          10 :     struct ldb_message *map = NULL;
    5542             : 
    5543          10 :     ret = setup_sysdb_tests(&test_ctx);
    5544          10 :     fail_if(ret != EOK, "Could not set up the test");
    5545             : 
    5546          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5547          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5548             : 
    5549          10 :     ret = sysdb_get_map_byname(test_ctx, test_ctx->domain,
    5550             :                                autofsmapname, &map);
    5551          10 :     fail_if(ret != ENOENT, "Expected ENOENT, got %d instead\n", ret);
    5552          10 :     fail_if(map != NULL, "Unexpected map found\n");
    5553          10 :     talloc_free(test_ctx);
    5554             : }
    5555          10 : END_TEST
    5556             : 
    5557          10 : START_TEST(test_autofs_store_entry_in_map)
    5558             : {
    5559             :     struct sysdb_test_ctx *test_ctx;
    5560             :     const char *autofsmapname;
    5561             :     const char *autofskey;
    5562             :     const char *autofsval;
    5563             :     errno_t ret;
    5564             :     int ii;
    5565          10 :     const int limit = 10;
    5566             : 
    5567          10 :     ret = setup_sysdb_tests(&test_ctx);
    5568          10 :     fail_if(ret != EOK, "Could not set up the test");
    5569             : 
    5570          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5571          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5572             : 
    5573         110 :     for (ii=0; ii < limit; ii++) {
    5574         100 :         autofskey = talloc_asprintf(test_ctx, "%s_testkey%d",
    5575             :                                     autofsmapname, ii);
    5576         100 :         fail_if(autofskey == NULL, "Out of memory\n");
    5577             : 
    5578         100 :         autofsval = talloc_asprintf(test_ctx, "testserver:/testval%d", ii);
    5579         100 :         fail_if(autofsval == NULL, "Out of memory\n");
    5580             : 
    5581         100 :         ret = sysdb_save_autofsentry(test_ctx->domain,
    5582             :                                      autofsmapname, autofskey,
    5583             :                                      autofsval, NULL);
    5584         100 :         fail_if(ret != EOK, "Could not save autofs entry %s", autofskey);
    5585             :     }
    5586             : 
    5587          10 :     talloc_free(test_ctx);
    5588             : }
    5589          10 : END_TEST
    5590             : 
    5591          10 : START_TEST(test_autofs_retrieve_keys_by_map)
    5592             : {
    5593             :     struct sysdb_test_ctx *test_ctx;
    5594             :     const char *autofsmapname;
    5595             :     errno_t ret;
    5596             :     size_t count;
    5597             :     struct ldb_message **entries;
    5598          10 :     const int expected = 10;
    5599             : 
    5600          10 :     ret = setup_sysdb_tests(&test_ctx);
    5601          10 :     fail_if(ret != EOK, "Could not set up the test");
    5602             : 
    5603          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5604          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5605             : 
    5606          10 :     ret = sysdb_autofs_entries_by_map(test_ctx, test_ctx->domain,
    5607             :                                       autofsmapname, &count, &entries);
    5608          10 :     fail_if(ret != EOK, "Cannot get autofs entries for map %s\n",
    5609             :             autofsmapname);
    5610          10 :     fail_if(count != expected, "Expected to find %d entries, got %d\n",
    5611             :             expected, count);
    5612          10 :     talloc_free(test_ctx);
    5613             : }
    5614          10 : END_TEST
    5615             : 
    5616          10 : START_TEST(test_autofs_key_duplicate)
    5617             : {
    5618             :     struct sysdb_test_ctx *test_ctx;
    5619             :     const char *autofsmapname;
    5620             :     const char *autofskey;
    5621             :     const char *autofsval;
    5622             :     errno_t ret;
    5623             : 
    5624          10 :     ret = setup_sysdb_tests(&test_ctx);
    5625          10 :     fail_if(ret != EOK, "Could not set up the test");
    5626             : 
    5627          10 :     autofsmapname = talloc_asprintf(test_ctx, "testmap%d", _i);
    5628          10 :     fail_if(autofsmapname == NULL, "Out of memory\n");
    5629             : 
    5630          10 :     autofskey = talloc_asprintf(test_ctx, "testkey");
    5631          10 :     fail_if(autofskey == NULL, "Out of memory\n");
    5632             : 
    5633          10 :     autofsval = talloc_asprintf(test_ctx, "testserver:/testval%d", _i);
    5634          10 :     fail_if(autofsval == NULL, "Out of memory\n");
    5635             : 
    5636          10 :     ret = sysdb_save_autofsentry(test_ctx->domain,
    5637             :                                  autofsmapname, autofskey,
    5638             :                                  autofsval, NULL);
    5639          10 :     fail_if(ret != EOK, "Could not save autofs entry %s", autofskey);
    5640          10 :     talloc_free(test_ctx);
    5641             : }
    5642          10 : END_TEST
    5643             : 
    5644           1 : START_TEST(test_autofs_get_duplicate_keys)
    5645             : {
    5646             :     struct sysdb_test_ctx *test_ctx;
    5647             :     const char *autofskey;
    5648             :     errno_t ret;
    5649           1 :     const char *attrs[] = { SYSDB_AUTOFS_ENTRY_KEY,
    5650             :                             SYSDB_AUTOFS_ENTRY_VALUE,
    5651             :                             NULL };
    5652             :     size_t count;
    5653             :     struct ldb_message **msgs;
    5654             :     struct ldb_dn *dn;
    5655             :     const char *filter;
    5656           1 :     const int expected = 10;
    5657             : 
    5658           1 :     ret = setup_sysdb_tests(&test_ctx);
    5659           1 :     fail_if(ret != EOK, "Could not set up the test");
    5660             : 
    5661           1 :     autofskey = talloc_asprintf(test_ctx, "testkey");
    5662           1 :     fail_if(autofskey == NULL, "Out of memory\n");
    5663             : 
    5664           1 :     filter = talloc_asprintf(test_ctx, "(&(objectclass=%s)(%s=%s))",
    5665             :                              SYSDB_AUTOFS_ENTRY_OC, SYSDB_AUTOFS_ENTRY_KEY, autofskey);
    5666           1 :     fail_if(filter == NULL, "Out of memory\n");
    5667             : 
    5668           1 :     dn = ldb_dn_new_fmt(test_ctx, test_ctx->sysdb->ldb, SYSDB_TMPL_CUSTOM_SUBTREE,
    5669           1 :                         AUTOFS_MAP_SUBDIR, test_ctx->domain->name);
    5670           1 :     fail_if(dn == NULL, "Out of memory\n");
    5671             : 
    5672           1 :     ret = sysdb_search_entry(test_ctx, test_ctx->sysdb, dn, LDB_SCOPE_SUBTREE,
    5673             :                              filter, attrs, &count, &msgs);
    5674           1 :     fail_unless(ret == EOK, "sysdb_search_entry returned [%d]", ret);
    5675           1 :     fail_if(count != expected, "Found %d entries with name %s, expected %d\n",
    5676             :             count, autofskey, expected);
    5677           1 :     talloc_free(test_ctx);
    5678             : }
    5679           1 : END_TEST
    5680             : 
    5681             : #endif /* BUILD_AUTOFS */
    5682             : 
    5683           3 : static struct confdb_ctx *test_cdb_domains_prep(TALLOC_CTX *mem_ctx)
    5684             : {
    5685             :     char *conf_db;
    5686             :     int ret;
    5687             :     struct confdb_ctx *confdb;
    5688             : 
    5689             :     /* Create tests directory if it doesn't exist */
    5690             :     /* (relative to current dir) */
    5691           3 :     ret = mkdir(TESTS_PATH, 0775);
    5692           3 :     if (ret == -1 && errno != EEXIST) {
    5693           0 :         fail("Could not create %s directory", TESTS_PATH);
    5694             :         return NULL;
    5695             :     }
    5696             : 
    5697           3 :     conf_db = talloc_asprintf(mem_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
    5698           3 :     ck_assert(conf_db != NULL);
    5699             : 
    5700             :     /* Make sure the test domain does not interfere with our testing */
    5701           3 :     ret = unlink(TESTS_PATH"/"TEST_CONF_FILE);
    5702           3 :     if (ret != EOK && errno != ENOENT) {
    5703           0 :         fail("Could not remove confdb %s\n", TESTS_PATH"/"TEST_CONF_FILE);
    5704             :         return NULL;
    5705             :     }
    5706             : 
    5707             :     /* Connect to the conf db */
    5708           3 :     ret = confdb_init(mem_ctx, &confdb, conf_db);
    5709           3 :     ck_assert_int_eq(ret, EOK);
    5710             : 
    5711           3 :     return confdb;
    5712             : }
    5713             : 
    5714           1 : START_TEST(test_confdb_list_all_domain_names_no_dom)
    5715             : {
    5716             :     int ret;
    5717             :     TALLOC_CTX *tmp_ctx;
    5718             :     struct confdb_ctx *confdb;
    5719             :     char **names;
    5720             : 
    5721           1 :     tmp_ctx = talloc_new(NULL);
    5722           1 :     ck_assert(tmp_ctx != NULL);
    5723             : 
    5724           1 :     confdb = test_cdb_domains_prep(tmp_ctx);
    5725           1 :     ck_assert(confdb != NULL);
    5726             : 
    5727             :     /* No domain */
    5728           1 :     ret = confdb_list_all_domain_names(tmp_ctx, confdb, &names);
    5729           1 :     ck_assert_int_eq(ret, EOK);
    5730           1 :     ck_assert(names != NULL);
    5731           1 :     ck_assert(names[0] == NULL);
    5732             : 
    5733           1 :     talloc_free(tmp_ctx);
    5734             : }
    5735           1 : END_TEST
    5736             : 
    5737           1 : START_TEST(test_confdb_list_all_domain_names_single_dom)
    5738             : {
    5739             :     int ret;
    5740             :     TALLOC_CTX *tmp_ctx;
    5741             :     struct confdb_ctx *confdb;
    5742             :     char **names;
    5743             : 
    5744             :     const char *val[2];
    5745           1 :     val[1] = NULL;
    5746             : 
    5747           1 :     tmp_ctx = talloc_new(NULL);
    5748           1 :     ck_assert(tmp_ctx != NULL);
    5749             : 
    5750           1 :     confdb = test_cdb_domains_prep(tmp_ctx);
    5751           1 :     ck_assert(confdb != NULL);
    5752             : 
    5753             :     /* One domain */
    5754           1 :     val[0] = "LOCAL";
    5755           1 :     ret = confdb_add_param(confdb, true,
    5756             :                            "config/sssd", "domains", val);
    5757           1 :     ck_assert_int_eq(ret, EOK);
    5758             : 
    5759           1 :     val[0] = "local";
    5760           1 :     ret = confdb_add_param(confdb, true,
    5761             :                            "config/domain/LOCAL", "id_provider", val);
    5762           1 :     ck_assert_int_eq(ret, EOK);
    5763             : 
    5764           1 :     ret = confdb_list_all_domain_names(tmp_ctx, confdb, &names);
    5765           1 :     ck_assert_int_eq(ret, EOK);
    5766           1 :     ck_assert(names != NULL);
    5767           1 :     ck_assert_str_eq(names[0], "LOCAL");
    5768           1 :     ck_assert(names[1] == NULL);
    5769             : 
    5770           1 :     talloc_free(tmp_ctx);
    5771             : }
    5772           1 : END_TEST
    5773             : 
    5774             : #define UPN_USER_NAME "upn_user"
    5775             : #define UPN_PRINC "upn_user@UPN.TEST"
    5776             : #define UPN_PRINC_WRONG_CASE "UpN_uSeR@uPn.TeSt"
    5777             : #define UPN_CANON_PRINC "upn_user@UPN.CANON"
    5778             : #define UPN_CANON_PRINC_WRONG_CASE "uPn_UsEr@UpN.CaNoN"
    5779             : 
    5780           1 : START_TEST(test_upn_basic)
    5781             : {
    5782             :     struct sysdb_test_ctx *test_ctx;
    5783             :     struct sysdb_attrs *attrs;
    5784             :     int ret;
    5785             :     struct ldb_message *msg;
    5786             :     const char *str;
    5787             : 
    5788             :     /* Setup */
    5789           1 :     ret = setup_sysdb_tests(&test_ctx);
    5790           1 :     if (ret != EOK) {
    5791           0 :         fail("Could not set up the test");
    5792             :         return;
    5793             :     }
    5794             : 
    5795           1 :     attrs = sysdb_new_attrs(test_ctx);
    5796           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed.\n");
    5797             : 
    5798           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, UPN_PRINC);
    5799           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    5800             : 
    5801           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_CANONICAL_UPN, UPN_CANON_PRINC);
    5802           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    5803             : 
    5804           1 :     ret = sysdb_store_user(test_ctx->domain,
    5805             :                            UPN_USER_NAME, "x",
    5806             :                            12345, 0, "UPN USER", "/home/upn_user",
    5807             :                            "/bin/bash", NULL,
    5808             :                            attrs, NULL, -1, 0);
    5809           1 :     fail_unless(ret == EOK, "Could not store user.");
    5810             : 
    5811           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5812             :                                    "abc@def.ghi", NULL, &msg);
    5813           1 :     fail_unless(ret == ENOENT,
    5814             :                 "sysdb_search_user_by_upn failed with non-existing UPN.");
    5815             : 
    5816           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5817             :                                    UPN_PRINC, NULL, &msg);
    5818           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_upn failed.");
    5819             : 
    5820           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    5821           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5822           1 :     fail_unless(strcmp(str, UPN_USER_NAME) == 0, "Expected [%s], got [%s].",
    5823             :                                                  UPN_USER_NAME, str);
    5824             : 
    5825           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
    5826           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5827           1 :     fail_unless(strcmp(str, UPN_PRINC) == 0,
    5828             :                 "Expected [%s], got [%s].", UPN_PRINC, str);
    5829             : 
    5830           1 :     talloc_free(test_ctx);
    5831             : }
    5832           1 : END_TEST
    5833             : 
    5834           1 : START_TEST(test_upn_basic_case)
    5835             : {
    5836             :     struct sysdb_test_ctx *test_ctx;
    5837             :     int ret;
    5838             :     struct ldb_message *msg;
    5839             :     const char *str;
    5840             : 
    5841             :     /* Setup */
    5842           1 :     ret = setup_sysdb_tests(&test_ctx);
    5843           1 :     if (ret != EOK) {
    5844           0 :         fail("Could not set up the test");
    5845             :         return;
    5846             :     }
    5847             : 
    5848           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5849             :                                    UPN_PRINC_WRONG_CASE, NULL, &msg);
    5850           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_upn failed.");
    5851             : 
    5852           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    5853           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5854           1 :     fail_unless(strcmp(str, UPN_USER_NAME) == 0, "Expected [%s], got [%s].",
    5855             :                                                  UPN_USER_NAME, str);
    5856             : 
    5857           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
    5858           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5859           1 :     fail_unless(strcmp(str, UPN_PRINC) == 0,
    5860             :                 "Expected [%s], got [%s].", UPN_PRINC, str);
    5861             : 
    5862           1 :     talloc_free(test_ctx);
    5863             : }
    5864           1 : END_TEST
    5865             : 
    5866           1 : START_TEST(test_upn_canon)
    5867             : {
    5868             :     struct sysdb_test_ctx *test_ctx;
    5869             :     int ret;
    5870             :     struct ldb_message *msg;
    5871             :     const char *str;
    5872             : 
    5873             :     /* Setup */
    5874           1 :     ret = setup_sysdb_tests(&test_ctx);
    5875           1 :     if (ret != EOK) {
    5876           0 :         fail("Could not set up the test");
    5877             :         return;
    5878             :     }
    5879             : 
    5880           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5881             :                                    UPN_CANON_PRINC, NULL, &msg);
    5882           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_upn failed.");
    5883             : 
    5884           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    5885           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5886           1 :     fail_unless(strcmp(str, UPN_USER_NAME) == 0, "Expected [%s], got [%s].",
    5887             :                                                  UPN_USER_NAME, str);
    5888             : 
    5889           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
    5890           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5891           1 :     fail_unless(strcmp(str, UPN_PRINC) == 0,
    5892             :                 "Expected [%s], got [%s].", UPN_PRINC, str);
    5893             : 
    5894           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_CANONICAL_UPN, NULL);
    5895           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5896           1 :     fail_unless(strcmp(str, UPN_CANON_PRINC) == 0,
    5897             :                 "Expected [%s], got [%s].", UPN_CANON_PRINC, str);
    5898             : 
    5899           1 :     talloc_free(test_ctx);
    5900             : }
    5901           1 : END_TEST
    5902             : 
    5903           1 : START_TEST(test_upn_canon_case)
    5904             : {
    5905             :     struct sysdb_test_ctx *test_ctx;
    5906             :     int ret;
    5907             :     struct ldb_message *msg;
    5908             :     const char *str;
    5909             : 
    5910             :     /* Setup */
    5911           1 :     ret = setup_sysdb_tests(&test_ctx);
    5912           1 :     if (ret != EOK) {
    5913           0 :         fail("Could not set up the test");
    5914             :         return;
    5915             :     }
    5916             : 
    5917           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5918             :                                    UPN_CANON_PRINC_WRONG_CASE, NULL, &msg);
    5919           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_upn failed.");
    5920             : 
    5921           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    5922           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5923           1 :     fail_unless(strcmp(str, UPN_USER_NAME) == 0, "Expected [%s], got [%s].",
    5924             :                                                  UPN_USER_NAME, str);
    5925             : 
    5926           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
    5927           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5928           1 :     fail_unless(strcmp(str, UPN_PRINC) == 0,
    5929             :                 "Expected [%s], got [%s].", UPN_PRINC, str);
    5930             : 
    5931           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_CANONICAL_UPN, NULL);
    5932           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5933           1 :     fail_unless(strcmp(str, UPN_CANON_PRINC) == 0,
    5934             :                 "Expected [%s], got [%s].", UPN_CANON_PRINC, str);
    5935             : 
    5936           1 :     talloc_free(test_ctx);
    5937             : }
    5938           1 : END_TEST
    5939             : 
    5940           1 : START_TEST(test_upn_dup)
    5941             : {
    5942             :     struct sysdb_test_ctx *test_ctx;
    5943             :     struct sysdb_attrs *attrs;
    5944             :     int ret;
    5945             :     struct ldb_message *msg;
    5946             :     const char *str;
    5947             : 
    5948             :     /* Setup */
    5949           1 :     ret = setup_sysdb_tests(&test_ctx);
    5950           1 :     if (ret != EOK) {
    5951           0 :         fail("Could not set up the test");
    5952             :         return;
    5953             :     }
    5954             : 
    5955           1 :     attrs = sysdb_new_attrs(test_ctx);
    5956           1 :     fail_unless(attrs != NULL, "sysdb_new_attrs failed.\n");
    5957             : 
    5958           1 :     ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, UPN_CANON_PRINC);
    5959           1 :     fail_unless(ret == EOK, "sysdb_attrs_add_string failed.");
    5960             : 
    5961           1 :     ret = sysdb_store_user(test_ctx->domain,
    5962             :                            UPN_USER_NAME"_dup", "x",
    5963             :                            23456, 0, "UPN USER DUP", "/home/upn_user_dup",
    5964             :                            "/bin/bash", NULL,
    5965             :                            attrs, NULL, -1, 0);
    5966           1 :     fail_unless(ret == EOK, "Could not store user.");
    5967             : 
    5968           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5969             :                                    UPN_CANON_PRINC, NULL, &msg);
    5970           1 :     fail_unless(ret == EINVAL,
    5971             :                 "sysdb_search_user_by_upn failed for duplicated UPN.");
    5972             : 
    5973           1 :     ret = sysdb_search_user_by_upn(test_ctx, test_ctx->domain,
    5974             :                                    UPN_PRINC, NULL, &msg);
    5975           1 :     fail_unless(ret == EOK, "sysdb_search_user_by_upn failed.");
    5976             : 
    5977           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
    5978           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5979           1 :     fail_unless(strcmp(str, UPN_USER_NAME) == 0, "Expected [%s], got [%s].",
    5980             :                                                  UPN_USER_NAME, str);
    5981             : 
    5982           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
    5983           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5984           1 :     fail_unless(strcmp(str, UPN_PRINC) == 0,
    5985             :                 "Expected [%s], got [%s].", UPN_PRINC, str);
    5986             : 
    5987           1 :     str = ldb_msg_find_attr_as_string(msg, SYSDB_CANONICAL_UPN, NULL);
    5988           1 :     fail_unless(str != NULL, "ldb_msg_find_attr_as_string failed.");
    5989           1 :     fail_unless(strcmp(str, UPN_CANON_PRINC) == 0,
    5990             :                 "Expected [%s], got [%s].", UPN_CANON_PRINC, str);
    5991             : 
    5992           1 :     talloc_free(test_ctx);
    5993             : }
    5994           1 : END_TEST
    5995             : 
    5996           1 : START_TEST(test_gpo_store_retrieve)
    5997             : {
    5998             :     struct sysdb_test_ctx *test_ctx;
    5999             :     errno_t ret;
    6000           1 :     struct ldb_result *result = NULL;
    6001             :     const char *guid;
    6002             :     int version;
    6003             :     static const char *test_guid = "3610EDA5-77EF-11D2-8DC5-00C04FA31A66";
    6004             : 
    6005           1 :     ret = setup_sysdb_tests(&test_ctx);
    6006           1 :     fail_if(ret != EOK, "Could not set up the test");
    6007             : 
    6008           1 :     ret = sysdb_gpo_get_gpo_by_guid(test_ctx, test_ctx->domain,
    6009             :                                     test_guid,
    6010             :                                     &result);
    6011           1 :     fail_if(ret != ENOENT, "GPO present in cache before store op");
    6012             : 
    6013           1 :     ret = sysdb_gpo_get_gpos(test_ctx, test_ctx->domain, &result);
    6014           1 :     fail_if(ret != ENOENT, "GPO present in cache before store op");
    6015             : 
    6016           1 :     ret = sysdb_gpo_store_gpo(test_ctx->domain,
    6017             :                               test_guid, 1, 5, 0);
    6018           1 :     fail_if(ret != EOK, "Could not store a test GPO");
    6019             : 
    6020           1 :     ret = sysdb_gpo_get_gpos(test_ctx, test_ctx->domain, &result);
    6021           1 :     fail_if(ret != EOK, "GPOs not in cache after store op");
    6022           1 :     fail_if(result == NULL);
    6023           1 :     fail_if(result->count != 1);
    6024             : 
    6025           1 :     result = NULL;
    6026           1 :     ret = sysdb_gpo_get_gpo_by_guid(test_ctx, test_ctx->domain,
    6027             :                                     test_guid, &result);
    6028           1 :     fail_if(ret != EOK, "GPO not in cache after store op");
    6029           1 :     fail_if(result == NULL);
    6030           1 :     fail_if(result->count != 1);
    6031             : 
    6032           1 :     guid = ldb_msg_find_attr_as_string(result->msgs[0],
    6033             :                                        SYSDB_GPO_GUID_ATTR, NULL);
    6034           1 :     ck_assert_str_eq(guid, test_guid);
    6035             : 
    6036           1 :     version = ldb_msg_find_attr_as_uint(result->msgs[0],
    6037             :                                         SYSDB_GPO_VERSION_ATTR, 0);
    6038           1 :     ck_assert_int_eq(version, 1);
    6039           1 :     talloc_free(test_ctx);
    6040             : }
    6041           1 : END_TEST
    6042             : 
    6043           1 : START_TEST(test_gpo_replace)
    6044             : {
    6045             :     struct sysdb_test_ctx *test_ctx;
    6046             :     errno_t ret;
    6047           1 :     struct ldb_result *result = NULL;
    6048             :     const char *guid;
    6049             :     int version;
    6050             :     static const char *test_guid = "3610EDA5-77EF-11D2-8DC5-00C04FA31A66";
    6051             : 
    6052           1 :     ret = setup_sysdb_tests(&test_ctx);
    6053           1 :     fail_if(ret != EOK, "Could not setup the test");
    6054             : 
    6055           1 :     ret = sysdb_gpo_get_gpo_by_guid(test_ctx, test_ctx->domain,
    6056             :                                     test_guid, &result);
    6057           1 :     fail_if(ret != EOK, "GPO not in cache after store op");
    6058           1 :     fail_if(result == NULL);
    6059           1 :     fail_if(result->count != 1);
    6060             : 
    6061           1 :     guid = ldb_msg_find_attr_as_string(result->msgs[0],
    6062             :                                        SYSDB_GPO_GUID_ATTR, NULL);
    6063           1 :     ck_assert_str_eq(guid, test_guid);
    6064             : 
    6065           1 :     version = ldb_msg_find_attr_as_uint(result->msgs[0],
    6066             :                                         SYSDB_GPO_VERSION_ATTR, 0);
    6067           1 :     ck_assert_int_eq(version, 1);
    6068             : 
    6069             :     /* Modify the version */
    6070           1 :     ret = sysdb_gpo_store_gpo(test_ctx->domain,
    6071             :                               test_guid, 2, 5, 0);
    6072           1 :     fail_if(ret != EOK, "Could not store a test GPO");
    6073             : 
    6074           1 :     ret = sysdb_gpo_get_gpo_by_guid(test_ctx, test_ctx->domain,
    6075             :                                     test_guid, &result);
    6076           1 :     fail_if(ret != EOK, "GPO not in cache after modify op");
    6077           1 :     fail_if(result == NULL);
    6078           1 :     fail_if(result->count != 1);
    6079             : 
    6080           1 :     guid = ldb_msg_find_attr_as_string(result->msgs[0],
    6081             :                                        SYSDB_GPO_GUID_ATTR, NULL);
    6082           1 :     ck_assert_str_eq(guid, test_guid);
    6083             : 
    6084           1 :     version = ldb_msg_find_attr_as_uint(result->msgs[0],
    6085             :                                         SYSDB_GPO_VERSION_ATTR, 0);
    6086           1 :     ck_assert_int_eq(version, 2);
    6087           1 :     talloc_free(test_ctx);
    6088             : }
    6089           1 : END_TEST
    6090             : 
    6091           1 : START_TEST(test_gpo_result)
    6092             : {
    6093             :     errno_t ret;
    6094             :     struct sysdb_test_ctx *test_ctx;
    6095           1 :     const char *allow_key = "SeRemoteInteractiveLogonRight";
    6096           1 :     const char *deny_key = "SeDenyRemoteInteractiveLogonRight";
    6097           1 :     const char *value = NULL;
    6098             : 
    6099           1 :     ret = setup_sysdb_tests(&test_ctx);
    6100           1 :     fail_if(ret != EOK, "Could not setup the test");
    6101             : 
    6102             :     /* No result in cache */
    6103           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6104             :                                            allow_key, &value);
    6105           1 :     ck_assert_int_eq(ret, ENOENT);
    6106             : 
    6107           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6108             :                                            deny_key, &value);
    6109           1 :     ck_assert_int_eq(ret, ENOENT);
    6110             : 
    6111             :     /* Delete with no result object is a noop */
    6112           1 :     ret = sysdb_gpo_delete_gpo_result_object(test_ctx, test_ctx->domain);
    6113           1 :     ck_assert_int_eq(ret, EOK);
    6114             : 
    6115             :     /* Store an allow value, triggering a new result object */
    6116           1 :     ret = sysdb_gpo_store_gpo_result_setting(test_ctx->domain,
    6117             :                                              allow_key, "allow_val1");
    6118           1 :     ck_assert_int_eq(ret, EOK);
    6119             : 
    6120             :     /* Now both searches should succeed, but only allow_key should return
    6121             :      * a valid value
    6122             :      */
    6123           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6124             :                                            allow_key, &value);
    6125           1 :     ck_assert_int_eq(ret, EOK);
    6126           1 :     ck_assert_str_eq(value, "allow_val1");
    6127             : 
    6128           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6129             :                                            deny_key, &value);
    6130           1 :     ck_assert_int_eq(ret, EOK);
    6131           1 :     fail_unless(value == NULL);
    6132             : 
    6133             :     /* Updating replaces the original value */
    6134           1 :     ret = sysdb_gpo_store_gpo_result_setting(test_ctx->domain,
    6135             :                                              allow_key, "allow_val2");
    6136           1 :     ck_assert_int_eq(ret, EOK);
    6137             : 
    6138           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6139             :                                            allow_key, &value);
    6140           1 :     ck_assert_int_eq(ret, EOK);
    6141           1 :     ck_assert_str_eq(value, "allow_val2");
    6142             : 
    6143             :     /* NULL removes the value completely */
    6144           1 :     ret = sysdb_gpo_store_gpo_result_setting(test_ctx->domain,
    6145             :                                              allow_key, NULL);
    6146           1 :     ck_assert_int_eq(ret, EOK);
    6147             : 
    6148           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6149             :                                            allow_key, &value);
    6150           1 :     ck_assert_int_eq(ret, EOK);
    6151           1 :     fail_unless(value == NULL);
    6152             : 
    6153             :     /* Delete the result */
    6154           1 :     ret = sysdb_gpo_delete_gpo_result_object(test_ctx, test_ctx->domain);
    6155           1 :     ck_assert_int_eq(ret, EOK);
    6156             : 
    6157             :     /* No result in cache */
    6158           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6159             :                                            allow_key, &value);
    6160           1 :     ck_assert_int_eq(ret, ENOENT);
    6161             : 
    6162           1 :     ret = sysdb_gpo_get_gpo_result_setting(test_ctx, test_ctx->domain,
    6163             :                                            deny_key, &value);
    6164           1 :     ck_assert_int_eq(ret, ENOENT);
    6165             : }
    6166           1 : END_TEST
    6167             : 
    6168           1 : START_TEST(test_confdb_list_all_domain_names_multi_dom)
    6169             : {
    6170             :     int ret;
    6171             :     TALLOC_CTX *tmp_ctx;
    6172             :     struct confdb_ctx *confdb;
    6173             :     char **names;
    6174             : 
    6175             :     const char *val[2];
    6176           1 :     val[1] = NULL;
    6177             : 
    6178           1 :     tmp_ctx = talloc_new(NULL);
    6179           1 :     ck_assert(tmp_ctx != NULL);
    6180             : 
    6181           1 :     confdb = test_cdb_domains_prep(tmp_ctx);
    6182           1 :     ck_assert(confdb != NULL);
    6183             : 
    6184             :     /* Two domains */
    6185           1 :     val[0] = "LOCAL";
    6186           1 :     ret = confdb_add_param(confdb, true,
    6187             :                            "config/sssd", "domains", val);
    6188           1 :     ck_assert_int_eq(ret, EOK);
    6189             : 
    6190           1 :     val[0] = "local";
    6191           1 :     ret = confdb_add_param(confdb, true,
    6192             :                            "config/domain/LOCAL", "id_provider", val);
    6193           1 :     ck_assert_int_eq(ret, EOK);
    6194             : 
    6195           1 :     val[0] = "REMOTE";
    6196           1 :     ret = confdb_add_param(confdb, true,
    6197             :                            "config/sssd", "domains", val);
    6198           1 :     ck_assert_int_eq(ret, EOK);
    6199             : 
    6200           1 :     val[0] = "local";
    6201           1 :     ret = confdb_add_param(confdb, true,
    6202             :                            "config/domain/REMOTE", "id_provider", val);
    6203           1 :     ck_assert_int_eq(ret, EOK);
    6204             : 
    6205           1 :     ret = confdb_list_all_domain_names(tmp_ctx, confdb, &names);
    6206           1 :     ck_assert_int_eq(ret, EOK);
    6207           1 :     ck_assert(names != NULL);
    6208           1 :     ck_assert_str_eq(names[0], "LOCAL");
    6209           1 :     ck_assert_str_eq(names[1], "REMOTE");
    6210           1 :     ck_assert(names[2] == NULL);
    6211           1 :     talloc_free(tmp_ctx);
    6212             : }
    6213           1 : END_TEST
    6214             : 
    6215           1 : START_TEST(test_sysdb_mark_entry_as_expired_ldb_dn)
    6216             : {
    6217             :     errno_t ret;
    6218             :     struct sysdb_test_ctx *test_ctx;
    6219           1 :     const char *attrs[] = { SYSDB_CACHE_EXPIRE, NULL };
    6220             :     size_t count;
    6221             :     struct ldb_message **msgs;
    6222             :     uint64_t expire;
    6223             :     struct ldb_dn *userdn;
    6224             : 
    6225           1 :     ret = setup_sysdb_tests(&test_ctx);
    6226           1 :     fail_if(ret != EOK, "Could not setup the test");
    6227             : 
    6228             :     /* Add something to database to test against */
    6229             : 
    6230           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    6231           1 :     ck_assert_int_eq(ret, EOK);
    6232             : 
    6233           1 :     ret = sysdb_add_user(test_ctx->domain, "testuser",
    6234             :                          2000, 0, "Test User", "/home/testuser",
    6235             :                          "/bin/bash",
    6236             :                          NULL, NULL, 500, 0);
    6237           1 :     ck_assert_int_eq(ret, EOK);
    6238             : 
    6239           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    6240           1 :     ck_assert_int_eq(ret, EOK);
    6241             : 
    6242           1 :     ret = sysdb_search_users(test_ctx, test_ctx->domain,
    6243             :                              "("SYSDB_UIDNUM"=2000)", attrs, &count, &msgs);
    6244           1 :     ck_assert_int_eq(ret, EOK);
    6245           1 :     ck_assert_int_eq(count, 1);
    6246             : 
    6247           1 :     expire = ldb_msg_find_attr_as_uint64(msgs[0], SYSDB_CACHE_EXPIRE, 0);
    6248           1 :     ck_assert(expire != 1);
    6249             : 
    6250           1 :     userdn = sysdb_user_dn(test_ctx, test_ctx->domain, "testuser");
    6251           1 :     ck_assert(userdn != NULL);
    6252             : 
    6253           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    6254           1 :     ck_assert_int_eq(ret, EOK);
    6255             : 
    6256             :     /* Expire entry */
    6257           1 :     ret = sysdb_mark_entry_as_expired_ldb_dn(test_ctx->domain, userdn);
    6258           1 :     ck_assert_int_eq(ret, EOK);
    6259             : 
    6260           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    6261           1 :     ck_assert_int_eq(ret, EOK);
    6262             : 
    6263           1 :     ret = sysdb_search_users(test_ctx, test_ctx->domain,
    6264             :                              "("SYSDB_UIDNUM"=2000)", attrs, &count, &msgs);
    6265           1 :     ck_assert_int_eq(ret, EOK);
    6266           1 :     ck_assert_int_eq(count, 1);
    6267             : 
    6268           1 :     expire = ldb_msg_find_attr_as_uint64(msgs[0], SYSDB_CACHE_EXPIRE, 0);
    6269           1 :     ck_assert_int_eq(expire, 1);
    6270             : 
    6271             :     /* Try to expire already expired entry. Should return EOK. */
    6272           1 :     ret = sysdb_transaction_start(test_ctx->sysdb);
    6273           1 :     ck_assert_int_eq(ret, EOK);
    6274             : 
    6275           1 :     ret = sysdb_mark_entry_as_expired_ldb_dn(test_ctx->domain, userdn);
    6276           1 :     ck_assert_int_eq(ret, EOK);
    6277             : 
    6278           1 :     ret = sysdb_transaction_commit(test_ctx->sysdb);
    6279           1 :     ck_assert_int_eq(ret, EOK);
    6280             : }
    6281           1 : END_TEST
    6282             : 
    6283           1 : Suite *create_sysdb_suite(void)
    6284             : {
    6285           1 :     Suite *s = suite_create("sysdb");
    6286             : 
    6287           1 :     TCase *tc_sysdb = tcase_create("SYSDB Tests");
    6288             : 
    6289             :     /* test getting next id works */
    6290           1 :     tcase_add_test(tc_sysdb, test_sysdb_get_new_id);
    6291             : 
    6292             :     /* Add a user with an automatic ID */
    6293           1 :     tcase_add_test(tc_sysdb, test_sysdb_user_new_id);
    6294             : 
    6295             :     /* Create a new user */
    6296           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_add_user,27000,27010);
    6297             : 
    6298             :     /* Verify the users were added */
    6299           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_getpwnam, 27000, 27010);
    6300             : 
    6301             :     /* Create a new group */
    6302           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_add_group, 28000, 28010);
    6303             : 
    6304             :     /* Verify the groups were added */
    6305           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_getgrnam, 28000, 28010);
    6306             : 
    6307             :     /* sysdb_group_dn_name returns the name of the group in question */
    6308           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_group_dn_name, 28000, 28010);
    6309             : 
    6310             :     /* sysdb_store_user allows setting attributes for existing users */
    6311           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_store_user_existing, 27000, 27010);
    6312             : 
    6313             :     /* test the change */
    6314           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_get_user_attr, 27000, 27010);
    6315             : 
    6316             :     /* Add and remove users in a group with sysdb_update_members */
    6317           1 :     tcase_add_test(tc_sysdb, test_sysdb_update_members);
    6318             : 
    6319             :     /* Remove the other half by gid */
    6320           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_local_group_by_gid, 28000, 28010);
    6321             : 
    6322             :     /* Remove the other half by uid */
    6323           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_local_user_by_uid, 27000, 27010);
    6324             : 
    6325             :     /* Create a new user */
    6326           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_store_user, 27010, 27020);
    6327             : 
    6328             :     /* Verify the users were added */
    6329           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_getpwnam, 27010, 27020);
    6330             : 
    6331             :     /* Verify the users can be queried by UID */
    6332           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_getpwuid, 27010, 27020);
    6333             : 
    6334             :     /* Enumerate the users */
    6335           1 :     tcase_add_test(tc_sysdb, test_sysdb_enumpwent);
    6336             : 
    6337             :     /* Change their attribute */
    6338           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_set_user_attr, 27010, 27020);
    6339             : 
    6340             :     /* Find the users by their new attribute */
    6341           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_search_users, 27010, 27020);
    6342             : 
    6343             :     /* Verify the change */
    6344           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_get_user_attr, 27010, 27020);
    6345             : 
    6346             :     /* Remove the attribute */
    6347           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_attrs, 27010, 27020);
    6348             : 
    6349             :     /* Create a new group */
    6350           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_store_group, 28010, 28020);
    6351             : 
    6352             :     /* Verify the groups were added */
    6353             : 
    6354             :     /* Verify the groups can be queried by GID */
    6355           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_getgrgid, 28010, 28020);
    6356             : 
    6357             :     /* Find the users by GID using a filter */
    6358           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_search_groups, 28010, 28020);
    6359             : 
    6360             :     /* Enumerate the groups */
    6361           1 :     tcase_add_test(tc_sysdb, test_sysdb_enumgrent);
    6362             : 
    6363             :     /* Add some members to the groups */
    6364           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_add_group_member, 28010, 28020);
    6365             : 
    6366             :     /* Test that sysdb_initgroups() works */
    6367           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_initgroups, 27010, 27020);
    6368             : 
    6369             :     /* Authenticate with missing cached password */
    6370           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_cached_authentication_missing_password,
    6371             :                         27010, 27011);
    6372             : 
    6373             :     /* Add a cached password */
    6374           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_cache_password, 27010, 27011);
    6375             : 
    6376             :     /* Authenticate against cached password */
    6377           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_cached_authentication_wrong_password,
    6378             :                         27010, 27011);
    6379           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_cached_authentication, 27010, 27011);
    6380             : 
    6381           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_cache_password_ex, 27010, 27011);
    6382             : 
    6383             :     /* ASQ search test */
    6384           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_prepare_asq_test_user, 28011, 28020);
    6385           1 :     tcase_add_test(tc_sysdb, test_sysdb_asq_search);
    6386             : 
    6387             :     /* Test search with more than one result */
    6388           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_all_users);
    6389             : 
    6390             :     /* Remove the members from the groups */
    6391           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_group_member, 28010, 28020);
    6392             : 
    6393             :     /* Remove the users by name */
    6394           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_local_user, 27010, 27020);
    6395             : 
    6396             :     /* Remove the groups by name */
    6397           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_local_group, 28010, 28020);
    6398             : 
    6399             :     /* test the ignore_not_found parameter for users */
    6400           1 :     tcase_add_test(tc_sysdb, test_sysdb_remove_nonexistent_user);
    6401             : 
    6402             :     /* test the ignore_not_found parameter for groups */
    6403           1 :     tcase_add_test(tc_sysdb, test_sysdb_remove_nonexistent_group);
    6404             : 
    6405             :     /* Create incomplete groups - remove will fail if the LDB objects don't exist */
    6406           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_add_incomplete_group, 28000, 28010);
    6407           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_local_group_by_gid, 28000, 28010);
    6408             : 
    6409             :     /* test custom operations */
    6410           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_store_custom, 29010, 29020);
    6411           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_custom_by_name);
    6412           1 :     tcase_add_test(tc_sysdb, test_sysdb_update_custom);
    6413           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_custom_update);
    6414           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_custom);
    6415           1 :     tcase_add_test(tc_sysdb, test_sysdb_delete_custom);
    6416           1 :     tcase_add_test(tc_sysdb, test_sysdb_delete_by_sid);
    6417             : 
    6418             :     /* test recursive delete */
    6419           1 :     tcase_add_test(tc_sysdb, test_sysdb_delete_recursive);
    6420             : 
    6421           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_replace_name);
    6422             : 
    6423           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_to_list);
    6424             : 
    6425             :     /* Test unusual characters */
    6426           1 :     tcase_add_test(tc_sysdb, test_odd_characters);
    6427             : 
    6428             :     /* Test sysdb enumerated flag */
    6429           1 :     tcase_add_test(tc_sysdb, test_sysdb_has_enumerated);
    6430             : 
    6431             :     /* Test originalDN searches */
    6432           1 :     tcase_add_test(tc_sysdb, test_sysdb_original_dn_case_insensitive);
    6433             : 
    6434             :     /* Test SID string searches */
    6435           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_sid_str);
    6436             : 
    6437             :     /* Test UUID string searches */
    6438           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_object_by_uuid);
    6439             : 
    6440             :     /* Test user by certificate searches */
    6441           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_user_by_cert);
    6442             : 
    6443             :     /* Test canonicalizing names */
    6444           1 :     tcase_add_test(tc_sysdb, test_sysdb_get_real_name);
    6445             : 
    6446             :     /* Test user and group renames */
    6447           1 :     tcase_add_test(tc_sysdb, test_group_rename);
    6448           1 :     tcase_add_test(tc_sysdb, test_user_rename);
    6449             : 
    6450             :     /* Test GetUserAttr with subdomain user */
    6451           1 :     tcase_add_test(tc_sysdb, test_sysdb_get_user_attr_subdomain);
    6452             : 
    6453             : /* ===== NETGROUP TESTS ===== */
    6454             : 
    6455             :     /* Create a new netgroup */
    6456           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_add_basic_netgroup, 27000, 27010);
    6457             : 
    6458             :     /* Verify the netgroups were added */
    6459           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_search_netgroup_by_name, 27000, 27010);
    6460             : 
    6461             :     /* Test setting attributes */
    6462           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_set_netgroup_attr, 27000, 27010);
    6463             : 
    6464             :     /* Verify they have been changed */
    6465           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_get_netgroup_attr, 27000, 27010);
    6466             : 
    6467             :     /* Remove half of them by name */
    6468           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_netgroup_by_name, 27000, 27005);
    6469             : 
    6470             :     /* Remove the other half by DN */
    6471           1 :     tcase_add_loop_test(tc_sysdb, test_sysdb_remove_netgroup_entry, 27005, 27010);
    6472             : 
    6473           1 :     tcase_add_test(tc_sysdb, test_netgroup_base_dn);
    6474             : 
    6475             : /* ===== SERVICE TESTS ===== */
    6476             : 
    6477             :     /* Create a new service */
    6478           1 :     tcase_add_test(tc_sysdb, test_sysdb_add_services);
    6479           1 :     tcase_add_test(tc_sysdb, test_sysdb_store_services);
    6480           1 :     tcase_add_test(tc_sysdb, test_sysdb_svc_remove_alias);
    6481             : 
    6482           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_lc_name_alias);
    6483             : 
    6484             : /* ===== UTIL TESTS ===== */
    6485           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_get_string_array);
    6486           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_val);
    6487           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_val_safe);
    6488           1 :     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_string_safe);
    6489             : 
    6490             : /* ===== Test search return empty result ===== */
    6491           1 :     tcase_add_test(tc_sysdb, test_sysdb_search_return_ENOENT);
    6492             : 
    6493             : /* ===== Misc ===== */
    6494           1 :     tcase_add_test(tc_sysdb, test_sysdb_set_get_bool);
    6495           1 :     tcase_add_test(tc_sysdb, test_sysdb_mark_entry_as_expired_ldb_dn);
    6496             : 
    6497             : /* Add all test cases to the test suite */
    6498           1 :     suite_add_tcase(s, tc_sysdb);
    6499             : 
    6500           1 :     TCase *tc_memberof = tcase_create("SYSDB member/memberof/memberuid Tests");
    6501             : 
    6502           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group, 0, 10);
    6503           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_user, 0, 10);
    6504           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_add_group_member,
    6505             :                         0, 10);
    6506           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_memberuid,
    6507             :                         0, 10);
    6508           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6509             :                         MBO_GROUP_BASE + 5, MBO_GROUP_BASE + 6);
    6510           1 :     tcase_add_loop_test(tc_memberof,
    6511             :                         test_sysdb_memberof_check_memberuid_without_group_5,
    6512             :                         0, 10);
    6513           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6514             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6515             : 
    6516           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group, 0, 10);
    6517           1 :     tcase_add_test(tc_memberof, test_sysdb_memberof_close_loop);
    6518           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_user, 0, 10);
    6519           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_add_group_member,
    6520             :                         0, 10);
    6521           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_memberuid_loop,
    6522             :                         0, 10);
    6523           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6524             :                         MBO_GROUP_BASE + 5, MBO_GROUP_BASE + 6);
    6525           1 :     tcase_add_loop_test(tc_memberof,
    6526             :                         test_sysdb_memberof_check_memberuid_loop_without_group_5,
    6527             :                         0, 10);
    6528           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6529             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6530             : 
    6531             :     /* Ghost users tests */
    6532           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group_with_ghosts,
    6533             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6534           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_ghosts,
    6535             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6536           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_remove_child_group_and_check_ghost,
    6537             :                         MBO_GROUP_BASE + 1, MBO_GROUP_BASE + 10);
    6538             :     /* Only one group should be left now */
    6539           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6540             :                         MBO_GROUP_BASE + 9 , MBO_GROUP_BASE + 10);
    6541             : 
    6542             :     /* ghost users - RFC2307 */
    6543             :     /* Add groups with ghost users */
    6544           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_add_group_with_ghosts,
    6545             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6546             :     /* Check the ghost user attribute */
    6547           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_ghost,
    6548             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6549             :     /* Add user entries, converting the ghost attributes to member attributes */
    6550             :     /* We only convert half of the users and keep the ghost attributes for the
    6551             :      * other half as we also want to test if we don't delete any ghost users
    6552             :      * by accident
    6553             :      */
    6554           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_convert_to_real_users,
    6555             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + NUM_GHOSTS/2);
    6556             :     /* Check the members and ghosts are there as appropriate */
    6557           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_convert,
    6558             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + NUM_GHOSTS);
    6559             :     /* Rename the other half */
    6560           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_ghost_replace,
    6561             :                         MBO_GROUP_BASE + NUM_GHOSTS/2 + 1,
    6562             :                         MBO_GROUP_BASE + NUM_GHOSTS);
    6563             :     /* Attempt to replace with the same data to check if noop works correctly */
    6564           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_ghost_replace_noop,
    6565             :                         MBO_GROUP_BASE + NUM_GHOSTS/2 + 1,
    6566             :                         MBO_GROUP_BASE + NUM_GHOSTS);
    6567             :     /* Remove the real users */
    6568           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_user_cleanup,
    6569             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + NUM_GHOSTS/2);
    6570           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6571             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + NUM_GHOSTS);
    6572             : 
    6573             :     /* ghost users - memberof mod_del */
    6574           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group_with_ghosts,
    6575             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6576           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_ghosts,
    6577             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6578           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_mod_del,
    6579             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6580           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6581             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + NUM_GHOSTS);
    6582             : 
    6583             :     /* ghost users - memberof mod_add */
    6584             :     /* Add groups without ghosts first */
    6585           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group, 0, 10);
    6586             :     /* Add ghosts to groups so that they propagate */
    6587           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_mod_add,
    6588             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6589             :     /* Check if the ghosts in fact propagated */
    6590           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_ghosts,
    6591             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6592             :     /* Clean up */
    6593           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6594             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6595             : 
    6596             :     /* ghost users - replace */
    6597           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group_with_ghosts,
    6598             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6599           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_ghosts,
    6600             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6601           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_mod_replace,
    6602             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6603           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6604             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6605             : 
    6606             :     /* ghost users - replace but retain inherited */
    6607           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_store_group_with_double_ghosts,
    6608             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6609           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_check_nested_double_ghosts,
    6610             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6611             : 
    6612             :     /* SSS_LDB_SEARCH */
    6613           1 :     tcase_add_test(tc_sysdb, test_SSS_LDB_SEARCH);
    6614             : 
    6615             :     /* This loop counts backwards so the indexing is a little odd */
    6616           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_memberof_mod_replace_keep,
    6617             :                         1 , 11);
    6618           1 :     tcase_add_loop_test(tc_memberof, test_sysdb_remove_local_group_by_gid,
    6619             :                         MBO_GROUP_BASE , MBO_GROUP_BASE + 10);
    6620             : 
    6621           1 :     suite_add_tcase(s, tc_memberof);
    6622             : 
    6623           1 :     TCase *tc_subdomain = tcase_create("SYSDB sub-domain Tests");
    6624             : 
    6625           1 :     tcase_add_test(tc_subdomain, test_sysdb_subdomain_store_user);
    6626           1 :     tcase_add_test(tc_subdomain, test_sysdb_subdomain_user_ops);
    6627           1 :     tcase_add_test(tc_subdomain, test_sysdb_subdomain_group_ops);
    6628             : 
    6629           1 :     suite_add_tcase(s, tc_subdomain);
    6630             : 
    6631             : #ifdef BUILD_AUTOFS
    6632           1 :     TCase *tc_autofs = tcase_create("SYSDB autofs Tests");
    6633             : 
    6634           1 :     tcase_add_loop_test(tc_autofs, test_autofs_create_map,
    6635             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6636             : 
    6637           1 :     tcase_add_loop_test(tc_autofs, test_autofs_retrieve_map,
    6638             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6639             : 
    6640           1 :     tcase_add_loop_test(tc_autofs, test_autofs_store_entry_in_map,
    6641             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6642             : 
    6643           1 :     tcase_add_loop_test(tc_autofs, test_autofs_retrieve_keys_by_map,
    6644             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6645             : 
    6646           1 :     tcase_add_loop_test(tc_autofs, test_autofs_delete_map,
    6647             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6648             : 
    6649           1 :     tcase_add_loop_test(tc_autofs, test_autofs_retrieve_map_neg,
    6650             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6651             : 
    6652           1 :     tcase_add_loop_test(tc_autofs, test_autofs_key_duplicate,
    6653             :                         TEST_AUTOFS_MAP_BASE, TEST_AUTOFS_MAP_BASE+10);
    6654             : 
    6655           1 :     tcase_add_test(tc_autofs, test_autofs_get_duplicate_keys);
    6656             : 
    6657           1 :     suite_add_tcase(s, tc_autofs);
    6658             : #endif
    6659             : 
    6660           1 :     TCase *tc_upn = tcase_create("SYSDB UPN tests");
    6661           1 :     tcase_add_test(tc_upn, test_upn_basic);
    6662           1 :     tcase_add_test(tc_upn, test_upn_basic_case);
    6663           1 :     tcase_add_test(tc_upn, test_upn_canon);
    6664           1 :     tcase_add_test(tc_upn, test_upn_canon_case);
    6665           1 :     tcase_add_test(tc_upn, test_upn_dup);
    6666             : 
    6667           1 :     suite_add_tcase(s, tc_upn);
    6668             : 
    6669           1 :     TCase *tc_gpo = tcase_create("SYSDB GPO tests");
    6670           1 :     tcase_add_test(tc_gpo, test_gpo_store_retrieve);
    6671           1 :     tcase_add_test(tc_gpo, test_gpo_replace);
    6672           1 :     tcase_add_test(tc_gpo, test_gpo_result);
    6673           1 :     suite_add_tcase(s, tc_gpo);
    6674             : 
    6675             :     /* ConfDB tests -- modify confdb, must always be last!! */
    6676           1 :     TCase *tc_confdb = tcase_create("confDB tests");
    6677             : 
    6678           1 :     tcase_add_test(tc_confdb, test_confdb_list_all_domain_names_no_dom);
    6679           1 :     tcase_add_test(tc_confdb, test_confdb_list_all_domain_names_single_dom);
    6680           1 :     tcase_add_test(tc_confdb, test_confdb_list_all_domain_names_multi_dom);
    6681           1 :     suite_add_tcase(s, tc_confdb);
    6682             : 
    6683           1 :     return s;
    6684             : }
    6685             : 
    6686           1 : int main(int argc, const char *argv[]) {
    6687             :     int opt;
    6688             :     poptContext pc;
    6689             :     int failure_count;
    6690           1 :     int no_cleanup = 0;
    6691             :     Suite *sysdb_suite;
    6692             :     SRunner *sr;
    6693             : 
    6694           7 :     struct poptOption long_options[] = {
    6695             :         POPT_AUTOHELP
    6696           5 :         SSSD_MAIN_OPTS
    6697             :         {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
    6698           1 :          _("Do not delete the test database after a test run"), NULL },
    6699             :         POPT_TABLEEND
    6700             :     };
    6701             : 
    6702             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
    6703           1 :     debug_level = SSSDBG_INVALID;
    6704             : 
    6705           1 :     pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    6706           1 :     while((opt = poptGetNextOpt(pc)) != -1) {
    6707             :         switch(opt) {
    6708             :         default:
    6709           0 :             fprintf(stderr, "\nInvalid option %s: %s\n\n",
    6710             :                     poptBadOption(pc, 0), poptStrerror(opt));
    6711           0 :             poptPrintUsage(pc, stderr, 0);
    6712           0 :             return 1;
    6713             :         }
    6714             :     }
    6715           1 :     poptFreeContext(pc);
    6716             : 
    6717           1 :     DEBUG_CLI_INIT(debug_level);
    6718             : 
    6719           1 :     if (!ldb_modules_path_is_set()) {
    6720           0 :         fprintf(stderr, "Warning: LDB_MODULES_PATH is not set, "
    6721             :                 "will use LDB plugins installed in system paths.\n");
    6722             :     }
    6723             : 
    6724           1 :     tests_set_cwd();
    6725             : 
    6726           1 :     test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
    6727             : 
    6728           1 :     sysdb_suite = create_sysdb_suite();
    6729           1 :     sr = srunner_create(sysdb_suite);
    6730             :     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    6731           1 :     srunner_run_all(sr, CK_ENV);
    6732           1 :     failure_count = srunner_ntests_failed(sr);
    6733           1 :     srunner_free(sr);
    6734           1 :     if (failure_count == 0 && !no_cleanup) {
    6735           1 :         test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
    6736             :     }
    6737           1 :     return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
    6738             : }

Generated by: LCOV version 1.10