test-util.c revision 4e731273edfe852a3eee2949cd20f49fd5b4f6d7
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
Copyright 2013 Thomas H.P. Andersen
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 <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <math.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "conf-parser.h"
#include "cpu-set-util.h"
#include "def.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "fstab-util.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "mkdir.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "rm-rf.h"
#include "signal-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
#include "util.h"
#include "virt.h"
#include "xattr-util.h"
static void test_streq_ptr(void) {
}
static void test_align_power2(void) {
unsigned long i, p2;
assert_se(ALIGN_POWER2(0) == 0);
for (i = 1; i < 131071; ++i) {
/* empty */ ;
}
/* empty */ ;
}
}
static void test_max(void) {
static const struct {
int a;
} val1 = {
};
int d = 0;
/* CONST_MAX returns (void) instead of a value if the passed arguments
* are not of the same type or not constant expressions. */
assert_se(d == 1);
}
static void test_container_of(void) {
struct mytype {
struct mytype,
struct mytype,
}
static void test_alloca(void) {
char *t;
memzero(t, 17);
}
static void test_div_round_up(void) {
int div;
/* basic tests */
/* test multiple evaluation */
div = 0;
/* overflow test with exact division */
assert_se(sizeof(0U) == 4);
/* overflow test with rounded division */
}
static void test_first_word(void) {
}
static void test_close_many(void) {
int fds[3];
char name0[] = "/tmp/test-close-many.XXXXXX";
char name1[] = "/tmp/test-close-many.XXXXXX";
char name2[] = "/tmp/test-close-many.XXXXXX";
}
static void test_parse_boolean(void) {
}
static void test_parse_pid(void) {
int r;
assert_se(r == 0);
assert_se(r == 0);
}
static void test_parse_uid(void) {
int r;
assert_se(r == 0);
}
static void test_safe_atou16(void) {
int r;
uint16_t l;
r = safe_atou16("12345", &l);
assert_se(r == 0);
assert_se(l == 12345);
r = safe_atou16("123456", &l);
r = safe_atou16("junk", &l);
}
static void test_safe_atoi16(void) {
int r;
int16_t l;
r = safe_atoi16("-12345", &l);
assert_se(r == 0);
assert_se(l == -12345);
r = safe_atoi16("36536", &l);
r = safe_atoi16("junk", &l);
}
static void test_safe_atolli(void) {
int r;
long long l;
r = safe_atolli("12345", &l);
assert_se(r == 0);
assert_se(l == 12345);
r = safe_atolli("junk", &l);
}
static void test_safe_atod(void) {
int r;
double d;
char *e;
r = safe_atod("junk", &d);
r = safe_atod("0.2244", &d);
assert_se(r == 0);
r = safe_atod("0,5", &d);
errno = 0;
strtod("0,5", &e);
assert_se(*e == ',');
/* Check if this really is locale independent */
r = safe_atod("0.2244", &d);
assert_se(r == 0);
r = safe_atod("0,5", &d);
errno = 0;
}
/* And check again, reset */
r = safe_atod("0.2244", &d);
assert_se(r == 0);
r = safe_atod("0,5", &d);
errno = 0;
strtod("0,5", &e);
assert_se(*e == ',');
}
static void test_strappend(void) {
}
static void test_strstrip(void) {
char *r;
char input[] = " hello, waldo. ";
}
static void test_delete_chars(void) {
char *r;
char input[] = " hello, waldo. abc";
}
static void test_in_charset(void) {
}
static void test_hexchar(void) {
}
static void test_unhexchar(void) {
}
static void test_base32hexchar(void) {
}
static void test_unbase32hexchar(void) {
}
static void test_base64char(void) {
}
static void test_unbase64char(void) {
}
static void test_octchar(void) {
}
static void test_unoctchar(void) {
}
static void test_decchar(void) {
}
static void test_undecchar(void) {
}
static void test_unhexmem(void) {
const char *hex = "efa214921";
const char *hex_invalid = "efa214921o";
}
/* https://tools.ietf.org/html/rfc4648#section-10 */
static void test_base32hexmem(void) {
char *b32;
}
static void test_unbase32hexmem(void) {
void *mem;
assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
}
/* https://tools.ietf.org/html/rfc4648#section-10 */
static void test_base64mem(void) {
char *b64;
}
static void test_unbase64mem(void) {
void *mem;
}
static void test_cescape(void) {
_cleanup_free_ char *escaped;
}
static void test_cunescape(void) {
_cleanup_free_ char *unescaped;
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
/* incomplete sequences */
}
static void test_foreach_word(void) {
size_t l;
int i = 0;
const char test[] = "test abc d\te f ";
const char * const expected[] = {
"test",
"abc",
"d",
"e",
"f",
"",
};
}
size_t l;
int i = 0;
_cleanup_free_ char *t = NULL;
printf("<%s>\n", t);
}
}
static void test_foreach_word_quoted(void) {
check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
STRV_MAKE("test",
"a",
"b",
"c",
"d",
"e",
"",
"",
"hhh",
"",
"",
"a b c"),
false);
check("test \"xxx",
STRV_MAKE("test"),
true);
check("test\\",
true);
}
static void test_memdup_multiply(void) {
int *dup;
}
static void test_u64log2(void) {
}
static void test_protect_errno(void) {
errno = 12;
{
errno = 11;
}
}
static void test_parse_size(void) {
}
static void test_parse_cpu_set(void) {
int ncpus;
int cpu;
/* Simple range (from CPUAffinity example) */
c = mfree(c);
/* A more interesting range */
c = mfree(c);
/* Quoted strings */
c = mfree(c);
/* Use commas as separators */
assert_se(!c);
/* Ranges */
assert_se(!c);
/* Garbage */
assert_se(!c);
/* Empty string */
c = NULL;
assert_se(!c);
/* Runnaway quoted string */
assert_se(!c);
}
static void test_config_parse_iec_uint64(void) {
assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
}
static void test_strextend(void) {
}
static void test_strrep(void) {
}
static void test_split_pair(void) {
free(a);
free(b);
free(a);
free(b);
}
static void test_fstab_node_to_udev_node(void) {
char *n;
n = fstab_node_to_udev_node("LABEL=applé/jack");
puts(n);
free(n);
n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
puts(n);
free(n);
n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
puts(n);
free(n);
n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
puts(n);
free(n);
n = fstab_node_to_udev_node("PONIES=awesome");
puts(n);
free(n);
n = fstab_node_to_udev_node("/dev/xda1");
puts(n);
free(n);
}
static void test_get_files_in_directory(void) {
}
static void test_in_set(void) {
}
static void test_writing_tmpfile(void) {
char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
int fd, r;
assert_se(r >= 0);
assert_se(r == 0);
}
static void test_hexdump(void) {
unsigned i;
for (i = 0; i < ELEMENTSOF(data); i++)
data[i] = i*2;
}
static void test_log2i(void) {
}
static void test_foreach_string(void) {
const char * const t[] = {
"foo",
"bar",
"waldo",
};
const char *x;
unsigned i = 0;
assert_se(i == 3);
FOREACH_STRING(x, "zzz")
}
static void test_filename_is_valid(void) {
int i;
for (i=0; i<FILENAME_MAX+1; i++)
foo[i] = 'a';
}
static void test_string_has_cc(void) {
}
static void test_ascii_strlower(void) {
char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
}
static void test_files_same(void) {
char name[] = "/tmp/test-files_same.XXXXXX";
char name_alias[] = "/tmp/test-files_same.alias";
}
static void test_is_valid_documentation_url(void) {
}
static void test_file_in_same_dir(void) {
char *t;
free(t);
free(t);
free(t);
free(t);
free(t);
}
static void test_endswith(void) {
}
static void test_endswith_no_case(void) {
}
static void test_close_nointr(void) {
char name[] = "/tmp/test-test-close_nointr.XXXXXX";
int fd;
}
static void test_unlink_noerrno(void) {
char name[] = "/tmp/test-close_nointr.XXXXXX";
int fd;
{
errno = -42;
}
}
static void test_readlink_and_make_absolute(void) {
char tempdir[] = "/tmp/test-readlink_and_make_absolute";
char name[] = "/tmp/test-readlink_and_make_absolute/original";
char name2[] = "test-readlink_and_make_absolute/original";
char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
char *r = NULL;
free(r);
free(r);
}
static void test_ignore_signals(void) {
}
static void test_strshorten(void) {
char s[] = "foobar";
}
static void test_strjoina(void) {
char *actual;
}
static void test_is_symlink(void) {
char name[] = "/tmp/test-is_symlink.XXXXXX";
char name_link[] = "/tmp/test-is_symlink.link";
}
static void test_search_and_fopen(void) {
char name[] = "/tmp/test-search_and_fopen.XXXXXX";
int fd = -1;
int r;
FILE *f;
assert_se(r >= 0);
fclose(f);
assert_se(r >= 0);
fclose(f);
assert_se(r >= 0);
fclose(f);
assert_se(r < 0);
assert_se(r < 0);
assert_se(r == 0);
assert_se(r < 0);
}
static void test_search_and_fopen_nulstr(void) {
char name[] = "/tmp/test-search_and_fopen.XXXXXX";
int fd = -1;
int r;
FILE *f;
assert_se(r >= 0);
fclose(f);
assert_se(r >= 0);
fclose(f);
assert_se(r < 0);
assert_se(r < 0);
assert_se(r == 0);
assert_se(r < 0);
}
static void test_glob_exists(void) {
char name[] = "/tmp/test-glob_exists.XXXXXX";
int fd = -1;
int r;
r = glob_exists("/tmp/test-glob_exists*");
assert_se(r == 1);
assert_se(r == 0);
r = glob_exists("/tmp/test-glob_exists*");
assert_se(r == 0);
}
static void test_execute_directory(void) {
char template_lo[] = "/tmp/test-readlink_and_make_absolute-lo.XXXXXXX";
char template_hi[] = "/tmp/test-readlink_and_make_absolute-hi.XXXXXXX";
assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0);
assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0);
assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0);
assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
}
static void test_extract_first_word(void) {
const char *p, *original;
char *t;
p = original = "foobar waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
free(t);
free(t);
assert_se(!t);
p = original = "\"";
free(t);
p = original = "\"";
p = original = "\'";
free(t);
p = original = "\'";
p = original = "\'fooo";
free(t);
p = original = "\'fooo";
p = original = "\'fooo";
free(t);
p = original = "\"fooo";
free(t);
p = original = "yay\'foo\'bar";
free(t);
p = original = "yay\'foo\'bar";
free(t);
p = original = " foobar ";
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
free(t);
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "\"foo\\";
p = original = "\"foo\\";
free(t);
p = original = "foo::bar";
free(t);
free(t);
assert_se(!t);
p = original = "foo\\:bar::waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foo\\";
p = original = "\"foo\\";
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
p = original = "fooo\\ bar quux";
free(t);
p = original = "\\w+@\\K[\\d.]+";
p = original = "\\w+@\\K[\\d.]+";
free(t);
p = original = "\\w+\\b";
free(t);
p = original = "-N ''";
free(t);
free(t);
p = original = ":foo\\:bar::waldo:";
assert_se(t);
free(t);
free(t);
assert_se(t);
free(t);
free(t);
free(t);
assert_se(!t);
assert_se(!p);
}
static void test_extract_first_word_and_warn(void) {
const char *p, *original;
char *t;
p = original = "foobar waldo";
free(t);
free(t);
assert_se(!t);
p = original = "\"foobar\" \'waldo\'";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
free(t);
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
free(t);
assert_se(!t);
p = original = "\"";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'fooo";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\'fooo";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = " foo\\ba\\x6ar ";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = " foo\\ba\\x6ar ";
free(t);
p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
free(t);
p = original = "fooo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
p = original = "\"foo\\";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
free(t);
p = original = "fooo\\ bar quux";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\\w+@\\K[\\d.]+";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
p = original = "\\w+\\b";
assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
free(t);
}
static void test_extract_many_words(void) {
const char *p, *original;
char *a, *b, *c;
p = original = "foobar waldi piep";
free(a);
free(b);
free(c);
p = original = "'foobar' wa\"ld\"i ";
free(a);
free(b);
p = original = "'foobar' wa\"ld\"i ";
free(a);
free(b);
p = original = "";
p = original = " ";
p = original = "foobar";
p = original = "foobar waldi";
free(a);
p = original = " foobar ";
free(a);
}
return 0;
}
static void test_parse_proc_cmdline(void) {
}
static void test_raw_clone(void) {
pid2 = raw_getpid();
if (pid == 0) {
} else {
int status;
}
}
static void test_same_fd(void) {
}
static void test_uid_ptr(void) {
}
char check[n];
}
static void test_sparse_write(void) {
const char test_a[] = "test";
const char test_b[] = "\0\0\0\0test\0\0\0\0";
const char test_c[] = "\0\0test\0\0\0\0";
const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0";
const char test_e[] = "test\0\0\0\0test";
char fn[] = "/tmp/sparseXXXXXX";
}
_cleanup_free_ char *r;
}
static void test_shell_escape(void) {
}
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
_cleanup_free_ char *r;
assert_se(r = shell_maybe_quote(s));
}
static void test_shell_maybe_quote(void) {
}
static void test_parse_mode(void) {
mode_t m;
}
static void test_tempfn(void) {
}
static void test_strcmp_ptr(void) {
}
static void test_fgetxattrat_fake(void) {
char t[] = "/var/tmp/xattrtestXXXXXX";
const char *x;
char v[3] = {};
int r;
x = strjoina(t, "/test");
goto cleanup;
assert_se(r >= 0);
safe_close(fd);
}
log_open();
test_max();
test_alloca();
test_hexchar();
test_octchar();
test_decchar();
test_cescape();
test_u64log2();
test_strrep();
test_in_set();
test_hexdump();
test_log2i();
test_same_fd();
test_uid_ptr();
test_tempfn();
return 0;
}