test-fileio: modernization

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-05-05 13:59:23 +02:00
parent ad0e687c07
commit d12ccbc302
+44 -13
View File
@@ -321,6 +321,8 @@ static void test_executable_is_script(void) {
char *command;
int r;
log_info("/* %s */", __func__);
assert_se(fmkostemp_safe(t, "w", &f) == 0);
fputs("#! /bin/script -a -b \ngoo goo", f);
fflush(f);
@@ -346,6 +348,8 @@ static void test_status_field(void) {
unsigned long long total = 0, buffers = 0;
int r;
log_info("/* %s */", __func__);
assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0);
puts(t);
assert_se(streq(t, "1"));
@@ -377,11 +381,11 @@ static void test_status_field(void) {
}
static void test_capeff(void) {
int pid, p;
log_info("/* %s */", __func__);
for (pid = 0; pid < 2; pid++) {
for (int pid = 0; pid < 2; pid++) {
_cleanup_free_ char *capeff = NULL;
int r;
int r, p;
r = get_process_capeff(0, &capeff);
log_info("capeff: '%s' (r=%d)", capeff, r);
@@ -402,6 +406,8 @@ static void test_write_string_stream(void) {
int fd;
char buf[64];
log_info("/* %s */", __func__);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
@@ -436,6 +442,8 @@ static void test_write_string_file(void) {
char buf[64] = {};
_cleanup_close_ int fd;
log_info("/* %s */", __func__);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
@@ -450,6 +458,8 @@ static void test_write_string_file_no_create(void) {
_cleanup_close_ int fd;
char buf[64] = {};
log_info("/* %s */", __func__);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
@@ -464,6 +474,8 @@ static void test_write_string_file_verify(void) {
_cleanup_free_ char *buf = NULL, *buf2 = NULL;
int r;
log_info("/* %s */", __func__);
r = read_one_line_file("/proc/version", &buf);
if (ERRNO_IS_PRIVILEGE(r))
return;
@@ -490,6 +502,8 @@ static void test_load_env_file_pairs(void) {
_cleanup_strv_free_ char **l = NULL;
char **k, **v;
log_info("/* %s */", __func__);
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
@@ -531,6 +545,8 @@ static void test_search_and_fopen(void) {
int fd, r;
FILE *f;
log_info("/* %s */", __func__);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
close(fd);
@@ -562,6 +578,8 @@ static void test_search_and_fopen(void) {
static void test_search_and_fopen_nulstr(void) {
const char dirs[] = "/tmp/foo/bar\0/tmp\0";
log_info("/* %s */", __func__);
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX";
int fd, r;
FILE *f;
@@ -595,12 +613,15 @@ static void test_writing_tmpfile(void) {
_cleanup_free_ char *contents = NULL;
size_t size;
_cleanup_close_ int fd = -1;
struct iovec iov[3];
int r;
iov[0] = IOVEC_MAKE_STRING("abc\n");
iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n");
iov[2] = IOVEC_MAKE_STRING("");
log_info("/* %s */", __func__);
struct iovec iov[] = {
IOVEC_MAKE_STRING("abc\n"),
IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"),
IOVEC_MAKE_STRING(""),
};
fd = mkostemp_safe(name);
printf("tmpfile: %s", name);
@@ -617,6 +638,8 @@ static void test_writing_tmpfile(void) {
static void test_tempfn(void) {
char *ret = NULL, *p;
log_info("/* %s */", __func__);
assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
free(ret);
@@ -659,8 +682,7 @@ static void test_fgetc(void) {
_cleanup_fclose_ FILE *f = NULL;
char c;
f = fmemopen_unlocked((void*) chars, sizeof(chars), "re");
assert_se(f);
assert_se(f = fmemopen_unlocked((void*) chars, sizeof(chars), "re"));
for (size_t i = 0; i < sizeof(chars); i++) {
assert_se(safe_fgetc(f, &c) == 1);
@@ -753,9 +775,9 @@ static void test_read_line_one_file(FILE *f) {
static void test_read_line(void) {
_cleanup_fclose_ FILE *f = NULL;
f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re");
assert_se(f);
log_info("/* %s */", __func__);
assert_se(f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re"));
test_read_line_one_file(f);
}
@@ -764,6 +786,8 @@ static void test_read_line2(void) {
int fd;
_cleanup_fclose_ FILE *f = NULL;
log_info("/* %s */", __func__);
fd = mkostemp_safe(name);
assert_se(fd >= 0);
assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
@@ -779,6 +803,8 @@ static void test_read_line3(void) {
_cleanup_free_ char *line = NULL;
int r;
log_info("/* %s */", __func__);
f = fopen("/proc/uptime", "re");
if (!f && IN_SET(errno, ENOENT, EPERM))
return;
@@ -811,10 +837,9 @@ static void test_read_line4(void) {
{ 6, "foo\n\r\0" },
};
size_t i;
int r;
for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
for (size_t i = 0; i < ELEMENTSOF(eof_endings); i++) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
@@ -839,6 +864,8 @@ static void test_read_nul_string(void) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
log_info("/* %s */", __func__);
assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r"));
assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
@@ -928,6 +955,8 @@ static void test_read_full_file_offset_size(void) {
size_t rbuf_size;
uint8_t buf[4711];
log_info("/* %s */", __func__);
random_bytes(buf, sizeof(buf));
assert_se(tempfn_random_child(NULL, NULL, &fn) >= 0);
@@ -969,6 +998,8 @@ static void test_read_virtual_file(size_t max_size) {
const char *filename;
int r;
log_info("/* %s (max_size=%zu) */", __func__, max_size);
FOREACH_STRING(filename,
"/proc/1/cmdline",
"/etc/nsswitch.conf",