aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/fstat_ut.cpp
blob: 8cdd8d169a9eb8435a2509f73a75ab0a6ee4eb98 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "fstat.h"
#include "file.h"
#include "sysstat.h"
#include "fs.h"

#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/testing/unittest/tests_data.h>

#include <util/folder/path.h>

Y_UNIT_TEST_SUITE(TestFileStat) {
    Y_UNIT_TEST(FileTest) {
        TString fileName = "f1.txt";
        TFileStat oFs;
        {
            TFile file(fileName.data(), OpenAlways | WrOnly);
            file.Write("1234567", 7);

            {
                TFileStat fs(file);
                UNIT_ASSERT(fs.IsFile());
                UNIT_ASSERT(!fs.IsDir());
                UNIT_ASSERT(!fs.IsSymlink());
                UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), (i64)fs.Size);
                UNIT_ASSERT(fs.MTime >= fs.CTime);
                UNIT_ASSERT(fs.NLinks == 1);
                oFs = fs;
            }

            UNIT_ASSERT(file.IsOpen());
            UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7);
            file.Close();
        }
        TFileStat cFs(fileName);
        UNIT_ASSERT(cFs.IsFile());
        UNIT_ASSERT(!cFs.IsDir());
        UNIT_ASSERT(!cFs.IsSymlink());
        UNIT_ASSERT_VALUES_EQUAL(cFs.Size, oFs.Size);
        UNIT_ASSERT(cFs.MTime >= oFs.MTime);
        UNIT_ASSERT_VALUES_EQUAL(cFs.CTime, oFs.CTime);
        UNIT_ASSERT_VALUES_EQUAL(cFs.NLinks, oFs.NLinks);
        UNIT_ASSERT_VALUES_EQUAL(cFs.Mode, oFs.Mode);
        UNIT_ASSERT_VALUES_EQUAL(cFs.Uid, oFs.Uid);
        UNIT_ASSERT_VALUES_EQUAL(cFs.Gid, oFs.Gid);
        UNIT_ASSERT_VALUES_EQUAL(cFs.INode, oFs.INode);
        UNIT_ASSERT(unlink(fileName.data()) == 0);
    }

    Y_UNIT_TEST(DirTest) {
        Mkdir("tmpd", MODE0777);
        TFileStat fs("tmpd");
        UNIT_ASSERT(!fs.IsFile());
        UNIT_ASSERT(fs.IsDir());
        UNIT_ASSERT(!fs.IsSymlink());
        // UNIT_ASSERT(fs.Size == 0); // it fails under unix
        UNIT_ASSERT(NFs::Remove("tmpd"));
        fs = TFileStat("tmpd");
        UNIT_ASSERT(!fs.IsFile());
        UNIT_ASSERT(!fs.IsDir());
        UNIT_ASSERT(!fs.IsSymlink());
        UNIT_ASSERT(fs.Size == 0);
        UNIT_ASSERT(fs.CTime == 0);
    }

#ifdef _win_
    // Symlinks require additional privileges on windows.
    // Skip test if we are not allowed to create one.
    // Wine returns true from NFs::SymLink, but actually does nothing
    #define SAFE_SYMLINK(target, link)                                                \
        do {                                                                          \
            auto res = NFs::SymLink(target, link);                                    \
            if (!res) {                                                               \
                auto err = LastSystemError();                                         \
                Cerr << "can't create symlink: " << LastSystemErrorText(err) << Endl; \
                UNIT_ASSERT(err == ERROR_PRIVILEGE_NOT_HELD);                         \
                return;                                                               \
            }                                                                         \
            if (!NFs::Exists(link) && IsWine()) {                                     \
                Cerr << "wine does not support symlinks" << Endl;                     \
                return;                                                               \
            }                                                                         \
        } while (false)

    bool IsWine() {
        HKEY subKey = nullptr;
        LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Wine", 0, KEY_READ, &subKey);
        if (result == ERROR_SUCCESS) {
            return true;
        }
        result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_READ, &subKey);
        if (result == ERROR_SUCCESS) {
            return true;
        }

        HMODULE hntdll = GetModuleHandle("ntdll.dll");
        if (!hntdll) {
            return false;
        }

        auto func = GetProcAddress(hntdll, "wine_get_version");
        return func != nullptr;
    }
#else
    #define SAFE_SYMLINK(target, link) UNIT_ASSERT(NFs::SymLink(target, link))
#endif

    Y_UNIT_TEST(SymlinkToExistingFileTest) {
        const auto path = GetOutputPath() / "file_1";
        const auto link = GetOutputPath() / "symlink_1";
        TFile(path, EOpenModeFlag::CreateNew | EOpenModeFlag::RdWr);
        SAFE_SYMLINK(path, link);

        const TFileStat statNoFollow(link, false);
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsNull(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(true, statNoFollow.IsFile(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsSymlink(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsDir(), ToString(statNoFollow.Mode));

        const TFileStat statFollow(link, true);
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsNull(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsFile(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(true, statFollow.IsSymlink(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsDir(), ToString(statFollow.Mode));
    }

    Y_UNIT_TEST(SymlinkToNonExistingFileTest) {
        const auto path = GetOutputPath() / "file_2";
        const auto link = GetOutputPath() / "symlink_2";
        SAFE_SYMLINK(path, link);

        const TFileStat statNoFollow(link, false);
        UNIT_ASSERT_VALUES_EQUAL_C(true, statNoFollow.IsNull(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsFile(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsSymlink(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsDir(), ToString(statNoFollow.Mode));

        const TFileStat statFollow(link, true);
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsNull(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsFile(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(true, statFollow.IsSymlink(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsDir(), ToString(statFollow.Mode));
    }

    Y_UNIT_TEST(SymlinkToFileThatCantExistTest) {
        const auto path = TFsPath("/path") / "that" / "does" / "not" / "exists";
        const auto link = GetOutputPath() / "symlink_3";
        SAFE_SYMLINK(path, link);

        const TFileStat statNoFollow(link, false);
        UNIT_ASSERT_VALUES_EQUAL_C(true, statNoFollow.IsNull(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsFile(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsSymlink(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsDir(), ToString(statNoFollow.Mode));

        const TFileStat statFollow(link, true);
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsNull(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsFile(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(true, statFollow.IsSymlink(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsDir(), ToString(statFollow.Mode));
    }

    Y_UNIT_TEST(FileDoesNotExistTest) {
        const auto path = TFsPath("/path") / "that" / "does" / "not" / "exists";

        const TFileStat statNoFollow(path, false);
        UNIT_ASSERT_VALUES_EQUAL_C(true, statNoFollow.IsNull(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsFile(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsSymlink(), ToString(statNoFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statNoFollow.IsDir(), ToString(statNoFollow.Mode));

        const TFileStat statFollow(path, true);
        UNIT_ASSERT_VALUES_EQUAL_C(true, statFollow.IsNull(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsFile(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsSymlink(), ToString(statFollow.Mode));
        UNIT_ASSERT_VALUES_EQUAL_C(false, statFollow.IsDir(), ToString(statFollow.Mode));
    }

    Y_UNIT_TEST(ChmodTest) {
        const TString fileName = "m.txt";
        TFile file(fileName.c_str(), OpenAlways | WrOnly);
        file.Write("1", 1);
        file.Close();

        const TFileStat statDefault(fileName);
        UNIT_ASSERT(Chmod(fileName.c_str(), statDefault.Mode) == 0);
        const TFileStat statUnchanged(fileName);
        UNIT_ASSERT_VALUES_EQUAL(statDefault.Mode, statUnchanged.Mode);

        UNIT_ASSERT(Chmod(fileName.c_str(), S_IRUSR | S_IRGRP | S_IROTH) == 0);
        const TFileStat statReadOnly(fileName);
        UNIT_ASSERT_VALUES_UNEQUAL(statDefault.Mode, statReadOnly.Mode);
        UNIT_ASSERT(Chmod(fileName.c_str(), statReadOnly.Mode) == 0);
        UNIT_ASSERT_VALUES_EQUAL(statReadOnly.Mode, TFileStat(fileName).Mode);

        UNIT_ASSERT(Chmod(fileName.c_str(), statDefault.Mode) == 0);
        UNIT_ASSERT(unlink(fileName.c_str()) == 0);
    }

#ifdef _win_
    Y_UNIT_TEST(WinArchiveDirectoryTest) {
        TFsPath dir = "archive_dir";
        dir.MkDirs();

        SetFileAttributesA(dir.c_str(), FILE_ATTRIBUTE_ARCHIVE);

        const TFileStat stat(dir);
        UNIT_ASSERT(stat.IsDir());
        UNIT_ASSERT(!stat.IsSymlink());
        UNIT_ASSERT(!stat.IsFile());
        UNIT_ASSERT(!stat.IsNull());
    }

    Y_UNIT_TEST(WinArchiveFileTest) {
        TFsPath filename = "archive_file";
        TFile file(filename, OpenAlways | WrOnly);
        file.Write("1", 1);
        file.Close();

        SetFileAttributesA(filename.c_str(), FILE_ATTRIBUTE_ARCHIVE);

        const TFileStat stat(filename);
        UNIT_ASSERT(!stat.IsDir());
        UNIT_ASSERT(!stat.IsSymlink());
        UNIT_ASSERT(stat.IsFile());
        UNIT_ASSERT(!stat.IsNull());
    }
#endif
} // Y_UNIT_TEST_SUITE(TestFileStat)