-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.cpp
More file actions
225 lines (188 loc) · 8.02 KB
/
Copy pathunit.cpp
File metadata and controls
225 lines (188 loc) · 8.02 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
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
/*
* Copyright (c) 2025 Maxtek Consulting
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <hyperpage.hpp>
#include <maxtest.hpp>
#include <filesystem>
class test_page : public hyperpage::page
{
public:
test_page(const std::string &path, const std::string &mime_type, const std::string &content) :
_path(path), _mime_type(mime_type), _content(content)
{
}
const std::string &get_path() const override
{
return _path;
}
const std::string &get_mime_type() const override
{
return _mime_type;
}
const uint8_t *get_content() const override
{
return reinterpret_cast<const uint8_t *>(_content.data());
}
size_t get_length() const override
{
return _content.size();
}
private:
std::string _path;
std::string _mime_type;
std::string _content;
};
static bool match_buffers(const uint8_t *a, size_t a_len, const uint8_t *b, size_t b_len)
{
bool result(false);
if(a_len == b_len)
{
result = true;
int offset = 0;
while(result && offset < a_len)
{
if(a[offset] != b[offset])
{
result = false;
}
offset++;
}
}
return result;
}
MAXTEST_MAIN
{
MAXTEST_TEST_CASE(store_load)
{
std::filesystem::path db_path = std::filesystem::path(args[0]) / "hyperpage_test.db";
hyperpage::writer writer(db_path.string());
test_page page("/test1.html", "text/html", "<html><body>Test 1</body></html>");
writer.store(page);
hyperpage::reader reader(db_path.string());
auto loaded_page = reader.load("/test1.html");
MAXTEST_ASSERT(loaded_page != nullptr);
MAXTEST_ASSERT(loaded_page->get_path() == page.get_path());
MAXTEST_ASSERT(loaded_page->get_mime_type() == page.get_mime_type());
MAXTEST_ASSERT(loaded_page->get_length() == page.get_length());
MAXTEST_ASSERT(match_buffers(loaded_page->get_content(), loaded_page->get_length(),
page.get_content(), page.get_length()));
loaded_page = reader.load("/test2.html");
MAXTEST_ASSERT(loaded_page == nullptr);
};
MAXTEST_TEST_CASE(open_database)
{
std::filesystem::path valid_path = std::filesystem::path(args[0]) / "hyperpage_test.db";
std::filesystem::path invalid_path = std::filesystem::path(args[0]);
std::unique_ptr<hyperpage::reader> reader;
std::unique_ptr<hyperpage::writer> writer;
bool exception_thrown = false;
reader.reset(new hyperpage::reader(valid_path.string()));
MAXTEST_ASSERT(reader != nullptr);
writer.reset(new hyperpage::writer(valid_path.string()));
MAXTEST_ASSERT(writer != nullptr);
try
{
reader.reset(new hyperpage::reader(invalid_path.string()));
}
catch(...)
{
exception_thrown = true;
}
MAXTEST_ASSERT(exception_thrown);
exception_thrown = false;
try
{
writer.reset(new hyperpage::writer(invalid_path.string()));
}
catch(...)
{
exception_thrown = true;
}
MAXTEST_ASSERT(exception_thrown);
};
MAXTEST_TEST_CASE(mime_type)
{
const std::string json_path = "test.json";
const std::string json_mime_type = "application/json";
auto file_type = hyperpage::mime_type(json_path);
MAXTEST_ASSERT(file_type == json_mime_type);
};
MAXTEST_TEST_CASE(overwrite_test)
{
std::filesystem::path db_path = std::filesystem::path(args[0]) / "hyperpage_overwrite_test.db";
// Remove any existing database file to start fresh
if (std::filesystem::exists(db_path)) {
std::filesystem::remove(db_path);
}
hyperpage::writer writer(db_path.string());
// Create a page and store it
test_page original_page("/index.html", "text/html", "<html><body>Original Content</body></html>");
writer.store(original_page);
// Now overwrite with updated content using the same writer
test_page updated_page("/index.html", "text/html", "<html><body>Updated Content</body></html>");
writer.store(updated_page);
// Verify updated content is actually stored
hyperpage::reader reader(db_path.string());
auto loaded_page = reader.load("/index.html");
MAXTEST_ASSERT(loaded_page != nullptr);
MAXTEST_ASSERT(loaded_page->get_path() == "/index.html");
MAXTEST_ASSERT(loaded_page->get_mime_type() == "text/html");
// Check updated content - this is where the issue should manifest
std::string updated_content = "<html><body>Updated Content</body></html>";
MAXTEST_ASSERT(loaded_page->get_length() == updated_content.size());
MAXTEST_ASSERT(match_buffers(loaded_page->get_content(), loaded_page->get_length(),
reinterpret_cast<const uint8_t*>(updated_content.data()), updated_content.size()));
};
MAXTEST_TEST_CASE(archive_size_no_growth_test)
{
std::filesystem::path db_path = std::filesystem::path(args[0]) / "hyperpage_size_test.db";
// Remove any existing database file to start fresh
if (std::filesystem::exists(db_path)) {
std::filesystem::remove(db_path);
}
// Create initial content
test_page test_page_content("/test.html", "text/html", "<html><body>Test Content</body></html>");
// Store initial content and record database size
{
hyperpage::writer writer(db_path.string());
writer.store(test_page_content);
}
size_t initial_size = std::filesystem::file_size(db_path);
// Overwrite with identical content multiple times
for (int i = 0; i < 5; ++i) {
hyperpage::writer writer(db_path.string());
writer.store(test_page_content); // Same content each time
}
size_t final_size = std::filesystem::file_size(db_path);
// The database size should not grow significantly when overwriting with identical content
// Allow for some variance due to SQLite overhead, but it shouldn't grow substantially
MAXTEST_ASSERT(final_size <= initial_size * 1.1); // Max 10% growth tolerance
// Verify content is still correct
hyperpage::reader reader(db_path.string());
auto loaded_page = reader.load("/test.html");
MAXTEST_ASSERT(loaded_page != nullptr);
MAXTEST_ASSERT(loaded_page->get_path() == "/test.html");
std::string expected_content = "<html><body>Test Content</body></html>";
MAXTEST_ASSERT(loaded_page->get_length() == expected_content.size());
MAXTEST_ASSERT(match_buffers(loaded_page->get_content(), loaded_page->get_length(),
reinterpret_cast<const uint8_t*>(expected_content.data()), expected_content.size()));
};
}