File: test_files.cpp

package info (click to toggle)
securefs 0.13.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 2,324 kB
  • sloc: cpp: 12,816; python: 764; sh: 13; makefile: 7
file content (93 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download
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
#include "catch.hpp"
#include "crypto.h"
#include "exceptions.h"
#include "file_table.h"
#include "files.h"

#include <algorithm>
#include <errno.h>
#include <set>
#include <string.h>
#include <vector>

static void test_file_table(unsigned max_padding_size)
{
    using namespace securefs;
    auto base_dir = OSService::temp_name("tmp/file_table", ".dir");
    OSService::get_default().ensure_directory(base_dir, 0755);

    key_type master_key(0x48);
    id_type null_id, file_id;
    generate_random(file_id.data(), file_id.size());
    const char* xattr_name = "com.apple.FinderInfo...";
    const securefs::PODArray<char, 32> xattr_value(0x11);

    {
        auto root = std::make_shared<OSService>(base_dir);
        ShardedFileTableImpl table(2, root, master_key, 0, 3000, 16, max_padding_size);
        auto dir = dynamic_cast<Directory*>(table.create_as(null_id, FileBase::DIRECTORY));
        DEFER(table.close(dir));

        FileLockGuard flg(*dir);
        table.create_as(file_id, FileBase::REGULAR_FILE);
        dir->add_entry(".", null_id, FileBase::DIRECTORY);
        dir->add_entry("..", null_id, FileBase::DIRECTORY);
        dir->add_entry("hello", file_id, FileBase::REGULAR_FILE);
        try
        {
            dir->setxattr(xattr_name, xattr_value.data(), xattr_value.size(), 0);
        }
        catch (const securefs::ExceptionBase& e)
        {
            REQUIRE(e.error_number() == ENOTSUP);
        }
    }

    {
        auto all_ids = find_all_ids(base_dir.c_str());
        REQUIRE(all_ids.size() == 2);
        REQUIRE(all_ids.find(null_id) != all_ids.end());
        REQUIRE(all_ids.find(file_id) != all_ids.end());
    }

    {
        auto root = std::make_shared<OSService>(base_dir);
        FileTableImpl table(2, root, master_key, 0, 3000, 16, max_padding_size);
        auto dir = dynamic_cast<Directory*>(table.open_as(null_id, FileBase::DIRECTORY));
        DEFER(table.close(dir));

        securefs::PODArray<char, 32> xattr_test_value(0);
        try
        {
            FileLockGuard flg(*dir);
            dir->getxattr(xattr_name, xattr_test_value.data(), xattr_test_value.size());
            REQUIRE(xattr_value == xattr_test_value);
        }
        catch (const securefs::ExceptionBase& e)
        {
            REQUIRE(e.error_number() == ENOTSUP);
        }

        std::set<std::string> filenames;
        FileLockGuard flg(*dir);
        dir->iterate_over_entries(
            [&](const std::string& fn, const id_type&, int)
            {
                filenames.insert(fn);
                return true;
            });
        REQUIRE((filenames == decltype(filenames){".", "..", "hello"}));
        id_type id;
        int type;
        dir->get_entry("hello", id, type);
        REQUIRE(memcmp(id.data(), file_id.data(), id.size()) == 0);
        bool is_regular_file = type == FileBase::REGULAR_FILE;
        REQUIRE(is_regular_file);
    }
}

TEST_CASE("File table")
{
    test_file_table(0);
    test_file_table(255);
}