util-tests.c revision db37eca433a5530975422b985b024a0e6ddbdc17
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi Stephen Gallagher <sgallagh@redhat.com>
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi Copyright (C) 2010 Red Hat
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi This program is free software; you can redistribute it and/or modify
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi it under the terms of the GNU General Public License as published by
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi the Free Software Foundation; either version 3 of the License, or
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi (at your option) any later version.
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi This program is distributed in the hope that it will be useful,
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi but WITHOUT ANY WARRANTY; without even the implied warranty of
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi GNU General Public License for more details.
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi You should have received a copy of the GNU General Public License
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi along with this program. If not, see <http://www.gnu.org/licenses/>.
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber#define FILENAME_TEMPLATE "tests-atomicio-XXXXXX"
8c3a756ed4cf22f9fdab5f95d067321e6ddcc425KATOH Yasufumi fail_unless(ret == EINVAL, "NULL input accepted");
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi ret = add_string_to_list(global_talloc_context, "ABC", &list);
8c3a756ed4cf22f9fdab5f95d067321e6ddcc425KATOH Yasufumi fail_unless(ret == EOK, "Adding string to non-existing list failed.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(list != NULL, "No new list created.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(list[0] != NULL, "String not added to new list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi "Wrong string added to newly created list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi "Missing terminating NULL in newly created list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi ret = add_string_to_list(global_talloc_context, "DEF", &list);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "Adding string to list failed.");
8adef7614d4340b4ee44a4441fadd530f48515edKATOH Yasufumi fail_unless(list != NULL, "No list returned.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(strcmp(list[0], "ABC") == 0, "Wrong first string in new list.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(strcmp(list[1], "DEF") == 0, "Wrong string added to list.");
8c3a756ed4cf22f9fdab5f95d067321e6ddcc425KATOH Yasufumi fail_unless(list[2] == NULL, "Missing terminating NULL.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi ret = add_string_to_list(global_talloc_context, "ABC", &list);
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi fail_unless(ret == EOK, "Adding string to empty list failed.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(list != NULL, "No list returned.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(list[0] != NULL, "String not added to empty list.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi "Wrong string added to empty list.");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi "Missing terminating NULL in newly created list.");
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi fail_unless(!is_in, "NULL string is in NULL list.");
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi is_in = string_in_list(NULL, empty_list, false);
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi fail_unless(!is_in, "NULL string is in empty list.");
adf4b4083369a8bb7ed3c2ce3dd85dcaed7323c4KATOH Yasufumi fail_unless(!is_in, "NULL string is in list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(!is_in, "String is in NULL list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi is_in = string_in_list("ABC", empty_list, false);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(!is_in, "String is in empty list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(is_in, "String is not case in-sensitive list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(!is_in, "Wrong string found in case sensitive list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(!is_in, "Wrong string found in list.");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char **parsed;
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Positive tests */
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed2[] = { "foo", "a", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed3[] = { "foo", "b", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed4[] = { "foo", "a c", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed5[] = { "foo", "a", "d", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed6[] = { "foo", "a", "e", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed7[] = { "foo", "a", "f", NULL };
ffeb76b4607be4b241db01ab65989d37b4529c97TAMUKI Shoichi const char *parsed8[] = { "foo", "a\tg", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char *parsed10[] = { " ", "foo", "\t", "\\'", NULL };
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_if(parsed == NULL && tc[i].parsed != NULL,
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi "Could not parse correct %d argument string '%s'\n",
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi ret = diff_string_lists(test_ctx, parsed, discard_const(tc[i].parsed),
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_ret[0] == NULL, "The parser returned more data than expected\n");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_exp[0] == NULL, "The parser returned less data than expected\n");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi for (parsed_len=0; parsed[parsed_len]; ++parsed_len);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi for (expected_len=0; tc[i].parsed[expected_len]; ++expected_len);
a1e4c206d5373b8ecd7906bff37f2601d65f022cKATOH Yasufumi "Test %d: length of 1st array [%d] != length of 2nd "
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi for (ii = 0; parsed[ii]; ii++) free(parsed[ii]);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Test with all values returned */
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[0], "a") == 0, "Missing \"a\" from only_l1");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(only_l1[1] == NULL, "only_l1 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l2[0], "d") == 0, "Missing \"d\" from only_l2");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(only_l2[1] == NULL, "only_l2 not NULL-terminated");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(strcmp(both[0], "c") == 0, "Missing \"c\" from both");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(strcmp(both[1], "b") == 0, "Missing \"b\" from both");
7a49a081dd1d4f92e3c82df344709e3206457e46KATOH Yasufumi fail_unless(both[2] == NULL, "both not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Test with restricted return values */
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[0], "a") == 0, "Missing \"a\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1[1] == NULL, "only_l1 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l2[0], "d") == 0, "Missing \"d\" from only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2[1] == NULL, "only_l2 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both == NULL, "Nothing returned to both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[0], "a") == 0, "Missing \"a\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1[1] == NULL, "only_l1 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2 == NULL, "Nothing returned to only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both == NULL, "Nothing returned to both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
ffeb76b4607be4b241db01ab65989d37b4529c97TAMUKI Shoichi fail_unless(strcmp(only_l2[0], "d") == 0, "Missing \"d\" from only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2[1] == NULL, "only_l2 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1 == NULL, "Nothing returned to only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both == NULL, "Nothing returned to both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Test with no overlap */
ffeb76b4607be4b241db01ab65989d37b4529c97TAMUKI Shoichi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
ffeb76b4607be4b241db01ab65989d37b4529c97TAMUKI Shoichi fail_unless(strcmp(only_l1[0], "a") == 0, "Missing \"a\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[1], "b") == 0, "Missing \"b\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[2], "c") == 0, "Missing \"c\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1[3] == NULL, "only_l1 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l2[0], "d") == 0, "Missing \"f\" from only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l2[1], "e") == 0, "Missing \"e\" from only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l2[2], "f") == 0, "Missing \"d\" from only_l2");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2[3] == NULL, "only_l2 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both[0] == NULL, "both should have zero entries");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Test with 100% overlap */
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1[0] == NULL, "only_l1 should have zero entries");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2[0] == NULL, "only_l2 should have zero entries");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(both[0], "a") == 0, "Missing \"a\" from both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(both[1], "b") == 0, "Missing \"b\" from both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(both[2], "c") == 0, "Missing \"c\" from both");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both[3] == NULL, "both is not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi /* Test with no second list */
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "diff_string_lists returned error [%d]", ret);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[0], "a") == 0, "Missing \"a\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[1], "b") == 0, "Missing \"b\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(only_l1[2], "c") == 0, "Missing \"c\" from only_l1");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l1[3] == NULL, "only_l1 not NULL-terminated");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(only_l2[0] == NULL, "only_l2 should have zero entries");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(both[0] == NULL, "both should have zero entries");
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi ret = sss_filter_sanitize(test_ctx, no_specials, &sanitized);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "no_specials error [%d][%s]",
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(no_specials, sanitized)==0,
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi "Expected [%s], got [%s]",
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi const char has_asterisk_expected[] = "\\2ausername";
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi ret = sss_filter_sanitize(test_ctx, has_asterisk, &sanitized);
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(ret == EOK, "has_asterisk error [%d][%s]",
cab79123082bef9ba265c9c8b9176b8b8afedd64KATOH Yasufumi fail_unless(strcmp(has_asterisk_expected, sanitized)==0,
int fd;
int flags;
&nlen);
fail_if(strlen((const char *) munchen_utf8_upcase) != nlen); /* This is not true for utf8 strings in general */
&nsize);
char *lcase;
bool ret;
unsigned int init_seed;
unsigned int seed;
for (i = 0; i < len; i++) {
void setup_atomicio(void)
int ret;
void teardown_atomicio(void)
int ret;
int fd;
errno = 0;
errno = 0;
errno = 0;
errno = 0;
total = 0;
errno = 0;
} while (numread != 0);
errno = 0;
errno = 0;
errno = 0;
int fd;
errno = 0;
struct split_data {
const char *input;
const char **expected_list;
bool trim;
bool skip_empty;
int expected_size;
int expected_ret;
int size;
const char *str_ref;
const char *str_out;
int num_of_tests;
(const char*[]){NULL},
NULL,
NULL,
0, EINVAL
for (a = 0; a < num_of_tests; a++) {
if (ret) {
struct check_ip_test_data {
const char *str_ipaddr;
bool expected_ret;
int num_of_tests;
int ret;
bool bret;
for (a = 0; a < num_of_tests; a++) {
int num_of_tests;
int ret;
bool bret;
for (a = 0; a < num_of_tests; a++) {
const char *host;
const char *domain;
bool expected;
bool ret;
const char * const * svcs;
bool found_nss = false;
for (i = 0; svcs[i]; i++) {
found_nss = true;
if (tz) {
const char *error;
char last_character;
error);
int opt;
int failure_count;
switch(opt) {
if (failure_count == 0) {
return EXIT_SUCCESS;
return EXIT_FAILURE;