tests: add tests for string lookup tables

The tests check if the tables have entries for all values
in the enum, and that the entries are unique.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-07-05 00:25:26 -04:00
parent ec26be514f
commit a8b409dbc9
7 changed files with 301 additions and 20 deletions

3
.gitignore vendored
View File

@ -134,6 +134,9 @@
/test-strbuf
/test-strv
/test-strxcpyx
/test-tables
/test-login-tables
/test-machine-tables
/test-time
/test-udev
/test-unit-file

View File

@ -1115,7 +1115,8 @@ tests += \
test-fileio \
test-time \
test-hashmap \
test-list
test-list \
test-tables
EXTRA_DIST += \
test/sched_idle_bad.service \
@ -1228,6 +1229,18 @@ test_list_CFLAGS = \
test_list_LDADD = \
libsystemd-core.la
test_tables_SOURCES = \
src/test/test-tables.c \
src/shared/test-tables.h
test_tables_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
test_tables_LDADD = \
libsystemd-core.la \
libsystemd-logs.la
test_prioq_SOURCES = \
src/test/test-prioq.c
@ -3583,17 +3596,29 @@ endif
if ENABLE_MACHINED
systemd_machined_SOURCES = \
src/machine/machined.c \
src/machine/machined.h \
src/machine/machined.h
systemd_machined_CFLAGS = \
$(libsystemd_machine_core_la_CFLAGS)
systemd_machined_LDADD = \
libsystemd-machine-core.la \
$(libsystemd_machine_core_la_LIBADD)
rootlibexec_PROGRAMS += \
systemd-machined
libsystemd_machine_core_la_SOURCES = \
src/machine/machined-dbus.c \
src/machine/machine.c \
src/machine/machine.h \
src/machine/machine-dbus.c
systemd_machined_CFLAGS = \
libsystemd_machine_core_la_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
systemd_machined_LDADD = \
libsystemd_machine_core_la_LIBADD = \
libsystemd-label.la \
libsystemd-audit.la \
libsystemd-shared.la \
@ -3602,8 +3627,8 @@ systemd_machined_LDADD = \
libsystemd-id128-internal.la \
libudev.la
rootlibexec_PROGRAMS += \
systemd-machined
noinst_LTLIBRARIES += \
libsystemd-machine-core.la
machinectl_SOURCES = \
src/machine/machinectl.c
@ -3620,6 +3645,19 @@ machinectl_LDADD = \
rootbin_PROGRAMS += \
machinectl
test_machine_tables_SOURCES = \
src/machine/test-machine-tables.c
test_machine_tables_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
test_machine_tables_LDADD = \
libsystemd-machine-core.la
tests += \
test-machine-tables
nodist_systemunit_DATA += \
units/systemd-machined.service
@ -3644,7 +3682,27 @@ endif
if ENABLE_LOGIND
systemd_logind_SOURCES = \
src/login/logind.c \
src/login/logind.h \
src/login/logind.h
nodist_systemd_logind_SOURCES = \
src/login/logind-gperf.c
systemd_logind_CFLAGS = \
$(libsystemd_logind_core_la_CFLAGS)
systemd_logind_LDADD = \
libsystemd-logind-core.la \
$(libsystemd_logind_core_la_LIBADD)
if HAVE_ACL
systemd_logind_SOURCES += \
src/login/logind-acl.c
systemd_logind_LDADD += \
libsystemd-acl.la
endif
libsystemd_logind_core_la_SOURCES = \
src/login/logind-dbus.c \
src/login/logind-device.c \
src/login/logind-device.h \
@ -3665,14 +3723,11 @@ systemd_logind_SOURCES = \
src/login/logind-user-dbus.c \
src/login/logind-acl.h
nodist_systemd_logind_SOURCES = \
src/login/logind-gperf.c
systemd_logind_CFLAGS = \
libsystemd_logind_core_la_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
$(DBUS_CFLAGS)
systemd_logind_LDADD = \
libsystemd_logind_core_la_LIBADD = \
libsystemd-label.la \
libsystemd-audit.la \
libsystemd-shared.la \
@ -3681,13 +3736,8 @@ systemd_logind_LDADD = \
libsystemd-id128-internal.la \
libudev.la
if HAVE_ACL
systemd_logind_SOURCES += \
src/login/logind-acl.c
systemd_logind_LDADD += \
libsystemd-acl.la
endif
noinst_LTLIBRARIES += \
libsystemd-logind-core.la
systemd_user_sessions_SOURCES = \
src/login/user-sessions.c
@ -3750,10 +3800,23 @@ test_inhibit_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
test_login_tables_SOURCES = \
src/login/test-login-tables.c
test_login_tables_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
test_login_tables_LDADD = \
libsystemd-logind-core.la
manual_tests += \
test-login \
test-inhibit
tests += \
test-login-tables
libsystemd_login_la_SOURCES = \
src/login/sd-login.c

View File

@ -391,9 +391,11 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
[CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
[CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
[CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
[CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
[CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
[CONDITION_SECURITY] = "ConditionSecurity",
[CONDITION_CAPABILITY] = "ConditionCapability",
[CONDITION_HOST] = "ConditionHost",
[CONDITION_AC_POWER] = "ConditionACPower",
[CONDITION_NULL] = "ConditionNull"

View File

@ -0,0 +1,35 @@
/***
This file is part of systemd
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "logind-action.h"
#include "logind-session.h"
#include "test-tables.h"
int main(int argc, char **argv) {
test_table(handle_action, HANDLE_ACTION);
test_table(inhibit_mode, INHIBIT_MODE);
test_table(kill_who, KILL_WHO);
test_table(session_class, SESSION_CLASS);
test_table(session_state, SESSION_STATE);
test_table(session_type, SESSION_TYPE);
test_table(user_state, USER_STATE);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,30 @@
/***
This file is part of systemd
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "machine.h"
#include "test-tables.h"
int main(int argc, char **argv) {
test_table(machine_class, MACHINE_CLASS);
test_table(machine_state, MACHINE_STATE);
test_table(kill_who, KILL_WHO);
return EXIT_SUCCESS;
}

46
src/shared/test-tables.h Normal file
View File

@ -0,0 +1,46 @@
/***
This file is part of systemd
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <stdlib.h>
typedef const char* (*lookup_t)(int);
typedef int (*reverse_t)(const char*);
static inline void _test_table(const char *name,
lookup_t lookup,
reverse_t reverse,
int size) {
int i;
for (i = 0; i < size; i++) {
const char* val = lookup(i);
int rev = -1;
if (val)
rev = reverse(val);
printf("%s: %d → %s → %d\n", name, i, val, rev);
if (!val || rev != i)
exit(EXIT_FAILURE);
}
}
#define test_table(lower, upper) \
_test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX)

102
src/test/test-tables.c Normal file
View File

@ -0,0 +1,102 @@
/***
This file is part of systemd
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "automount.h"
#include "cgroup.h"
#include "condition.h"
#include "device.h"
#include "execute.h"
#include "exit-status.h"
#include "install.h"
#include "job.h"
#include "kill.h"
#include "log.h"
#include "logs-show.h"
#include "mount.h"
#include "path-lookup.h"
#include "path.h"
#include "scope.h"
#include "service.h"
#include "slice.h"
#include "snapshot.h"
#include "socket-util.h"
#include "socket.h"
#include "swap.h"
#include "target.h"
#include "timer.h"
#include "unit-name.h"
#include "unit.h"
#include "util.h"
#include "test-tables.h"
int main(int argc, char **argv) {
test_table(automount_result, AUTOMOUNT_RESULT);
test_table(automount_state, AUTOMOUNT_STATE);
test_table(cgroup_device_policy, CGROUP_DEVICE_POLICY);
test_table(condition_type, CONDITION_TYPE);
test_table(device_state, DEVICE_STATE);
test_table(exec_input, EXEC_INPUT);
test_table(exec_output, EXEC_OUTPUT);
test_table(job_mode, JOB_MODE);
test_table(job_result, JOB_RESULT);
test_table(job_state, JOB_STATE);
test_table(job_type, JOB_TYPE);
test_table(kill_mode, KILL_MODE);
test_table(kill_who, KILL_WHO);
test_table(log_target, LOG_TARGET);
test_table(mount_exec_command, MOUNT_EXEC_COMMAND);
test_table(mount_result, MOUNT_RESULT);
test_table(mount_state, MOUNT_STATE);
test_table(notify_access, NOTIFY_ACCESS);
test_table(output_mode, OUTPUT_MODE);
test_table(path_result, PATH_RESULT);
test_table(path_state, PATH_STATE);
test_table(path_type, PATH_TYPE);
test_table(scope_result, SCOPE_RESULT);
test_table(scope_state, SCOPE_STATE);
test_table(service_exec_command, SERVICE_EXEC_COMMAND);
test_table(service_restart, SERVICE_RESTART);
test_table(service_result, SERVICE_RESULT);
test_table(service_state, SERVICE_STATE);
test_table(service_type, SERVICE_TYPE);
test_table(slice_state, SLICE_STATE);
test_table(snapshot_state, SNAPSHOT_STATE);
test_table(socket_address_bind_ipv6_only, SOCKET_ADDRESS_BIND_IPV6_ONLY);
test_table(socket_exec_command, SOCKET_EXEC_COMMAND);
test_table(socket_result, SOCKET_RESULT);
test_table(socket_state, SOCKET_STATE);
test_table(start_limit_action, SERVICE_START_LIMIT);
test_table(swap_exec_command, SWAP_EXEC_COMMAND);
test_table(swap_result, SWAP_RESULT);
test_table(swap_state, SWAP_STATE);
test_table(systemd_running_as, SYSTEMD_RUNNING_AS);
test_table(target_state, TARGET_STATE);
test_table(timer_base, TIMER_BASE);
test_table(timer_result, TIMER_RESULT);
test_table(timer_state, TIMER_STATE);
test_table(unit_active_state, UNIT_ACTIVE_STATE);
test_table(unit_dependency, UNIT_DEPENDENCY);
test_table(unit_file_change_type, UNIT_FILE_CHANGE_TYPE);
test_table(unit_file_state, UNIT_FILE_STATE);
test_table(unit_load_state, UNIT_LOAD_STATE);
test_table(unit_type, UNIT_TYPE);
return EXIT_SUCCESS;
}