summaryrefslogtreecommitdiff
path: root/src/test/test-fs-util.c
blob: e98da45197c4e094f67aa452248ce0cadb54ba82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***
  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 <unistd.h>

#include "systemd-basic/alloc-util.h"
#include "systemd-basic/fd-util.h"
#include "systemd-basic/fileio.h"
#include "systemd-basic/fs-util.h"
#include "systemd-basic/macro.h"
#include "systemd-basic/mkdir.h"
#include "systemd-basic/rm-rf.h"
#include "systemd-basic/string-util.h"
#include "systemd-basic/strv.h"
#include "systemd-basic/util.h"

static void test_unlink_noerrno(void) {
        char name[] = "/tmp/test-close_nointr.XXXXXX";
        int fd;

        fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
        assert_se(fd >= 0);
        assert_se(close_nointr(fd) >= 0);

        {
                PROTECT_ERRNO;
                errno = -42;
                assert_se(unlink_noerrno(name) >= 0);
                assert_se(errno == -42);
                assert_se(unlink_noerrno(name) < 0);
                assert_se(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;

        assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
        assert_se(touch(name) >= 0);

        assert_se(symlink(name, name_alias) >= 0);
        assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
        assert_se(streq(r, name));
        free(r);
        assert_se(unlink(name_alias) >= 0);

        assert_se(chdir(tempdir) >= 0);
        assert_se(symlink(name2, name_alias) >= 0);
        assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
        assert_se(streq(r, name));
        free(r);
        assert_se(unlink(name_alias) >= 0);

        assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
}

static void test_get_files_in_directory(void) {
        _cleanup_strv_free_ char **l = NULL, **t = NULL;

        assert_se(get_files_in_directory("/tmp", &l) >= 0);
        assert_se(get_files_in_directory(".", &t) >= 0);
        assert_se(get_files_in_directory(".", NULL) >= 0);
}

static void test_var_tmp(void) {
        char *tmp_dir = NULL;
        char *tmpdir_backup = NULL;
        const char *default_var_tmp = NULL;
        const char *var_name;
        bool do_overwrite = true;

        default_var_tmp = "/var/tmp";
        var_name = "TMPDIR";

        if (getenv(var_name) != NULL) {
                tmpdir_backup = strdup(getenv(var_name));
                assert_se(tmpdir_backup != NULL);
        }

        unsetenv(var_name);

        var_tmp(&tmp_dir);
        assert_se(!strcmp(tmp_dir, default_var_tmp));

        free(tmp_dir);

        setenv(var_name, "/tmp", do_overwrite);
        assert_se(!strcmp(getenv(var_name), "/tmp"));

        var_tmp(&tmp_dir);
        assert_se(!strcmp(tmp_dir, "/tmp"));

        free(tmp_dir);

        setenv(var_name, "/88_does_not_exist_88", do_overwrite);
        assert_se(!strcmp(getenv(var_name), "/88_does_not_exist_88"));

        var_tmp(&tmp_dir);
        assert_se(!strcmp(tmp_dir, default_var_tmp));

        free(tmp_dir);

        if (tmpdir_backup != NULL)  {
                setenv(var_name, tmpdir_backup, do_overwrite);
                assert_se(!strcmp(getenv(var_name), tmpdir_backup));
                free(tmpdir_backup);
        }
}

int main(int argc, char *argv[]) {
        test_unlink_noerrno();
        test_readlink_and_make_absolute();
        test_get_files_in_directory();
        test_var_tmp();

        return 0;
}