~aleteoryx/muditaos

ref: c34a85f49470b224954fbb89342e1df79e6e5362 muditaos/module-platform/linux/tests/unittest_filesystem_ext4.cpp -rw-r--r-- 11.5 KiB
c34a85f4 — Lefucjusz [MOS-1037] Fix disappearing 'Call' label in phonebook 2 years ago
                                                                                
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <platform/linux/DiskImage.hpp>

#include <purefs/fs/filesystem.hpp>
#include <purefs/blkdev/disk_manager.hpp>
#include <purefs/fs/drivers/filesystem_ext4.hpp>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>

namespace
{
    constexpr auto disk_image = "ext4test.img";

    auto prepare_filesystem(std::string_view dev_name)
        -> std::pair<std::unique_ptr<purefs::fs::filesystem>, std::shared_ptr<purefs::blkdev::disk_manager>>
    {
        using namespace purefs;

        auto dm   = std::make_shared<blkdev::disk_manager>();
        auto disk = std::make_shared<blkdev::disk_image>(disk_image);

        if (dm->register_device(disk, dev_name) != 0) {
            return {};
        }

        auto fs_core       = std::make_unique<fs::filesystem>(dm);
        const auto vfs_lfs = std::make_shared<fs::drivers::filesystem_ext4>();

        if (fs_core->register_filesystem("ext4", vfs_lfs) != 0) {
            return {};
        }

        return std::make_pair(std::move(fs_core), std::move(dm));
    }
} // namespace

TEST_CASE("ext4: Basic mount and functionality")
{
    using namespace purefs;

    auto dm   = std::make_shared<blkdev::disk_manager>();
    auto disk = std::make_shared<blkdev::disk_image>(disk_image);
    REQUIRE(disk);
    REQUIRE(dm->register_device(disk, "emmc0") == 0);

    fs::filesystem fscore(dm);
    const auto vfs_ext = std::make_shared<fs::drivers::filesystem_ext4>();
    REQUIRE(vfs_ext->mount_count() == 0);
    REQUIRE(fscore.register_filesystem("ext4", vfs_ext) == 0);

    REQUIRE(fscore.mount("emmc0part0", "/sys", "ext4") == 0);
    REQUIRE(vfs_ext->mount_count() == 1);

    REQUIRE(fscore.mount("dummy0part0", "/dummy", "ext4") == -ENXIO);
    REQUIRE(fscore.umount("/ala") == -ENOENT);
    REQUIRE(fscore.mount("emmc0part0", "/sys", "vfat") == -EBUSY);
    REQUIRE(fscore.mount("emmc0part0", "/path", "vfat") == -EBUSY);
    struct statvfs ssv
    {};
    REQUIRE(fscore.stat_vfs("/sys/", ssv) == 0);
    REQUIRE(fscore.stat_vfs("/sys", ssv) == 0);

    REQUIRE(fscore.umount("/sys") == 0);
    REQUIRE(vfs_ext->mount_count() == 0);

    REQUIRE(fscore.mount("emmc0part0", "/path", "ext4") == 0);
    REQUIRE(vfs_ext->mount_count() == 1);
    REQUIRE(fscore.umount("/path") == 0);
}

TEST_CASE("ext4: Read tests")
{
    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4") == 0);

    const auto fd = fs_core->open("/sys/test_read_1.txt", O_RDONLY, 0);
    REQUIRE(fd >= 3);

    static char buf[64];

    REQUIRE(fs_core->read(fd, buf, 8) == 8);
    REQUIRE(memcmp(buf, "01234567", 8) == 0);
    REQUIRE(fs_core->seek(fd, 4, SEEK_SET) == 4);
    REQUIRE(fs_core->read(fd, buf, 8) == 8);
    REQUIRE(memcmp(buf, "456789AB", 8) == 0);
    struct stat st
    {};
    REQUIRE(fs_core->fstat(fd, st) == 0);
    REQUIRE((st.st_mode & S_IFREG) != 0);
    REQUIRE((st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == (S_IRUSR | S_IRGRP | S_IROTH));

    REQUIRE(fs_core->close(fd) == 0);
    REQUIRE(fs_core->umount("/sys") == 0);
}

TEST_CASE("ext4: Write tests")
{
    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4") == 0);

    static constexpr auto filename = "/sys/test_write_tmp_1.txt";
    auto fd                        = fs_core->open(filename, O_CREAT | O_RDWR, 0);
    REQUIRE(fd >= 3);

    const std::string str = "Hello, littlefs!";
    REQUIRE(fs_core->write(fd, str.c_str(), str.length()) == static_cast<ssize_t>(str.length()));

    // reopen the file to flush write buffers
    REQUIRE(fs_core->close(fd) == 0);
    fd = fs_core->open(filename, O_RDONLY, 0);
    REQUIRE(fd >= 3);

    static char buf[64];
    REQUIRE(fs_core->read(fd, buf, str.length()) == static_cast<ssize_t>(str.length()));
    REQUIRE(memcmp(buf, str.c_str(), str.length()) == 0);

    REQUIRE(fs_core->close(fd) == 0);
    REQUIRE(fs_core->unlink(filename) == 0);

    // Test the ftruncate
    static constexpr auto trunc_fname = "/sys/test_truncate.bin";
    static auto trunc_fsize           = 1024 * 256 + 4;
    fd                                = fs_core->open(trunc_fname, O_CREAT | O_RDWR, 0);
    REQUIRE(fd >= 3);
    REQUIRE(fs_core->ftruncate(fd, trunc_fsize) == 0);
    REQUIRE(fs_core->close(fd) == 0);
    struct stat st
    {};
    REQUIRE(fs_core->stat(trunc_fname, st) == 0);
    REQUIRE((st.st_mode & S_IFREG) != 0);
    REQUIRE(st.st_size == trunc_fsize);
    REQUIRE(fs_core->unlink(trunc_fname) == 0);

    REQUIRE(fs_core->umount("/sys") == 0);
}

TEST_CASE("ext4: Read-only filesystem tests")
{
    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4", purefs::fs::mount_flags::read_only) == 0);

    const auto fd = fs_core->open("/sys/test_read_1.txt", O_RDONLY, 0);
    REQUIRE(fd >= 3);

    struct stat st
    {};
    REQUIRE(fs_core->fstat(fd, st) == 0);
    REQUIRE((st.st_mode & S_IFREG) != 0);
    REQUIRE((st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == (S_IRUSR | S_IRGRP | S_IROTH));
    REQUIRE((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0);

    REQUIRE(fs_core->close(fd) == 0);
    REQUIRE(fs_core->open("/sys/test_read_1.txt", O_RDWR, 0) == -EACCES);
    REQUIRE(fs_core->mkdir("/sys/tmp_dir", 0) == -EACCES);
    REQUIRE(fs_core->unlink("/sys/dummy_file.txt") == -EACCES);
    REQUIRE(fs_core->umount("/sys") == 0);
}

TEST_CASE("ext4: Directory tests")
{
    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4") == 0);

    const std::string path = "/sys/test_mkdir_tmp";
    REQUIRE(fs_core->mkdir(path, 0) == 0);
    REQUIRE(fs_core->mkdir(path, 0) == -EEXIST);
    {
        const std::vector<std::string> filenames = {
            "document.txt", "image.jpg", "ringtone.mp3", "data.csv", "picture.png"};
        std::vector<int> fds;
        std::transform(filenames.begin(), filenames.end(), std::back_inserter(fds), [&](const auto &filename) {
            return fs_core->open(path + "/" + filename, O_CREAT | O_RDWR, 0);
        });
        REQUIRE(std::all_of(fds.begin(), fds.end(), [](auto fd) { return fd >= 3; }));
        for (auto fd : fds) {
            REQUIRE(fs_core->close(fd) == 0);
        }

        {
            const auto dh = fs_core->diropen(path);
            REQUIRE(dh);
            std::vector<std::string> dir_filenames;
            int dir_status = 0;
            for (;;) {
                std::string fn;
                struct stat st
                {};
                dir_status = fs_core->dirnext(dh, fn, st);

                if (dir_status == 0) {
                    dir_filenames.push_back(fn);
                }
                else {
                    break;
                }
            }

            REQUIRE(dir_status == -ENODATA);
            REQUIRE(std::all_of(filenames.begin(), filenames.end(), [&dir_filenames](const auto &fn) {
                return std::find(dir_filenames.begin(), dir_filenames.end(), fn) != dir_filenames.end();
            }));
            REQUIRE(fs_core->dirclose(dh) == 0);
        }
        {
            const auto dh = fs_core->diropen(path);
            REQUIRE(dh);
            struct stat st
            {};
            std::string first_fn;
            REQUIRE(fs_core->dirnext(dh, first_fn, st) == 0);

            for (std::string tmp_fn; fs_core->dirnext(dh, tmp_fn, st) == 0;) {}
            REQUIRE(fs_core->dirreset(dh) == 0);

            std::string reset_fn;
            REQUIRE(fs_core->dirnext(dh, reset_fn, st) == 0);
            REQUIRE(reset_fn == first_fn);
            REQUIRE(fs_core->dirclose(dh) == 0);
        }

        for (const auto &filename : filenames) {
            REQUIRE(fs_core->unlink(path + "/" + filename) == 0);
        }
        REQUIRE(fs_core->rmdir(path) == 0);
        {
            // Opendir on the root path without tailing /
            const auto diro = fs_core->diropen("/sys");
            REQUIRE(diro);
            REQUIRE(fs_core->dirclose(diro) == 0);
        }
    }

    REQUIRE(fs_core->umount("/sys") == 0);
}

TEST_CASE("ext4: Remount RO->RW->RW")
{
    using namespace purefs;
    static constexpr auto filename = "/sys/remount_test.txt";

    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4", fs::mount_flags::read_only) == 0);
    REQUIRE(fs_core->open(filename, O_RDWR | O_CREAT, 0) == -EACCES);

    const std::string wr_str = "remount_test";

    {
        REQUIRE(fs_core->mount("", "/sys", "", fs::mount_flags::remount) == 0);
        const auto fd = fs_core->open(filename, O_RDWR | O_CREAT, 0);
        REQUIRE(fd >= 3);
        REQUIRE(fs_core->write(fd, wr_str.c_str(), wr_str.length()) == static_cast<ssize_t>(wr_str.length()));
        REQUIRE(fs_core->close(fd) == 0);
    }

    {
        REQUIRE(fs_core->mount("", "/sys", "", fs::mount_flags::remount | fs::mount_flags::read_only) == 0);
        const auto fd = fs_core->open(filename, O_RDONLY, 0);
        REQUIRE(fd >= 3);
        char buf[64];
        REQUIRE(fs_core->read(fd, buf, wr_str.length()) == static_cast<ssize_t>(wr_str.length()));
        REQUIRE(memcmp(buf, wr_str.c_str(), wr_str.length()) == 0);
        REQUIRE(fs_core->close(fd) == 0);
    }

    {
        REQUIRE(fs_core->mount("", "/sys", "", fs::mount_flags::remount) == 0);
        REQUIRE(fs_core->unlink(filename) == 0);
    }

    REQUIRE(fs_core->umount("/sys") == 0);
}

TEST_CASE("ext4: stat extended")
{
    using namespace purefs;
    auto [fs_core, dm] = prepare_filesystem("emmc0");
    REQUIRE(fs_core);
    REQUIRE(fs_core->mount("emmc0part0", "/sys", "ext4") == 0);
    // Check if it is a directory
    struct stat st
    {};
    REQUIRE(fs_core->stat("/sys", st) == 0);
    REQUIRE(S_ISDIR(st.st_mode));
    REQUIRE(fs_core->stat("/sys/", st) == 0);
    REQUIRE(S_ISDIR(st.st_mode));
    // Check for dir and subdir
    const auto dir = "/sys/advdirx";
    const auto fil = "/sys/advdirx/advfile";
    // Create directory and truncated file
    REQUIRE(fs_core->mkdir(dir, 0755) == 0);
    auto fd = fs_core->open(fil, O_CREAT | O_RDWR, 0);
    REQUIRE(fd >= 3);
    REQUIRE(fs_core->ftruncate(fd, 124567) == 0);
    REQUIRE(fs_core->close(fd) == 0);
    fd = -1;

    // Now check for stat for directories and sub dirs
    // Root dir
    REQUIRE(fs_core->stat("/sys", st) == 0);
    REQUIRE(S_ISFIFO(st.st_mode) == 0);
    REQUIRE(S_ISCHR(st.st_mode) == 0);
    REQUIRE(S_ISDIR(st.st_mode));
    REQUIRE(S_ISBLK(st.st_mode) == 0);
    REQUIRE(S_ISLNK(st.st_mode) == 0);
    REQUIRE(S_ISSOCK(st.st_mode) == 0);
    REQUIRE(S_ISREG(st.st_mode) == 0);

    // Sub dir
    REQUIRE(fs_core->stat(dir, st) == 0);
    REQUIRE(S_ISFIFO(st.st_mode) == 0);
    REQUIRE(S_ISCHR(st.st_mode) == 0);
    REQUIRE(S_ISDIR(st.st_mode));
    REQUIRE(S_ISBLK(st.st_mode) == 0);
    REQUIRE(S_ISLNK(st.st_mode) == 0);
    REQUIRE(S_ISSOCK(st.st_mode) == 0);
    REQUIRE(S_ISREG(st.st_mode) == 0);

    // Check for file
    REQUIRE(0 == fs_core->stat(fil, st));
    REQUIRE(124567 == st.st_size);
    REQUIRE(S_ISFIFO(st.st_mode) == 0);
    REQUIRE(S_ISCHR(st.st_mode) == 0);
    REQUIRE(S_ISDIR(st.st_mode) == 0);
    REQUIRE(S_ISBLK(st.st_mode) == 0);
    REQUIRE(S_ISLNK(st.st_mode) == 0);
    REQUIRE(S_ISSOCK(st.st_mode) == 0);
    REQUIRE(S_ISREG(st.st_mode));

    // Final cleanup
    REQUIRE(0 == fs_core->unlink(fil));
    REQUIRE(0 == fs_core->rmdir(dir));

    // Final umount
    REQUIRE(fs_core->umount("/sys") == 0);
}