Move hostname setup logic to new shared/hostname-setup.[ch]
No functional change, just moving a bunch of things around. Before we needed a rather complicated setup to test hostname_setup(), because the code was in src/core/. When things are moved to src/shared/ we can just test it as any function. The test is still "unsafe" because hostname_setup() may modify the hostname.
This commit is contained in:
+5
-11
@@ -105,17 +105,6 @@ tests += [
|
||||
libmount,
|
||||
libblkid]],
|
||||
|
||||
[['src/test/test-hostname.c'],
|
||||
[libcore,
|
||||
libshared],
|
||||
[threads,
|
||||
librt,
|
||||
libseccomp,
|
||||
libselinux,
|
||||
libmount,
|
||||
libblkid],
|
||||
'', 'unsafe'],
|
||||
|
||||
[['src/test/test-dns-domain.c'],
|
||||
[libcore,
|
||||
libshared],
|
||||
@@ -339,6 +328,11 @@ tests += [
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-hostname-setup.c'],
|
||||
[],
|
||||
[],
|
||||
'', 'unsafe'],
|
||||
|
||||
[['src/test/test-hostname-util.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
#include "tmpfile-util.h"
|
||||
|
||||
static void test_read_etc_hostname(void) {
|
||||
char path[] = "/tmp/hostname.XXXXXX";
|
||||
char *hostname;
|
||||
int fd;
|
||||
|
||||
fd = mkostemp_safe(path);
|
||||
assert(fd > 0);
|
||||
close(fd);
|
||||
|
||||
/* simple hostname */
|
||||
assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* with comment */
|
||||
assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* with comment and extra whitespace */
|
||||
assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* cleans up name */
|
||||
assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foobar.com"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* no value set */
|
||||
hostname = (char*) 0x1234;
|
||||
assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
|
||||
assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
|
||||
|
||||
/* nonexisting file */
|
||||
assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
|
||||
assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
|
||||
|
||||
unlink(path);
|
||||
}
|
||||
|
||||
static void test_hostname_setup(void) {
|
||||
int r;
|
||||
|
||||
r = hostname_setup();
|
||||
if (r < 0)
|
||||
log_error_errno(r, "hostname: %m");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_setup_logging(LOG_INFO);
|
||||
|
||||
test_read_etc_hostname();
|
||||
test_hostname_setup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "fileio.h"
|
||||
#include "hostname-util.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
#include "tmpfile-util.h"
|
||||
#include "util.h"
|
||||
|
||||
static void test_hostname_is_valid(void) {
|
||||
assert_se(hostname_is_valid("foobar", 0));
|
||||
@@ -91,55 +91,6 @@ static void test_hostname_cleanup(void) {
|
||||
assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
|
||||
}
|
||||
|
||||
static void test_read_etc_hostname(void) {
|
||||
char path[] = "/tmp/hostname.XXXXXX";
|
||||
char *hostname;
|
||||
int fd;
|
||||
|
||||
fd = mkostemp_safe(path);
|
||||
assert(fd > 0);
|
||||
close(fd);
|
||||
|
||||
/* simple hostname */
|
||||
assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* with comment */
|
||||
assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* with comment and extra whitespace */
|
||||
assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foo"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* cleans up name */
|
||||
assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == 0);
|
||||
assert_se(hostname);
|
||||
assert_se(streq(hostname, "foobar.com"));
|
||||
hostname = mfree(hostname);
|
||||
|
||||
/* no value set */
|
||||
hostname = (char*) 0x1234;
|
||||
assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
|
||||
assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
|
||||
|
||||
/* nonexisting file */
|
||||
assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
|
||||
assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
|
||||
|
||||
unlink(path);
|
||||
}
|
||||
|
||||
static void test_hostname_malloc(void) {
|
||||
_cleanup_free_ char *h = NULL, *l = NULL;
|
||||
|
||||
@@ -158,12 +109,10 @@ static void test_fallback_hostname(void) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
log_parse_environment();
|
||||
log_open();
|
||||
test_setup_logging(LOG_INFO);
|
||||
|
||||
test_hostname_is_valid();
|
||||
test_hostname_cleanup();
|
||||
test_read_etc_hostname();
|
||||
test_hostname_malloc();
|
||||
|
||||
test_fallback_hostname();
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "hostname-setup.h"
|
||||
#include "util.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int r;
|
||||
|
||||
r = hostname_setup();
|
||||
if (r < 0)
|
||||
log_error_errno(r, "hostname: %m");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user