File: testbsafile.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (356 lines) | stat: -rw-r--r-- 12,119 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
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
#include "operators.hpp"

#include <components/bsa/compressedbsafile.hpp>
#include <components/files/memorystream.hpp>
#include <components/testing/util.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <format>
#include <fstream>
#include <memory>
#include <sstream>
#include <vector>

namespace Bsa
{
    namespace
    {
        using namespace ::testing;

        struct Free
        {
            void operator()(void* ptr) const { std::free(ptr); }
        };

        struct Buffer
        {
            std::unique_ptr<char, Free> mData;
            std::size_t mCapacity;
        };

        struct Header
        {
            uint32_t mFormat;
            uint32_t mDirSize;
            uint32_t mFileCount;
        };

        struct Archive
        {
            Header mHeader;
            std::vector<std::uint32_t> mOffsets;
            std::vector<char> mStringBuffer;
            std::vector<BSAFile::Hash> mHashes;
            std::size_t mTailSize;
        };

        struct TestBSAFile final : public BSAFile
        {
            void readHeader(std::istream& input) override { BSAFile::readHeader(input); }

            void writeHeader() override { throw std::logic_error("TestBSAFile::writeHeader is not implemented"); }
        };

        void writeArchive(const Archive& value, std::ostream& stream)
        {
            stream.write(reinterpret_cast<const char*>(&value.mHeader), sizeof(value.mHeader));

            if (!value.mOffsets.empty())
                stream.write(reinterpret_cast<const char*>(value.mOffsets.data()),
                    value.mOffsets.size() * sizeof(std::uint32_t));

            if (!value.mStringBuffer.empty())
                stream.write(reinterpret_cast<const char*>(value.mStringBuffer.data()), value.mStringBuffer.size());

            for (const BSAFile::Hash& hash : value.mHashes)
                stream.write(reinterpret_cast<const char*>(&hash), sizeof(BSAFile::Hash));

            const std::size_t chunkSize = 4096;
            std::vector<char> chunk(chunkSize);
            for (std::size_t i = 0; i < value.mTailSize; i += chunkSize)
                stream.write(reinterpret_cast<const char*>(chunk.data()), std::min(chunk.size(), value.mTailSize - i));
        }

        std::filesystem::path makeOutputPath()
        {
            const auto testInfo = UnitTest::GetInstance()->current_test_info();
            return TestingOpenMW::outputFilePath(
                std::format("{}.{}.bsa", testInfo->test_suite_name(), testInfo->name()));
        }

        Buffer makeBsaBuffer(std::uint32_t fileSize, std::uint32_t fileOffset)
        {
            std::ostringstream stream;

            const Header header{
                .mFormat = static_cast<std::uint32_t>(BsaVersion::Uncompressed),
                .mDirSize = 14,
                .mFileCount = 1,
            };

            const BSAFile::Hash hash{
                .mLow = 0xaaaabbbb,
                .mHigh = 0xccccdddd,
            };

            const Archive archive{
                .mHeader = header,
                .mOffsets = { fileSize, fileOffset, 0 },
                .mStringBuffer = { 'a', '\0' },
                .mHashes = { hash },
                .mTailSize = 0,
            };

            writeArchive(archive, stream);

            const std::string data = std::move(stream).str();

            const std::size_t capacity = static_cast<std::size_t>(fileSize) + static_cast<std::size_t>(fileOffset) + 34;
            std::unique_ptr<char, Free> buffer(reinterpret_cast<char*>(std::malloc(capacity)));

            if (buffer == nullptr)
                throw std::bad_alloc();

            std::memcpy(buffer.get(), data.data(), data.size());

            return Buffer{ .mData = std::move(buffer), .mCapacity = capacity };
        }

        TEST(BSAFileTest, shouldHandleEmpty)
        {
            const std::filesystem::path path = makeOutputPath();

            {
                std::ofstream stream;
                stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
                stream.open(path, std::ios::binary);
            }

            BSAFile file;
            EXPECT_THROW(file.open(path), std::runtime_error);

            EXPECT_THAT(file.getList(), IsEmpty());
        }

        TEST(BSAFileTest, shouldHandleZeroFiles)
        {
            const std::filesystem::path path = makeOutputPath();

            {
                std::ofstream stream;
                stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);

                stream.open(path, std::ios::binary);

                const Header header{
                    .mFormat = static_cast<std::uint32_t>(BsaVersion::Uncompressed),
                    .mDirSize = 0,
                    .mFileCount = 0,
                };

                const Archive archive{
                    .mHeader = header,
                    .mOffsets = {},
                    .mStringBuffer = {},
                    .mHashes = {},
                    .mTailSize = 0,
                };

                writeArchive(archive, stream);
            }

            BSAFile file;
            file.open(path);

            EXPECT_THAT(file.getList(), IsEmpty());
        }

        TEST(BSAFileTest, shouldHandleSingleFile)
        {
            const std::filesystem::path path = makeOutputPath();

            {
                std::ofstream stream;
                stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);

                stream.open(path, std::ios::binary);

                const Header header{
                    .mFormat = static_cast<std::uint32_t>(BsaVersion::Uncompressed),
                    .mDirSize = 14,
                    .mFileCount = 1,
                };

                const BSAFile::Hash hash{
                    .mLow = 0xaaaabbbb,
                    .mHigh = 0xccccdddd,
                };

                const Archive archive{
                    .mHeader = header,
                    .mOffsets = { 42, 0, 0 },
                    .mStringBuffer = { 'a', '\0' },
                    .mHashes = { hash },
                    .mTailSize = 42,
                };

                writeArchive(archive, stream);
            }

            BSAFile file;
            file.open(path);

            std::vector<char> namesBuffer = { 'a', '\0' };

            EXPECT_THAT(file.getList(),
                ElementsAre(BSAFile::FileStruct{
                    .mFileSize = 42,
                    .mOffset = 34,
                    .mHash = BSAFile::Hash{ .mLow = 0xaaaabbbb, .mHigh = 0xccccdddd },
                    .mNameOffset = 0,
                    .mNameSize = 1,
                    .mNamesBuffer = &namesBuffer,
                }));
        }

        TEST(BSAFileTest, shouldHandleTwoFiles)
        {
            const std::filesystem::path path = makeOutputPath();

            {
                std::ofstream stream;
                stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);

                stream.open(path, std::ios::binary);

                const std::uint32_t fileSize1 = 42;
                const std::uint32_t fileSize2 = 13;

                const Header header{
                    .mFormat = static_cast<std::uint32_t>(BsaVersion::Uncompressed),
                    .mDirSize = 28,
                    .mFileCount = 2,
                };

                const BSAFile::Hash hash1{
                    .mLow = 0xaaaabbbb,
                    .mHigh = 0xccccdddd,
                };

                const BSAFile::Hash hash2{
                    .mLow = 0x11112222,
                    .mHigh = 0x33334444,
                };

                const Archive archive{
                    .mHeader = header,
                    .mOffsets = { fileSize1, 0, fileSize2, fileSize1, 0, 2 },
                    .mStringBuffer = { 'a', '\0', 'b', '\0' },
                    .mHashes = { hash1, hash2 },
                    .mTailSize = fileSize1 + fileSize2,
                };

                writeArchive(archive, stream);
            }

            BSAFile file;
            file.open(path);

            std::vector<char> namesBuffer = { 'a', '\0', 'b', '\0' };

            EXPECT_THAT(file.getList(),
                ElementsAre(
                    BSAFile::FileStruct{
                        .mFileSize = 42,
                        .mOffset = 56,
                        .mHash = BSAFile::Hash{ .mLow = 0xaaaabbbb, .mHigh = 0xccccdddd },
                        .mNameOffset = 0,
                        .mNameSize = 1,
                        .mNamesBuffer = &namesBuffer,
                    },
                    BSAFile::FileStruct{
                        .mFileSize = 13,
                        .mOffset = 98,
                        .mHash = BSAFile::Hash{ .mLow = 0x11112222, .mHigh = 0x33334444 },
                        .mNameOffset = 2,
                        .mNameSize = 1,
                        .mNamesBuffer = &namesBuffer,
                    }));
        }

        TEST(BSAFileTest, shouldHandleSomewhatLargeFiles)
        {
            constexpr std::uint32_t maxUInt32 = std::numeric_limits<uint32_t>::max();
            constexpr std::uint32_t fileSize = maxUInt32 / 4;
            constexpr std::uint32_t fileOffset = maxUInt32 / 4 - 34;
            const Buffer buffer = makeBsaBuffer(fileSize, fileOffset);

            TestBSAFile file;
            // Use capacity assuming we never read beyond small header.
            Files::IMemStream stream(buffer.mData.get(), buffer.mCapacity);
            file.readHeader(stream);

            std::vector<char> namesBuffer = { 'a', '\0' };

            EXPECT_THAT(file.getList(),
                ElementsAre(BSAFile::FileStruct{
                    .mFileSize = maxUInt32 / 4,
                    .mOffset = maxUInt32 / 4,
                    .mHash = BSAFile::Hash{ .mLow = 0xaaaabbbb, .mHigh = 0xccccdddd },
                    .mNameOffset = 0,
                    .mNameSize = 1,
                    .mNamesBuffer = &namesBuffer,
                }));
        }

// std::streambuf in MSVC does not support buffers larger than 2**31 - 1:
// https://developercommunity.visualstudio.com/t/stdbasic-stringbuf-is-broken/290124
#ifndef _MSC_VER
        TEST(BSAFileTest, shouldHandleSingleFileAtTheEndOfLargeFile)
        {
            constexpr std::uint32_t maxUInt32 = std::numeric_limits<uint32_t>::max();
            constexpr std::uint32_t fileSize = maxUInt32;
            constexpr std::uint32_t fileOffset = maxUInt32 - 34;
            const Buffer buffer = makeBsaBuffer(fileSize, fileOffset);

            TestBSAFile file;
            // Use capacity assuming we never read beyond small header.
            Files::IMemStream stream(buffer.mData.get(), buffer.mCapacity);
            file.readHeader(stream);

            std::vector<char> namesBuffer = { 'a', '\0' };

            EXPECT_THAT(file.getList(),
                ElementsAre(BSAFile::FileStruct{
                    .mFileSize = maxUInt32,
                    .mOffset = maxUInt32,
                    .mHash = BSAFile::Hash{ .mLow = 0xaaaabbbb, .mHigh = 0xccccdddd },
                    .mNameOffset = 0,
                    .mNameSize = 1,
                    .mNamesBuffer = &namesBuffer,
                }));
        }

        TEST(BSAFileTest, shouldThrowExceptionOnTooBigAbsoluteOffset)
        {
            constexpr std::uint32_t maxUInt32 = std::numeric_limits<uint32_t>::max();
            constexpr std::uint32_t fileSize = maxUInt32;
            constexpr std::uint32_t fileOffset = maxUInt32 - 34 + 1;
            const Buffer buffer = makeBsaBuffer(fileSize, fileOffset);

            TestBSAFile file;
            // Use capacity assuming we never read beyond small header.
            Files::IMemStream stream(buffer.mData.get(), buffer.mCapacity);
            EXPECT_THROW(file.readHeader(stream), std::runtime_error);

            EXPECT_THAT(file.getList(), IsEmpty());
        }
#endif
    }
}