LCOV - code coverage report
Current view: top level - tests - sss_idmap-tests.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 276 276 100.0 %
Date: 2015-10-19 Functions: 29 29 100.0 %

          Line data    Source code
       1             : /*
       2             :     SSSD - Test for idmap library
       3             : 
       4             :     Authors:
       5             :         Sumit Bose <sbose@redhat.com>
       6             : 
       7             :     Copyright (C) 2012 Red Hat
       8             : 
       9             :     This program is free software; you can redistribute it and/or modify
      10             :     it under the terms of the GNU General Public License as published by
      11             :     the Free Software Foundation; either version 3 of the License, or
      12             :     (at your option) any later version.
      13             : 
      14             :     This program is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :     GNU General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License
      20             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include <check.h>
      24             : 
      25             : #include "lib/idmap/sss_idmap.h"
      26             : #include "lib/idmap/sss_idmap_private.h"
      27             : #include "tests/common_check.h"
      28             : 
      29             : #define IDMAP_RANGE_MIN 1234
      30             : #define IDMAP_RANGE_MAX 9876
      31             : 
      32             : #define IDMAP_RANGE_MIN2 11234
      33             : #define IDMAP_RANGE_MAX2 19876
      34             : 
      35             : const char test_sid[] = "S-1-5-21-2127521184-1604012920-1887927527-72713";
      36             : uint8_t test_bin_sid[] = {0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15,
      37             :                           0x00, 0x00, 0x00, 0xA0, 0x65, 0xCF, 0x7E, 0x78, 0x4B,
      38             :                           0x9B, 0x5F, 0xE7, 0x7C, 0x87, 0x70, 0x09, 0x1C, 0x01,
      39             :                           0x00};
      40             : size_t test_bin_sid_length = sizeof(test_bin_sid);
      41             : 
      42             : struct dom_sid test_smb_sid = {1, 5, {0, 0, 0, 0, 0, 5}, {21, 2127521184, 1604012920, 1887927527, 72713, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
      43             : 
      44             : const char large_sid[] = "S-1-5-21-1-2-4294967295-1000";
      45             : const char too_large_sid[] = "S-1-5-21-1-2-4294967296-1000";
      46             : 
      47             : struct sss_idmap_ctx *idmap_ctx;
      48             : 
      49         120 : static void *idmap_talloc(size_t size, void *pvt)
      50             : {
      51         120 :     return talloc_size(pvt, size);
      52             : }
      53             : 
      54         140 : static void idmap_talloc_free(void *ptr, void *pvt)
      55             : {
      56         140 :     talloc_free(ptr);
      57         140 : }
      58             : 
      59             : 
      60          19 : void idmap_ctx_setup(void)
      61             : {
      62             :     enum idmap_error_code err;
      63             : 
      64          19 :     err = sss_idmap_init(idmap_talloc, global_talloc_context, idmap_talloc_free,
      65             :                          &idmap_ctx);
      66             : 
      67          19 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
      68          19 :     fail_unless(idmap_ctx != NULL, "sss_idmap_init returned NULL.");
      69          19 : }
      70             : 
      71          19 : void idmap_ctx_teardown(void)
      72             : {
      73             :     enum idmap_error_code err;
      74             : 
      75          19 :     err = sss_idmap_free(idmap_ctx);
      76          19 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
      77          19 : }
      78             : 
      79           7 : void idmap_add_domain_setup(void)
      80             : {
      81             :     enum idmap_error_code err;
      82           7 :     struct sss_idmap_range range = {IDMAP_RANGE_MIN, IDMAP_RANGE_MAX};
      83             : 
      84           7 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom", "S-1-5-21-1-2-3", &range);
      85           7 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_add_domain failed.");
      86           7 : }
      87             : 
      88           1 : START_TEST(idmap_test_is_domain_sid)
      89             : {
      90             :     size_t c;
      91           1 :     const char *invalid[] = { "abc",
      92             :                               "S-1-2-3-4-5-6",
      93             :                               "S-1-5-21-1",
      94             :                               "S-1-5-21-1-2-123456789012345678",
      95             :                               "S-1-5-21-1+2+3",
      96             :                               "S-1-5-21-a-b-c",
      97             :                               "S-1-5-21-1-2-3-4",
      98             :                               NULL };
      99             : 
     100           1 :     fail_if(is_domain_sid(NULL), "is_domain_sid() returned true for [NULL]");
     101           8 :     for (c = 0; invalid[c] != NULL; c++) {
     102           7 :         fail_if(is_domain_sid(invalid[c]),
     103             :                 "is_domain_sid() returned true for [%s]", invalid[c]);
     104             :     }
     105             : 
     106           1 :     fail_unless(is_domain_sid("S-1-5-21-1-2-3"),
     107             :                 "is_domain_sid() returned true for [S-1-5-21-1-2-3]");
     108             : }
     109           1 : END_TEST
     110             : 
     111           1 : START_TEST(idmap_test_init_malloc)
     112             : {
     113             :     enum idmap_error_code err;
     114           1 :     struct sss_idmap_ctx *ctx = NULL;
     115             : 
     116           1 :     err = sss_idmap_init(NULL, NULL, NULL, &ctx);
     117             : 
     118           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
     119           1 :     fail_unless(ctx != NULL, "sss_idmap_init returned NULL.");
     120             : 
     121           1 :     err = sss_idmap_free(ctx);
     122           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
     123             : }
     124           1 : END_TEST
     125             : 
     126           1 : START_TEST(idmap_test_init_talloc)
     127             : {
     128             :     enum idmap_error_code err;
     129           1 :     struct sss_idmap_ctx *ctx = NULL;
     130             : 
     131           1 :     err = sss_idmap_init(idmap_talloc, global_talloc_context, idmap_talloc_free,
     132             :                          &ctx);
     133             : 
     134           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
     135           1 :     fail_unless(ctx != NULL, "sss_idmap_init returned NULL.");
     136             : 
     137           1 :     err = sss_idmap_free(ctx);
     138           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
     139             : }
     140           1 : END_TEST
     141             : 
     142           1 : START_TEST(idmap_test_add_domain)
     143             : {
     144           1 :     idmap_add_domain_setup();
     145             : }
     146           1 : END_TEST
     147             : 
     148           1 : START_TEST(idmap_test_add_domain_collisions)
     149             : {
     150             :     enum idmap_error_code err;
     151           1 :     struct sss_idmap_range range = {IDMAP_RANGE_MIN, IDMAP_RANGE_MAX};
     152           1 :     struct sss_idmap_range range2 = {IDMAP_RANGE_MIN2, IDMAP_RANGE_MAX2};
     153             : 
     154           1 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom", "S-1-5-21-1-2-3", &range);
     155           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_add_domain failed.");
     156             : 
     157           1 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom", "S-1-5-21-1-2-4",
     158             :                                &range2);
     159           1 :     fail_unless(err == IDMAP_COLLISION,
     160             :                 "sss_idmap_add_domain added domain with the same name.");
     161             : 
     162           1 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom2", "S-1-5-21-1-2-3",
     163             :                                &range2);
     164           1 :     fail_unless(err == IDMAP_COLLISION,
     165             :                 "sss_idmap_add_domain added domain with the same SID.");
     166             : 
     167           1 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom2", "S-1-5-21-1-2-4",
     168             :                                &range);
     169           1 :     fail_unless(err == IDMAP_COLLISION,
     170             :                 "sss_idmap_add_domain added domain with the same range.");
     171             : 
     172           1 :     err = sss_idmap_add_domain(idmap_ctx, "test.dom2", "S-1-5-21-1-2-4",
     173             :                                &range2);
     174           1 :     fail_unless(err == IDMAP_SUCCESS,
     175             :                 "sss_idmap_add_domain failed to add second domain.");
     176             : }
     177           1 : END_TEST
     178             : 
     179           1 : START_TEST(idmap_test_add_domain_collisions_ext_mapping)
     180             : {
     181             :     enum idmap_error_code err;
     182           1 :     struct sss_idmap_range range = {IDMAP_RANGE_MIN, IDMAP_RANGE_MAX};
     183           1 :     struct sss_idmap_range range2 = {IDMAP_RANGE_MIN2, IDMAP_RANGE_MAX2};
     184             : 
     185           1 :     err = sss_idmap_add_domain_ex(idmap_ctx, "test.dom", "S-1-5-21-1-2-3",
     186             :                                   &range, NULL, 0, true);
     187           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_add_domain failed.");
     188             : 
     189           1 :     err = sss_idmap_add_domain_ex(idmap_ctx, "test.dom", "S-1-5-21-1-2-4",
     190             :                                   &range2, NULL, 0, true);
     191           1 :     fail_unless(err == IDMAP_COLLISION,
     192             :                 "sss_idmap_add_domain added domain with the same name.");
     193             : 
     194           1 :     err = sss_idmap_add_domain_ex(idmap_ctx, "test.dom2", "S-1-5-21-1-2-3",
     195             :                                   &range2, NULL, 0, true);
     196           1 :     fail_unless(err == IDMAP_COLLISION,
     197             :                 "sss_idmap_add_domain added domain with the same SID.");
     198             : 
     199           1 :     err = sss_idmap_add_domain_ex(idmap_ctx, "test.dom2", "S-1-5-21-1-2-4",
     200             :                                   &range, NULL, 0, true);
     201           1 :     fail_unless(err == IDMAP_SUCCESS,
     202             :                 "sss_idmap_add_domain failed to add second domain with " \
     203             :                 "external mapping and the same range.");
     204             : }
     205           1 : END_TEST
     206             : 
     207           1 : START_TEST(idmap_test_sid2uid)
     208             : {
     209             :     enum idmap_error_code err;
     210             :     uint32_t id;
     211             : 
     212           1 :     err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3333-1000", &id);
     213           1 :     fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_sid_to_unix did not detect "
     214             :                                         "unknown domain");
     215             : 
     216           1 :     err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3-10000", &id);
     217           1 :     fail_unless(err == IDMAP_NO_RANGE, "sss_idmap_sid_to_unix did not detect "
     218             :                                        "RID out of range");
     219             : 
     220           1 :     err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3-1000", &id);
     221           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_sid_to_unix failed.");
     222           1 :     fail_unless(id == (1000 + IDMAP_RANGE_MIN),
     223             :                 "sss_idmap_sid_to_unix returned wrong id, "
     224             :                 "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
     225             : }
     226           1 : END_TEST
     227             : 
     228           1 : START_TEST(idmap_test_bin_sid2uid)
     229             : {
     230             :     enum idmap_error_code err;
     231             :     uint32_t id;
     232           1 :     uint8_t *bin_sid = NULL;
     233             :     size_t length;
     234             : 
     235           1 :     err = sss_idmap_sid_to_bin_sid(idmap_ctx, "S-1-5-21-1-2-3-1000",
     236             :                                    &bin_sid, &length);
     237           1 :     fail_unless(err == IDMAP_SUCCESS, "Failed to convert SID to binary SID");
     238             : 
     239           1 :     err = sss_idmap_bin_sid_to_unix(idmap_ctx, bin_sid, length , &id);
     240           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_bin_sid_to_unix failed.");
     241           1 :     fail_unless(id == (1000 + IDMAP_RANGE_MIN),
     242             :                 "sss_idmap_bin_sid_to_unix returned wrong id, "
     243             :                 "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
     244             : 
     245           1 :     sss_idmap_free_bin_sid(idmap_ctx, bin_sid);
     246             : }
     247           1 : END_TEST
     248             : 
     249           1 : START_TEST(idmap_test_dom_sid2uid)
     250             : {
     251             :     enum idmap_error_code err;
     252             :     uint32_t id;
     253           1 :     struct sss_dom_sid *dom_sid = NULL;
     254             : 
     255           1 :     err = sss_idmap_sid_to_dom_sid(idmap_ctx, "S-1-5-21-1-2-3-1000", &dom_sid);
     256           1 :     fail_unless(err == IDMAP_SUCCESS, "Failed to convert SID to SID structure");
     257             : 
     258           1 :     err = sss_idmap_dom_sid_to_unix(idmap_ctx, dom_sid, &id);
     259           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_dom_sid_to_unix failed.");
     260           1 :     fail_unless(id == (1000 + IDMAP_RANGE_MIN),
     261             :                 "sss_idmap_dom_sid_to_unix returned wrong id, "
     262             :                 "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
     263             : 
     264           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     265             : }
     266           1 : END_TEST
     267             : 
     268           1 : START_TEST(idmap_test_uid2sid)
     269             : {
     270             :     enum idmap_error_code err;
     271             :     char *sid;
     272             : 
     273           1 :     err = sss_idmap_unix_to_sid(idmap_ctx, 10000, &sid);
     274           1 :     fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_sid did not detect "
     275             :                                         "id out of range");
     276             : 
     277           1 :     err = sss_idmap_unix_to_sid(idmap_ctx, 2234, &sid);
     278           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_sid failed.");
     279           1 :     fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
     280             :                 "sss_idmap_unix_to_sid returned wrong SID, "
     281             :                 "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
     282             : 
     283           1 :     sss_idmap_free_sid(idmap_ctx, sid);
     284             : }
     285           1 : END_TEST
     286             : 
     287           1 : START_TEST(idmap_test_uid2dom_sid)
     288             : {
     289             :     enum idmap_error_code err;
     290           1 :     struct sss_dom_sid *dom_sid = NULL;
     291           1 :     char *sid = NULL;
     292             : 
     293           1 :     err = sss_idmap_unix_to_dom_sid(idmap_ctx, 10000, &dom_sid);
     294           1 :     fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_dom_sid did not detect "
     295             :                                         "id out of range");
     296             : 
     297           1 :     err = sss_idmap_unix_to_dom_sid(idmap_ctx, 2234, &dom_sid);
     298           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_dom_sid failed.");
     299             : 
     300           1 :     err = sss_idmap_dom_sid_to_sid(idmap_ctx, dom_sid, &sid);
     301           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_dom_sid_to_sid failed.");
     302             : 
     303           1 :     fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
     304             :                 "sss_idmap_unix_to_dom_sid returned wrong SID, "
     305             :                 "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
     306             : 
     307           1 :     sss_idmap_free_sid(idmap_ctx, sid);
     308           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     309             : }
     310           1 : END_TEST
     311             : 
     312           1 : START_TEST(idmap_test_uid2bin_sid)
     313             : {
     314             :     enum idmap_error_code err;
     315           1 :     uint8_t *bin_sid = NULL;
     316             :     size_t length;
     317           1 :     char *sid = NULL;
     318             : 
     319           1 :     err = sss_idmap_unix_to_bin_sid(idmap_ctx, 10000, &bin_sid, &length);
     320           1 :     fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_bin_sid did not detect "
     321             :                                         "id out of range");
     322             : 
     323           1 :     err = sss_idmap_unix_to_bin_sid(idmap_ctx, 2234, &bin_sid, &length);
     324           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_bin_sid failed.");
     325             : 
     326           1 :     err = sss_idmap_bin_sid_to_sid(idmap_ctx, bin_sid, length, &sid);
     327           1 :     fail_unless(err == IDMAP_SUCCESS, "sss_idmap_bin_sid_to_sid failed.");
     328             : 
     329           1 :     fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
     330             :                 "sss_idmap_unix_to_bin_sid returned wrong SID, "
     331             :                 "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
     332             : 
     333           1 :     sss_idmap_free_sid(idmap_ctx, sid);
     334           1 :     sss_idmap_free_bin_sid(idmap_ctx, bin_sid);
     335             : }
     336           1 : END_TEST
     337             : 
     338           1 : START_TEST(idmap_test_bin_sid2dom_sid)
     339             : {
     340           1 :     struct sss_dom_sid *dom_sid = NULL;
     341             :     enum idmap_error_code err;
     342           1 :     uint8_t *new_bin_sid = NULL;
     343             :     size_t new_bin_sid_length;
     344             : 
     345           1 :     err = sss_idmap_bin_sid_to_dom_sid(idmap_ctx, test_bin_sid,
     346             :                                        test_bin_sid_length, &dom_sid);
     347             : 
     348           1 :     fail_unless(err == IDMAP_SUCCESS,
     349             :                 "Failed to convert binary SID to struct sss_dom_sid.");
     350             : 
     351           1 :     err = sss_idmap_dom_sid_to_bin_sid(idmap_ctx, dom_sid, &new_bin_sid,
     352             :                                        &new_bin_sid_length);
     353           1 :     fail_unless(err == IDMAP_SUCCESS,
     354             :                 "Failed to convert struct sss_dom_sid to binary SID.");
     355             : 
     356           1 :     fail_unless(new_bin_sid_length == test_bin_sid_length,
     357             :                 "Length of binary SIDs do not match.");
     358           1 :     fail_unless(memcmp(test_bin_sid, new_bin_sid, test_bin_sid_length) == 0,
     359             :                 "Binary SIDs do not match.");
     360             : 
     361           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     362           1 :     sss_idmap_free_bin_sid(idmap_ctx, new_bin_sid);
     363             : }
     364           1 : END_TEST
     365             : 
     366           1 : START_TEST(idmap_test_sid2dom_sid)
     367             : {
     368           1 :     struct sss_dom_sid *dom_sid = NULL;
     369             :     enum idmap_error_code err;
     370           1 :     char *new_sid = NULL;
     371             : 
     372           1 :     err = sss_idmap_sid_to_dom_sid(idmap_ctx, "S-1-5-21-1-2-3-1000", &dom_sid);
     373             : 
     374           1 :     fail_unless(err == IDMAP_SUCCESS,
     375             :                 "Failed to convert SID string to struct sss_dom_sid.");
     376             : 
     377           1 :     err = sss_idmap_dom_sid_to_sid(idmap_ctx, dom_sid, &new_sid);
     378           1 :     fail_unless(err == IDMAP_SUCCESS,
     379             :                 "Failed to convert struct sss_dom_sid to SID string.");
     380             : 
     381           1 :     fail_unless(new_sid != NULL, "SID string not set");
     382           1 :     fail_unless(strlen("S-1-5-21-1-2-3-1000") == strlen(new_sid),
     383             :                 "Length of SID strings do not match.");
     384           1 :     fail_unless(strcmp("S-1-5-21-1-2-3-1000", new_sid) == 0,
     385             :                 "SID strings do not match.");
     386             : 
     387           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     388           1 :     sss_idmap_free_sid(idmap_ctx, new_sid);
     389             : }
     390           1 : END_TEST
     391             : 
     392           1 : START_TEST(idmap_test_large_and_too_large_sid)
     393             : {
     394           1 :     struct sss_dom_sid *dom_sid = NULL;
     395             :     enum idmap_error_code err;
     396           1 :     char *new_sid = NULL;
     397             : 
     398           1 :     err = sss_idmap_sid_to_dom_sid(idmap_ctx, large_sid, &dom_sid);
     399             : 
     400           1 :     fail_unless(err == IDMAP_SUCCESS,
     401             :                 "Failed to convert SID string with a UINT32_MAX component "
     402             :                 "to struct sss_dom_sid.");
     403             : 
     404           1 :     err = sss_idmap_dom_sid_to_sid(idmap_ctx, dom_sid, &new_sid);
     405           1 :     fail_unless(err == IDMAP_SUCCESS,
     406             :                 "Failed to convert struct sss_dom_sid to SID string.");
     407             : 
     408           1 :     fail_unless(new_sid != NULL, "SID string not set");
     409           1 :     fail_unless(strlen(large_sid) == strlen(new_sid),
     410             :                 "Length of SID strings do not match.");
     411           1 :     fail_unless(strcmp(large_sid, new_sid) == 0,
     412             :                 "SID strings do not match, expected [%s], got [%s]",
     413             :                 large_sid, new_sid);
     414             : 
     415           1 :     err = sss_idmap_sid_to_dom_sid(idmap_ctx, too_large_sid, &dom_sid);
     416           1 :     fail_unless(err == IDMAP_SID_INVALID,
     417             :                 "Trying to convert  a SID with a too large component "
     418             :                 "did not return IDMAP_SID_INVALID");
     419             : 
     420           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     421           1 :     sss_idmap_free_sid(idmap_ctx, new_sid);
     422             : }
     423           1 : END_TEST
     424             : 
     425           1 : START_TEST(idmap_test_sid2bin_sid)
     426             : {
     427             :     enum idmap_error_code err;
     428             :     size_t length;
     429           1 :     uint8_t *bin_sid = NULL;
     430             : 
     431           1 :     err = sss_idmap_sid_to_bin_sid(idmap_ctx, test_sid, &bin_sid, &length);
     432           1 :     fail_unless(err == IDMAP_SUCCESS,
     433             :                 "Failed to convert SID string to binary sid.");
     434           1 :     fail_unless(length == test_bin_sid_length,
     435             :                 "Size of binary SIDs do not match, got [%d], expected [%d]",
     436             :                 length, test_bin_sid_length);
     437           1 :     fail_unless(memcmp(bin_sid, test_bin_sid, test_bin_sid_length) == 0,
     438             :                 "Binary SIDs do not match");
     439             : 
     440           1 :     sss_idmap_free_bin_sid(idmap_ctx, bin_sid);
     441             : }
     442           1 : END_TEST
     443             : 
     444           1 : START_TEST(idmap_test_bin_sid2sid)
     445             : {
     446             :     enum idmap_error_code err;
     447           1 :     char *sid = NULL;
     448             : 
     449           1 :     err = sss_idmap_bin_sid_to_sid(idmap_ctx, test_bin_sid, test_bin_sid_length,
     450             :                                    &sid);
     451           1 :     fail_unless(err == IDMAP_SUCCESS,
     452             :                 "Failed to convert binary SID to SID string.");
     453           1 :     fail_unless(strcmp(sid, test_sid) == 0, "SID strings do not match, "
     454             :                                             "expected [%s], get [%s]",
     455             :                                             test_sid, sid);
     456             : 
     457           1 :     sss_idmap_free_sid(idmap_ctx, sid);
     458             : }
     459           1 : END_TEST
     460             : 
     461           1 : START_TEST(idmap_test_smb_sid2dom_sid)
     462             : {
     463           1 :     struct sss_dom_sid *dom_sid = NULL;
     464             :     enum idmap_error_code err;
     465           1 :     struct dom_sid *new_smb_sid = NULL;
     466             : 
     467           1 :     err = sss_idmap_smb_sid_to_dom_sid(idmap_ctx, &test_smb_sid, &dom_sid);
     468           1 :     fail_unless(err == IDMAP_SUCCESS,
     469             :                 "Failed to convert samba dom_sid to struct sss_dom_sid.");
     470             : 
     471           1 :     err = sss_idmap_dom_sid_to_smb_sid(idmap_ctx, dom_sid, &new_smb_sid);
     472           1 :     fail_unless(err == IDMAP_SUCCESS,
     473             :                 "Failed to convert struct sss_dom_sid to samba dom_sid.");
     474             : 
     475           1 :     fail_unless(memcmp(&test_smb_sid, new_smb_sid, sizeof(struct dom_sid)) == 0,
     476             :                 "Samba dom_sid-s do not match.");
     477             : 
     478           1 :     sss_idmap_free_dom_sid(idmap_ctx, dom_sid);
     479           1 :     sss_idmap_free_smb_sid(idmap_ctx, new_smb_sid);
     480             : }
     481           1 : END_TEST
     482             : 
     483           1 : START_TEST(idmap_test_smb_sid2bin_sid)
     484             : {
     485             :     enum idmap_error_code err;
     486             :     size_t length;
     487           1 :     uint8_t *bin_sid = NULL;
     488             : 
     489           1 :     err = sss_idmap_smb_sid_to_bin_sid(idmap_ctx, &test_smb_sid,
     490             :                                        &bin_sid, &length);
     491           1 :     fail_unless(err == IDMAP_SUCCESS,
     492             :                 "Failed to convert samba dom_sid to binary sid.");
     493           1 :     fail_unless(length == test_bin_sid_length,
     494             :                 "Size of binary SIDs do not match, got [%d], expected [%d]",
     495             :                 length, test_bin_sid_length);
     496           1 :     fail_unless(memcmp(bin_sid, test_bin_sid, test_bin_sid_length) == 0,
     497             :                 "Binary SIDs do not match.");
     498             : 
     499           1 :     sss_idmap_free_bin_sid(idmap_ctx, bin_sid);
     500             : }
     501           1 : END_TEST
     502             : 
     503           1 : START_TEST(idmap_test_bin_sid2smb_sid)
     504             : {
     505             :     enum idmap_error_code err;
     506           1 :     struct dom_sid *smb_sid = NULL;
     507             : 
     508           1 :     err = sss_idmap_bin_sid_to_smb_sid(idmap_ctx, test_bin_sid,
     509             :                                        test_bin_sid_length, &smb_sid);
     510           1 :     fail_unless(err == IDMAP_SUCCESS,
     511             :                 "Failed to convert binary sid to samba dom_sid.");
     512           1 :     fail_unless(memcmp(&test_smb_sid, smb_sid, sizeof(struct dom_sid)) == 0,
     513             :                  "Samba dom_sid structs do not match.");
     514             : 
     515           1 :     sss_idmap_free_smb_sid(idmap_ctx, smb_sid);
     516             : }
     517           1 : END_TEST
     518             : 
     519           1 : START_TEST(idmap_test_smb_sid2sid)
     520             : {
     521             :     enum idmap_error_code err;
     522           1 :     char *sid = NULL;
     523             : 
     524           1 :     err = sss_idmap_smb_sid_to_sid(idmap_ctx, &test_smb_sid, &sid);
     525           1 :     fail_unless(err == IDMAP_SUCCESS,
     526             :                 "Failed to convert samba dom_sid to sid string.");
     527           1 :     fail_unless(strcmp(sid, test_sid) == 0, "SID strings do not match, "
     528             :                                             "expected [%s], get [%s]",
     529             :                                             test_sid, sid);
     530             : 
     531           1 :     sss_idmap_free_sid(idmap_ctx, sid);
     532             : }
     533           1 : END_TEST
     534             : 
     535           1 : START_TEST(idmap_test_sid2smb_sid)
     536             : {
     537             :     enum idmap_error_code err;
     538           1 :     struct dom_sid *smb_sid = NULL;
     539             : 
     540           1 :     err = sss_idmap_sid_to_smb_sid(idmap_ctx, test_sid, &smb_sid);
     541           1 :     fail_unless(err == IDMAP_SUCCESS,
     542             :                 "Failed to convert binary sid to samba dom_sid.");
     543           1 :     fail_unless(memcmp(&test_smb_sid, smb_sid, sizeof(struct dom_sid)) == 0,
     544             :                  "Samba dom_sid structs do not match.");
     545             : 
     546           1 :     sss_idmap_free_smb_sid(idmap_ctx, smb_sid);
     547             : }
     548           1 : END_TEST
     549             : 
     550             : 
     551           1 : Suite *idmap_test_suite (void)
     552             : {
     553           1 :     Suite *s = suite_create ("IDMAP");
     554             : 
     555           1 :     TCase *tc_init = tcase_create("IDMAP init tests");
     556           1 :     tcase_add_checked_fixture(tc_init,
     557             :                               ck_leak_check_setup,
     558             :                               ck_leak_check_teardown);
     559             : 
     560           1 :     tcase_add_test(tc_init, idmap_test_init_malloc);
     561           1 :     tcase_add_test(tc_init, idmap_test_init_talloc);
     562           1 :     tcase_add_test(tc_init, idmap_test_is_domain_sid);
     563             : 
     564           1 :     suite_add_tcase(s, tc_init);
     565             : 
     566           1 :     TCase *tc_dom = tcase_create("IDMAP domain tests");
     567           1 :     tcase_add_checked_fixture(tc_dom,
     568             :                               ck_leak_check_setup,
     569             :                               ck_leak_check_teardown);
     570           1 :     tcase_add_checked_fixture(tc_dom,
     571             :                               idmap_ctx_setup,
     572             :                               idmap_ctx_teardown);
     573             : 
     574           1 :     tcase_add_test(tc_dom, idmap_test_add_domain);
     575           1 :     tcase_add_test(tc_dom, idmap_test_add_domain_collisions);
     576           1 :     tcase_add_test(tc_dom, idmap_test_add_domain_collisions_ext_mapping);
     577             : 
     578           1 :     suite_add_tcase(s, tc_dom);
     579             : 
     580           1 :     TCase *tc_conv = tcase_create("IDMAP SID conversion tests");
     581           1 :     tcase_add_checked_fixture(tc_conv,
     582             :                               ck_leak_check_setup,
     583             :                               ck_leak_check_teardown);
     584           1 :     tcase_add_checked_fixture(tc_conv,
     585             :                               idmap_ctx_setup,
     586             :                               idmap_ctx_teardown);
     587             : 
     588           1 :     tcase_add_test(tc_conv, idmap_test_bin_sid2dom_sid);
     589           1 :     tcase_add_test(tc_conv, idmap_test_sid2dom_sid);
     590           1 :     tcase_add_test(tc_conv, idmap_test_sid2bin_sid);
     591           1 :     tcase_add_test(tc_conv, idmap_test_bin_sid2sid);
     592           1 :     tcase_add_test(tc_conv, idmap_test_smb_sid2dom_sid);
     593           1 :     tcase_add_test(tc_conv, idmap_test_smb_sid2bin_sid);
     594           1 :     tcase_add_test(tc_conv, idmap_test_bin_sid2smb_sid);
     595           1 :     tcase_add_test(tc_conv, idmap_test_smb_sid2sid);
     596           1 :     tcase_add_test(tc_conv, idmap_test_sid2smb_sid);
     597           1 :     tcase_add_test(tc_conv, idmap_test_large_and_too_large_sid);
     598             : 
     599           1 :     suite_add_tcase(s, tc_conv);
     600             : 
     601           1 :     TCase *tc_map = tcase_create("IDMAP mapping tests");
     602           1 :     tcase_add_checked_fixture(tc_map,
     603             :                               ck_leak_check_setup,
     604             :                               ck_leak_check_teardown);
     605           1 :     tcase_add_checked_fixture(tc_map,
     606             :                               idmap_ctx_setup,
     607             :                               idmap_ctx_teardown);
     608           1 :     tcase_add_checked_fixture(tc_map,
     609             :                               idmap_add_domain_setup,
     610             :                               NULL);
     611             : 
     612           1 :     tcase_add_test(tc_map, idmap_test_sid2uid);
     613           1 :     tcase_add_test(tc_map, idmap_test_bin_sid2uid);
     614           1 :     tcase_add_test(tc_map, idmap_test_dom_sid2uid);
     615           1 :     tcase_add_test(tc_map, idmap_test_uid2sid);
     616           1 :     tcase_add_test(tc_map, idmap_test_uid2dom_sid);
     617           1 :     tcase_add_test(tc_map, idmap_test_uid2bin_sid);
     618             : 
     619           1 :     suite_add_tcase(s, tc_map);
     620             : 
     621           1 :     return s;
     622             : }
     623           1 : int main(int argc, const char *argv[])
     624             : {
     625             :     int number_failed;
     626             : 
     627           1 :     tests_set_cwd();
     628             : 
     629           1 :     Suite *s = idmap_test_suite();
     630           1 :     SRunner *sr = srunner_create(s);
     631             : 
     632             :     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
     633           1 :     srunner_run_all(sr, CK_ENV);
     634           1 :     number_failed = srunner_ntests_failed (sr);
     635           1 :     srunner_free (sr);
     636             : 
     637           1 :     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
     638             : }

Generated by: LCOV version 1.10