commit
9044e4a9d4
5 files changed
+1215
No files matched your search
@@ -0,0 +1,205 @@
|
||||
#include <echoion/plugins/short_url/InternalStore.hpp>
|
||||
|
||||
#include <Void/Sqlite/Sqlite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
class TemporaryDirectory
|
||||
{
|
||||
public:
|
||||
|
||||
TemporaryDirectory()
|
||||
{
|
||||
std::random_device random;
|
||||
path_ =
|
||||
std::filesystem::temp_directory_path() /
|
||||
("echoion-shortener-internal-" +
|
||||
std::to_string(static_cast<unsigned long long>(random())));
|
||||
std::filesystem::create_directories(path_);
|
||||
}
|
||||
|
||||
~TemporaryDirectory()
|
||||
{
|
||||
std::error_code error;
|
||||
std::filesystem::remove_all(path_, error);
|
||||
}
|
||||
|
||||
TemporaryDirectory(const TemporaryDirectory&) = delete;
|
||||
TemporaryDirectory& operator=(const TemporaryDirectory&) = delete;
|
||||
|
||||
[[nodiscard]] const std::filesystem::path& path() const noexcept
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::filesystem::path path_;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::int64_t nowUnixSeconds()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
using Echoion::Plugins::ShortUrl::InternalConfig;
|
||||
using Echoion::Plugins::ShortUrl::InternalErrorCode;
|
||||
using Echoion::Plugins::ShortUrl::InternalStore;
|
||||
|
||||
int failures{};
|
||||
const auto check = [&failures](const bool condition,
|
||||
const std::string_view message) {
|
||||
if (!condition) {
|
||||
std::cerr << message << '\n';
|
||||
++failures;
|
||||
}
|
||||
};
|
||||
|
||||
TemporaryDirectory temporary;
|
||||
const auto databasePath = temporary.path() / "data" / "links.db";
|
||||
const InternalConfig config{
|
||||
.databasePath = databasePath,
|
||||
.publicBaseUrl = "https://short.example/s",
|
||||
};
|
||||
std::string persistentKey;
|
||||
{
|
||||
auto opened = InternalStore::open(config);
|
||||
check(opened.has_value(), "store did not open");
|
||||
if (!opened) {
|
||||
return 1;
|
||||
}
|
||||
auto& store = **opened;
|
||||
|
||||
const auto invalid = store.shorten("file:///etc/passwd");
|
||||
check(!invalid &&
|
||||
invalid.error().code == InternalErrorCode::invalidRequest,
|
||||
"non-HTTP URL was accepted");
|
||||
|
||||
auto first = store.shorten("https://example.com/a?x=1");
|
||||
auto duplicate = store.shorten("https://example.com/a?x=1");
|
||||
check(first.has_value() && duplicate.has_value(),
|
||||
"valid URL could not be shortened");
|
||||
if (first && duplicate) {
|
||||
persistentKey = first->key;
|
||||
check(first->key == duplicate->key,
|
||||
"identical URL did not deduplicate to the same key");
|
||||
check(first->key.size() == 10,
|
||||
"generated key does not contain exactly ten characters");
|
||||
}
|
||||
|
||||
std::vector<std::future<std::string>> concurrent;
|
||||
for (int index{}; index < 8; ++index) {
|
||||
concurrent.push_back(std::async(std::launch::async, [&store] {
|
||||
auto result = store.shorten("https://example.com/concurrent");
|
||||
return result ? result->key : std::string{};
|
||||
}));
|
||||
}
|
||||
std::string concurrentKey;
|
||||
for (auto& future : concurrent) {
|
||||
const auto key = future.get();
|
||||
check(!key.empty(), "concurrent shorten request failed");
|
||||
if (concurrentKey.empty()) {
|
||||
concurrentKey = key;
|
||||
}
|
||||
check(key == concurrentKey,
|
||||
"concurrent identical requests produced different keys");
|
||||
}
|
||||
|
||||
auto custom = store.shorten("https://example.com/custom", "AbC123");
|
||||
check(custom && custom->key == "AbC123", "custom key was not honored");
|
||||
auto conflict = store.shorten("https://example.com/other", "AbC123");
|
||||
check(!conflict && conflict.error().code == InternalErrorCode::conflict,
|
||||
"custom-key collision was not reported as a conflict");
|
||||
|
||||
if (first) {
|
||||
auto resolved = store.resolve(first->key);
|
||||
check(resolved &&
|
||||
resolved->targetUrl == "https://example.com/a?x=1",
|
||||
"short link did not resolve to its target");
|
||||
}
|
||||
auto statistics = store.stats();
|
||||
check(statistics && statistics->hits == 1,
|
||||
"resolve did not update hit statistics");
|
||||
|
||||
if (custom) {
|
||||
auto revoked = store.revoke(custom->key);
|
||||
check(revoked.has_value(), "short link could not be revoked");
|
||||
auto hidden = store.resolve(custom->key);
|
||||
check(!hidden && hidden.error().code == InternalErrorCode::notFound,
|
||||
"revoked short link remained resolvable");
|
||||
}
|
||||
|
||||
auto expiring = store.shorten("https://example.com/expiring", "Expire1",
|
||||
nowUnixSeconds() + 3600);
|
||||
check(expiring.has_value(), "expiring short link could not be created");
|
||||
if (expiring) {
|
||||
auto database = Void::Sqlite::Database::open(databasePath);
|
||||
check(database.has_value(),
|
||||
"test could not reopen SQLite database");
|
||||
if (database) {
|
||||
auto update = database->prepare(
|
||||
"UPDATE short_links SET expires_at = ? WHERE key = ?");
|
||||
check(update.has_value(),
|
||||
"test could not prepare expiry update");
|
||||
if (update) {
|
||||
update->bind(1, nowUnixSeconds() - 1)
|
||||
.bind(2, expiring->key);
|
||||
check(update->step().has_value(),
|
||||
"test could not force link expiration");
|
||||
}
|
||||
}
|
||||
auto expired = store.resolve(expiring->key);
|
||||
check(!expired &&
|
||||
expired.error().code == InternalErrorCode::expired,
|
||||
"expired short link did not report expiry");
|
||||
}
|
||||
|
||||
auto cleaned = store.cleanup(true);
|
||||
check(cleaned && *cleaned >= 2,
|
||||
"cleanup did not remove expired and revoked links");
|
||||
}
|
||||
|
||||
{
|
||||
auto resolverOnly = InternalStore::open({
|
||||
.databasePath = databasePath,
|
||||
.publicBaseUrl = {},
|
||||
});
|
||||
check(resolverOnly.has_value(),
|
||||
"resolver-only store did not open without a public base URL");
|
||||
if (resolverOnly) {
|
||||
auto resolved = (*resolverOnly)->resolve(persistentKey);
|
||||
check(resolved &&
|
||||
resolved->targetUrl == "https://example.com/a?x=1",
|
||||
"resolver-only store could not read an existing link");
|
||||
auto unavailable =
|
||||
(*resolverOnly)->shorten("https://example.com/new");
|
||||
check(!unavailable && unavailable.error().code ==
|
||||
InternalErrorCode::unavailable,
|
||||
"resolver-only store unexpectedly created a short link");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
auto reopened = InternalStore::open(config);
|
||||
check(reopened.has_value(), "persistent store did not reopen");
|
||||
if (reopened) {
|
||||
auto persisted = (*reopened)->shorten("https://example.com/a?x=1");
|
||||
check(persisted.has_value(), "persisted link could not be loaded");
|
||||
}
|
||||
}
|
||||
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user