Skip to content

Commit

Permalink
Merge pull request systemd#2792 from ronnychevalier/rc/tests_movev2
Browse files Browse the repository at this point in the history
tests: move out unrelated tests from test-util to their own file
  • Loading branch information
keszybz committed Mar 10, 2016
2 parents ef240bf + 31b5d98 commit c41d3b3
Show file tree
Hide file tree
Showing 23 changed files with 1,892 additions and 1,441 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
/test-acd
/test-acl-util
/test-af-list
/test-alloc-util
/test-architecture
/test-arphrd-list
/test-ask-password-api
Expand Down Expand Up @@ -166,6 +167,7 @@
/test-conf-parser
/test-copy
/test-coredump-vacuum
/test-cpu-set-util
/test-daemon
/test-date
/test-device-nodes
Expand All @@ -181,20 +183,26 @@
/test-ellipsize
/test-engine
/test-env-replace
/test-escape
/test-event
/test-execute
/test-extract-word
/test-fd-util
/test-fdset
/test-fileio
/test-firewall-util
/test-fs-util
/test-fstab-util
/test-glob-util
/test-hashmap
/test-hexdecoct
/test-hostname
/test-hostname-util
/test-id128
/test-inhibit
/test-install
/test-install-root
/test-io-util
/test-ipcrm
/test-ipv4ll
/test-ipv4ll-manual
Expand Down Expand Up @@ -235,6 +243,7 @@
/test-path-lookup
/test-path-util
/test-prioq
/test-proc-cmdline
/test-process-util
/test-pty
/test-qcow2
Expand All @@ -254,6 +263,7 @@
/test-siphash24
/test-sleep
/test-socket-util
/test-stat-util
/test-strbuf
/test-string-util
/test-strip-tab-ansi
Expand All @@ -273,6 +283,8 @@
/test-util
/test-verbs
/test-watchdog
/test-web-util
/test-xattr-util
/test-xml
/timedatectl
/udevadm
Expand Down
84 changes: 84 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,18 @@ tests += \
test-utf8 \
test-ellipsize \
test-util \
test-cpu-set-util \
test-hexdecoct \
test-escape \
test-alloc-util \
test-proc-cmdline \
test-io-util \
test-glob-util \
test-xattr-util \
test-fs-util \
test-web-util \
test-stat-util \
test-fd-util \
test-string-util \
test-extract-word \
test-parse-util \
Expand Down Expand Up @@ -1761,6 +1773,78 @@ test_util_SOURCES = \
test_util_LDADD = \
libshared.la

test_hexdecoct_SOURCES = \
src/test/test-hexdecoct.c

test_hexdecoct_LDADD = \
libbasic.la

test_alloc_util_SOURCES = \
src/test/test-alloc-util.c

test_alloc_util_LDADD = \
libbasic.la

test_xattr_util_SOURCES = \
src/test/test-xattr-util.c

test_xattr_util_LDADD = \
libbasic.la

test_io_util_SOURCES = \
src/test/test-io-util.c

test_io_util_LDADD = \
libbasic.la

test_glob_util_SOURCES = \
src/test/test-glob-util.c

test_glob_util_LDADD = \
libbasic.la

test_fs_util_SOURCES = \
src/test/test-fs-util.c

test_fs_util_LDADD = \
libbasic.la

test_proc_cmdline_SOURCES = \
src/test/test-proc-cmdline.c

test_proc_cmdline_LDADD = \
libbasic.la

test_fd_util_SOURCES = \
src/test/test-fd-util.c

test_fd_util_LDADD = \
libbasic.la

test_web_util_SOURCES = \
src/test/test-web-util.c

test_web_util_LDADD = \
libbasic.la

test_cpu_set_util_SOURCES = \
src/test/test-cpu-set-util.c

test_cpu_set_util_LDADD = \
libbasic.la

test_stat_util_SOURCES = \
src/test/test-stat-util.c

test_stat_util_LDADD = \
libbasic.la

test_escape_SOURCES = \
src/test/test-escape.c

test_escape_LDADD = \
libbasic.la

test_string_util_SOURCES = \
src/test/test-string-util.c

Expand Down
55 changes: 55 additions & 0 deletions src/test/test-alloc-util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 "alloc-util.h"
#include "macro.h"
#include "util.h"

static void test_alloca(void) {
static const uint8_t zero[997] = { };
char *t;

t = alloca_align(17, 512);
assert_se(!((uintptr_t)t & 0xff));
memzero(t, 17);

t = alloca0_align(997, 1024);
assert_se(!((uintptr_t)t & 0x1ff));
assert_se(!memcmp(t, zero, 997));
}

static void test_memdup_multiply(void) {
int org[] = {1, 2, 3};
int *dup;

dup = (int*)memdup_multiply(org, sizeof(int), 3);

assert_se(dup);
assert_se(dup[0] == 1);
assert_se(dup[1] == 2);
assert_se(dup[2] == 3);
free(dup);
}

int main(int argc, char *argv[]) {
test_alloca();
test_memdup_multiply();

return 0;
}
9 changes: 9 additions & 0 deletions src/test/test-conf-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ static void test_config_parse_nsec(void) {
test_config_parse_nsec_one("garbage", 0);
}

static void test_config_parse_iec_uint64(void) {
uint64_t offset = 0;
assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
assert_se(offset == 4 * 1024 * 1024);

assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
}

int main(int argc, char **argv) {
log_parse_environment();
log_open();
Expand All @@ -230,6 +238,7 @@ int main(int argc, char **argv) {
test_config_parse_mode();
test_config_parse_sec();
test_config_parse_nsec();
test_config_parse_iec_uint64();

return 0;
}
143 changes: 143 additions & 0 deletions src/test/test-cpu-set-util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 "alloc-util.h"
#include "cpu-set-util.h"
#include "macro.h"

static void test_parse_cpu_set(void) {
cpu_set_t *c = NULL;
int ncpus;
int cpu;

/* Simple range (from CPUAffinity example) */
ncpus = parse_cpu_set_and_warn("1 2", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
c = mfree(c);

/* A more interesting range */
ncpus = parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 4; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Quoted strings */
ncpus = parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Use commas as separators */
ncpus = parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 4; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Commas with spaces (and trailing comma, space) */
ncpus = parse_cpu_set_and_warn("0, 1, 2, 3, 4, 5, 6, 7, ", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 8; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Ranges */
ncpus = parse_cpu_set_and_warn("0-3,8-11", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 4; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Ranges with trailing comma, space */
ncpus = parse_cpu_set_and_warn("0-3 8-11, ", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
for (cpu = 0; cpu < 4; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 8; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Negative range (returns empty cpu_set) */
ncpus = parse_cpu_set_and_warn("3-0", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 0);
c = mfree(c);

/* Overlapping ranges */
ncpus = parse_cpu_set_and_warn("0-7 4-11", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
for (cpu = 0; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Mix ranges and individual CPUs */
ncpus = parse_cpu_set_and_warn("0,1 4-11", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus >= 1024);
assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 10);
assert_se(CPU_ISSET_S(0, CPU_ALLOC_SIZE(ncpus), c));
assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
for (cpu = 4; cpu < 12; cpu++)
assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
c = mfree(c);

/* Garbage */
ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus < 0);
assert_se(!c);

/* Range with garbage */
ncpus = parse_cpu_set_and_warn("0-3 8-garbage", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus < 0);
assert_se(!c);

/* Empty string */
c = NULL;
ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus == 0); /* empty string returns 0 */
assert_se(!c);

/* Runnaway quoted string */
ncpus = parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c, NULL, "fake", 1, "CPUAffinity");
assert_se(ncpus < 0);
assert_se(!c);
}

int main(int argc, char *argv[]) {
test_parse_cpu_set();

return 0;
}
Loading

0 comments on commit c41d3b3

Please sign in to comment.