LCOV - code coverage report
Current view: top level - tests/cmocka - test_sdap.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 504 511 98.6 %
Date: 2015-10-19 Functions: 45 46 97.8 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Jakub Hrozek <jhrozek@redhat.com>
       4             : 
       5             :     Copyright (C) 2014 Red Hat
       6             : 
       7             :     This program is free software; you can redistribute it and/or modify
       8             :     it under the terms of the GNU General Public License as published by
       9             :     the Free Software Foundation; either version 3 of the License, or
      10             :     (at your option) any later version.
      11             : 
      12             :     This program is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :     GNU General Public License for more details.
      16             : 
      17             :     You should have received a copy of the GNU General Public License
      18             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include <talloc.h>
      22             : #include <tevent.h>
      23             : #include <errno.h>
      24             : #include <popt.h>
      25             : 
      26             : #include "tests/cmocka/common_mock.h"
      27             : #include "providers/ldap/ldap_opts.h"
      28             : #include "providers/ipa/ipa_opts.h"
      29             : #include "util/crypto/sss_crypto.h"
      30             : 
      31             : /* mock an LDAP entry */
      32             : struct mock_ldap_attr {
      33             :     const char *name;
      34             :     const char **values;
      35             : };
      36             : 
      37             : struct mock_ldap_entry {
      38             :     const char *dn;
      39             :     struct mock_ldap_attr *attrs;
      40             : };
      41             : 
      42             : struct mock_ldap_entry *global_ldap_entry;
      43             : 
      44          12 : static int mock_ldap_entry_iter(void)
      45             : {
      46          12 :     return sss_mock_type(int);
      47             : }
      48             : 
      49          37 : static struct mock_ldap_entry *mock_ldap_entry_get(void)
      50             : {
      51          37 :     return sss_mock_ptr_type(struct mock_ldap_entry *);
      52             : }
      53             : 
      54           8 : void set_entry_parse(struct mock_ldap_entry *entry)
      55             : {
      56           8 :     will_return_always(mock_ldap_entry_get, entry);
      57           8 : }
      58             : 
      59           3 : LDAPDerefRes *mock_deref_res(TALLOC_CTX *mem_ctx,
      60             :                              struct mock_ldap_entry *entry)
      61             : {
      62             :     LDAPDerefRes *dref;
      63           3 :     LDAPDerefVal *dval, *dvaltail = NULL;
      64             :     size_t nattr;
      65             :     size_t nval;
      66             : 
      67           3 :     dref = talloc_zero(mem_ctx, LDAPDerefRes);
      68           3 :     assert_non_null(dref);
      69             : 
      70           3 :     dref->derefVal.bv_val = talloc_strdup(dref, entry->dn);
      71           3 :     assert_non_null(dref->derefVal.bv_val);
      72           3 :     dref->derefVal.bv_len = strlen(entry->dn);
      73             : 
      74           3 :     if (entry->attrs == NULL) {
      75             :         /* no attributes, done */
      76           1 :         return dref;
      77             :     }
      78             : 
      79           7 :     for (nattr = 0; entry->attrs[nattr].name; nattr++) {
      80           5 :         dval = talloc_zero(dref, LDAPDerefVal);
      81           5 :         assert_non_null(dval);
      82             : 
      83           5 :         dval->type = talloc_strdup(dval, entry->attrs[nattr].name);
      84           5 :         assert_non_null(dval->type);
      85             : 
      86           5 :         for (nval = 0; entry->attrs[nattr].values[nval]; nval++);
      87             : 
      88           5 :         dval->vals = talloc_zero_array(dval, struct berval, nval+1);
      89           5 :         assert_non_null(dval->vals);
      90          10 :         for (nval = 0; entry->attrs[nattr].values[nval]; nval++) {
      91          10 :             dval->vals[nval].bv_val = talloc_strdup(dval->vals,
      92           5 :                                              entry->attrs[nattr].values[nval]);
      93           5 :             assert_non_null(dval->vals[nval].bv_val);
      94           5 :             dval->vals[nval].bv_len = strlen(dval->vals[nval].bv_val);
      95             :         }
      96             : 
      97           5 :         if (dvaltail != NULL) {
      98           3 :             dvaltail->next = dval;
      99           3 :             dvaltail = dvaltail->next;
     100             :         } else {
     101           2 :             dvaltail = dval;
     102           2 :             dref->attrVals = dval;
     103             :         }
     104             :     }
     105             : 
     106           2 :     return dref;
     107             : }
     108             : 
     109             : /* libldap wrappers */
     110           8 : int __wrap_ldap_set_option(LDAP *ld,
     111             :                            int option,
     112             :                            void *invalue)
     113             : {
     114           8 :     return LDAP_OPT_SUCCESS;
     115             : }
     116             : 
     117           8 : char *__wrap_ldap_get_dn(LDAP *ld, LDAPMessage *entry)
     118             : {
     119           8 :     struct mock_ldap_entry *ldap_entry = mock_ldap_entry_get();
     120           8 :     return discard_const(ldap_entry->dn);
     121             : }
     122             : 
     123          19 : void __wrap_ldap_memfree(void *p)
     124             : {
     125          19 :     return;
     126             : }
     127             : 
     128          12 : struct berval **__wrap_ldap_get_values_len(LDAP *ld,
     129             :                                            LDAPMessage *entry,
     130             :                                            LDAP_CONST char *target)
     131             : {
     132             :     size_t count, i;
     133             :     struct berval **vals;
     134             :     const char **attrvals;
     135          12 :     struct mock_ldap_entry *ldap_entry = mock_ldap_entry_get();
     136             : 
     137          12 :     if (target == NULL) return NULL;
     138          12 :     if (ldap_entry == NULL) return NULL;
     139             :     /* Should we return empty array here? */
     140          12 :     if (ldap_entry->attrs == NULL) return NULL;
     141             : 
     142          12 :     attrvals = NULL;
     143          23 :     for (i = 0; ldap_entry->attrs[i].name != NULL; i++) {
     144          22 :         if (strcmp(ldap_entry->attrs[i].name, target) == 0) {
     145          11 :             attrvals = ldap_entry->attrs[i].values;
     146          11 :             break;
     147             :         }
     148             :     }
     149             : 
     150          12 :     if (attrvals == NULL) {
     151           1 :         return NULL;
     152             :     }
     153             : 
     154          11 :     count = 0;
     155          24 :     for (i = 0; attrvals[i]; i++) {
     156          13 :         count++;
     157             :     }
     158             : 
     159          11 :     vals = talloc_zero_array(global_talloc_context,
     160             :                              struct berval *,
     161             :                              count + 1);
     162          11 :     assert_non_null(vals);
     163             : 
     164          24 :     for (i = 0; attrvals[i]; i++) {
     165          13 :         vals[i] = talloc_zero(vals, struct berval);
     166          13 :         assert_non_null(vals[i]);
     167             : 
     168          13 :         vals[i]->bv_val = talloc_strdup(vals[i], attrvals[i]);
     169          13 :         if (vals[i]->bv_val == NULL) {
     170           0 :             talloc_free(vals);
     171           0 :             return NULL;
     172             :         }
     173          13 :         vals[i]->bv_len = strlen(attrvals[i]);
     174             :     }
     175             : 
     176          11 :     return vals;
     177             : }
     178             : 
     179          11 : void __wrap_ldap_value_free_len(struct berval **vals)
     180             : {
     181          11 :     talloc_free(vals);  /* Allocated on global_talloc_context */
     182          11 : }
     183             : 
     184           5 : char *__wrap_ldap_first_attribute(LDAP *ld,
     185             :                                   LDAPMessage *entry,
     186             :                                   BerElement **berout)
     187             : {
     188           5 :     struct mock_ldap_entry *ldap_entry = mock_ldap_entry_get();
     189             : 
     190           5 :     if (ldap_entry == NULL) return NULL;
     191           5 :     if (ldap_entry->attrs == NULL) return NULL;
     192             : 
     193           5 :     will_return(mock_ldap_entry_iter, 1);
     194           5 :     return discard_const(ldap_entry->attrs[0].name);
     195             : }
     196             : 
     197          12 : char *__wrap_ldap_next_attribute(LDAP *ld,
     198             :                                  LDAPMessage *entry,
     199             :                                  BerElement *ber)
     200             : {
     201          12 :     struct mock_ldap_entry *ldap_entry = mock_ldap_entry_get();
     202             : 
     203          12 :     int index = mock_ldap_entry_iter();
     204             :     char *val;
     205             : 
     206          12 :     val = discard_const(ldap_entry->attrs[index].name);
     207          12 :     if (val != NULL) {
     208           7 :         will_return(mock_ldap_entry_iter, index+1);
     209             :     }
     210          12 :     return val;
     211             : }
     212             : 
     213             : /* Mock parsing search base without overlinking the test */
     214           0 : errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
     215             :                                struct dp_option *opts, int class,
     216             :                                struct sdap_search_base ***_search_bases)
     217             : {
     218           0 :     return EOK;
     219             : }
     220             : 
     221             : /* Utility function */
     222          10 : void assert_entry_has_attr(struct sysdb_attrs *attrs,
     223             :                            const char *attr,
     224             :                            const char *value)
     225             : {
     226             :     const char *v;
     227             :     int ret;
     228             : 
     229          10 :     ret = sysdb_attrs_get_string(attrs, attr, &v);
     230          10 :     assert_int_equal(ret, ERR_OK);
     231          10 :     assert_non_null(v);
     232          10 :     assert_string_equal(v, value);
     233          10 : }
     234             : 
     235           2 : void assert_entry_has_no_attr(struct sysdb_attrs *attrs,
     236             :                               const char *attr)
     237             : {
     238             :     int ret;
     239             :     const char *v;
     240           2 :     ret = sysdb_attrs_get_string(attrs, attr, &v);
     241           2 :     assert_int_equal(ret, ENOENT);
     242           2 : }
     243             : 
     244             : struct parse_test_ctx {
     245             :     struct sdap_handle sh;
     246             :     struct sdap_msg sm;
     247             : };
     248             : 
     249          11 : static int parse_entry_test_setup(void **state)
     250             : {
     251             :     struct parse_test_ctx *test_ctx;
     252             : 
     253          11 :     assert_true(leak_check_setup());
     254             : 
     255          11 :     test_ctx = talloc_zero(global_talloc_context, struct parse_test_ctx);
     256          11 :     assert_non_null(test_ctx);
     257             : 
     258          11 :     check_leaks_push(test_ctx);
     259          11 :     *state = test_ctx;
     260          11 :     return 0;
     261             : }
     262             : 
     263          11 : static int parse_entry_test_teardown(void **state)
     264             : {
     265          11 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     266             :                                                       struct parse_test_ctx);
     267             : 
     268          11 :     assert_true(check_leaks_pop(test_ctx) == true);
     269          11 :     talloc_free(test_ctx);
     270          11 :     assert_true(leak_check_teardown());
     271          11 :     return 0;
     272             : }
     273             : 
     274           1 : void test_parse_with_map(void **state)
     275             : {
     276             :     int ret;
     277             :     struct sysdb_attrs *attrs;
     278           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     279             :                                                       struct parse_test_ctx);
     280             :     struct mock_ldap_entry test_ipa_user;
     281             :     struct sdap_attr_map *map;
     282             :     struct ldb_message_element *el;
     283             :     uint8_t *decoded_key;
     284             :     size_t key_len;
     285             : 
     286           1 :     const char *oc_values[] = { "posixAccount", NULL };
     287           1 :     const char *uid_values[] = { "tuser1", NULL };
     288           1 :     const char *extra_values[] = { "extra", NULL };
     289           1 :     const char *multi_values[] = { "svc1", "svc2", NULL };
     290           1 :     const char *ssh_values[] = { "1234", NULL };
     291           1 :     struct mock_ldap_attr test_ipa_user_attrs[] = {
     292             :         { .name = "objectClass", .values = oc_values },
     293             :         { .name = "uid", .values = uid_values },
     294             :         { .name = "extra", .values = extra_values },
     295             :         { .name = "authorizedService", .values = multi_values },
     296             :         { .name = "ipaSshPubKey", .values = ssh_values },
     297             :         { NULL, NULL }
     298             :     };
     299             : 
     300           1 :     test_ipa_user.dn = "cn=testuser,dc=example,dc=com";
     301           1 :     test_ipa_user.attrs = test_ipa_user_attrs;
     302           1 :     set_entry_parse(&test_ipa_user);
     303             : 
     304           1 :     ret = sdap_copy_map(test_ctx, ipa_user_map, SDAP_OPTS_USER, &map);
     305           1 :     assert_int_equal(ret, ERR_OK);
     306             : 
     307           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     308             :                            map, SDAP_OPTS_USER,
     309             :                            &attrs, false);
     310           1 :     assert_int_equal(ret, ERR_OK);
     311             : 
     312           1 :     assert_int_equal(attrs->num, 4);
     313             : 
     314             :     /* Every entry has a DN */
     315           1 :     assert_entry_has_attr(attrs, SYSDB_ORIG_DN,
     316             :                           "cn=testuser,dc=example,dc=com");
     317             :     /* Test the single-valued attribute */
     318           1 :     assert_entry_has_attr(attrs, SYSDB_NAME, "tuser1");
     319             : 
     320             :     /* Multivalued attributes must return all values */
     321           1 :     ret = sysdb_attrs_get_el_ext(attrs, SYSDB_AUTHORIZED_SERVICE, false, &el);
     322           1 :     assert_int_equal(ret, ERR_OK);
     323           1 :     assert_int_equal(el->num_values, 2);
     324           1 :     assert_true((strcmp((const char *) el->values[0].data, "svc1") == 0 &&
     325             :                     strcmp((const char *) el->values[1].data, "svc2") == 0) ||
     326             :                 (strcmp((const char *) el->values[1].data, "svc1") == 0 &&
     327             :                     strcmp((const char *) el->values[0].data, "svc2") == 0));
     328             : 
     329             :     /* The SSH attribute must be base64 encoded */
     330           1 :     ret = sysdb_attrs_get_el_ext(attrs, SYSDB_SSH_PUBKEY, false, &el);
     331           1 :     assert_int_equal(ret, ERR_OK);
     332           1 :     assert_int_equal(el->num_values, 1);
     333           1 :     decoded_key = sss_base64_decode(test_ctx,
     334           1 :                                     (const char *)el->values[0].data,
     335             :                                     &key_len);
     336           1 :     assert_non_null(decoded_key);
     337           1 :     assert_memory_equal(decoded_key, "1234", key_len);
     338             : 
     339             :     /* The extra attribute must not be downloaded, it's not present in map */
     340           1 :     assert_entry_has_no_attr(attrs, "extra");
     341             : 
     342           1 :     talloc_free(decoded_key);
     343           1 :     talloc_free(map);
     344           1 :     talloc_free(attrs);
     345           1 : }
     346             : 
     347             : /* Some searches, like rootDSE search do not use any map */
     348           1 : void test_parse_no_map(void **state)
     349             : {
     350             :     int ret;
     351             :     struct sysdb_attrs *attrs;
     352           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     353             :                                                       struct parse_test_ctx);
     354             :     struct mock_ldap_entry test_nomap_entry;
     355             :     struct ldb_message_element *el;
     356             : 
     357           1 :     const char *foo_values[] = { "fooval1", "fooval2", NULL };
     358           1 :     const char *bar_values[] = { "barval1", NULL };
     359           1 :     struct mock_ldap_attr test_nomap_entry_attrs[] = {
     360             :         { .name = "foo", .values = foo_values },
     361             :         { .name = "bar", .values = bar_values },
     362             :         { NULL, NULL }
     363             :     };
     364             : 
     365           1 :     test_nomap_entry.dn = "cn=testentry,dc=example,dc=com";
     366           1 :     test_nomap_entry.attrs = test_nomap_entry_attrs;
     367           1 :     set_entry_parse(&test_nomap_entry);
     368             : 
     369           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     370             :                            NULL, 0, &attrs, false);
     371           1 :     assert_int_equal(ret, ERR_OK);
     372             : 
     373           1 :     assert_int_equal(attrs->num, 3);
     374           1 :     assert_entry_has_attr(attrs, SYSDB_ORIG_DN,
     375             :                           "cn=testentry,dc=example,dc=com");
     376           1 :     assert_entry_has_attr(attrs, "bar", "barval1");
     377             :     /* Multivalued attributes must return all values */
     378           1 :     ret = sysdb_attrs_get_el_ext(attrs, "foo", false, &el);
     379           1 :     assert_int_equal(ret, ERR_OK);
     380           1 :     assert_int_equal(el->num_values, 2);
     381           1 :     assert_true((strcmp((const char *) el->values[0].data, "fooval1") == 0 &&
     382             :                     strcmp((const char *) el->values[1].data, "fooval2") == 0) ||
     383             :                 (strcmp((const char *) el->values[1].data, "fooval1") == 0 &&
     384             :                     strcmp((const char *) el->values[0].data, "fooval2") == 0));
     385             : 
     386             : 
     387           1 :     talloc_free(attrs);
     388           1 : }
     389             : 
     390             : /* Only DN and OC, no real attributes */
     391           1 : void test_parse_no_attrs(void **state)
     392             : {
     393             :     int ret;
     394             :     struct sysdb_attrs *attrs;
     395           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     396             :                                                       struct parse_test_ctx);
     397             :     struct mock_ldap_entry test_rfc2307_user;
     398             :     struct sdap_attr_map *map;
     399             : 
     400           1 :     const char *oc_values[] = { "posixAccount", NULL };
     401           1 :     struct mock_ldap_attr test_rfc2307_user_attrs[] = {
     402             :         { .name = "objectClass", .values = oc_values },
     403             :         { NULL, NULL }
     404             :     };
     405             : 
     406           1 :     test_rfc2307_user.dn = "cn=testuser,dc=example,dc=com";
     407           1 :     test_rfc2307_user.attrs = test_rfc2307_user_attrs;
     408           1 :     set_entry_parse(&test_rfc2307_user);
     409             : 
     410           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &map);
     411           1 :     assert_int_equal(ret, ERR_OK);
     412             : 
     413           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     414             :                            map, SDAP_OPTS_USER,
     415             :                            &attrs, false);
     416           1 :     assert_int_equal(ret, ERR_OK);
     417             : 
     418           1 :     assert_int_equal(attrs->num, 1);
     419           1 :     assert_entry_has_attr(attrs, SYSDB_ORIG_DN,
     420             :                           "cn=testuser,dc=example,dc=com");
     421             : 
     422           1 :     talloc_free(map);
     423           1 :     talloc_free(attrs);
     424           1 : }
     425             : 
     426           1 : void test_parse_dups(void **state)
     427             : {
     428             :     int ret;
     429             :     struct sysdb_attrs *attrs;
     430           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     431             :                                                       struct parse_test_ctx);
     432             :     struct mock_ldap_entry test_dupattr_user;
     433             :     struct sdap_attr_map *map;
     434             :     int i;
     435             : 
     436           1 :     const char *oc_values[] = { "posixAccount", NULL };
     437           1 :     const char *uid_values[] = { "1234", NULL };
     438           1 :     struct mock_ldap_attr test_dupattr_attrs[] = {
     439             :         { .name = "objectClass", .values = oc_values },
     440             :         { .name = "idNumber", .values = uid_values },
     441             :         { NULL, NULL }
     442             :     };
     443             : 
     444           1 :     test_dupattr_user.dn = "cn=dupuser,dc=example,dc=com";
     445           1 :     test_dupattr_user.attrs = test_dupattr_attrs;
     446           1 :     set_entry_parse(&test_dupattr_user);
     447             : 
     448           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &map);
     449           1 :     assert_int_equal(ret, ERR_OK);
     450             :     /* Set both uidNumber and gidNumber to idNumber */
     451          38 :     for (i = 0; i < SDAP_OPTS_USER; i++) {
     452          37 :         if (map[i].name == NULL) continue;
     453             : 
     454          30 :         if (strcmp(map[i].name, "uidNumber") == 0
     455          29 :              || strcmp(map[i].name, "gidNumber") == 0) {
     456           2 :             map[i].name = discard_const("idNumber");
     457             :         }
     458             :     }
     459             : 
     460           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     461             :                            map, SDAP_OPTS_USER,
     462             :                            &attrs, false);
     463           1 :     assert_int_equal(ret, ERR_OK);
     464             : 
     465           1 :     assert_int_equal(attrs->num, 3);
     466             : 
     467             :     /* Every entry has a DN */
     468           1 :     assert_entry_has_attr(attrs, SYSDB_ORIG_DN,
     469             :                           "cn=dupuser,dc=example,dc=com");
     470             :     /* Test the single-valued attribute */
     471           1 :     assert_entry_has_attr(attrs, SYSDB_UIDNUM, "1234");
     472           1 :     assert_entry_has_attr(attrs, SYSDB_GIDNUM, "1234");
     473             : 
     474           1 :     talloc_free(map);
     475           1 :     talloc_free(attrs);
     476           1 : }
     477             : 
     478           1 : void test_parse_deref(void **state)
     479             : {
     480             :     errno_t ret;
     481             :     struct sdap_attr_map_info minfo;
     482           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     483             :                                                       struct parse_test_ctx);
     484             :     struct sdap_deref_attrs **res;
     485             :     LDAPDerefRes *dref;
     486             : 
     487           1 :     const char *oc_values[] = { "posixAccount", NULL };
     488           1 :     const char *uid_values[] = { "tuser1", NULL };
     489           1 :     const char *extra_values[] = { "extra", NULL };
     490           1 :     struct mock_ldap_attr test_ipa_user_attrs[] = {
     491             :         { .name = "objectClass", .values = oc_values },
     492             :         { .name = "uid", .values = uid_values },
     493             :         { .name = "extra", .values = extra_values },
     494             :         { NULL, NULL }
     495             :     };
     496             :     struct mock_ldap_entry test_ipa_user;
     497           1 :     test_ipa_user.dn = "cn=testuser,dc=example,dc=com";
     498           1 :     test_ipa_user.attrs = test_ipa_user_attrs;
     499             : 
     500           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &minfo.map);
     501           1 :     minfo.num_attrs = SDAP_OPTS_USER;
     502           1 :     assert_int_equal(ret, ERR_OK);
     503             : 
     504           1 :     dref = mock_deref_res(test_ctx, &test_ipa_user);
     505           1 :     assert_non_null(dref);
     506             : 
     507           1 :     ret = sdap_parse_deref(test_ctx, &minfo, 1, dref, &res);
     508           1 :     talloc_free(dref);
     509           1 :     talloc_free(minfo.map);
     510           1 :     assert_int_equal(ret, ERR_OK);
     511           1 :     assert_non_null(res);
     512             : 
     513             :     /* The extra attribute must not be downloaded, it's not present in map */
     514           1 :     assert_non_null(res[0]);
     515           1 :     assert_true(res[0]->map == minfo.map);
     516             : 
     517           1 :     assert_entry_has_attr(res[0]->attrs, SYSDB_ORIG_DN,
     518             :                           "cn=testuser,dc=example,dc=com");
     519           1 :     assert_entry_has_attr(res[0]->attrs, SYSDB_NAME, "tuser1");
     520           1 :     assert_entry_has_no_attr(res[0]->attrs, "extra");
     521           1 :     talloc_free(res);
     522           1 : }
     523             : 
     524           1 : void test_parse_deref_no_attrs(void **state)
     525             : {
     526             :     errno_t ret;
     527             :     struct sdap_attr_map_info minfo;
     528           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     529             :                                                       struct parse_test_ctx);
     530             :     struct sdap_deref_attrs **res;
     531             :     LDAPDerefRes *dref;
     532             : 
     533             :     struct mock_ldap_entry test_ipa_user;
     534           1 :     test_ipa_user.dn = "cn=testuser,dc=example,dc=com";
     535           1 :     test_ipa_user.attrs = NULL;
     536             : 
     537           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &minfo.map);
     538           1 :     minfo.num_attrs = SDAP_OPTS_USER;
     539           1 :     assert_int_equal(ret, ERR_OK);
     540             : 
     541           1 :     dref = mock_deref_res(test_ctx, &test_ipa_user);
     542           1 :     assert_non_null(dref);
     543             : 
     544           1 :     ret = sdap_parse_deref(test_ctx, &minfo, 1, dref, &res);
     545           1 :     talloc_free(dref);
     546           1 :     talloc_free(minfo.map);
     547           1 :     assert_int_equal(ret, ERR_OK);
     548           1 :     assert_null(res); /* res must be NULL on receiving no attributes */
     549           1 : }
     550             : 
     551           1 : void test_parse_deref_map_mismatch(void **state)
     552             : {
     553             :     errno_t ret;
     554             :     struct sdap_attr_map_info minfo;
     555           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     556             :                                                       struct parse_test_ctx);
     557             :     struct sdap_deref_attrs **res;
     558             :     LDAPDerefRes *dref;
     559             : 
     560           1 :     const char *oc_values[] = { "posixAccount", NULL };
     561           1 :     const char *uid_values[] = { "tuser1", NULL };
     562           1 :     struct mock_ldap_attr test_ipa_user_attrs[] = {
     563             :         { .name = "objectClass", .values = oc_values },
     564             :         { .name = "uid", .values = uid_values },
     565             :         { NULL, NULL }
     566             :     };
     567             :     struct mock_ldap_entry test_ipa_user;
     568           1 :     test_ipa_user.dn = "cn=testuser,dc=example,dc=com";
     569           1 :     test_ipa_user.attrs = test_ipa_user_attrs;
     570             : 
     571           1 :     ret = sdap_copy_map(test_ctx, rfc2307_group_map, SDAP_OPTS_GROUP, &minfo.map);
     572           1 :     minfo.num_attrs = SDAP_OPTS_GROUP;
     573           1 :     assert_int_equal(ret, ERR_OK);
     574             : 
     575           1 :     dref = mock_deref_res(test_ctx, &test_ipa_user);
     576           1 :     assert_non_null(dref);
     577             : 
     578           1 :     ret = sdap_parse_deref(test_ctx, &minfo, 1, dref, &res);
     579           1 :     talloc_free(dref);
     580           1 :     talloc_free(minfo.map);
     581           1 :     assert_int_equal(ret, ERR_OK);
     582           1 :     assert_non_null(res);
     583             :     /* the group map didn't match, so no attrs will be parsed out of the map */
     584           1 :     assert_null(res[0]->attrs);
     585           1 :     talloc_free(res);
     586           1 : }
     587             : 
     588           1 : void test_parse_secondary_oc(void **state)
     589             : {
     590             :     int ret;
     591             :     struct sysdb_attrs *attrs;
     592           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     593             :                                                       struct parse_test_ctx);
     594             :     struct mock_ldap_entry test_rfc2307_group;
     595             :     struct sdap_attr_map *map;
     596             : 
     597           1 :     const char *oc_values[] = { "secondaryOC", NULL };
     598           1 :     const char *uid_values[] = { "tgroup1", NULL };
     599           1 :     struct mock_ldap_attr test_rfc2307_group_attrs[] = {
     600             :         { .name = "objectClass", .values = oc_values },
     601             :         { .name = "uid", .values = uid_values },
     602             :         { NULL, NULL }
     603             :     };
     604             : 
     605           1 :     test_rfc2307_group.dn = "cn=testgroup,dc=example,dc=com";
     606           1 :     test_rfc2307_group.attrs = test_rfc2307_group_attrs;
     607           1 :     set_entry_parse(&test_rfc2307_group);
     608             : 
     609           1 :     ret = sdap_copy_map(test_ctx, rfc2307_group_map, SDAP_OPTS_GROUP, &map);
     610           1 :     assert_int_equal(ret, ERR_OK);
     611           1 :     map[SDAP_OC_GROUP_ALT].name = discard_const("secondaryOC");
     612             : 
     613           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     614             :                            map, SDAP_OPTS_GROUP,
     615             :                            &attrs, false);
     616           1 :     assert_int_equal(ret, ERR_OK);
     617             : 
     618           1 :     talloc_free(map);
     619           1 :     talloc_free(attrs);
     620           1 : }
     621             : 
     622             : /* Negative test - objectclass doesn't match the map */
     623           1 : void test_parse_bad_oc(void **state)
     624             : {
     625             :     int ret;
     626             :     struct sysdb_attrs *attrs;
     627           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     628             :                                                       struct parse_test_ctx);
     629             :     struct mock_ldap_entry test_rfc2307_user;
     630             :     struct sdap_attr_map *map;
     631             : 
     632           1 :     const char *oc_values[] = { "someRandomValueWhoCaresItsAUnitTest", NULL };
     633           1 :     const char *uid_values[] = { "tuser1", NULL };
     634           1 :     struct mock_ldap_attr test_rfc2307_user_attrs[] = {
     635             :         { .name = "objectClass", .values = oc_values },
     636             :         { .name = "uid", .values = uid_values },
     637             :         { NULL, NULL }
     638             :     };
     639             : 
     640           1 :     test_rfc2307_user.dn = "cn=testuser,dc=example,dc=com";
     641           1 :     test_rfc2307_user.attrs = test_rfc2307_user_attrs;
     642           1 :     set_entry_parse(&test_rfc2307_user);
     643             : 
     644           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &map);
     645           1 :     assert_int_equal(ret, ERR_OK);
     646             : 
     647           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     648             :                            map, SDAP_OPTS_USER,
     649             :                            &attrs, false);
     650           1 :     assert_int_not_equal(ret, ERR_OK);
     651             : 
     652           1 :     talloc_free(map);
     653           1 : }
     654             : 
     655             : /* Negative test - the entry has no objectClass. Just make sure
     656             :  * we don't crash
     657             :  */
     658           1 : void test_parse_no_oc(void **state)
     659             : {
     660             :     int ret;
     661             :     struct sysdb_attrs *attrs;
     662           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     663             :                                                       struct parse_test_ctx);
     664             :     struct mock_ldap_entry test_rfc2307_user;
     665             :     struct sdap_attr_map *map;
     666             : 
     667           1 :     const char *uid_values[] = { "tuser1", NULL };
     668           1 :     struct mock_ldap_attr test_rfc2307_user_attrs[] = {
     669             :         { .name = "uid", .values = uid_values },
     670             :         { NULL, NULL }
     671             :     };
     672             : 
     673           1 :     test_rfc2307_user.dn = "cn=testuser,dc=example,dc=com";
     674           1 :     test_rfc2307_user.attrs = test_rfc2307_user_attrs;
     675           1 :     set_entry_parse(&test_rfc2307_user);
     676             : 
     677           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &map);
     678           1 :     assert_int_equal(ret, ERR_OK);
     679             : 
     680           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     681             :                            map, SDAP_OPTS_USER,
     682             :                            &attrs, false);
     683           1 :     assert_int_not_equal(ret, ERR_OK);
     684             : 
     685           1 :     talloc_free(map);
     686           1 : }
     687             : 
     688             : /* Negative test - the entry has no DN. Just make sure
     689             :  * we don't crash and detect the failure.
     690             :  */
     691           1 : void test_parse_no_dn(void **state)
     692             : {
     693             :     int ret;
     694             :     struct sysdb_attrs *attrs;
     695           1 :     struct parse_test_ctx *test_ctx = talloc_get_type_abort(*state,
     696             :                                                       struct parse_test_ctx);
     697             :     struct mock_ldap_entry test_rfc2307_user;
     698             :     struct sdap_attr_map *map;
     699             : 
     700           1 :     const char *oc_values[] = { "posixAccount", NULL };
     701           1 :     const char *uid_values[] = { "tuser1", NULL };
     702           1 :     struct mock_ldap_attr test_rfc2307_user_attrs[] = {
     703             :         { .name = "objectClass", .values = oc_values },
     704             :         { .name = "uid", .values = uid_values },
     705             :         { NULL, NULL }
     706             :     };
     707             : 
     708           1 :     test_rfc2307_user.dn = NULL;        /* Test */
     709           1 :     test_rfc2307_user.attrs = test_rfc2307_user_attrs;
     710           1 :     set_entry_parse(&test_rfc2307_user);
     711             : 
     712           1 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map, SDAP_OPTS_USER, &map);
     713           1 :     assert_int_equal(ret, ERR_OK);
     714             : 
     715           1 :     ret = sdap_parse_entry(test_ctx, &test_ctx->sh, &test_ctx->sm,
     716             :                            map, SDAP_OPTS_USER,
     717             :                            &attrs, false);
     718           1 :     assert_int_not_equal(ret, ERR_OK);
     719             : 
     720           1 :     talloc_free(map);
     721           1 : }
     722             : 
     723             : struct copy_map_entry_test_ctx {
     724             :     struct sdap_attr_map *src_map;
     725             :     struct sdap_attr_map *dst_map;
     726             : };
     727             : 
     728           2 : static int copy_map_entry_test_setup(void **state)
     729             : {
     730             :     int ret;
     731             :     struct copy_map_entry_test_ctx *test_ctx;
     732             : 
     733           2 :     assert_true(leak_check_setup());
     734             : 
     735           2 :     test_ctx = talloc_zero(global_talloc_context,
     736             :                            struct copy_map_entry_test_ctx);
     737           2 :     assert_non_null(test_ctx);
     738             : 
     739           2 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map,
     740             :                         SDAP_OPTS_USER, &test_ctx->src_map);
     741           2 :     assert_int_equal(ret, ERR_OK);
     742             : 
     743           2 :     ret = sdap_copy_map(test_ctx, rfc2307_user_map,
     744             :                         SDAP_OPTS_USER, &test_ctx->dst_map);
     745           2 :     assert_int_equal(ret, ERR_OK);
     746             : 
     747           2 :     check_leaks_push(test_ctx);
     748           2 :     *state = test_ctx;
     749           2 :     return 0;
     750             : }
     751             : 
     752           2 : static int copy_map_entry_test_teardown(void **state)
     753             : {
     754           2 :     struct copy_map_entry_test_ctx *test_ctx = talloc_get_type_abort(*state,
     755             :                                                struct copy_map_entry_test_ctx);
     756           2 :     assert_true(check_leaks_pop(test_ctx) == true);
     757           2 :     talloc_free(test_ctx);
     758           2 :     assert_true(leak_check_teardown());
     759           2 :     return 0;
     760             : }
     761             : 
     762           2 : static const char *copy_uuid(struct copy_map_entry_test_ctx *test_ctx)
     763             : {
     764             :     errno_t ret;
     765             : 
     766           2 :     assert_null(test_ctx->dst_map[SDAP_AT_USER_UUID].name);
     767           2 :     ret = sdap_copy_map_entry(test_ctx->src_map, test_ctx->dst_map,
     768             :                               SDAP_AT_USER_UUID);
     769           2 :     assert_int_equal(ret, EOK);
     770           2 :     return test_ctx->dst_map[SDAP_AT_USER_UUID].name;
     771             : }
     772             : 
     773           1 : static void test_sdap_copy_map_entry(void **state)
     774             : {
     775           1 :     struct copy_map_entry_test_ctx *test_ctx = talloc_get_type_abort(*state,
     776             :                                                struct copy_map_entry_test_ctx);
     777           1 :     const char *uuid_set_val = "test_uuid_val";
     778           1 :     const char *uuid_val = NULL;
     779             : 
     780           1 :     test_ctx->src_map[SDAP_AT_USER_UUID].name = discard_const(uuid_set_val);
     781             : 
     782           1 :     uuid_val = copy_uuid(test_ctx);
     783           1 :     assert_non_null(uuid_val);
     784           1 :     assert_string_equal(uuid_val, uuid_set_val);
     785           1 :     talloc_free(test_ctx->dst_map[SDAP_AT_USER_UUID].name);
     786           1 : }
     787             : 
     788           1 : static void test_sdap_copy_map_entry_null_name(void **state)
     789             : {
     790           1 :     struct copy_map_entry_test_ctx *test_ctx = talloc_get_type_abort(*state,
     791             :                                                struct copy_map_entry_test_ctx);
     792           1 :     const char *uuid_val = NULL;
     793             : 
     794           1 :     uuid_val = copy_uuid(test_ctx);
     795           1 :     assert_null(uuid_val);
     796           1 : }
     797             : 
     798             : struct test_sdap_inherit_ctx {
     799             :     struct sdap_options *parent_sdap_opts;
     800             :     struct sdap_options *child_sdap_opts;
     801             : };
     802             : 
     803           8 : struct sdap_options *mock_sdap_opts(TALLOC_CTX *mem_ctx)
     804             : {
     805             :     int ret;
     806             :     struct sdap_options *opts;
     807             : 
     808           8 :     opts = talloc_zero(mem_ctx, struct sdap_options);
     809           8 :     assert_non_null(opts);
     810             : 
     811           8 :     ret = sdap_copy_map(opts, rfc2307_user_map,
     812             :                         SDAP_OPTS_USER, &opts->user_map);
     813           8 :     assert_int_equal(ret, ERR_OK);
     814             : 
     815           8 :     ret = dp_copy_defaults(opts, default_basic_opts,
     816             :                            SDAP_OPTS_BASIC, &opts->basic);
     817           8 :     assert_int_equal(ret, ERR_OK);
     818             : 
     819           8 :     return opts;
     820             : }
     821             : 
     822           4 : static int test_sdap_inherit_option_setup(void **state)
     823             : {
     824             :     int ret;
     825             :     struct test_sdap_inherit_ctx *test_ctx;
     826             : 
     827           4 :     assert_true(leak_check_setup());
     828             : 
     829           4 :     test_ctx = talloc_zero(global_talloc_context,
     830             :                            struct test_sdap_inherit_ctx);
     831           4 :     assert_non_null(test_ctx);
     832             : 
     833           4 :     test_ctx->child_sdap_opts = talloc_zero(test_ctx, struct sdap_options);
     834             : 
     835           4 :     test_ctx->parent_sdap_opts = mock_sdap_opts(test_ctx);
     836           4 :     assert_non_null(test_ctx->parent_sdap_opts);
     837           4 :     test_ctx->child_sdap_opts = mock_sdap_opts(test_ctx);
     838           4 :     assert_non_null(test_ctx->child_sdap_opts);
     839             : 
     840           4 :     test_ctx->parent_sdap_opts->user_map[SDAP_AT_USER_PRINC].name = \
     841             :                                                   discard_const("test_princ");
     842             : 
     843           4 :     ret = dp_opt_set_int(test_ctx->parent_sdap_opts->basic,
     844             :                          SDAP_PURGE_CACHE_TIMEOUT, 123);
     845           4 :     assert_int_equal(ret, EOK);
     846             : 
     847           4 :     *state = test_ctx;
     848           4 :     return 0;
     849             : }
     850             : 
     851           4 : static int test_sdap_inherit_option_teardown(void **state)
     852             : {
     853           4 :     struct test_sdap_inherit_ctx *test_ctx = \
     854           4 :                 talloc_get_type_abort(*state, struct test_sdap_inherit_ctx);
     855             : 
     856           4 :     talloc_free(test_ctx);
     857           4 :     assert_true(leak_check_teardown());
     858           4 :     return 0;
     859             : }
     860             : 
     861           1 : static void test_sdap_inherit_option_null(void **state)
     862             : {
     863           1 :     struct test_sdap_inherit_ctx *test_ctx = \
     864           1 :                 talloc_get_type_abort(*state, struct test_sdap_inherit_ctx);
     865             :     int val;
     866             : 
     867           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     868             :                          SDAP_PURGE_CACHE_TIMEOUT);
     869           1 :     assert_int_equal(val, 0);
     870             : 
     871           1 :     sdap_inherit_options(NULL,
     872             :                          test_ctx->parent_sdap_opts,
     873             :                          test_ctx->child_sdap_opts);
     874             : 
     875           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     876             :                          SDAP_PURGE_CACHE_TIMEOUT);
     877           1 :     assert_int_equal(val, 0);
     878           1 : }
     879             : 
     880           1 : static void test_sdap_inherit_option_notset(void **state)
     881             : {
     882           1 :     struct test_sdap_inherit_ctx *test_ctx = \
     883           1 :                 talloc_get_type_abort(*state, struct test_sdap_inherit_ctx);
     884             :     int val;
     885           1 :     const char *inherit_options[] = { "ldap_use_tokengroups", NULL };
     886             : 
     887           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     888             :                          SDAP_PURGE_CACHE_TIMEOUT);
     889           1 :     assert_int_equal(val, 0);
     890             : 
     891             :     /* parent has nondefault, but it's not supposed to be inherited */
     892           1 :     sdap_inherit_options(discard_const(inherit_options),
     893             :                          test_ctx->parent_sdap_opts,
     894             :                          test_ctx->child_sdap_opts);
     895             : 
     896           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     897             :                          SDAP_PURGE_CACHE_TIMEOUT);
     898           1 :     assert_int_equal(val, 0);
     899           1 : }
     900             : 
     901           1 : static void test_sdap_inherit_option_basic(void **state)
     902             : {
     903           1 :     struct test_sdap_inherit_ctx *test_ctx = \
     904           1 :                 talloc_get_type_abort(*state, struct test_sdap_inherit_ctx);
     905             :     int val;
     906           1 :     const char *inherit_options[] = { "ldap_purge_cache_timeout", NULL };
     907             : 
     908           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     909             :                          SDAP_PURGE_CACHE_TIMEOUT);
     910           1 :     assert_int_equal(val, 0);
     911             : 
     912             :     /* parent has nondefault, but it's not supposed to be inherited */
     913           1 :     sdap_inherit_options(discard_const(inherit_options),
     914             :                          test_ctx->parent_sdap_opts,
     915             :                          test_ctx->child_sdap_opts);
     916             : 
     917           1 :     val = dp_opt_get_int(test_ctx->child_sdap_opts->basic,
     918             :                          SDAP_PURGE_CACHE_TIMEOUT);
     919           1 :     assert_int_equal(val, 123);
     920           1 : }
     921             : 
     922           1 : static void test_sdap_inherit_option_user(void **state)
     923             : {
     924           1 :     struct test_sdap_inherit_ctx *test_ctx = \
     925           1 :                 talloc_get_type_abort(*state, struct test_sdap_inherit_ctx);
     926           1 :     const char *inherit_options[] = { "ldap_user_principal", NULL };
     927             : 
     928           1 :     assert_string_equal(
     929             :             test_ctx->child_sdap_opts->user_map[SDAP_AT_USER_PRINC].name,
     930             :             "krbPrincipalName");
     931             : 
     932             :     /* parent has nondefault, but it's not supposed to be inherited */
     933           1 :     sdap_inherit_options(discard_const(inherit_options),
     934             :                          test_ctx->parent_sdap_opts,
     935             :                          test_ctx->child_sdap_opts);
     936             : 
     937           1 :     assert_string_equal(
     938             :             test_ctx->child_sdap_opts->user_map[SDAP_AT_USER_PRINC].name,
     939             :             "test_princ");
     940             : 
     941           1 :     talloc_free(test_ctx->child_sdap_opts->user_map[SDAP_AT_USER_PRINC].name);
     942           1 : }
     943             : 
     944             : struct copy_dom_obj_test_ctx {
     945             :     struct sdap_options *opts;
     946             : 
     947             :     struct sss_domain_info *parent;
     948             :     struct sss_domain_info *child;
     949             : 
     950             :     struct sdap_domain *parent_sd;
     951             :     struct sdap_domain *child_sd;
     952             : 
     953             :     struct sysdb_attrs **ldap_objects;
     954             :     struct sysdb_attrs **dom_objects;
     955             : };
     956             : 
     957           4 : static struct sysdb_attrs *test_obj(TALLOC_CTX *mem_ctx,
     958             :                                     const char *name,
     959             :                                     const char *basedn)
     960             : {
     961             :     errno_t ret;
     962             :     const char *orig_dn;
     963             :     struct sysdb_attrs *obj;
     964             : 
     965           4 :     obj = sysdb_new_attrs(mem_ctx);
     966           4 :     assert_non_null(obj);
     967             : 
     968           4 :     orig_dn = talloc_asprintf(obj, "CN=%s,%s", name, basedn);
     969           4 :     assert_non_null(orig_dn);
     970             : 
     971           4 :     ret = sysdb_attrs_add_string(obj, SYSDB_ORIG_DN, orig_dn);
     972           4 :     assert_int_equal(ret, EOK);
     973             : 
     974           4 :     ret = sysdb_attrs_add_string(obj, SYSDB_NAME, name);
     975           4 :     assert_int_equal(ret, EOK);
     976             : 
     977           4 :     return obj;
     978             : }
     979             : 
     980           4 : static struct sdap_domain *create_sdap_domain(struct sdap_options *opts,
     981             :                                               struct sss_domain_info *dom)
     982             : {
     983             :     errno_t ret;
     984             :     struct sdap_domain *sdom;
     985             : 
     986           4 :     ret = sdap_domain_add(opts, dom, &sdom);
     987           4 :     assert_int_equal(ret, EOK);
     988             : 
     989           4 :     sdom->search_bases = talloc_array(sdom, struct sdap_search_base *, 2);
     990           4 :     assert_non_null(sdom->search_bases);
     991           4 :     sdom->search_bases[1] = NULL;
     992             : 
     993           4 :     ret = sdap_create_search_base(sdom, sdom->basedn,
     994             :                                   LDAP_SCOPE_SUBTREE,
     995             :                                   NULL,
     996           4 :                                   &sdom->search_bases[0]);
     997           4 :     assert_int_equal(ret, EOK);
     998             : 
     999           4 :     return sdom;
    1000             : }
    1001             : 
    1002           2 : static int sdap_copy_objects_in_dom_setup(void **state)
    1003             : {
    1004             :     struct copy_dom_obj_test_ctx *test_ctx;
    1005             : 
    1006           2 :     test_ctx = talloc_zero(NULL,
    1007             :                            struct copy_dom_obj_test_ctx);
    1008           2 :     assert_non_null(test_ctx);
    1009             : 
    1010           2 :     test_ctx->opts = talloc_zero(test_ctx, struct sdap_options);
    1011           2 :     assert_non_null(test_ctx->opts);
    1012             : 
    1013           2 :     test_ctx->parent = named_domain(test_ctx, "win.trust.test", NULL);
    1014           2 :     assert_non_null(test_ctx->parent);
    1015             : 
    1016           2 :     test_ctx->child = named_domain(test_ctx, "child.win.trust.test",
    1017             :                                    test_ctx->parent);
    1018           2 :     assert_non_null(test_ctx->child);
    1019             : 
    1020           2 :     test_ctx->parent_sd = create_sdap_domain(test_ctx->opts,
    1021             :                                              test_ctx->parent);
    1022           2 :     assert_non_null(test_ctx->parent_sd);
    1023             : 
    1024           2 :     test_ctx->child_sd = create_sdap_domain(test_ctx->opts,
    1025             :                                             test_ctx->child);
    1026           2 :     assert_non_null(test_ctx->child_sd);
    1027             : 
    1028             :     /* These two objects were 'returned by LDAP' */
    1029           2 :     test_ctx->ldap_objects = talloc_zero_array(test_ctx,
    1030             :                                                struct sysdb_attrs *, 2);
    1031           2 :     assert_non_null(test_ctx->ldap_objects);
    1032             : 
    1033           4 :     test_ctx->ldap_objects[0] = test_obj(test_ctx->ldap_objects, "parent",
    1034           2 :                                          test_ctx->parent_sd->basedn);
    1035           2 :     assert_non_null(test_ctx->ldap_objects[0]);
    1036             : 
    1037           4 :     test_ctx->ldap_objects[1] = test_obj(test_ctx->ldap_objects, "child",
    1038           2 :                                          test_ctx->child_sd->basedn);
    1039           2 :     assert_non_null(test_ctx->ldap_objects[1]);
    1040             : 
    1041             :     /* This is the array we'll filter to */
    1042           2 :     test_ctx->dom_objects = talloc_zero_array(test_ctx,
    1043             :                                               struct sysdb_attrs *, 2);
    1044           2 :     assert_non_null(test_ctx->dom_objects);
    1045             : 
    1046           2 :     *state = test_ctx;
    1047           2 :     return 0;
    1048             : }
    1049             : 
    1050           2 : static int sdap_copy_objects_in_dom_teardown(void **state)
    1051             : {
    1052           2 :     struct copy_dom_obj_test_ctx *test_ctx = talloc_get_type_abort(*state,
    1053             :                                                  struct copy_dom_obj_test_ctx);
    1054             : 
    1055           2 :     talloc_free(test_ctx);
    1056           2 :     return 0;
    1057             : }
    1058             : 
    1059           1 : static void test_sdap_copy_objects_in_dom(void **state)
    1060             : {
    1061           1 :     struct copy_dom_obj_test_ctx *test_ctx = talloc_get_type_abort(*state,
    1062             :                                                  struct copy_dom_obj_test_ctx);
    1063             :     size_t count;
    1064             : 
    1065           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[0]),
    1066             :                      test_ctx->ldap_objects);
    1067           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[1]),
    1068             :                      test_ctx->ldap_objects);
    1069             : 
    1070           1 :     assert_null(test_ctx->dom_objects[0]);
    1071           1 :     assert_null(test_ctx->dom_objects[1]);
    1072             : 
    1073           1 :     count = sdap_steal_objects_in_dom(test_ctx->opts,
    1074             :                                       test_ctx->dom_objects,
    1075             :                                       0,
    1076             :                                       test_ctx->parent,
    1077             :                                       test_ctx->ldap_objects,
    1078             :                                       2, true);
    1079           1 :     assert_int_equal(count, 1);
    1080             : 
    1081           1 :     assert_non_null(test_ctx->dom_objects[0]);
    1082           1 :     assert_non_null(test_ctx->dom_objects[0] == test_ctx->ldap_objects[0]);
    1083           1 :     assert_null(test_ctx->dom_objects[1]);
    1084             : 
    1085           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[0]),
    1086             :                      test_ctx->dom_objects);
    1087             : 
    1088           1 :     count = sdap_steal_objects_in_dom(test_ctx->opts,
    1089             :                                       test_ctx->dom_objects,
    1090             :                                       1,
    1091             :                                       test_ctx->child,
    1092             :                                       test_ctx->ldap_objects,
    1093             :                                       2, true);
    1094           1 :     assert_int_equal(count, 1);
    1095             : 
    1096           1 :     assert_non_null(test_ctx->dom_objects[1]);
    1097           1 :     assert_non_null(test_ctx->dom_objects[1] == test_ctx->ldap_objects[1]);
    1098           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[1]),
    1099             :                      test_ctx->dom_objects);
    1100           1 : }
    1101             : 
    1102           1 : static void test_sdap_copy_objects_in_dom_nofilter(void **state)
    1103             : {
    1104           1 :     struct copy_dom_obj_test_ctx *test_ctx = talloc_get_type_abort(*state,
    1105             :                                                  struct copy_dom_obj_test_ctx);
    1106             :     size_t count;
    1107             : 
    1108           1 :     count = sdap_steal_objects_in_dom(test_ctx->opts,
    1109             :                                       test_ctx->dom_objects,
    1110             :                                       0,
    1111             :                                       test_ctx->parent,
    1112             :                                       test_ctx->ldap_objects,
    1113             :                                       2, false);
    1114           1 :     assert_int_equal(count, 2);
    1115             : 
    1116           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[0]),
    1117             :                      test_ctx->dom_objects);
    1118           1 :     assert_ptr_equal(talloc_parent(test_ctx->ldap_objects[1]),
    1119             :                      test_ctx->dom_objects);
    1120           1 : }
    1121             : 
    1122           1 : int main(int argc, const char *argv[])
    1123             : {
    1124             :     poptContext pc;
    1125             :     int opt;
    1126           6 :     struct poptOption long_options[] = {
    1127             :         POPT_AUTOHELP
    1128           5 :         SSSD_DEBUG_OPTS
    1129             :         POPT_TABLEEND
    1130             :     };
    1131             : 
    1132           1 :     const struct CMUnitTest tests[] = {
    1133             :         cmocka_unit_test_setup_teardown(test_parse_with_map,
    1134             :                                         parse_entry_test_setup,
    1135             :                                         parse_entry_test_teardown),
    1136             :         cmocka_unit_test_setup_teardown(test_parse_no_map,
    1137             :                                         parse_entry_test_setup,
    1138             :                                         parse_entry_test_teardown),
    1139             :         cmocka_unit_test_setup_teardown(test_parse_no_attrs,
    1140             :                                         parse_entry_test_setup,
    1141             :                                         parse_entry_test_teardown),
    1142             :         cmocka_unit_test_setup_teardown(test_parse_dups,
    1143             :                                         parse_entry_test_setup,
    1144             :                                         parse_entry_test_teardown),
    1145             :         cmocka_unit_test_setup_teardown(test_parse_deref,
    1146             :                                         parse_entry_test_setup,
    1147             :                                         parse_entry_test_teardown),
    1148             :         cmocka_unit_test_setup_teardown(test_parse_deref_no_attrs,
    1149             :                                         parse_entry_test_setup,
    1150             :                                         parse_entry_test_teardown),
    1151             :         cmocka_unit_test_setup_teardown(test_parse_secondary_oc,
    1152             :                                         parse_entry_test_setup,
    1153             :                                         parse_entry_test_teardown),
    1154             :         /* Negative tests */
    1155             :         cmocka_unit_test_setup_teardown(test_parse_no_oc,
    1156             :                                         parse_entry_test_setup,
    1157             :                                         parse_entry_test_teardown),
    1158             :         cmocka_unit_test_setup_teardown(test_parse_bad_oc,
    1159             :                                         parse_entry_test_setup,
    1160             :                                         parse_entry_test_teardown),
    1161             :         cmocka_unit_test_setup_teardown(test_parse_no_dn,
    1162             :                                         parse_entry_test_setup,
    1163             :                                         parse_entry_test_teardown),
    1164             :         cmocka_unit_test_setup_teardown(test_parse_deref_map_mismatch,
    1165             :                                         parse_entry_test_setup,
    1166             :                                         parse_entry_test_teardown),
    1167             : 
    1168             :         /* Map option tests */
    1169             :         cmocka_unit_test_setup_teardown(test_sdap_copy_map_entry,
    1170             :                                         copy_map_entry_test_setup,
    1171             :                                         copy_map_entry_test_teardown),
    1172             :         cmocka_unit_test_setup_teardown(test_sdap_copy_map_entry_null_name,
    1173             :                                         copy_map_entry_test_setup,
    1174             :                                         copy_map_entry_test_teardown),
    1175             : 
    1176             :         /* Option inherit tests */
    1177             :         cmocka_unit_test_setup_teardown(test_sdap_inherit_option_null,
    1178             :                                         test_sdap_inherit_option_setup,
    1179             :                                         test_sdap_inherit_option_teardown),
    1180             :         cmocka_unit_test_setup_teardown(test_sdap_inherit_option_notset,
    1181             :                                         test_sdap_inherit_option_setup,
    1182             :                                         test_sdap_inherit_option_teardown),
    1183             :         cmocka_unit_test_setup_teardown(test_sdap_inherit_option_basic,
    1184             :                                         test_sdap_inherit_option_setup,
    1185             :                                         test_sdap_inherit_option_teardown),
    1186             :         cmocka_unit_test_setup_teardown(test_sdap_inherit_option_user,
    1187             :                                         test_sdap_inherit_option_setup,
    1188             :                                         test_sdap_inherit_option_teardown),
    1189             : 
    1190             :         /* Per-domain object filter tests */
    1191             :         cmocka_unit_test_setup_teardown(test_sdap_copy_objects_in_dom,
    1192             :                                         sdap_copy_objects_in_dom_setup,
    1193             :                                         sdap_copy_objects_in_dom_teardown),
    1194             :         cmocka_unit_test_setup_teardown(test_sdap_copy_objects_in_dom_nofilter,
    1195             :                                         sdap_copy_objects_in_dom_setup,
    1196             :                                         sdap_copy_objects_in_dom_teardown),
    1197             :     };
    1198             : 
    1199             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
    1200           1 :     debug_level = SSSDBG_INVALID;
    1201             : 
    1202           1 :     pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    1203           1 :     while((opt = poptGetNextOpt(pc)) != -1) {
    1204             :         switch(opt) {
    1205             :         default:
    1206           0 :             fprintf(stderr, "\nInvalid option %s: %s\n\n",
    1207             :                     poptBadOption(pc, 0), poptStrerror(opt));
    1208           0 :             poptPrintUsage(pc, stderr, 0);
    1209           0 :             return 1;
    1210             :         }
    1211             :     }
    1212           1 :     poptFreeContext(pc);
    1213             : 
    1214           1 :     DEBUG_CLI_INIT(debug_level);
    1215             : 
    1216             :     /* Even though normally the tests should clean up after themselves
    1217             :      * they might not after a failed run. Remove the old db to be sure */
    1218           1 :     tests_set_cwd();
    1219             : 
    1220           1 :     return cmocka_run_group_tests(tests, NULL, NULL);
    1221             : }

Generated by: LCOV version 1.10