~aleteoryx/muditaos

ref: sign_test muditaos/module-vfs/tests/unittest_filesystem_inotify.cpp -rw-r--r-- 7.5 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include <purefs/fs/notifier.hpp>
#include <purefs/fs/inotify_message.hpp>

namespace purefs::fs
{

    struct notifier_mock final : public internal::notifier
    {
        virtual ~notifier_mock() = default;
        auto send_notification(std::shared_ptr<sys::Service> svc,
                               inotify_flags flags,
                               std::string_view name,
                               std::string_view name_dst) const -> void override
        {
            messages.emplace_back(flags, name, name_dst);
            xsvc.push_back(svc.get());
        }
        mutable std::vector<message::inotify> messages;
        mutable std::vector<sys::Service *> xsvc;
    };
} // namespace purefs::fs

namespace sys
{
    class Service
    {};
} // namespace sys

TEST_CASE("Notifier base class test")
{
    using namespace purefs::fs;
    auto svc = std::make_shared<sys::Service>();
    notifier_mock notify;
    notify.register_path("/sys/ala",
                         svc,
                         inotify_flags::open | inotify_flags::close_nowrite | inotify_flags::close_write |
                             inotify_flags::attrib | inotify_flags::move_src | inotify_flags::move_dst);
    SECTION("open and close simple")
    {
        notify.notify_open("/sys/ala", 77, true);
        notify.notify_close(77);
        REQUIRE(notify.messages.size() == 2);
        REQUIRE(notify.messages[0].name == "/sys/ala");
        REQUIRE(notify.messages[0].name_prev.empty());
        REQUIRE((notify.messages[0].flags && inotify_flags::open));
        REQUIRE(notify.messages[1].name == "/sys/ala");
        REQUIRE(notify.messages[1].name_prev.empty());
        REQUIRE((notify.messages[1].flags && inotify_flags::close_nowrite));
        notify.messages.clear();
    }
    SECTION("open and close pairing")
    {
        notify.notify_open("/sys/ala/1", 100, true);
        notify.notify_open("/sys/ala/2", 200, false);
        notify.notify_open("/sys/ala/3", 300, true);
        REQUIRE(notify.messages.size() == 3);
        notify.notify(200, inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 4);
        notify.notify_close(200);
        REQUIRE(notify.messages.size() == 5);
        notify.notify_close(200);
        REQUIRE(notify.messages.size() == 5);
        REQUIRE(notify.messages[0].name == "/sys/ala/1");
        REQUIRE((notify.messages[0].flags && inotify_flags::open));
        REQUIRE(notify.messages[1].name == "/sys/ala/2");
        REQUIRE((notify.messages[1].flags && inotify_flags::open));
        REQUIRE(notify.messages[2].name == "/sys/ala/3");
        REQUIRE((notify.messages[2].flags && inotify_flags::open));
        REQUIRE(notify.messages[3].name == "/sys/ala/2");
        REQUIRE((notify.messages[3].flags && inotify_flags::attrib));
        REQUIRE(notify.messages[4].name == "/sys/ala/2");
        REQUIRE((notify.messages[4].flags && inotify_flags::close_write));
        notify.messages.clear();
        notify.notify_close(100);
        notify.notify_close(300);
        REQUIRE(notify.messages.size() == 2);
        REQUIRE(notify.messages[0].name == "/sys/ala/1");
        REQUIRE((notify.messages[0].flags && inotify_flags::close_nowrite));
        REQUIRE(notify.messages[1].name == "/sys/ala/3");
        REQUIRE((notify.messages[1].flags && inotify_flags::close_nowrite));
        notify.notify_close(300);
        notify.notify_close(300);
        REQUIRE(notify.messages.size() == 2);
        notify.messages.clear();
    }
    SECTION("Other notifies tests")
    {
        notify.notify("/sys/ala/100", "/sys/ala/555", inotify_flags::move_src);
        notify.notify("/sys/ala/555", "/sys/ala/100", inotify_flags::move_dst);
        notify.notify("/sys/ala/999/989", inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 3);
        REQUIRE(notify.messages[0].name == "/sys/ala/100");
        REQUIRE(notify.messages[0].name_prev == "/sys/ala/555");
        REQUIRE((notify.messages[0].flags && inotify_flags::move_src));
        REQUIRE(notify.messages[1].name == "/sys/ala/555");
        REQUIRE(notify.messages[1].name_prev == "/sys/ala/100");
        REQUIRE((notify.messages[1].flags && inotify_flags::move_dst));
    }
}

TEST_CASE("Filtering test")
{
    using namespace purefs::fs;
    SECTION("Different mask for services")
    {
        notifier_mock notify;
        auto svc1 = std::make_shared<sys::Service>();
        auto svc2 = std::make_shared<sys::Service>();
        notify.register_path(
            "/sys/path1", svc1, inotify_flags::open | inotify_flags::close_nowrite | inotify_flags::close_write);
        notify.register_path("/sys/path1/", svc2, inotify_flags::open | inotify_flags::close_write);
        notify.notify_open("/sys/path1/file.txt", 100, false);
        notify.notify_open("/sys/path1/file1.txt", 200, true);
        REQUIRE(notify.messages.size() == 4);
        notify.notify_close(100);
        REQUIRE(notify.messages.size() == 6);
        notify.notify_close(200);
        REQUIRE(notify.messages.size() == 7);
        // Checking values in the queue
        REQUIRE(notify.messages[0].name == "/sys/path1/file.txt");
        REQUIRE((notify.messages[0].flags && inotify_flags::open));
        REQUIRE(notify.xsvc[0] == svc1.get());
        REQUIRE(notify.messages[1].name == "/sys/path1/file.txt");
        REQUIRE((notify.messages[1].flags && inotify_flags::open));
        REQUIRE(notify.xsvc[1] == svc2.get());
        REQUIRE(notify.messages[2].name == "/sys/path1/file1.txt");
        REQUIRE((notify.messages[2].flags && inotify_flags::open));
        REQUIRE(notify.xsvc[2] == svc1.get());
        REQUIRE(notify.messages[3].name == "/sys/path1/file1.txt");
        REQUIRE((notify.messages[3].flags && inotify_flags::open));
        REQUIRE(notify.xsvc[3] == svc2.get());
        REQUIRE(notify.messages[4].name == "/sys/path1/file.txt");
        REQUIRE((notify.messages[4].flags && inotify_flags::close_write));
        REQUIRE(notify.xsvc[4] == svc1.get());
        REQUIRE(notify.messages[5].name == "/sys/path1/file.txt");
        REQUIRE((notify.messages[5].flags && inotify_flags::close_write));
        REQUIRE(notify.xsvc[5] == svc2.get());
        REQUIRE(notify.messages[6].name == "/sys/path1/file1.txt");
        REQUIRE((notify.messages[6].flags && inotify_flags::close_nowrite));
        REQUIRE(notify.xsvc[6] == svc1.get());
    }
    SECTION("Different paths for services")
    {
        notifier_mock notify;
        auto svc1 = std::make_shared<sys::Service>();
        auto svc2 = std::make_shared<sys::Service>();
        notify.register_path("/sys/path1", svc1, inotify_flags::attrib);
        notify.register_path("/sys/path2", svc2, inotify_flags::attrib);
        notify.notify("/sys/path1/file.txt", inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 1);
        notify.notify("/sys/path1/file2.txt", inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 2);
        notify.notify("/sys/path3", inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 2);
        notify.notify("/sys/path2/", inotify_flags::attrib);
        REQUIRE(notify.messages.size() == 3);
        REQUIRE(notify.messages[0].name == "/sys/path1/file.txt");
        REQUIRE(notify.xsvc[0] == svc1.get());
        REQUIRE(notify.messages[1].name == "/sys/path1/file2.txt");
        REQUIRE(notify.xsvc[1] == svc1.get());
        REQUIRE(notify.messages[2].name == "/sys/path2");
        REQUIRE(notify.xsvc[2] == svc2.get());
    }
}