~aleteoryx/muditaos

ref: 7ce1d5d18ca65166b9420931cd98a48014a113c9 muditaos/module-platform/linux/tests/unittest_disk_manager.cpp -rw-r--r-- 11.0 KiB
7ce1d5d1 — Dawid Wojtas [BH-1619] Use default brightness 3 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
// 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/blkdev/disk_manager.hpp>

#include <platform/linux/DiskImage.hpp>

#include "test-setup.hpp"

#include <filesystem>
#include <fstream>

namespace
{
    constexpr auto part_disk_image     = "test_disk.img";
    constexpr auto part_disk_image_ext = "test_disk_ext.img";
    constexpr auto part_disk_image_bad = "test_disk_bad.img";
    constexpr auto eeprom_image        = "test_eeprom.bin";
} // namespace

TEST_CASE("Registering and unregistering device")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(::testing::vfs::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>(part_disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto parts = dm.partitions("emmc0");
    REQUIRE(parts.size() == 3);
    unsigned long prev_start = 0;
    int num{};
    for (const auto &part : parts) {
        REQUIRE(part.physical_number > 0);
        REQUIRE(part.start_sector > 0);
        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("Parsing and checking extended partititons")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(part_disk_image_ext);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto parts = dm.partitions("emmc0");

    REQUIRE(parts.size() == 7);
    unsigned long prev_start = 0;
    int num{};
    for (const auto &part : parts) {
        REQUIRE(part.physical_number > 0);
        REQUIRE(part.start_sector >= 1);
        REQUIRE(part.num_sectors > 0);
        REQUIRE(part.num_sectors < 0xFF);
        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("Parsing and checking invalid partititons")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(part_disk_image_bad);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto parts = dm.partitions("emmc0");

    REQUIRE(parts.size() == 0);
}

TEST_CASE("RW boundary checking")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(::testing::vfs::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("Alternative partitions in the disk manager")
{
    using namespace purefs;
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(::testing::vfs::disk_image);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "emmc0") == 0);
    const auto sect_size  = dm.get_info("emmc0", blkdev::info_type::sector_size);
    const auto sect_size1 = dm.get_info("emmc0sys1", blkdev::info_type::sector_size);
    REQUIRE(sect_size > 0);
    REQUIRE(sect_size == sect_size1);
    const auto sect_count = dm.get_info("emmc0sys1", blkdev::info_type::sector_count);
    REQUIRE(sect_count == (32 * 1024L * 1024L) / sect_size);
    const auto sect_count1 = dm.get_info("emmc0", blkdev::info_type::sector_count);
    REQUIRE(sect_count1 > sect_count);
    std::vector<char> buf1(sect_size, 0);
    REQUIRE(dm.read("emmc0", buf1.data(), 0, 1) == 0);
    std::vector<char> buf2(sect_size, 0xAA);
    REQUIRE(dm.write("emmc0sys1", buf2.data(), 0, 1) == 0);
    std::fill(std::begin(buf2), std::end(buf2), 0xBB);
    REQUIRE(dm.write("emmc0sys0", buf2.data(), 0, 1) == 0);
    REQUIRE(dm.read("emmc0", buf2.data(), 0, 1) == 0);
    REQUIRE(buf1 == buf2);
    std::fill(std::begin(buf2), std::end(buf2), 0xAA);
    REQUIRE(dm.read("emmc0sys1", buf1.data(), 0, 1) == 0);
    REQUIRE(buf1 == buf2);
    std::fill(std::begin(buf2), std::end(buf2), 0xBB);
    REQUIRE(dm.read("emmc0sys0", buf1.data(), 0, 1) == 0);
    REQUIRE(buf1 == buf2);
}

TEST_CASE("Disk manager EEPROM emulation")
{
    static constexpr auto eeprom_size        = 32768;
    static constexpr auto eeprom_sector_size = 64;

    using namespace purefs;
    std::ofstream ofc(eeprom_image);
    ofc.close();
    std::filesystem::resize_file(eeprom_image, eeprom_size);
    blkdev::disk_manager dm;
    auto disk = std::make_shared<blkdev::disk_image>(eeprom_image, eeprom_sector_size, 1);
    REQUIRE(disk);
    REQUIRE(dm.register_device(disk, "nvrom0", blkdev::flags::no_parts_scan) == 0);
    const auto sect_size  = dm.get_info("nvrom0", blkdev::info_type::sector_size);
    const auto sect_count = dm.get_info("nvrom0", blkdev::info_type::sector_count);
    REQUIRE(sect_size == eeprom_sector_size);
    REQUIRE(sect_count == eeprom_size / sect_size);
    std::vector<char> buf_in1(sect_size, 0xAA);
    std::vector<char> buf_in2(sect_size, 0xBB);
    REQUIRE(dm.write("nvrom0", buf_in1.data(), 0, 1) == 0);
    REQUIRE(dm.write("nvrom0", buf_in2.data(), sect_count - 1, 1) == 0);
    std::vector<char> buf_out1(sect_size);
    std::vector<char> buf_out2(sect_size);
    REQUIRE(dm.read("nvrom0", buf_out1.data(), 0, 1) == 0);
    REQUIRE(dm.read("nvrom0", buf_out2.data(), sect_count - 1, 1) == 0);
    REQUIRE(buf_in1 == buf_out1);
    REQUIRE(buf_in2 == buf_out2);
}

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>(::testing::vfs::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);
        }
    }
}