-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_BinaryFile.cpp
365 lines (335 loc) · 12.7 KB
/
test_BinaryFile.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Created by nbdy on 14.01.22.
//
#include <gtest/gtest.h>
#include "FileUtils.h"
#include "binfmt.h"
#include "test_common.h"
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, initializeNonExistingFile) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, initializeExistingFile) {
system("touch /tmp/test.bin");
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, appendEntry) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
EXPECT_EQ(file.append(TestBinaryEntry{1}), binfmt::ErrorCode::OK);
EXPECT_EQ(file.getOffset(), 1);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, appendContainer) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
EXPECT_EQ(file.append(TestBinaryEntryContainer{TestBinaryEntry{1}}), binfmt::ErrorCode::OK);
EXPECT_EQ(file.getOffset(), 1);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, appendEntries) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
EXPECT_EQ(file.append(
{TestBinaryEntry{1}, TestBinaryEntry{2}, TestBinaryEntry{3}}), binfmt::ErrorCode::OK);
EXPECT_EQ(file.getOffset(), 3);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, appendContainers) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
std::vector<TestBinaryEntryContainer> containers = {TestBinaryEntryContainer{TestBinaryEntry{1}},
TestBinaryEntryContainer{TestBinaryEntry{2}},
TestBinaryEntryContainer{TestBinaryEntry{3}}};
EXPECT_EQ(file.append(containers), binfmt::ErrorCode::OK);
EXPECT_EQ(file.getOffset(), 3);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, removeEntryAtEnd) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
std::vector<TestBinaryEntry> entries = {TestBinaryEntry{1}, TestBinaryEntry{2}, TestBinaryEntry{3}};
EXPECT_EQ(file.append(entries), binfmt::ErrorCode::OK);
auto fileSize = file.getFileSize();
EXPECT_EQ(file.getOffset(), 3);
EXPECT_TRUE(file.removeEntryAtEnd());
EXPECT_EQ(file.getOffset(), 2);
EXPECT_LT(file.getFileSize(), fileSize);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, rollover10Entries) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader(0xABC, 0, 10));
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
// Check if our max entries are 10
EXPECT_EQ(file.getHeader().maxEntries, 10);
// Append 10 entries
std::vector<TestBinaryEntry> entries = {
TestBinaryEntry{1}, TestBinaryEntry{2}, TestBinaryEntry{3},
TestBinaryEntry{4}, TestBinaryEntry{5}, TestBinaryEntry{6},
TestBinaryEntry{7}, TestBinaryEntry{8}, TestBinaryEntry{9},
TestBinaryEntry{10}};
EXPECT_EQ(file.append(entries), binfmt::ErrorCode::OK);
// Check if we have 10 entries
EXPECT_EQ(file.getHeader().count, 10);
// the offset should be 0, since we rolled over with the tenth entry
EXPECT_EQ(file.getOffset(), 0);
// Append another entry to first position (rollover)
auto eleventhEntry = TestBinaryEntry{11};
TestBinaryEntryContainer eleventhEntryContainer{eleventhEntry};
EXPECT_EQ(file.append(eleventhEntry), binfmt::ErrorCode::OK);
// Check if our count is 11
EXPECT_EQ(file.getHeader().count, 11);
// Our offset should be 1 now
EXPECT_EQ(file.getOffset(), 1);
// Check if the contents are equal
TestBinaryEntryContainer readEleventhEntryContainer{};
EXPECT_TRUE(file.getEntry(0, readEleventhEntryContainer));
EXPECT_EQ(readEleventhEntryContainer.checksum,
eleventhEntryContainer.checksum);
// Add 10 more entries to trigger a rollover with a list
std::vector<TestBinaryEntry> entries2 = {
TestBinaryEntry{12}, TestBinaryEntry{13}, TestBinaryEntry{14},
TestBinaryEntry{15}, TestBinaryEntry{16}, TestBinaryEntry{17},
TestBinaryEntry{18}, TestBinaryEntry{19}, TestBinaryEntry{20},
TestBinaryEntry{21}};
// We should end up at 1 again, since we will roll over
EXPECT_EQ(file.append(entries2), binfmt::ErrorCode::OK);
// The count should be 21
EXPECT_EQ(file.getHeader().count, 21);
// Our offset should be 1 again
EXPECT_EQ(file.getOffset(), 1);
// Check if the contents are equal
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, getEntriesFromTo) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
std::vector<TestBinaryEntryContainer> cache {};
for (uint32_t i = 0; i < 2000; i++) {
cache.emplace_back(TestBinaryEntry{i});
EXPECT_EQ(file.append(cache[i]), binfmt::ErrorCode::OK);
}
std::vector<TestBinaryEntryContainer> entries;
EXPECT_TRUE(file.getEntriesFromTo(entries, 0, 1000));
for (uint32_t i = 0; i < 1000; i++) {
EXPECT_EQ(entries[i].checksum, cache[i].checksum);
}
EXPECT_EQ(entries.size(), 1000);
EXPECT_TRUE(file.deleteFile());
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(binfmt, getEntriesChunked) {
TestBinaryFile file("/tmp/test.bin", TestBinaryHeader{});
EXPECT_EQ(file.getErrorCode(), binfmt::ErrorCode::OK);
std::vector<TestBinaryEntryContainer> cache;
for (uint32_t i = 0; i < 2000; i++) {
cache.emplace_back(TestBinaryEntry{i});
EXPECT_EQ(file.append(cache[i]), binfmt::ErrorCode::OK);
}
std::vector<TestBinaryEntryContainer> entries;
file.getEntriesChunked(
[&entries](const std::vector<TestBinaryEntryContainer> &i_Entries) {
for (auto &e : i_Entries) {
entries.emplace_back(e);
}
},
0, 1000, 100);
EXPECT_EQ(entries.size(), 1000);
for (uint32_t i = 0; i < 1000; i++) {
EXPECT_EQ(entries[i].checksum, cache[i].checksum);
}
EXPECT_EQ(entries.size(), 1000);
EXPECT_TRUE(file.deleteFile());
}
// ----------------------- BinaryFile tests
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testDeleteFile) {
auto t = getRandomTestFile();
EXPECT_EQ(t.getHeaderSize(), sizeof(TestBinaryHeader));
EXPECT_EQ(t.getEntrySize(), sizeof(TestBinaryEntry));
EXPECT_EQ(t.getContainerSize(), sizeof(TestBinaryEntryContainer));
EXPECT_EQ(t.getHeader().magic, TestBinaryHeader{}.magic);
EXPECT_EQ(t.getErrorCode(), binfmt::ErrorCode::OK);
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testClear) {
auto t = getRandomTestFile();
int a = appendRandomAmountOfEntries(t);
EXPECT_GT(a, 0);
EXPECT_EQ(t.getEntryCount(), a);
EXPECT_TRUE(t.clear());
EXPECT_TRUE(t.isEmpty());
EXPECT_EQ(t.getEntryCount(), 0);
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testGetFileSize) {
auto t = getRandomTestFile();
EXPECT_EQ(t.getErrorCode(), binfmt::ErrorCode::OK);
auto a = appendRandomAmountOfEntries(t);
auto s = binfmt::FileUtils::GetFileSize<TestBinaryHeader,
TestBinaryEntryContainer>(a);
auto se = binfmt::FileUtils::GetFileSize<TestBinaryHeader,
TestBinaryEntryContainer>(0);
EXPECT_EQ(t.getFileSize(), s);
t.clear();
EXPECT_EQ(t.getFileSize(), se);
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testGetAllEntries) {
auto t = getRandomTestFile();
auto ae = appendRandomAmountOfEntriesV(t);
std::vector<TestBinaryEntryContainer> entries(ae.size());
auto ok = t.getAllEntries(entries);
EXPECT_TRUE(ok);
for (int i = 0; i < ae.size(); i++) {
auto v = entries[i];
EXPECT_TRUE(v.isEntryValid());
auto o = ae[i];
EXPECT_TRUE(o.isEntryValid());
auto eq = v.checksum == o.checksum;
EXPECT_TRUE(eq);
}
EXPECT_EQ(entries.size(), ae.size());
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testGetEntriesFrom) {
auto t = getRandomTestFile();
auto ae = appendExactAmountOfEntriesV(t, 20);
EXPECT_EQ(ae.size(), 20);
std::vector<TestBinaryEntryContainer> entries;
EXPECT_TRUE(t.getEntriesFrom(
entries, 5, 10)); // here is where the 5 comes from, also we take the next
// 10 entries and only the next 10 entries
EXPECT_EQ(entries.size(), 10); // yep, those are actually just 10 entries
auto entry = entries.at(6); // 5 + 6 = 11
auto o = ae.at(11); // 11 = 5 + 6
EXPECT_EQ(entry.checksum, o.checksum); // also their contents are equal
cleanupTestFile(t);
}
/*
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testRemoveEntryAt) {
auto t = getRandomTestFile();
auto ae = appendExactAmountOfEntriesV(t, TEST_MAX_ENTRIES);
EXPECT_EQ(ae.size(), TEST_MAX_ENTRIES);
EXPECT_TRUE(t.removeEntryAt(42));
auto ec = t.getEntryCount();
EXPECT_EQ(ec, TEST_MAX_ENTRIES - 1);
auto t0 = TestBinaryEntryContainer(TestBinaryEntry{-1337, 13.37});
auto ok = t.setEntryAt(t0, 50);
EXPECT_TRUE(ok);
TestBinaryEntryContainer t1{};
TestBinaryEntry te1{};
ok = t.getEntryAt(t1, 50);
EXPECT_TRUE(ok);
ok = t.getEntryAt(te1, 50);
EXPECT_TRUE(ok);
EXPECT_EQ(t0.checksum, t1.checksum);
TestBinaryEntryContainer tbec(te1);
EXPECT_EQ(tbec.checksum, t0.checksum);
cleanupTestFile(t);
}
*/
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testNoHeader) {
static std::string filePath("/tmp/binfmt_no_hdr_test.bin");
static std::string touchCmd("touch ");
static std::string cmd(touchCmd + filePath);
std::ofstream o(filePath, std::ios::binary | std::ios::out);
o.write(" ", 1);
o.close();
TestBinaryFile f(filePath, TestBinaryHeader{});
EXPECT_EQ(f.getErrorCode(), binfmt::ErrorCode::OK);
cleanupTestFile(f);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testAppend) {
auto t = getRandomTestFile();
t.append(generateRandomTestEntryContainer());
t.append(generateRandomTestEntry());
EXPECT_EQ(t.getEntryCount(), 2);
std::vector<TestBinaryEntry> entries;
entries.push_back(generateRandomTestEntry());
entries.push_back(generateRandomTestEntry());
t.append(entries);
EXPECT_EQ(t.getEntryCount(), 4);
appendExactAmountOfEntriesV(t, 100 - 4);
EXPECT_EQ(t.getEntryCount(), 100);
entries.clear();
entries.push_back(generateRandomTestEntry());
entries.push_back(generateRandomTestEntry());
t.append(entries);
auto tc = t.getEntryCount();
EXPECT_EQ(tc, 102);
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(EntryLimitedBinaryFile, testRollover) {
auto t = getRandomTestEntryLimitedFile();
// Can it actually store the desired amount of entries?
EXPECT_EQ(t.getHeader().maxEntries, TEST_MAX_ENTRIES);
// Append some entries
auto ae = appendExactAmountOfEntriesV(t, t.getHeader().maxEntries - 1);
// Did the right amount of entries get appended?
EXPECT_EQ(t.getOffset(), t.getHeader().maxEntries - 1);
// Append a few more entries
auto ee = appendExactAmountOfEntriesV(t, 43);
// Has the file rolled over?
EXPECT_EQ(t.getOffset(), 42);
// Are the contents of the appended entries correct?
std::vector<TestBinaryEntryContainer> entries;
EXPECT_TRUE(t.getEntriesFrom(entries, 0, 42));
// Are the contents of the appended entries correct?
for (uint32_t idx = 1; idx < ee.size(); idx++) {
EXPECT_EQ(ee[idx].checksum, entries[idx - 1].checksum);
}
// Cleanup
cleanupTestFile(t);
}
// NOLINTNEXTLINE(cert-err58-cpp)
TEST(BinaryFile, testReopen) {
{
TestBinaryFile t = getRandomTestEntryLimitedFile();
t.append(generateRandomTestEntryContainer());
EXPECT_EQ(t.getOffset(), 1);
}
{
TestBinaryFile t = getRandomTestEntryLimitedFile();
t.append(generateRandomTestEntryContainer());
EXPECT_EQ(t.getOffset(), 2);
}
{
TestBinaryFile t = getRandomTestEntryLimitedFile();
std::vector<TestBinaryEntryContainer> entries;
entries.resize(TEST_MAX_ENTRIES - 3);
std::fill(entries.begin(), entries.end(), generateRandomTestEntryContainer());
EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK);
EXPECT_EQ(t.getOffset(), TEST_MAX_ENTRIES - 1);
}
{
TestBinaryFile t = getRandomTestEntryLimitedFile();
std::vector<TestBinaryEntryContainer> entries;
entries.resize(20);
std::fill(entries.begin(), entries.end(), generateRandomTestEntryContainer());
EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK);
EXPECT_EQ(t.getOffset(), 19);
EXPECT_EQ(t.getEntryCount(), 2019);
}
getRandomTestFile().deleteFile();
}