navid/veins / ac26a51bd7b5f4a22beb0ca203a433857c68b337b79aa5ce14683290ab9ae3a7 / xveins/tests/unit/core_tests.cpp
core_tests.cpp
← xveins/tests/unit · 12422 bytes · raw · download
#include "test_support.h"
#include "core/common.h"
#include "unit/unit_helpers.h"
#include <cstring>
#include <string>
#include <vector>
namespace vn::unit {
namespace {
void test_varint(test::Context& t) {
const std::vector<std::uint64_t> values = {
0, 1, 2, 127, 128, 255, 256, 300, 16384, 1'000'000, 0xffffffffull,
};
for (std::uint64_t value : values) {
ByteWriter writer;
writer.varint(value);
Reader reader(writer.view());
const auto decoded = reader.varint();
t.expect(decoded.has_value());
if (decoded)
t.expect_eq(*decoded, value);
t.expect(reader.finish().has_value());
}
ByteWriter max_writer;
max_writer.varint(0xffffffffffffffffull);
Reader max_reader(max_writer.view());
const auto max_value = max_reader.varint();
t.expect(max_value.has_value());
if (max_value)
t.expect_eq(*max_value, 0xffffffffffffffffull);
const std::uint8_t non_minimal[] = {0x80, 0x00};
Reader reader(Bytes(non_minimal, sizeof(non_minimal)));
const auto decoded = reader.varint();
t.expect(!decoded.has_value());
if (!decoded)
t.expect(decoded.error().kind == ErrorKind::NonMinimalVarint);
const std::uint8_t truncated[] = {0x80};
Reader truncated_reader(Bytes(truncated, sizeof(truncated)));
t.expect(!truncated_reader.varint().has_value());
const std::uint8_t overflow[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02};
Reader overflow_reader(Bytes(overflow, sizeof(overflow)));
const auto overflowing = overflow_reader.varint();
t.expect(!overflowing.has_value());
if (!overflowing)
t.expect(overflowing.error().kind == ErrorKind::VarintOverflow);
}
void test_reader_integers(test::Context& t) {
const std::uint8_t one[] = {0x7f};
const std::uint8_t two[] = {0x34, 0x12};
const std::uint8_t four[] = {0x78, 0x56, 0x34, 0x12};
const std::uint8_t eight[] = {0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01};
Reader r1(Bytes(one, 1));
t.expect_eq(*r1.u8(), 0x7f);
t.expect(r1.finish().has_value());
Reader r2(Bytes(two, 2));
t.expect_eq(*r2.u16le(), 0x1234);
t.expect(r2.finish().has_value());
Reader r4(Bytes(four, 4));
t.expect_eq(*r4.u32le(), 0x12345678u);
t.expect(r4.finish().has_value());
Reader r8(Bytes(eight, 8));
t.expect_eq(*r8.u64le(), 0x0123456789abcdefull);
t.expect(r8.finish().has_value());
Reader negative(Bytes(two, 2));
t.expect_eq(*negative.i16le(), static_cast<std::int16_t>(0x1234));
for (std::size_t length = 0; length < 8; ++length) {
Reader cut(Bytes(eight, length));
t.expect(!cut.u64le().has_value());
}
Reader missing(Bytes{});
t.expect(!missing.u8().has_value());
t.expect(!missing.u16le().has_value());
t.expect(!missing.u32le().has_value());
t.expect(!missing.u64le().has_value());
t.expect(!missing.i16le().has_value());
t.expect(!missing.i64le().has_value());
t.expect(!missing.id().has_value());
t.expect(!missing.opt_id().has_value());
t.expect(missing.finish().has_value());
const std::uint8_t none_tag[] = {0};
Reader none_reader(Bytes(none_tag, 1));
const auto none = none_reader.opt_id();
t.expect(none.has_value());
if (none)
t.expect(!none->has_value());
const std::uint8_t bad_tag[] = {2};
Reader tagged(Bytes(bad_tag, 1));
t.expect(!tagged.opt_id().has_value());
Reader read_u64(Bytes(eight, 8));
t.expect_eq(Reader::read_u64le_at(Bytes(eight, 8), 0), 0x0123456789abcdefull);
t.expect_eq(Reader::read_u64le_at(Bytes(eight, 8), 1), std::uint64_t{0});
t.expect_eq(Reader::read_u64le_at(Bytes(eight, 4), 0), std::uint64_t{0});
t.expect_eq(Reader::read_u64le_at(Bytes(eight, 8), 99), std::uint64_t{0});
}
void test_reader_sized_values(test::Context& t) {
ByteWriter writer;
writer.u8(1);
writer.u16le(0x2345);
writer.u32le(0x3456789a);
writer.u64le(0x456789abcdef0123ull);
writer.varint(300);
writer.length_prefixed("payload");
writer.id(make_id(7));
const auto data = writer.take();
Reader full(bytes_of(data));
t.expect_eq(*full.u8(), 1);
t.expect_eq(*full.u16le(), 0x2345);
t.expect_eq(*full.u32le(), 0x3456789au);
t.expect_eq(*full.u64le(), 0x456789abcdef0123ull);
t.expect_eq(*full.varint(), 300u);
t.expect_eq(to_string(*full.bytes(64)), "payload");
t.expect_eq(std::memcmp(full.id()->data(), make_id(7).data(), ID_LEN), 0);
t.expect(full.finish().has_value());
for (std::size_t length = 0; length < data.size(); ++length) {
Reader cut(bytes_of(data).first(length));
(void)cut.u8();
(void)cut.u16le();
(void)cut.u32le();
(void)cut.u64le();
(void)cut.varint();
(void)cut.bytes(64);
(void)cut.id();
}
ByteWriter too_long;
too_long.varint(9);
too_long.raw("short");
Reader limited(too_long.view());
const auto limited_value = limited.bytes(8);
t.expect(!limited_value.has_value());
if (!limited_value)
t.expect(limited_value.error().kind == ErrorKind::LimitExceeded);
ByteWriter short_body;
short_body.varint(9);
short_body.raw("short");
Reader truncated(short_body.view());
const auto truncated_value = truncated.bytes(16);
t.expect(!truncated_value.has_value());
if (!truncated_value)
t.expect(truncated_value.error().kind == ErrorKind::UnexpectedEnd);
ByteWriter list;
list.varint(1000);
Reader list_reader(list.view());
t.expect(!list_reader.list_len(1'000'000).has_value());
Reader raw_empty(Bytes{});
t.expect(raw_empty.raw(0).has_value());
t.expect(!raw_empty.raw(1).has_value());
}
void test_reader_sig_and_time(test::Context& t) {
ByteWriter writer;
writer.sig(Reader::Sig{"Name", "mail@example.com", -1234, -90});
writer.time_ms(Reader::TimeMs{1'700'000'000'123, 120});
const auto data = writer.take();
Reader reader(bytes_of(data));
const auto sig = reader.sig(MAX_NAME_LEN, MAX_NAME_LEN);
t.expect(sig.has_value());
if (sig) {
t.expect_eq(sig->name, "Name");
t.expect_eq(sig->email, "mail@example.com");
t.expect_eq(sig->seconds, -1234);
t.expect_eq(sig->tz_min, -90);
}
const auto time = reader.time_ms();
t.expect(time.has_value());
if (time) {
t.expect_eq(time->ms, 1'700'000'000'123);
t.expect_eq(time->tz_min, 120);
}
t.expect(reader.finish().has_value());
for (std::size_t length = 0; length < data.size(); ++length) {
Reader cut(bytes_of(data).first(length));
(void)cut.sig(MAX_NAME_LEN, MAX_NAME_LEN);
(void)cut.time_ms();
}
}
void test_identifiers(test::Context& t) {
const std::string text = "hello";
const Id id = text_id(text);
const std::string hex = hex_encode(id);
t.expect_eq(hex.size(), ID_LEN * 2);
const auto parsed = parse_id(hex);
t.expect(parsed.has_value());
if (parsed)
t.expect_eq(std::memcmp(parsed->data(), id.data(), ID_LEN), 0);
t.expect(parse_id(hex.substr(0, 63)).has_value() == false);
t.expect(!parse_id("not-hex").has_value());
t.expect(!parse_id(std::string(64, 'z')).has_value());
t.expect(parse_id(hex.substr(0, 62) + "AF").has_value());
std::array<std::uint8_t, 1> out{};
t.expect(!hex_decode("0000", MutBytes(out.data(), out.size())));
t.expect(hex_decode("ff", MutBytes(out.data(), out.size())));
t.expect_eq(out[0], static_cast<std::uint8_t>(0xff));
ChangeId change{};
for (std::size_t i = 0; i < change.size(); ++i)
change[i] = static_cast<std::uint8_t>(i);
const std::string encoded = base32_encode(change);
t.expect(!encoded.empty());
t.expect(base32_prefix_matches(encoded, encoded.substr(0, 4)));
std::string upper = encoded.substr(0, 4);
for (char& c : upper)
if (c >= 'a' && c <= 'z')
c = static_cast<char>(c - 32);
t.expect(base32_prefix_matches(encoded, upper));
t.expect(!base32_prefix_matches(encoded, "!!!!"));
t.expect(!base32_prefix_matches(encoded, std::string(encoded.size() + 1, 'a')));
const std::string empty_hash =
"af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262";
t.expect_eq(hex_encode(Bytes(blake3_hash(Bytes{}))), empty_hash);
t.expect_eq(std::memcmp(object_id(TYPE_BLOB, bytes_of(text)).data(), id.data(), ID_LEN), 0);
t.expect_ne(std::memcmp(object_id(TYPE_TREE, bytes_of(text)).data(), id.data(), ID_LEN), 0);
}
void test_byte_helpers(test::Context& t) {
t.expect(equals(bytes_of("abc"), bytes_of("abc")));
t.expect(!equals(bytes_of("abc"), bytes_of("abd")));
t.expect(!equals(bytes_of("abc"), bytes_of("ab")));
t.expect(compare(bytes_of("abc"), bytes_of("abd")) < 0);
t.expect(compare(bytes_of("abc"), bytes_of("abc")) == 0);
t.expect(compare(bytes_of("abc"), bytes_of("ab")) > 0);
t.expect(ascii_iequals(bytes_of("AbC"), bytes_of("aBc")));
t.expect(!ascii_iequals(bytes_of("AbC"), bytes_of("aBd")));
t.expect(starts_with(bytes_of("abcdef"), bytes_of("abc")));
t.expect(!starts_with(bytes_of("ab"), bytes_of("abc")));
t.expect_eq(to_string(bytes_of("text")), "text");
t.expect_eq(str_of(Bytes{}).size(), 0u);
t.expect_eq(type_name(TYPE_BLOB), "blob");
t.expect_eq(type_name(static_cast<std::uint8_t>(99)), "unknown");
t.expect(type_valid(TYPE_CHUNK));
t.expect(type_valid(TYPE_VIEW));
t.expect(!type_valid(0));
t.expect(!type_valid(static_cast<std::uint8_t>(TYPE_VIEW + 1)));
}
void test_file_helpers(test::Context& t) {
const TempDir temp;
const fs::path file = temp.path / "nested" / "file.txt";
t.expect(ensure_directory(file.parent_path()));
t.expect(write_file(file, bytes_of("content")));
t.expect(path_exists(file));
t.expect(path_is_file(file));
t.expect(!path_is_dir(file));
t.expect_eq(path_file_size(file).value_or(0), 7u);
const auto read = read_file_opt(file);
t.expect(read.has_value());
if (read)
t.expect_eq(to_string(bytes_of(*read)), "content");
t.expect(!read_file_opt(file, 6).has_value());
t.expect(read_file_opt(file, 7).has_value());
t.expect(!read_file(temp.path / "missing").has_value());
const auto range = read_file_range(file, 2, 3);
t.expect(range.has_value());
if (range)
t.expect_eq(to_string(bytes_of(*range)), "nte");
const auto beyond = read_file_range(file, 100, 3);
t.expect(beyond.has_value());
if (beyond)
t.expect(beyond->empty());
t.expect(write_atomic(file, bytes_of("replacement")));
const auto replaced = read_file_opt(file);
t.expect(replaced.has_value());
if (replaced)
t.expect_eq(to_string(bytes_of(*replaced)), "replacement");
t.expect(remove_file(file));
t.expect(!path_exists(file));
t.expect(same_or_under(temp.path / "a" / "b", temp.path));
t.expect(same_or_under(temp.path, temp.path));
t.expect(!same_or_under(temp.path, temp.path / "a"));
t.expect_eq(basename_string(file), "file.txt");
t.expect(!write_file(temp.path, bytes_of("cannot replace directory")));
t.expect(ensure_directory(temp.path / "nested"));
t.expect(list_directory(temp.path / "missing").empty());
const fs::path link = temp.path / "link";
std::error_code ec;
fs::create_symlink("nested/file.txt", link, ec);
if (!ec) {
t.expect(path_is_symlink(link));
const auto target = path_read_symlink(link);
t.expect(target.has_value());
if (target)
t.expect_eq(*target, "nested/file.txt");
const auto info = stat_path(link);
t.expect(info.has_value());
if (info)
t.expect_eq(info->kind, 3);
}
t.expect(remove_tree(temp.path / "missing-tree"));
}
} // namespace
test::Suite core_suite() {
return {"unit-core",
{
{"varint", test_varint},
{"reader_integers", test_reader_integers},
{"reader_sized_values", test_reader_sized_values},
{"reader_sig_and_time", test_reader_sig_and_time},
{"identifiers", test_identifiers},
{"byte_helpers", test_byte_helpers},
{"file_helpers", test_file_helpers},
}};
}
} // namespace vn::unit