-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathunit_fat.cpp
More file actions
159 lines (140 loc) · 3.91 KB
/
unit_fat.cpp
File metadata and controls
159 lines (140 loc) · 3.91 KB
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
#include <common.cxx>
#include <fs/disk.hpp>
#include <fs/memdisk.hpp>
#include <util/sha1.hpp>
#include <unistd.h>
using namespace fs;
static MemDisk* mdisk = nullptr;
static Disk_ptr disk = nullptr;
CASE("Prepare custom memdisk")
{
const char* rootp(getenv("INCLUDEOS_SRC"));
std::string path="memdisk.fat";
if (access(path.c_str(),F_OK) == -1)
{
if (rootp == nullptr) path = "..";
else path = std::string(rootp) + "/test";
path += "/memdisk.fat";
}
auto* fp = fopen(path.c_str(), "rb");
EXPECT(fp != nullptr);
fseek(fp, 0L, SEEK_END);
long int size = ftell(fp);
rewind(fp);
// read file into buffer
char* buffer = new char[size];
EXPECT(buffer);
size_t res = fread(buffer, size, 1, fp);
EXPECT(res == 1);
// create memdisk
mdisk = new MemDisk(buffer, buffer + size);
EXPECT(mdisk);
}
CASE("Initialize FAT fs")
{
disk = std::make_shared<Disk> (*mdisk);
disk->init_fs(
[&lest_env] (auto err, File_system& fs)
{
EXPECT(!err);
EXPECT(fs.is_valid() == true);
// EXPECT(fs.name() == "FAT32"); // FIXME: test fails
Dirent dirent = fs.stat("/");
EXPECT(dirent.is_valid() == true);
});
}
CASE("List root directory")
{
auto& fs = disk->fs();
auto res = fs.ls("/");
// validate root directory
EXPECT(!res.error);
EXPECT(res.entries->size() == 8);
// validate each entry
auto& ents = *res.entries;
EXPECT(ents.at(0).name() == ".");
EXPECT(ents.at(1).name() == "..");
EXPECT(ents.at(2).name() == "folder");
EXPECT(ents.at(3).name() == "test.pem");
EXPECT(ents.at(4).name() == "test.der");
EXPECT(ents.at(5).name() == "build.sh");
EXPECT(ents.at(6).name() == "server.key");
EXPECT(ents.at(7).name() == "test.key");
}
CASE("Validate /folder")
{
auto& fs = disk->fs();
// validate folder sync
auto res = fs.ls("/folder/");
EXPECT(!res.error);
EXPECT(res.entries->size() == 3);
// validate each entry
auto& ents = *res.entries;
EXPECT(ents.at(0).name() == ".");
EXPECT(ents.at(1).name() == "..");
EXPECT(ents.at(2).name() == "file.txt");
// validate folder async
fs.ls("/folder",
[&lest_env] (auto error, Dirvec_ptr dirvec)
{
EXPECT(!error);
auto& ents = *dirvec;
EXPECT(ents.at(0).name() == ".");
EXPECT(ents.at(1).name() == "..");
EXPECT(ents.at(2).name() == "file.txt");
});
}
CASE("Validate /folder using dirent")
{
auto& fs = disk->fs();
// get dirent by using stat()
auto ent = fs.stat("/folder");
EXPECT(ent.is_valid());
EXPECT(ent.is_dir());
// validate folder sync using dirent
auto res = fs.ls(ent);
EXPECT(!res.error);
EXPECT(res.entries->size() == 3);
// validate each entry
auto& ents = *res.entries;
EXPECT(ents.at(0).name() == ".");
EXPECT(ents.at(1).name() == "..");
EXPECT(ents.at(2).name() == "file.txt");
// validate folder async using dirent
fs.ls(ent,
[&lest_env] (auto error, Dirvec_ptr dirvec)
{
EXPECT(!error);
auto& ents = *dirvec;
EXPECT(ents.at(0).name() == ".");
EXPECT(ents.at(1).name() == "..");
EXPECT(ents.at(2).name() == "file.txt");
});
}
CASE("Validate /folder/file.txt")
{
auto& fs = disk->fs();
// read and validate file
auto buffer = fs.read_file("/folder/file.txt");
EXPECT(buffer.size() == 24);
SHA1 sha1;
sha1.update(buffer.data(), buffer.size());
const std::string hash = sha1.as_hex();
EXPECT(hash == "de1d4db81aa9344377ccfd49f5a239533d4f4ee3");
const std::string text((const char*) buffer.data(), buffer.size());
EXPECT(text == "This file contains text\n");
}
CASE("Stat /folder/file.txt")
{
auto& fs = disk->fs();
// get dirent by stat()
auto ent = fs.stat("/folder/file.txt");
EXPECT(ent.is_valid());
EXPECT(ent.is_file());
EXPECT(ent.size() == 24);
// read and validate file using dirent
auto buffer = fs.read(ent, 0, ent.size());
EXPECT(buffer.size() == 24);
const std::string text((const char*) buffer.data(), buffer.size());
EXPECT(text == "This file contains text\n");
}