sss_idmap-tests.c revision 2998435fcc95857b73049b3955af9889ab595f24
7fb4c0766e858653c9776474005a6ae6d94828afgryzor/*
7fb4c0766e858653c9776474005a6ae6d94828afgryzor SSSD - Test for idmap library
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor Authors:
7fb4c0766e858653c9776474005a6ae6d94828afgryzor Sumit Bose <sbose@redhat.com>
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor Copyright (C) 2012 Red Hat
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor This program is free software; you can redistribute it and/or modify
7fb4c0766e858653c9776474005a6ae6d94828afgryzor it under the terms of the GNU General Public License as published by
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen the Free Software Foundation; either version 3 of the License, or
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen (at your option) any later version.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen This program is distributed in the hope that it will be useful,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor but WITHOUT ANY WARRANTY; without even the implied warranty of
7fb4c0766e858653c9776474005a6ae6d94828afgryzor MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7fb4c0766e858653c9776474005a6ae6d94828afgryzor GNU General Public License for more details.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd
7fb4c0766e858653c9776474005a6ae6d94828afgryzor You should have received a copy of the GNU General Public License
7fb4c0766e858653c9776474005a6ae6d94828afgryzor along with this program. If not, see <http://www.gnu.org/licenses/>.
7fb4c0766e858653c9776474005a6ae6d94828afgryzor*/
3f08db06526d6901aa08c110b5bc7dde6bc39905nd
7fb4c0766e858653c9776474005a6ae6d94828afgryzor#include <check.h>
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung#include "lib/idmap/sss_idmap.h"
7fb4c0766e858653c9776474005a6ae6d94828afgryzor#include "lib/idmap/sss_idmap_private.h"
c3c006c28c5b03892ccaef6e4d2cbb15a13a2072rbowen#include "tests/common.h"
c3c006c28c5b03892ccaef6e4d2cbb15a13a2072rbowen
7fb4c0766e858653c9776474005a6ae6d94828afgryzor#define IDMAP_RANGE_MIN 1234
7fb4c0766e858653c9776474005a6ae6d94828afgryzor#define IDMAP_RANGE_MAX 9876
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sfconst char test_sid[] = "S-1-5-21-2127521184-1604012920-1887927527-72713";
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sfuint8_t test_bin_sid[] = {0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15,
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf 0x00, 0x00, 0x00, 0xA0, 0x65, 0xCF, 0x7E, 0x78, 0x4B,
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf 0x9B, 0x5F, 0xE7, 0x7C, 0x87, 0x70, 0x09, 0x1C, 0x01,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor 0x00};
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalicsize_t test_bin_sid_length = sizeof(test_bin_sid);
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic
7fb4c0766e858653c9776474005a6ae6d94828afgryzorstruct 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}};
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzorstruct sss_idmap_ctx *idmap_ctx;
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorstatic void *idmap_talloc(size_t size, void *pvt)
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor return talloc_size(pvt, size);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalicstatic void idmap_talloc_free(void *ptr, void *pvt)
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic{
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic talloc_free(ptr);
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic}
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalicvoid idmap_ctx_setup(void)
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic{
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf enum idmap_error_code err;
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd err = sss_idmap_init(idmap_talloc, global_talloc_context, idmap_talloc_free,
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic &idmap_ctx);
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
f9442c8a9869d3525a1ae6ed3e85c65d408e8a70sf fail_unless(idmap_ctx != NULL, "sss_idmap_init returned NULL.");
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic}
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalicvoid idmap_ctx_teardown(void)
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic{
70f5253b24dd333c67fb6502d557a8b48ad3ba87igalic enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_free(idmap_ctx);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorvoid idmap_add_domain_setup(void)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_idmap_range range = {IDMAP_RANGE_MIN, IDMAP_RANGE_MAX};
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_add_domain(idmap_ctx, "test.dom", "S-1-5-21-1-2-3", &range);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_add_domain failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_is_domain_sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor size_t c;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor const char *invalid[] = { "abc",
9534272616b71aaea50aeec4162e749a96aebd7fsf "S-1-2-3-4-5-6",
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "S-1-5-21-1",
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "S-1-5-21-1-2-123456789012345678",
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "S-1-5-21-1+2+3",
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor "S-1-5-21-a-b-c",
9534272616b71aaea50aeec4162e749a96aebd7fsf "S-1-5-21-1-2-3-4",
9534272616b71aaea50aeec4162e749a96aebd7fsf NULL };
9534272616b71aaea50aeec4162e749a96aebd7fsf
9534272616b71aaea50aeec4162e749a96aebd7fsf fail_if(is_domain_sid(NULL), "is_domain_sid() returned true for [NULL]");
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor for (c = 0; invalid[c] != NULL; c++) {
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_if(is_domain_sid(invalid[c]),
9534272616b71aaea50aeec4162e749a96aebd7fsf "is_domain_sid() returned true for [%s]", invalid[c]);
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor }
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor fail_unless(is_domain_sid("S-1-5-21-1-2-3"),
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor "is_domain_sid() returned true for [S-1-5-21-1-2-3]");
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
e7eb98e34575dcf6536530bcf08e00d7ab3d3124gryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_init_malloc)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_idmap_ctx *ctx = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_init(NULL, NULL, NULL, &ctx);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(ctx != NULL, "sss_idmap_init returned NULL.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_free(ctx);
c2ed7ae314df26c4cd9e3a7224dabc6baa3f4dceigalic fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_init_talloc)
9534272616b71aaea50aeec4162e749a96aebd7fsf{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_idmap_ctx *ctx = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_init(idmap_talloc, global_talloc_context, idmap_talloc_free,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor &ctx);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_init failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(ctx != NULL, "sss_idmap_init returned NULL.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_free(ctx);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_free failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_add_domain)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor idmap_add_domain_setup();
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_sid2uid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint32_t id;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3333-1000", &id);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_sid_to_unix did not detect "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "unknown domain");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3-10000", &id);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_NO_RANGE, "sss_idmap_sid_to_unix did not detect "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "RID out of range");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_unix(idmap_ctx, "S-1-5-21-1-2-3-1000", &id);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_sid_to_unix failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(id == (1000 + IDMAP_RANGE_MIN),
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_sid_to_unix returned wrong id, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_bin_sid2uid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint32_t id;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint8_t *bin_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor size_t length;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_bin_sid(idmap_ctx, "S-1-5-21-1-2-3-1000",
7fb4c0766e858653c9776474005a6ae6d94828afgryzor &bin_sid, &length);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "Failed to convert SID to binary SID");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_bin_sid_to_unix(idmap_ctx, bin_sid, length , &id);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_bin_sid_to_unix failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(id == (1000 + IDMAP_RANGE_MIN),
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_bin_sid_to_unix returned wrong id, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(bin_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_dom_sid2uid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint32_t id;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_dom_sid *dom_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_dom_sid(idmap_ctx, "S-1-5-21-1-2-3-1000", &dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "Failed to convert SID to SID structure");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_dom_sid_to_unix(idmap_ctx, dom_sid, &id);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_dom_sid_to_unix failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(id == (1000 + IDMAP_RANGE_MIN),
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_dom_sid_to_unix returned wrong id, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "got [%d], expected [%d].", id, 1000 + IDMAP_RANGE_MIN);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_uid2sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor char *sid;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_sid(idmap_ctx, 10000, &sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_sid did not detect "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "id out of range");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_sid(idmap_ctx, 2234, &sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_sid failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_unix_to_sid returned wrong SID, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_uid2dom_sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_dom_sid *dom_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor char *sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_dom_sid(idmap_ctx, 10000, &dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_dom_sid did not detect "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "id out of range");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_dom_sid(idmap_ctx, 2234, &dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_dom_sid failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_dom_sid_to_sid(idmap_ctx, dom_sid, &sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_dom_sid_to_sid failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_unix_to_dom_sid returned wrong SID, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_uid2bin_sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint8_t *bin_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor size_t length;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor char *sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_bin_sid(idmap_ctx, 10000, &bin_sid, &length);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_NO_DOMAIN, "sss_idmap_unix_to_bin_sid did not detect "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "id out of range");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_unix_to_bin_sid(idmap_ctx, 2234, &bin_sid, &length);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_unix_to_bin_sid failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_bin_sid_to_sid(idmap_ctx, bin_sid, length, &sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS, "sss_idmap_bin_sid_to_sid failed.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(strcmp(sid, "S-1-5-21-1-2-3-1000") == 0,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "sss_idmap_unix_to_bin_sid returned wrong SID, "
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "expected [%s], got [%s].", "S-1-5-21-1-2-3-1000", sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(bin_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_bin_sid2dom_sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_dom_sid *dom_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor uint8_t *new_bin_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor size_t new_bin_sid_length;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_bin_sid_to_dom_sid(idmap_ctx, test_bin_sid,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor test_bin_sid_length, &dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "Failed to convert binary SID to struct sss_dom_sid.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_dom_sid_to_bin_sid(idmap_ctx, dom_sid, &new_bin_sid,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor &new_bin_sid_length);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "Failed to convert struct sss_dom_sid to binary SID.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(new_bin_sid_length == test_bin_sid_length,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "Length of binary SIDs do not match.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(memcmp(test_bin_sid, new_bin_sid, test_bin_sid_length) == 0,
0236ede61169f34359c234625a1d792b97d7db12nd "Binary SIDs do not match.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor talloc_free(new_bin_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_sid2dom_sid)
7fb4c0766e858653c9776474005a6ae6d94828afgryzor{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor struct sss_dom_sid *dom_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor char *new_sid = NULL;
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor err = sss_idmap_sid_to_dom_sid(idmap_ctx, "S-1-5-21-1-2-3-1000", &dom_sid);
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(err == IDMAP_SUCCESS,
7fb4c0766e858653c9776474005a6ae6d94828afgryzor "Failed to convert SID string to struct sss_dom_sid.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim err = sss_idmap_dom_sid_to_sid(idmap_ctx, dom_sid, &new_sid);
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim fail_unless(err == IDMAP_SUCCESS,
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim "Failed to convert struct sss_dom_sid to SID string.");
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim fail_unless(new_sid != NULL, "SID string not set");
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim fail_unless(strlen("S-1-5-21-1-2-3-1000") == strlen(new_sid),
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim "Length of SID strings do not match.");
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim fail_unless(strcmp("S-1-5-21-1-2-3-1000", new_sid) == 0,
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim "SID strings do not match.");
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim talloc_free(dom_sid);
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim talloc_free(new_sid);
b03f9485e6dfcf9326e6122f91eaa1ced8939818jim}
7fb4c0766e858653c9776474005a6ae6d94828afgryzorEND_TEST
7fb4c0766e858653c9776474005a6ae6d94828afgryzor
7fb4c0766e858653c9776474005a6ae6d94828afgryzorSTART_TEST(idmap_test_sid2bin_sid)
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung{
7fb4c0766e858653c9776474005a6ae6d94828afgryzor enum idmap_error_code err;
5effc8b39fae5cd169d17f342bfc265705840014rbowen size_t length;
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd uint8_t *bin_sid = NULL;
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd err = sss_idmap_sid_to_bin_sid(idmap_ctx, test_sid, &bin_sid, &length);
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd fail_unless(err == IDMAP_SUCCESS,
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd "Failed to convert SID string to binary sid.");
7fb4c0766e858653c9776474005a6ae6d94828afgryzor fail_unless(length == test_bin_sid_length,
"Size of binary SIDs do not match, got [%d], expected [%d]",
length, test_bin_sid_length);
fail_unless(memcmp(bin_sid, test_bin_sid, test_bin_sid_length) == 0,
"Binary SIDs do not match");
talloc_free(bin_sid);
}
END_TEST
START_TEST(idmap_test_bin_sid2sid)
{
enum idmap_error_code err;
char *sid = NULL;
err = sss_idmap_bin_sid_to_sid(idmap_ctx, test_bin_sid, test_bin_sid_length,
&sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert binary SID to SID string.");
fail_unless(strcmp(sid, test_sid) == 0, "SID strings do not match, "
"expected [%s], get [%s]",
test_sid, sid);
talloc_free(sid);
}
END_TEST
START_TEST(idmap_test_smb_sid2dom_sid)
{
struct sss_dom_sid *dom_sid = NULL;
enum idmap_error_code err;
struct dom_sid *new_smb_sid = NULL;
err = sss_idmap_smb_sid_to_dom_sid(idmap_ctx, &test_smb_sid, &dom_sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert samba dom_sid to struct sss_dom_sid.");
err = sss_idmap_dom_sid_to_smb_sid(idmap_ctx, dom_sid, &new_smb_sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert struct sss_dom_sid to samba dom_sid.");
fail_unless(memcmp(&test_smb_sid, new_smb_sid, sizeof(struct dom_sid)) == 0,
"Samba dom_sid-s do not match.");
talloc_free(dom_sid);
talloc_free(new_smb_sid);
}
END_TEST
START_TEST(idmap_test_smb_sid2bin_sid)
{
enum idmap_error_code err;
size_t length;
uint8_t *bin_sid = NULL;
err = sss_idmap_smb_sid_to_bin_sid(idmap_ctx, &test_smb_sid,
&bin_sid, &length);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert samba dom_sid to binary sid.");
fail_unless(length == test_bin_sid_length,
"Size of binary SIDs do not match, got [%d], expected [%d]",
length, test_bin_sid_length);
fail_unless(memcmp(bin_sid, test_bin_sid, test_bin_sid_length) == 0,
"Binary SIDs do not match.");
talloc_free(bin_sid);
}
END_TEST
START_TEST(idmap_test_bin_sid2smb_sid)
{
enum idmap_error_code err;
struct dom_sid *smb_sid = NULL;
err = sss_idmap_bin_sid_to_smb_sid(idmap_ctx, test_bin_sid,
test_bin_sid_length, &smb_sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert binary sid to samba dom_sid.");
fail_unless(memcmp(&test_smb_sid, smb_sid, sizeof(struct dom_sid)) == 0,
"Samba dom_sid structs do not match.");
talloc_free(smb_sid);
}
END_TEST
START_TEST(idmap_test_smb_sid2sid)
{
enum idmap_error_code err;
char *sid = NULL;
err = sss_idmap_smb_sid_to_sid(idmap_ctx, &test_smb_sid, &sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert samba dom_sid to sid string.");
fail_unless(strcmp(sid, test_sid) == 0, "SID strings do not match, "
"expected [%s], get [%s]",
test_sid, sid);
talloc_free(sid);
}
END_TEST
START_TEST(idmap_test_sid2smb_sid)
{
enum idmap_error_code err;
struct dom_sid *smb_sid = NULL;
err = sss_idmap_sid_to_smb_sid(idmap_ctx, test_sid, &smb_sid);
fail_unless(err == IDMAP_SUCCESS,
"Failed to convert binary sid to samba dom_sid.");
fail_unless(memcmp(&test_smb_sid, smb_sid, sizeof(struct dom_sid)) == 0,
"Samba dom_sid structs do not match.");
talloc_free(smb_sid);
}
END_TEST
Suite *idmap_test_suite (void)
{
Suite *s = suite_create ("IDMAP");
TCase *tc_init = tcase_create("IDMAP init tests");
tcase_add_checked_fixture(tc_init,
leak_check_setup,
leak_check_teardown);
tcase_add_test(tc_init, idmap_test_init_malloc);
tcase_add_test(tc_init, idmap_test_init_talloc);
tcase_add_test(tc_init, idmap_test_is_domain_sid);
suite_add_tcase(s, tc_init);
TCase *tc_dom = tcase_create("IDMAP domain tests");
tcase_add_checked_fixture(tc_dom,
leak_check_setup,
leak_check_teardown);
tcase_add_checked_fixture(tc_dom,
idmap_ctx_setup,
idmap_ctx_teardown);
tcase_add_test(tc_dom, idmap_test_add_domain);
suite_add_tcase(s, tc_dom);
TCase *tc_conv = tcase_create("IDMAP SID conversion tests");
tcase_add_checked_fixture(tc_conv,
leak_check_setup,
leak_check_teardown);
tcase_add_checked_fixture(tc_conv,
idmap_ctx_setup,
idmap_ctx_teardown);
tcase_add_test(tc_conv, idmap_test_bin_sid2dom_sid);
tcase_add_test(tc_conv, idmap_test_sid2dom_sid);
tcase_add_test(tc_conv, idmap_test_sid2bin_sid);
tcase_add_test(tc_conv, idmap_test_bin_sid2sid);
tcase_add_test(tc_conv, idmap_test_smb_sid2dom_sid);
tcase_add_test(tc_conv, idmap_test_smb_sid2bin_sid);
tcase_add_test(tc_conv, idmap_test_bin_sid2smb_sid);
tcase_add_test(tc_conv, idmap_test_smb_sid2sid);
tcase_add_test(tc_conv, idmap_test_sid2smb_sid);
suite_add_tcase(s, tc_conv);
TCase *tc_map = tcase_create("IDMAP mapping tests");
tcase_add_checked_fixture(tc_map,
leak_check_setup,
leak_check_teardown);
tcase_add_checked_fixture(tc_map,
idmap_ctx_setup,
idmap_ctx_teardown);
tcase_add_checked_fixture(tc_map,
idmap_add_domain_setup,
NULL);
tcase_add_test(tc_map, idmap_test_sid2uid);
tcase_add_test(tc_map, idmap_test_bin_sid2uid);
tcase_add_test(tc_map, idmap_test_dom_sid2uid);
tcase_add_test(tc_map, idmap_test_uid2sid);
tcase_add_test(tc_map, idmap_test_uid2dom_sid);
tcase_add_test(tc_map, idmap_test_uid2bin_sid);
suite_add_tcase(s, tc_map);
return s;
}
int main(int argc, const char *argv[])
{
int number_failed;
tests_set_cwd();
Suite *s = idmap_test_suite();
SRunner *sr = srunner_create(s);
/* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed (sr);
srunner_free (sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}