~aleteoryx/muditaos

ref: 0cea74888868b24f01bba4b916e754b04041ca98 muditaos/module-vfs/tests/unittest_disk_manager.cpp -rw-r--r-- 6.8 KiB
0cea7488 — Wiktor S. Ovalle Correa [EGD-5596] Fix fread() handling of EOF 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <purefs/blkdev/disk_manager.hpp>
#include <purefs/blkdev/disk_image.hpp>

namespace
{
    constexpr auto disk_image = "PurePhone.img";
}
TEST_CASE("Registering and unregistering device")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    REQUIRE(dm.register_device(disk, "emmc0") == -EEXIST);
    REQUIRE(dm.unregister_device("emmc124") == -ENOENT);
    const auto handle = dm.device_handle("emmc0");
    REQUIRE(handle);
    REQUIRE(dm.unregister_device("emmc0") == 0);
    const auto handle2 = dm.device_handle("emmc0");
    REQUIRE(handle2 == nullptr);
}

TEST_CASE("Parsing and checking partititons")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto parts = dm.partitions("emmc0");
    REQUIRE(parts.size() > 1);
    unsigned long prev_start = 0;
    int num{};
    for (const auto &part : parts) {
        REQUIRE(part.physical_number > 0);
        REQUIRE(part.start_sector >= 2048);
        REQUIRE(part.num_sectors > 0);
        REQUIRE(part.start_sector >= prev_start);
        REQUIRE(part.type > 0);
        REQUIRE(part.name == ("emmc0part" + std::to_string(num++)));
        prev_start = part.num_sectors + part.start_sector;
    }
}

TEST_CASE("RW boundary checking")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto parts = dm.partitions("emmc0");
    REQUIRE(parts.size() > 1);
    const auto &part1 = parts[0];
    // Read sector by disk name
    const auto sect_size = dm.get_info("emmc0", blkdev::info_type::sector_size);
    REQUIRE(sect_size > 0);
    std::vector<uint8_t> buf1(sect_size), buf2(sect_size);
    REQUIRE(dm.read("emmc0", buf1.data(), part1.start_sector, 1) == 0);
    REQUIRE(dm.read(part1.name, buf2.data(), 0, 1) == 0);
    REQUIRE(buf1 == buf2);
}

TEST_CASE("Null pointer passed to disk manager functions")
{
    using namespace purefs;
    blkdev::disk_manager dm;

    SECTION("Register device function")
    {
        std::shared_ptr<blkdev::disk> disk = nullptr;
        REQUIRE(dm.register_device(disk, "emmc0") == -EINVAL);
    }

    SECTION("Write function")
    {
        REQUIRE(dm.write(static_cast<blkdev::disk_fd>(nullptr),
                         nullptr,
                         static_cast<uint64_t>(0),
                         static_cast<std::size_t>(0)) == -EINVAL);
    }

    SECTION("Read function")
    {
        REQUIRE(dm.read(static_cast<blkdev::disk_fd>(nullptr),
                        nullptr,
                        static_cast<uint64_t>(0),
                        static_cast<std::size_t>(0)) == -EINVAL);
    }

    SECTION("Erase function")
    {
        REQUIRE(dm.erase(static_cast<blkdev::disk_fd>(nullptr), 0, 0) == -EINVAL);
    }

    SECTION("Sync function")
    {
        REQUIRE(dm.sync(static_cast<blkdev::disk_fd>(nullptr)) == -EINVAL);
    }

    SECTION("PM control function")
    {
        REQUIRE(dm.pm_control(static_cast<blkdev::disk_fd>(nullptr), blkdev::pm_state::power_off) == -EINVAL);
    }

    SECTION("Status function")
    {
        REQUIRE(dm.status(static_cast<blkdev::disk_fd>(nullptr)) == blkdev::media_status::error);
    }

    SECTION("Partitions function")
    {
        REQUIRE(dm.partitions(static_cast<blkdev::disk_fd>(nullptr)).empty());
    }

    SECTION("Get info function")
    {
        REQUIRE(dm.get_info(static_cast<blkdev::disk_fd>(nullptr), blkdev::info_type::sector_size) == 0);
    }

    SECTION("Reread partitions function")
    {
        REQUIRE(dm.reread_partitions(static_cast<blkdev::disk_fd>(nullptr)) == -EINVAL);
    }
}

TEST_CASE("Boundary checks for partitions")
{
    using namespace purefs;
    blkdev::disk_manager dm;

    SECTION("Register device function")
    {
        REQUIRE(dm.unregister_device("") == -ENOENT);
    }

    SECTION("Write function")
    {
        REQUIRE(dm.write("", nullptr, static_cast<uint64_t>(0), static_cast<std::size_t>(0)) == -ENOENT);
    }

    SECTION("Read function")
    {
        REQUIRE(dm.read("", nullptr, static_cast<uint64_t>(0), static_cast<std::size_t>(0)) == -ENOENT);
    }

    SECTION("Erase function")
    {
        REQUIRE(dm.erase("", 0, 0) == -ENOENT);
    }

    SECTION("Sync function")
    {
        REQUIRE(dm.sync("") == -ENOENT);
    }

    SECTION("PM control function")
    {
        REQUIRE(dm.pm_control("", blkdev::pm_state::power_off) == -ENOENT);
    }

    SECTION("Status function")
    {
        REQUIRE(dm.status("") == blkdev::media_status::error);
    }

    SECTION("Partitions function")
    {
        REQUIRE(dm.partitions("").empty());
    }

    SECTION("Get info function")
    {
        REQUIRE(dm.get_info("", blkdev::info_type::sector_size) == -ENOENT);
    }

    SECTION("Reread partitions function")
    {
        REQUIRE(dm.reread_partitions("") == -ENOENT);
    }
}

TEST_CASE("Disk sectors out of range for partition")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc1") == 0);
    const auto parts = dm.partitions("emmc1");
    REQUIRE(parts.size() > 1);
    const auto sectors   = dm.get_info("emmc1", blkdev::info_type::sector_count);
    const auto sect_size = dm.get_info("emmc1", blkdev::info_type::sector_size);

    SECTION("Read out of range ")
    {
        std::vector<uint8_t> buf(sect_size);
        SECTION("Pass lba bigger than sectors")
        {
            REQUIRE(dm.read("emmc1", buf.data(), sectors + 1, 1) == -ERANGE);
        }
        SECTION("Pass sum of lba and count bigger than sectors")
        {
            REQUIRE(dm.read("emmc1", buf.data(), sectors - 1, 2) == -ERANGE);
        }
    }

    SECTION("Write out of range")
    {
        std::vector<uint8_t> buf(sect_size);
        SECTION("Pass lba bigger than sectors")
        {
            REQUIRE(dm.write("emmc1", buf.data(), sectors + 1, 1) == -ERANGE);
        }
        SECTION("Pass sum of lba and count bigger than sectors")
        {
            REQUIRE(dm.write("emmc1", buf.data(), sectors - 1, 2) == -ERANGE);
        }
    }

    SECTION("Erase out of range")
    {
        SECTION("Pass lba bigger than sectors")
        {
            REQUIRE(dm.erase("emmc1", sectors + 1, 1) == -ERANGE);
        }
        SECTION("Pass sum of lba and count bigger than sectors")
        {
            REQUIRE(dm.erase("emmc1", sectors - 1, 2) == -ERANGE);
        }
    }
}