Line data Source code
1 : /*
2 : Authors:
3 : Sumit Bose <sbose@redhat.com>
4 :
5 : Copyright (C) 2013 Red Hat
6 :
7 : Test for the NSS Responder ID-SID mapping interface
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 <errno.h>
24 : #include <stdarg.h>
25 : #include <stddef.h>
26 : #include <setjmp.h>
27 : #include <cmocka.h>
28 :
29 :
30 : #include "util/util.h"
31 : #include "util/sss_endian.h"
32 :
33 : #include "sss_client/idmap/sss_nss_idmap.h"
34 : #include "tests/cmocka/common_mock.h"
35 :
36 : #include <nss.h>
37 : #include "sss_client/sss_cli.h"
38 :
39 : struct sss_nss_make_request_test_data {
40 : uint8_t *repbuf;
41 : size_t replen;
42 : int errnop;
43 : enum nss_status nss_status;
44 : };
45 :
46 : #if (__BYTE_ORDER == __LITTLE_ENDIAN)
47 : uint8_t buf1[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
48 : uint8_t buf2[] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
49 : uint8_t buf3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
50 : uint8_t buf4[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 'x'};
51 :
52 : uint8_t buf_orig1[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 'k', 'e', 'y', 0x00, 'v', 'a', 'l', 'u', 'e', 0x00};
53 : #elif (__BYTE_ORDER == __BIG_ENDIAN)
54 : uint8_t buf1[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
55 : uint8_t buf2[] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
56 : uint8_t buf3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 0x00};
57 : uint8_t buf4[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't', 'x'};
58 :
59 : uint8_t buf_orig1[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'k', 'e', 'y', 0x00, 'v', 'a', 'l', 'u', 'e', 0x00};
60 : #else
61 : #error "unknow endianess"
62 : #endif
63 :
64 5 : enum nss_status sss_nss_make_request(enum sss_cli_command cmd,
65 : struct sss_cli_req_data *rd,
66 : uint8_t **repbuf, size_t *replen,
67 : int *errnop)
68 : {
69 : struct sss_nss_make_request_test_data *d;
70 :
71 5 : d = sss_mock_ptr_type(struct sss_nss_make_request_test_data *);
72 :
73 5 : *replen = d->replen;
74 5 : *errnop = d->errnop;
75 :
76 : /* the caller must be able to free repbuf. */
77 5 : if (*replen != 0 && d->repbuf != NULL) {
78 5 : *repbuf = malloc(*replen);
79 5 : assert_non_null(*repbuf);
80 5 : memcpy(*repbuf, d->repbuf, *replen);
81 : }
82 :
83 5 : return d->nss_status;
84 : }
85 :
86 1 : void test_getsidbyname(void **state)
87 : {
88 : int ret;
89 1 : char *sid = NULL;
90 : size_t c;
91 : enum sss_id_type type;
92 :
93 : struct test_data {
94 : struct sss_nss_make_request_test_data d;
95 : int ret;
96 : const char *str;
97 1 : } d[] = {
98 : {{buf1, sizeof(buf1), 0, NSS_STATUS_SUCCESS}, EOK, "test"},
99 : {{buf2, sizeof(buf2), 0, NSS_STATUS_SUCCESS}, EBADMSG, NULL},
100 : {{buf3, sizeof(buf3), 0, NSS_STATUS_SUCCESS}, ENOENT, NULL},
101 : {{buf4, sizeof(buf4), 0, NSS_STATUS_SUCCESS}, EBADMSG, NULL},
102 : {{NULL, 0, 0, 0}, 0, NULL}
103 : };
104 :
105 1 : ret = sss_nss_getsidbyname(NULL, NULL, NULL);
106 1 : assert_int_equal(ret, EINVAL);
107 :
108 1 : ret = sss_nss_getsidbyname("", NULL, NULL);
109 1 : assert_int_equal(ret, EINVAL);
110 :
111 1 : ret = sss_nss_getsidbyname("", &sid, NULL);
112 1 : assert_int_equal(ret, EINVAL);
113 1 : free(sid);
114 1 : sid = NULL;
115 :
116 5 : for (c = 0; d[c].d.repbuf != NULL; c++) {
117 4 : will_return(sss_nss_make_request, &d[0].d);
118 :
119 4 : ret = sss_nss_getsidbyname("test", &sid, &type);
120 4 : assert_int_equal(ret, d[0].ret);
121 4 : if (ret == EOK) {
122 4 : assert_string_equal(sid, d[0].str);
123 4 : assert_int_equal(type, 0);
124 : }
125 4 : free(sid);
126 4 : sid = NULL;
127 : }
128 1 : }
129 :
130 1 : void test_getorigbyname(void **state)
131 : {
132 : int ret;
133 : struct sss_nss_kv *kv_list;
134 : enum sss_id_type type;
135 1 : struct sss_nss_make_request_test_data d = {buf_orig1, sizeof(buf_orig1), 0, NSS_STATUS_SUCCESS};
136 :
137 1 : will_return(sss_nss_make_request, &d);
138 1 : ret = sss_nss_getorigbyname("test", &kv_list, &type);
139 1 : assert_int_equal(ret, EOK);
140 1 : assert_int_equal(type, SSS_ID_TYPE_UID);
141 1 : assert_string_equal(kv_list[0].key, "key");
142 1 : assert_string_equal(kv_list[0].value, "value");
143 1 : assert_null(kv_list[1].key);
144 1 : assert_null(kv_list[1].value);
145 :
146 1 : sss_nss_free_kv(kv_list);
147 1 : }
148 :
149 1 : int main(int argc, const char *argv[])
150 : {
151 :
152 1 : const struct CMUnitTest tests[] = {
153 : cmocka_unit_test(test_getsidbyname),
154 : cmocka_unit_test(test_getorigbyname),
155 : };
156 :
157 1 : return cmocka_run_group_tests(tests, NULL, NULL);
158 : }
|