New upstream version 4.15.1
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)
|
||||
|
||||
if HAVE_CMOCKA
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
||||
check_PROGRAMS = \
|
||||
test_adds \
|
||||
test_atoi_strtoi \
|
||||
test_atoi_strtou_noneg \
|
||||
test_chkname \
|
||||
test_sprintf \
|
||||
test_strncpy \
|
||||
test_strtcpy \
|
||||
test_xasprintf \
|
||||
test_zustr2stp
|
||||
|
||||
if ENABLE_LOGIND
|
||||
check_PROGRAMS += \
|
||||
test_logind
|
||||
endif # ENABLE_LOGIND
|
||||
|
||||
check_PROGRAMS += \
|
||||
$(NULL)
|
||||
|
||||
test_adds_SOURCES = \
|
||||
../../lib/adds.c \
|
||||
test_adds.c \
|
||||
$(NULL)
|
||||
test_adds_CFLAGS = \
|
||||
$(AM_FLAGS) \
|
||||
$(NULL)
|
||||
test_adds_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_adds_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_atoi_strtoi_SOURCES = \
|
||||
../../lib/atoi/strtoi.c \
|
||||
test_atoi_strtoi.c \
|
||||
$(NULL)
|
||||
test_atoi_strtoi_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_atoi_strtoi_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_atoi_strtoi_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_atoi_strtou_noneg_SOURCES = \
|
||||
../../lib/atoi/strtou_noneg.c \
|
||||
test_atoi_strtou_noneg.c \
|
||||
$(NULL)
|
||||
test_atoi_strtou_noneg_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_atoi_strtou_noneg_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_atoi_strtou_noneg_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_chkname_SOURCES = \
|
||||
../../lib/chkname.c \
|
||||
test_chkname.c \
|
||||
$(NULL)
|
||||
test_chkname_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_chkname_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_chkname_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_logind_SOURCES = \
|
||||
../../lib/logind.c \
|
||||
test_logind.c \
|
||||
$(NULL)
|
||||
test_logind_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
-lsystemd \
|
||||
$(NULL)
|
||||
test_logind_LDFLAGS = \
|
||||
-Wl,-wrap,prefix_getpwnam \
|
||||
-Wl,-wrap,sd_uid_get_sessions \
|
||||
$(NULL)
|
||||
test_logind_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(LIBSYSTEMD) \
|
||||
$(NULL)
|
||||
|
||||
test_sprintf_SOURCES = \
|
||||
../../lib/string/sprintf.c \
|
||||
test_sprintf.c \
|
||||
$(NULL)
|
||||
test_sprintf_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_sprintf_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_sprintf_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_strncpy_SOURCES = \
|
||||
test_strncpy.c \
|
||||
$(NULL)
|
||||
test_strncpy_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_strncpy_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_strncpy_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_strtcpy_SOURCES = \
|
||||
../../lib/string/strtcpy.c \
|
||||
test_strtcpy.c \
|
||||
$(NULL)
|
||||
test_strtcpy_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_strtcpy_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_strtcpy_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_xasprintf_SOURCES = \
|
||||
../../lib/string/sprintf.c \
|
||||
test_xasprintf.c \
|
||||
$(NULL)
|
||||
test_xasprintf_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_xasprintf_LDFLAGS = \
|
||||
-Wl,-wrap,vasprintf \
|
||||
-Wl,-wrap,exit \
|
||||
$(NULL)
|
||||
test_xasprintf_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_zustr2stp_SOURCES = \
|
||||
test_zustr2stp.c \
|
||||
$(NULL)
|
||||
test_zustr2stp_CFLAGS = \
|
||||
$(AM_CFLAGS) \
|
||||
$(NULL)
|
||||
test_zustr2stp_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_zustr2stp_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
endif # HAVE_CMOCKA
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,105 @@
|
||||
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "adds.h"
|
||||
|
||||
|
||||
static void test_addsl_2_ok(void **state);
|
||||
static void test_addsl_2_underflow(void **state);
|
||||
static void test_addsl_2_overflow(void **state);
|
||||
static void test_addsl_3_ok(void **state);
|
||||
static void test_addsl_3_underflow(void **state);
|
||||
static void test_addsl_3_overflow(void **state);
|
||||
static void test_addsl_5_ok(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_addsl_2_ok),
|
||||
cmocka_unit_test(test_addsl_2_underflow),
|
||||
cmocka_unit_test(test_addsl_2_overflow),
|
||||
cmocka_unit_test(test_addsl_3_ok),
|
||||
cmocka_unit_test(test_addsl_3_underflow),
|
||||
cmocka_unit_test(test_addsl_3_overflow),
|
||||
cmocka_unit_test(test_addsl_5_ok),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_2_ok(void **state)
|
||||
{
|
||||
assert_true(addsl(1, 3) == 1 + 3);
|
||||
assert_true(addsl(-4321, 7) == -4321 + 7);
|
||||
assert_true(addsl(1, 1) == 1 + 1);
|
||||
assert_true(addsl(-1, -2) == -1 - 2);
|
||||
assert_true(addsl(LONG_MAX, -1) == LONG_MAX - 1);
|
||||
assert_true(addsl(LONG_MIN, 1) == LONG_MIN + 1);
|
||||
assert_true(addsl(LONG_MIN, LONG_MAX) == LONG_MIN + LONG_MAX);
|
||||
assert_true(addsl(0, 0) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_2_underflow(void **state)
|
||||
{
|
||||
assert_true(addsl(LONG_MIN, -1) == LONG_MIN);
|
||||
assert_true(addsl(LONG_MIN + 3, -7) == LONG_MIN);
|
||||
assert_true(addsl(LONG_MIN, LONG_MIN) == LONG_MIN);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_2_overflow(void **state)
|
||||
{
|
||||
assert_true(addsl(LONG_MAX, 1) == LONG_MAX);
|
||||
assert_true(addsl(LONG_MAX - 3, 7) == LONG_MAX);
|
||||
assert_true(addsl(LONG_MAX, LONG_MAX) == LONG_MAX);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_3_ok(void **state)
|
||||
{
|
||||
assert_true(addsl(1, 2, 3) == 1 + 2 + 3);
|
||||
assert_true(addsl(LONG_MIN, -3, 4) == LONG_MIN + 4 - 3);
|
||||
assert_true(addsl(LONG_MAX, LONG_MAX, LONG_MIN)
|
||||
== LONG_MAX + LONG_MIN + LONG_MAX);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_3_underflow(void **state)
|
||||
{
|
||||
assert_true(addsl(LONG_MIN, 2, -3) == LONG_MIN);
|
||||
assert_true(addsl(LONG_MIN, -1, 0) == LONG_MIN);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_3_overflow(void **state)
|
||||
{
|
||||
assert_true(addsl(LONG_MAX, -1, 2) == LONG_MAX);
|
||||
assert_true(addsl(LONG_MAX, +1, 0) == LONG_MAX);
|
||||
assert_true(addsl(LONG_MAX, LONG_MAX, 0)== LONG_MAX);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_addsl_5_ok(void **state)
|
||||
{
|
||||
assert_true(addsl(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN, 44) == 42);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "atoi/strtoi.h"
|
||||
#include "atoi/strtou_noneg.h"
|
||||
|
||||
|
||||
static void test_strtoi(void **state);
|
||||
static void test_strtou(void **state);
|
||||
static void test_strtou_noneg(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_strtoi),
|
||||
cmocka_unit_test(test_strtou),
|
||||
cmocka_unit_test(test_strtou_noneg),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_strtoi(void **state)
|
||||
{
|
||||
int status;
|
||||
char *end;
|
||||
|
||||
errno = 0;
|
||||
assert_true(strtoi_("42", NULL, -1, 1, 2, &status) == 1);
|
||||
assert_true(status == EINVAL);
|
||||
|
||||
assert_true(strtoi_("40", &end, 5, INTMAX_MIN, INTMAX_MAX, &status) == 20);
|
||||
assert_true(status == 0);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtoi_("-40", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == -40);
|
||||
assert_true(status == 0);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtoi_("z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 0);
|
||||
assert_true(status == ECANCELED);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtoi_(" 5", &end, 0, 3, 4, &status) == 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 5);
|
||||
assert_true(status == ENOTSUP);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, 4, &status) == 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(errno == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_strtou(void **state)
|
||||
{
|
||||
int status;
|
||||
char *end;
|
||||
|
||||
errno = 0;
|
||||
assert_true(strtou_("42", NULL, -1, 1, 2, &status) == 1);
|
||||
assert_true(status == EINVAL);
|
||||
|
||||
assert_true(strtou_("40", &end, 5, 0, UINTMAX_MAX, &status) == 20);
|
||||
assert_true(status == 0);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_("-40", &end, 0, 0, UINTMAX_MAX, &status) == -40ull);
|
||||
assert_true(status == 0);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_("z", &end, 0, 0, UINTMAX_MAX, &status) == 0);
|
||||
assert_true(status == ECANCELED);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtou_(" 5", &end, 0, 3, 4, &status) == 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_("5z", &end, 0, 0, UINTMAX_MAX, &status) == 5);
|
||||
assert_true(status == ENOTSUP);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtou_("5z", &end, 0, 0, 4, &status) == 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(errno == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_strtou_noneg(void **state)
|
||||
{
|
||||
int status;
|
||||
char *end;
|
||||
|
||||
errno = 0;
|
||||
assert_true(strtou_noneg("42", NULL, -1, 1, 2, &status)
|
||||
== 1);
|
||||
assert_true(status == EINVAL);
|
||||
|
||||
assert_true(strtou_noneg("40", &end, 5, 0, UINTMAX_MAX, &status)
|
||||
== 20);
|
||||
assert_true(status == 0);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_noneg("-40", &end, 0, 2, UINTMAX_MAX, &status)
|
||||
== 2);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_noneg("z", &end, 0, 0, UINTMAX_MAX, &status)
|
||||
== 0);
|
||||
assert_true(status == ECANCELED);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtou_noneg(" 5", &end, 0, 3, 4, &status)
|
||||
== 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "") == 0);
|
||||
|
||||
assert_true(strtou_noneg("5z", &end, 0, 0, UINTMAX_MAX, &status)
|
||||
== 5);
|
||||
assert_true(status == ENOTSUP);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(strtou_noneg("5z", &end, 0, 0, 4, &status)
|
||||
== 4);
|
||||
assert_true(status == ERANGE);
|
||||
assert_true(strcmp(end, "z") == 0);
|
||||
|
||||
assert_true(errno == 0);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "atoi/strtou_noneg.h"
|
||||
|
||||
|
||||
static void test_strtoul_noneg(void **state);
|
||||
static void test_strtoull_noneg(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_strtoul_noneg),
|
||||
cmocka_unit_test(test_strtoull_noneg),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_strtoul_noneg(void **state)
|
||||
{
|
||||
errno = 0;
|
||||
assert_true(strtoul_noneg("42", NULL, 0) == 42);
|
||||
assert_true(errno == 0);
|
||||
|
||||
assert_true(strtoul_noneg("-1", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
errno = 0;
|
||||
assert_true(strtoul_noneg("-3", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
errno = 0;
|
||||
assert_true(strtoul_noneg("-0xFFFFFFFFFFFFFFFF", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
|
||||
errno = 0;
|
||||
assert_true(strtoul_noneg("-0x10000000000000000", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_strtoull_noneg(void **state)
|
||||
{
|
||||
errno = 0;
|
||||
assert_true(strtoull_noneg("42", NULL, 0) == 42);
|
||||
assert_true(errno == 0);
|
||||
|
||||
assert_true(strtoull_noneg("-1", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
errno = 0;
|
||||
assert_true(strtoull_noneg("-3", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
errno = 0;
|
||||
assert_true(strtoull_noneg("-0xFFFFFFFFFFFFFFFF", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
|
||||
errno = 0;
|
||||
assert_true(strtoull_noneg("-0x10000000000000000", NULL, 0) == 0);
|
||||
assert_true(errno == ERANGE);
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "alloc.h"
|
||||
#include "chkname.h"
|
||||
|
||||
|
||||
static void test_is_valid_user_name_ok(void **state);
|
||||
static void test_is_valid_user_name_ok_dollar(void **state);
|
||||
static void test_is_valid_user_name_nok_dash(void **state);
|
||||
static void test_is_valid_user_name_nok_dir(void **state);
|
||||
static void test_is_valid_user_name_nok_dollar(void **state);
|
||||
static void test_is_valid_user_name_nok_empty(void **state);
|
||||
static void test_is_valid_user_name_nok_numeric(void **state);
|
||||
static void test_is_valid_user_name_nok_otherchars(void **state);
|
||||
static void test_is_valid_user_name_long(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_is_valid_user_name_ok),
|
||||
cmocka_unit_test(test_is_valid_user_name_ok_dollar),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_dash),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_dir),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_dollar),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_empty),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_numeric),
|
||||
cmocka_unit_test(test_is_valid_user_name_nok_otherchars),
|
||||
cmocka_unit_test(test_is_valid_user_name_long),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_ok(void **state)
|
||||
{
|
||||
assert_true(is_valid_user_name("alx"));
|
||||
assert_true(is_valid_user_name("u-ser"));
|
||||
assert_true(is_valid_user_name("u"));
|
||||
assert_true(is_valid_user_name("I"));
|
||||
assert_true(is_valid_user_name("_"));
|
||||
assert_true(is_valid_user_name("_.-"));
|
||||
assert_true(is_valid_user_name(".007"));
|
||||
assert_true(is_valid_user_name("0_0"));
|
||||
assert_true(is_valid_user_name("some_longish_user_name_sHould_also_be_valid.wHY_not"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_ok_dollar(void **state)
|
||||
{
|
||||
// Non-POSIX extension for Samba 3.x "add machine script".
|
||||
assert_true(is_valid_user_name("dollar$"));
|
||||
assert_true(is_valid_user_name("SSS$"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_dash(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name("-"));
|
||||
assert_true(false == is_valid_user_name("-not-valid"));
|
||||
assert_true(false == is_valid_user_name("--C"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_dir(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name("."));
|
||||
assert_true(false == is_valid_user_name(".."));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_dollar(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name("$"));
|
||||
assert_true(false == is_valid_user_name("$dollar"));
|
||||
assert_true(false == is_valid_user_name("mo$ney"));
|
||||
assert_true(false == is_valid_user_name("do$$ar"));
|
||||
assert_true(false == is_valid_user_name("foo$bar$"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_empty(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name(""));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_numeric(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name("6"));
|
||||
assert_true(false == is_valid_user_name("42"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_nok_otherchars(void **state)
|
||||
{
|
||||
assert_true(false == is_valid_user_name("no spaces"));
|
||||
assert_true(false == is_valid_user_name("no,"));
|
||||
assert_true(false == is_valid_user_name("no;"));
|
||||
assert_true(false == is_valid_user_name("no:"));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_is_valid_user_name_long(void **state)
|
||||
{
|
||||
size_t max;
|
||||
char *name;
|
||||
|
||||
max = sysconf(_SC_LOGIN_NAME_MAX);
|
||||
name = MALLOC(max + 1, char);
|
||||
assert_true(name != NULL);
|
||||
|
||||
memset(name, '_', max);
|
||||
|
||||
name[max] = '\0';
|
||||
assert_true(false == is_valid_user_name(name));
|
||||
|
||||
name[max - 1] = '\0';
|
||||
assert_true(is_valid_user_name(name));
|
||||
|
||||
free(name);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Iker Pedrosa <ipedrosa@redhat.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <pwd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "prototypes.h"
|
||||
|
||||
/***********************
|
||||
* WRAPPERS
|
||||
**********************/
|
||||
struct passwd *
|
||||
__wrap_prefix_getpwnam(uid_t uid)
|
||||
{
|
||||
return (struct passwd*) mock();
|
||||
}
|
||||
|
||||
int
|
||||
__wrap_sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions)
|
||||
{
|
||||
return mock();
|
||||
}
|
||||
|
||||
/***********************
|
||||
* TEST
|
||||
**********************/
|
||||
static void test_active_sessions_count_return_ok(void **state)
|
||||
{
|
||||
int count;
|
||||
struct passwd *pw = malloc(sizeof(struct passwd));
|
||||
|
||||
will_return(__wrap_prefix_getpwnam, pw);
|
||||
will_return(__wrap_sd_uid_get_sessions, 1);
|
||||
|
||||
count = active_sessions_count("testuser", 0);
|
||||
|
||||
assert_int_equal(count, 1);
|
||||
}
|
||||
|
||||
static void test_active_sessions_count_prefix_getpwnam_failure(void **state)
|
||||
{
|
||||
int count;
|
||||
struct passwd *pw = NULL;
|
||||
|
||||
will_return(__wrap_prefix_getpwnam, pw);
|
||||
|
||||
count = active_sessions_count("testuser", 0);
|
||||
|
||||
assert_int_equal(count, 0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_active_sessions_count_return_ok),
|
||||
cmocka_unit_test(test_active_sessions_count_prefix_getpwnam_failure),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "sizeof.h"
|
||||
#include "string/sprintf.h"
|
||||
|
||||
|
||||
static void test_SNPRINTF_trunc(void **state);
|
||||
static void test_SNPRINTF_ok(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_SNPRINTF_trunc),
|
||||
cmocka_unit_test(test_SNPRINTF_ok),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_SNPRINTF_trunc(void **state)
|
||||
{
|
||||
char buf[NITEMS("foo")];
|
||||
|
||||
// Test that we're not returning SIZE_MAX
|
||||
assert_true(SNPRINTF(buf, "f%su", "oo") < 0);
|
||||
assert_true(strcmp(buf, "foo") == 0);
|
||||
|
||||
assert_true(SNPRINTF(buf, "barbaz") == -1);
|
||||
assert_true(strcmp(buf, "bar") == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_SNPRINTF_ok(void **state)
|
||||
{
|
||||
char buf[NITEMS("foo")];
|
||||
|
||||
assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo"));
|
||||
assert_true(strcmp(buf, "foo") == 0);
|
||||
|
||||
assert_true(SNPRINTF(buf, "%do", 1) == strlen("1o"));
|
||||
assert_true(strcmp(buf, "1o") == 0);
|
||||
|
||||
assert_true(SNPRINTF(buf, "f") == strlen("f"));
|
||||
assert_true(strcmp(buf, "f") == 0);
|
||||
|
||||
assert_true(SNPRINTF(buf, "") == strlen(""));
|
||||
assert_true(strcmp(buf, "") == 0);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "sizeof.h"
|
||||
#include "string/strncpy.h"
|
||||
|
||||
|
||||
static void test_STRNCPY_trunc(void **state);
|
||||
static void test_STRNCPY_fit(void **state);
|
||||
static void test_STRNCPY_pad(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_STRNCPY_trunc),
|
||||
cmocka_unit_test(test_STRNCPY_fit),
|
||||
cmocka_unit_test(test_STRNCPY_pad),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_trunc(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[4] = {'f', 'o', 'o', 'o'};
|
||||
char res1[3] = {'f', 'o', 'o'};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[5] = "barb";
|
||||
char res2[3] = {'b', 'a', 'r'};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_fit(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[3] = {'b', 'a', 'z'};
|
||||
char res1[3] = {'b', 'a', 'z'};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[4] = "qwe";
|
||||
char res2[3] = {'q', 'w', 'e'};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_pad(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[3] = "as";
|
||||
char res1[3] = {'a', 's', 0};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[3] = "";
|
||||
char res2[3] = {0, 0, 0};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
|
||||
char src3[3] = {'a', 0, 'b'};
|
||||
char res3[3] = {'a', 0, 0};
|
||||
assert_true(memcmp(res3, STRNCPY(buf, src3), sizeof(buf)) == 0);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "sizeof.h"
|
||||
#include "string/strtcpy.h"
|
||||
|
||||
|
||||
static void test_STRTCPY_trunc(void **state);
|
||||
static void test_STRTCPY_ok(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_STRTCPY_trunc),
|
||||
cmocka_unit_test(test_STRTCPY_ok),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRTCPY_trunc(void **state)
|
||||
{
|
||||
char buf[NITEMS("foo")];
|
||||
|
||||
// Test that we're not returning SIZE_MAX
|
||||
assert_true(STRTCPY(buf, "fooo") < 0);
|
||||
assert_string_equal(buf, "foo");
|
||||
|
||||
assert_int_equal(STRTCPY(buf, "barbaz"), -1);
|
||||
assert_string_equal(buf, "bar");
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRTCPY_ok(void **state)
|
||||
{
|
||||
char buf[NITEMS("foo")];
|
||||
|
||||
assert_int_equal(STRTCPY(buf, "foo"), strlen("foo"));
|
||||
assert_string_equal(buf, "foo");
|
||||
|
||||
assert_int_equal(STRTCPY(buf, "fo"), strlen("fo"));
|
||||
assert_string_equal(buf, "fo");
|
||||
|
||||
assert_int_equal(STRTCPY(buf, "f"), strlen("f"));
|
||||
assert_string_equal(buf, "f");
|
||||
|
||||
assert_int_equal(STRTCPY(buf, ""), strlen(""));
|
||||
assert_string_equal(buf, "");
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "string/sprintf.h"
|
||||
|
||||
|
||||
#define assert_unreachable() assert_true(0)
|
||||
|
||||
#define XASPRINTF_CALLED (-36)
|
||||
#define EXIT_CALLED (42)
|
||||
#define TEST_OK (-6)
|
||||
|
||||
|
||||
static jmp_buf jmpb;
|
||||
|
||||
|
||||
/**********************
|
||||
* WRAPPERS
|
||||
**********************/
|
||||
int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
|
||||
int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
|
||||
void __wrap_exit(int status);
|
||||
|
||||
|
||||
int
|
||||
__wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap)
|
||||
{
|
||||
return mock() == -1 ? -1 : __real_vasprintf(p, fmt, ap);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
__wrap_exit(int status)
|
||||
{
|
||||
longjmp(jmpb, EXIT_CALLED);
|
||||
}
|
||||
|
||||
|
||||
/**********************
|
||||
* TEST
|
||||
**********************/
|
||||
static void test_xasprintf_exit(void **state);
|
||||
static void test_xasprintf_ok(void **state);
|
||||
|
||||
|
||||
static void
|
||||
test_xasprintf_exit(void **state)
|
||||
{
|
||||
volatile int len;
|
||||
char *volatile p;
|
||||
|
||||
will_return(__wrap_vasprintf, -1);
|
||||
|
||||
len = 0;
|
||||
|
||||
switch (setjmp(jmpb)) {
|
||||
case 0:
|
||||
len = XASPRINTF_CALLED;
|
||||
len = xasprintf(&p, "foo%s", "bar");
|
||||
assert_unreachable();
|
||||
break;
|
||||
case EXIT_CALLED:
|
||||
assert_int_equal(len, XASPRINTF_CALLED);
|
||||
len = TEST_OK;
|
||||
break;
|
||||
default:
|
||||
assert_unreachable();
|
||||
break;
|
||||
}
|
||||
|
||||
assert_int_equal(len, TEST_OK);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_xasprintf_ok(void **state)
|
||||
{
|
||||
int len;
|
||||
char *p;
|
||||
|
||||
// Trick: it will actually return the length, not 0.
|
||||
will_return(__wrap_vasprintf, 0);
|
||||
|
||||
len = xasprintf(&p, "foo%d%s", 1, "bar");
|
||||
assert_int_equal(len, strlen("foo1bar"));
|
||||
assert_string_equal(p, "foo1bar");
|
||||
free(p);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_xasprintf_exit),
|
||||
cmocka_unit_test(test_xasprintf_ok),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "string/zustr2stp.h"
|
||||
|
||||
|
||||
static void test_ZUSTR2STP(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_ZUSTR2STP),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_ZUSTR2STP(void **state)
|
||||
{
|
||||
char src[3] = {'1', '2', '3'};
|
||||
char dst[4];
|
||||
|
||||
assert_true(ZUSTR2STP(&dst, src) == dst + strlen("123"));
|
||||
assert_true(strcmp("123", dst) == 0);
|
||||
|
||||
src[2] = '\0';
|
||||
assert_true(ZUSTR2STP(&dst, src) == dst + strlen("12"));
|
||||
assert_true(strcmp("12", dst) == 0);
|
||||
|
||||
src[1] = '\0';
|
||||
assert_true(ZUSTR2STP(&dst, src) == dst + strlen("1"));
|
||||
assert_true(strcmp("1", dst) == 0);
|
||||
|
||||
src[0] = '\0';
|
||||
assert_true(ZUSTR2STP(&dst, src) == dst + strlen(""));
|
||||
assert_true(strcmp("", dst) == 0);
|
||||
}
|
||||
Reference in New Issue
Block a user