022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik/*
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik Authors:
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik Lukas Slebodnik <slebodnikl@redhat.com>
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik Copyright (C) 2014 Red Hat
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik This program is free software; you can redistribute it and/or modify
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik it under the terms of the GNU General Public License as published by
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik the Free Software Foundation; either version 3 of the License, or
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik (at your option) any later version.
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik This program is distributed in the hope that it will be useful,
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik but WITHOUT ANY WARRANTY; without even the implied warranty of
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik GNU General Public License for more details.
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik You should have received a copy of the GNU General Public License
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik along with this program. If not, see <http://www.gnu.org/licenses/>.
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik*/
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik#include "util/util.h"
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik#include "tests/cmocka/common_mock.h"
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnikvoid test_replace_whitespaces(void **state)
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik{
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik TALLOC_CTX *mem_ctx;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *input_str = "Lorem ipsum dolor sit amet";
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *res;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik size_t i;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik struct {
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *input;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *output;
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek const char replace_char;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik } data_set[] = {
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "", "", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", "-", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a b c d", "a-b-c-d", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " a b c d ", "-a-b-c-d-", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", "^", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a b c d", "a^b^c^d", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " a b c d ", "^a^b^c^d^", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", "^", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", " ", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", " ", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a b c d", "a b c d", ' ' },
92d19f76449817dfb125da9510d478a30eed37bcSumit Bose { "a b^c d", "a b^c d", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { NULL, NULL, '\0' },
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik };
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik mem_ctx = talloc_new(NULL);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_non_null(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik check_leaks_push(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_replace_space(mem_ctx, input_str, '\0');
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek assert_string_equal(res, input_str);
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek talloc_zfree(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_replace_space(mem_ctx, input_str, '\0');
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek assert_string_equal(res, input_str);
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek talloc_zfree(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik for (i=0; data_set[i].input != NULL; ++i) {
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_replace_space(mem_ctx, data_set[i].input,
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek data_set[i].replace_char);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_non_null(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_string_equal(res, data_set[i].output);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik talloc_zfree(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik }
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_true(check_leaks_pop(mem_ctx) == true);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik talloc_free(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik}
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnikvoid test_reverse_replace_whitespaces(void **state)
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik{
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik TALLOC_CTX *mem_ctx;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik char *input_str = discard_const_p(char, "Lorem ipsum dolor sit amet");
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik char *res;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik size_t i;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik struct {
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *input;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik const char *output;
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek const char replace_char;
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik } data_set[] = {
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "", "", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "-", " ", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "----", " ", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a-b-c-d", "a b c d", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "-a-b-c-d-", " a b c d ", '-' },
92d19f76449817dfb125da9510d478a30eed37bcSumit Bose { "a b c d", "a b c d", '-' },
92d19f76449817dfb125da9510d478a30eed37bcSumit Bose { " a b c d ", " a b c d ", '-' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "^", " ", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "^^^^", " ", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a^b^c^d", "a b c d", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "^a^b^c^d^", " a b c d ", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", " ", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " ", " ", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "abcd", "abcd", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { "a b c d", "a b c d", ' ' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { " a b c d ", " a b c d ", ' ' },
92d19f76449817dfb125da9510d478a30eed37bcSumit Bose { "a b^c d", "a b^c d", '^' },
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek { NULL, NULL, '\0' },
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik };
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik mem_ctx = talloc_new(NULL);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_non_null(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik check_leaks_push(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek assert_string_equal(res, input_str);
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek talloc_free(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek assert_string_equal(res, input_str);
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek talloc_free(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik for (i=0; data_set[i].input != NULL; ++i) {
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik input_str = discard_const_p(char, data_set[i].input);
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek res = sss_reverse_replace_space(mem_ctx, input_str,
1f3127e88a87953f059c9a70d3582ae1719594b1Jakub Hrozek data_set[i].replace_char);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_non_null(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_string_equal(res, data_set[i].output);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik talloc_zfree(res);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik }
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik assert_true(check_leaks_pop(mem_ctx) == true);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik talloc_free(mem_ctx);
022c6b90bb37851c0e8704c0e5388ebc113c6470Lukas Slebodnik}
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bosevoid test_guid_blob_to_string_buf(void **state)
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose{
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose int ret;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose char str_buf[GUID_STR_BUF_SIZE];
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose size_t c;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose /* How to get test data:
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * The objectGUID attribute contains a 16byte long binary value
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * representing the GUID of the object. This data can be converted
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * manually to the string representation but it might be easier to use
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * LDAP_SERVER_EXTENDED_DN_OID as described in [MS-ADST] section
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * 3.1.1.3.4.1.5. This is an LDAP extended control which adds the GUID and
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * the SID to the DN of an object. This can be activate with the -E
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * ldapsearch option like:
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose *
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * ldapsearch -E 1.2.840.113556.1.4.529=::MAMCAQE= ....
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose *
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * where 'MAMCAQE=' is the base64 encoded BER sequence with the integer
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * value 1 (see [MS-ADTS] for details about possible values).
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose *
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * Btw, if you want to use the string representation of a GUID to search
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * for an object in AD you have to use the GUID as the search base in the
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * following form:
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose *
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * ldapsearch b '<GUID=fea80d8d-dbd5-4f84-8574-7db0477f962e>' ...
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose *
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose * (please note that the '<' and '>' are really needed).
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose */
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose struct test_data {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose uint8_t blob[16];
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose const char *guid_str;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose } test_data[] = {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose {{0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose 0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e},
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose "fea80d8d-dbd5-4f84-8574-7db0477f962e"},
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose {{0x91, 0x7e, 0x2e, 0xf8, 0x4e, 0x44, 0xfa, 0x4e,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose 0xb1, 0x13, 0x08, 0x98, 0x63, 0x49, 0x6c, 0xc6},
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose "f82e7e91-444e-4efa-b113-089863496cc6"},
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose {{0}, NULL}
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose };
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = guid_blob_to_string_buf(NULL, str_buf, GUID_STR_BUF_SIZE);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EINVAL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", NULL,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose GUID_STR_BUF_SIZE);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EINVAL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", str_buf, 0);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EINVAL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose for (c = 0; test_data[c].guid_str != NULL; c++) {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = guid_blob_to_string_buf(test_data[c].blob, str_buf,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose sizeof(str_buf));
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_string_equal(test_data[c].guid_str, str_buf);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose }
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose}
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bosevoid test_get_last_x_chars(void **state)
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose{
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose const char *s;
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars(NULL, 0);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars("abc", 0);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_non_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_string_equal(s, "");
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars("abc", 1);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_non_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_string_equal(s, "c");
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars("abc", 2);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_non_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_string_equal(s, "bc");
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars("abc", 3);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_non_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_string_equal(s, "abc");
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose s = get_last_x_chars("abc", 4);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_non_null(s);
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose assert_string_equal(s, "abc");
a99845006f96f9d1e7af871ec67c71cee8408a62Sumit Bose}
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bosevoid test_concatenate_string_array(void **state)
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose{
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose TALLOC_CTX *mem_ctx;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose char **a1;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose size_t a1_len = 2;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose char **a2;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose size_t a2_len = 3;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose char **res;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose size_t c;
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose mem_ctx = talloc_new(NULL);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(mem_ctx);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose check_leaks_push(mem_ctx);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose res = concatenate_string_array(mem_ctx, NULL, 0, NULL, 0);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(res);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_null(res[0]);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose talloc_free(res);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose a1 = talloc_array(mem_ctx, char *, a1_len);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(a1);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose for (c = 0; c < a1_len; c++) {
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose a1[c] = talloc_asprintf(a1, "%zu", c);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(a1[c]);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose }
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose a2 = talloc_array(mem_ctx, char *, a2_len);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(a2);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose for (c = 0; c < a2_len; c++) {
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose a2[c] = talloc_asprintf(a2, "%zu", c + a1_len);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(a2[c]);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose }
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose res = concatenate_string_array(mem_ctx, a1, a1_len, a2, a2_len);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_non_null(res);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_null(res[a1_len + a2_len]);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose for (c = 0; c < (a1_len + a2_len); c++) {
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_string_equal(res[c], talloc_asprintf(res, "%zu", c));
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose }
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose talloc_free(res);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose /* Since concatenate_string_array() uses talloc_realloc on a1 it should
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose * not be needed to free a1 explicitly. */
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose talloc_free(a2);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose assert_true(check_leaks_pop(mem_ctx) == true);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose talloc_free(mem_ctx);
28f336bdb32db0b89cb98174a3f8e308e4e928dbSumit Bose}