navid/veins / 5f489932bf8cd48c68a3599c0e2aac314c74798da55faada5c77dc1a7bdf9cc2 / xveins/tests/unit_tests.cpp
unit_tests.cpp
← xveins/tests · 21431 bytes · raw · download
#include "test_support.h"
#include "core/common.h"
#include "object/object.h"
#include "store/store.h"
#include "worktree/attributes.h"
#include "worktree/pathspec.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace vn;
namespace {
Id make_id(std::uint8_t seed) {
Id id{};
for (std::size_t i = 0; i < id.size(); ++i)
id[i] = static_cast<std::uint8_t>(seed + i);
return id;
}
Id text_id(std::string_view text) {
return object_id(TYPE_BLOB, bytes_of(text));
}
std::vector<std::uint8_t> bytes_from(std::string_view text) {
return std::vector<std::uint8_t>(text.begin(), text.end());
}
struct TempDir {
fs::path path;
TempDir() {
path = fs::temp_directory_path() / ("vn-unit-" + std::to_string(random_u64()));
fs::create_directories(path);
}
~TempDir() {
std::error_code ec;
fs::remove_all(path, ec);
}
};
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());
}
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());
}
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("not-hex").has_value());
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)));
t.expect(!base32_prefix_matches(encoded, "!!!!"));
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_blob_roundtrip(test::Context& t) {
const std::string small = "small blob";
const auto encoded = encode_inline_blob(bytes_of(small));
t.expect(encoded.has_value());
if (!encoded)
return;
const auto decoded = decode_blob(bytes_of(*encoded));
t.expect(decoded.has_value());
if (decoded) {
t.expect(decoded->form == BlobForm::Inline);
t.expect_eq(to_string(bytes_of(decoded->data)), small);
}
const std::string large(200'000, 'x');
const BlobBuild build = build_blob(bytes_of(large));
t.expect(!build.objects.empty());
const auto root = decode_blob(bytes_of(build.objects.back().body));
t.expect(root.has_value());
if (root)
t.expect(root->form != BlobForm::Inline);
t.expect_eq(fastcdc_next(bytes_of(std::string(1024, 'a'))), 1024u);
const std::size_t chunk = fastcdc_next(bytes_of(large));
t.expect(chunk >= 16384);
t.expect(chunk <= 262144);
}
void test_tree_roundtrip(test::Context& t) {
const Id first = text_id("first");
const Id second = text_id("second");
const std::vector<TreeEntry> entries = {
TreeEntry{"a.txt", KIND_FILE, first},
TreeEntry{"b.txt", KIND_EXECUTABLE, second},
TreeEntry{"dir", KIND_SUBTREE, first},
};
const auto body = encode_tree(entries);
t.expect(body.has_value());
if (body) {
const auto decoded = decode_tree(bytes_of(*body));
t.expect(decoded.has_value());
if (decoded) {
t.expect_eq(decoded->entries.size(), entries.size());
if (decoded->entries.size() == entries.size()) {
for (std::size_t i = 0; i < entries.size(); ++i) {
t.expect_eq(decoded->entries[i].name, entries[i].name);
t.expect_eq(decoded->entries[i].kind, entries[i].kind);
t.expect_eq(std::memcmp(decoded->entries[i].target.data(),
entries[i].target.data(), ID_LEN),
0);
}
}
}
}
const std::vector<TreeEntry> unsorted = {
TreeEntry{"b.txt", KIND_FILE, first},
TreeEntry{"a.txt", KIND_FILE, second},
};
const auto rejected = encode_tree(unsorted);
t.expect(!rejected.has_value());
if (!rejected)
t.expect(rejected.error().kind == ErrorKind::Unsorted);
const std::vector<TreeEntry> duplicate = {
TreeEntry{"a.txt", KIND_FILE, first},
TreeEntry{"a.txt", KIND_FILE, second},
};
t.expect(!encode_tree(duplicate).has_value());
t.expect(!encode_tree({TreeEntry{"a/b", KIND_FILE, first}}).has_value());
t.expect(!encode_tree({TreeEntry{".veins", KIND_FILE, first}}).has_value());
}
Commit make_commit() {
Commit commit;
commit.version = 1;
commit.parents = {make_id(1)};
commit.root_tree = make_id(2);
for (std::size_t i = 0; i < commit.change_id.size(); ++i)
commit.change_id[i] = static_cast<std::uint8_t>(i + 3);
commit.author = Sig{"Author", "author@example.com", 1'700'000'000, 0};
commit.committer = Sig{"Committer", "committer@example.com", 1'700'000'001, -120};
commit.message = "commit message";
commit.renames_state = RENAMES_RECORDED;
commit.renames = {Rename{0, 1, "old.txt", "new.txt"}};
commit.extra = {KeyValue{"encoding", "UTF-8"}};
return commit;
}
void test_commit_roundtrip(test::Context& t) {
const Commit commit = make_commit();
const auto body = encode_commit(commit);
const auto decoded = decode_commit(bytes_of(body));
t.expect(decoded.has_value());
if (!decoded)
return;
t.expect_eq(decoded->version, commit.version);
t.expect_eq(decoded->parents.size(), commit.parents.size());
t.expect_eq(decoded->message, commit.message);
t.expect_eq(decoded->author.name, commit.author.name);
t.expect_eq(decoded->author.email, commit.author.email);
t.expect_eq(decoded->author.seconds, commit.author.seconds);
t.expect_eq(decoded->committer.tz_min, commit.committer.tz_min);
t.expect_eq(decoded->renames.size(), 1u);
t.expect_eq(decoded->extra.size(), 1u);
t.expect_eq(std::memcmp(decoded->root_tree.data(), commit.root_tree.data(), ID_LEN), 0);
auto wrong_version = body;
wrong_version[0] = 2;
const auto rejected = decode_commit(bytes_of(wrong_version));
t.expect(!rejected.has_value());
if (!rejected)
t.expect(rejected.error().kind == ErrorKind::UnsupportedVersion);
auto trailing = body;
trailing.push_back(0);
t.expect(!decode_commit(bytes_of(trailing)).has_value());
}
void test_conflict_roundtrip(test::Context& t) {
Conflict conflict;
conflict.adds = {
ConflictValue{KIND_FILE, text_id("one")},
ConflictValue{KIND_FILE, text_id("two")},
};
conflict.removes = {ConflictValue{KIND_FILE, text_id("three")}};
const auto body = encode_conflict(conflict);
const auto decoded = decode_conflict(bytes_of(body));
t.expect(decoded.has_value());
if (decoded) {
t.expect_eq(decoded->adds.size(), 2u);
t.expect_eq(decoded->removes.size(), 1u);
t.expect_eq(decoded->adds[0].kind, KIND_FILE);
}
Conflict invalid;
invalid.adds = {
ConflictValue{KIND_FILE, text_id("same")},
ConflictValue{KIND_FILE, text_id("same")},
};
invalid.removes = {ConflictValue{KIND_FILE, text_id("other")}};
t.expect(!decode_conflict(bytes_of(encode_conflict(invalid))).has_value());
Conflict no_removes;
no_removes.adds = {ConflictValue{KIND_FILE, text_id("only")}};
t.expect(!decode_conflict(bytes_of(encode_conflict(no_removes))).has_value());
}
View make_view() {
const Id a = make_id(10);
const Id b = make_id(20);
View view;
view.heads = a < b ? std::vector<Id>{a, b} : std::vector<Id>{b, a};
Bookmark bookmark;
bookmark.name = "master";
bookmark.targets.adds = {view.heads.front()};
view.bookmarks = {bookmark};
view.tags = {TagRef{"v1", view.heads.back()}};
view.remote_refs = {RemoteRef{"origin", "master", view.heads.front()}};
view.working_copies = {WorkspaceRef{"default", view.heads.back()}};
return view;
}
void test_view_roundtrip(test::Context& t) {
const View view = make_view();
const auto decoded = decode_view(bytes_of(encode_view(view)));
t.expect(decoded.has_value());
if (!decoded)
return;
t.expect_eq(decoded->heads.size(), 2u);
t.expect_eq(decoded->bookmarks.size(), 1u);
t.expect_eq(decoded->bookmarks[0].name, "master");
t.expect_eq(decoded->tags.size(), 1u);
t.expect_eq(decoded->remote_refs.size(), 1u);
t.expect_eq(decoded->working_copies.size(), 1u);
View invalid = view;
invalid.bookmarks[0].targets.adds.clear();
t.expect(!decode_view(bytes_of(encode_view(invalid))).has_value());
}
void test_operation_roundtrip(test::Context& t) {
Operation op;
op.parents = {make_id(1), make_id(2)};
op.view = make_id(3);
op.start = Reader::TimeMs{1'700'000'000'123, 0};
op.end = Reader::TimeMs{1'700'000'000'456, 0};
op.hostname = "host";
op.username = "user";
op.description = "describe";
op.tags = {KeyValue{"kind", "test"}};
const auto decoded = decode_operation(bytes_of(encode_operation(op)));
t.expect(decoded.has_value());
if (!decoded)
return;
t.expect_eq(decoded->parents.size(), 2u);
t.expect_eq(decoded->start.ms, op.start.ms);
t.expect_eq(decoded->end.ms, op.end.ms);
t.expect_eq(decoded->description, op.description);
t.expect_eq(decoded->tags.size(), 1u);
}
void test_pack_roundtrip(test::Context& t) {
const auto tree_body = encode_tree({TreeEntry{"a.txt", KIND_FILE, text_id("a")}});
t.expect(tree_body.has_value());
if (!tree_body)
return;
std::vector<RawObject> objects;
objects.push_back(RawObject{TYPE_TREE, *tree_body, object_id(TYPE_TREE, bytes_of(*tree_body))});
const std::string blob = "packed blob data";
objects.push_back(RawObject{TYPE_BLOB, bytes_from(blob), text_id(blob)});
const PackOutput output = encode_pack(objects);
t.expect_eq(output.count, 2u);
const auto pack = decode_pack(output.pack);
t.expect(pack.has_value());
const auto index =
decode_index(output.index, pack ? std::optional(pack->trailer) : std::nullopt);
t.expect(index.has_value());
if (!pack || !index)
return;
t.expect_eq(pack->records.size(), 2u);
t.expect_eq(index->ids.size(), 2u);
for (std::size_t i = 0; i < pack->records.size(); ++i) {
const auto data = pack->get(i);
t.expect(data.has_value());
if (data)
t.expect_eq(data->size(), pack->records[i].raw_len);
t.expect(index->find(pack->records[i].id).has_value());
}
t.expect(!decode_index(output.index, std::array<std::uint8_t, 32>{}).has_value());
}
void test_order_log_nodes(test::Context& t) {
t.expect(order_log_nodes({}).empty());
const LogOrderNode a{make_id(1), 100, {}};
const LogOrderNode b{make_id(2), 100, {a.id}};
auto order = order_log_nodes({a, b});
t.expect_eq(order.size(), 2u);
if (order.size() == 2) {
t.expect_eq(order[0], 1u);
t.expect_eq(order[1], 0u);
}
const LogOrderNode c{make_id(3), 100, {b.id}};
order = order_log_nodes({a, b, c});
t.expect_eq(order.size(), 3u);
if (order.size() == 3) {
t.expect_eq(order[0], 2u);
t.expect_eq(order[1], 1u);
t.expect_eq(order[2], 0u);
}
const LogOrderNode older_child{make_id(4), 1, {a.id}};
order = order_log_nodes({a, older_child});
t.expect_eq(order.size(), 2u);
if (order.size() == 2) {
t.expect_eq(order[0], 1u);
t.expect_eq(order[1], 0u);
}
const LogOrderNode newer{make_id(5), 200, {}};
const LogOrderNode middle{make_id(6), 150, {}};
order = order_log_nodes({a, newer, middle});
t.expect_eq(order.size(), 3u);
if (order.size() == 3) {
t.expect_eq(order[0], 1u);
t.expect_eq(order[1], 2u);
t.expect_eq(order[2], 0u);
}
const LogOrderNode merge{make_id(7), 100, {a.id, newer.id}};
order = order_log_nodes({a, newer, merge});
t.expect_eq(order.size(), 3u);
if (order.size() == 3)
t.expect_eq(order[0], 2u);
const LogOrderNode orphan{make_id(8), 50, {make_id(99)}};
order = order_log_nodes({orphan});
t.expect_eq(order.size(), 1u);
const LogOrderNode cycle_a{make_id(9), 10, {make_id(10)}};
const LogOrderNode cycle_b{make_id(10), 10, {make_id(9)}};
order = order_log_nodes({cycle_a, cycle_b});
t.expect_eq(order.size(), 2u);
}
void test_pathspecs(test::Context& t) {
t.expect(pathspec_has_glob("src/*.cpp"));
t.expect(pathspec_has_glob("a?.txt"));
t.expect(!pathspec_has_glob("src/file.cpp"));
const fs::path root = "/tmp/vn-pathspec/repo";
const auto relative = pathspec_normalize(root, "sub", "./a/../b.txt");
t.expect(relative.has_value());
if (relative)
t.expect_eq(*relative, "sub/b.txt");
t.expect(!pathspec_normalize(root, "", "../outside").has_value());
const auto absolute = pathspec_normalize(root, "", "/tmp/vn-pathspec/repo/a/./b");
t.expect(absolute.has_value());
if (absolute)
t.expect_eq(*absolute, "a/b");
t.expect(!pathspec_normalize(root, "", "/tmp/vn-pathspec/other/a").has_value());
}
void test_ignore_rules(test::Context& t) {
t.expect_eq(glob_match("*.tmp", "file.tmp"), true);
t.expect_eq(glob_match("*.tmp", "file.txt"), false);
t.expect_eq(glob_match("a?c", "abc"), true);
t.expect_eq(glob_match("src/**/x", "src/a/b/x"), true);
t.expect_eq(glob_match("*", "a/b"), false);
t.expect_eq(glob_match("**/Testing/Temporary/*", "xveins/tests/Testing/Temporary/CTest.log"),
true);
t.expect_eq(glob_match("*/Testing/Temporary/*", "xveins/tests/Testing/Temporary/CTest.log"),
false);
const TempDir temp;
t.expect(
write_file(temp.path / ".vnexclude", bytes_of("*.tmp\nbuild/\n**/Testing/Temporary/*\n")));
t.expect(ignore_path_ignored(temp.path, "note.tmp"));
t.expect(ignore_path_ignored(temp.path, "build/out.bin"));
t.expect(ignore_path_ignored(temp.path, "xveins/tests/Testing/Temporary/CTest.log"));
t.expect(!ignore_path_ignored(temp.path, "note.txt"));
IgnoreRules rules = ignore_empty();
rules.items.push_back(IgnoreRule{"*.log", false, false, false, false, ""});
rules.items.push_back(IgnoreRule{"keep.log", true, false, false, false, ""});
t.expect(ignore_ignored(rules, "debug.log", false));
t.expect(!ignore_ignored(rules, "keep.log", false));
t.expect(valid_name("file.txt"));
t.expect(!valid_name("dir/file"));
t.expect(!valid_name(".veins"));
t.expect(!valid_name(".."));
t.expect(valid_path("a/b/c"));
t.expect(!valid_path("/a"));
t.expect(!valid_path("a//b"));
t.expect(!valid_path("a/../b"));
}
void test_attributes(test::Context& t) {
const TempDir temp;
const std::string rules_text = "*.c txt eol:lf\n"
"*.cmd txt eol:crlf\n"
"*.ssa gen\n"
"**/generated/* gen\n"
"*.bin gen bin\n"
"special.bin -gen\n";
t.expect(write_file(temp.path / ".vnattribs", bytes_of(rules_text)));
const AttributeRules rules = AttributeRules::load(temp.path);
const auto source = rules.lookup("src/main.c");
t.expect(source.text == AttributeText::Text);
t.expect(source.eol == AttributeEol::Lf);
t.expect(!source.generated);
const auto script = rules.lookup("scripts/build.cmd");
t.expect(script.text == AttributeText::Text);
t.expect(script.eol == AttributeEol::Crlf);
t.expect(rules.lookup("build/out.ssa").generated);
t.expect(rules.lookup("pkg/generated/table.h").generated);
const auto binary = rules.lookup("assets/blob.bin");
t.expect(binary.generated);
t.expect(binary.text == AttributeText::Binary);
const auto overridden = rules.lookup("special.bin");
t.expect(!overridden.generated);
t.expect(overridden.text == AttributeText::Binary);
const auto plain = rules.lookup("notes.txt");
t.expect(plain.text == AttributeText::Auto);
t.expect(plain.eol == AttributeEol::None);
t.expect(!plain.generated);
const auto lf = normalize_eol(bytes_of("a\r\nb\rc\nd"), AttributeEol::Lf);
t.expect_eq(to_string(bytes_of(lf)), "a\nb\nc\nd");
const auto crlf = normalize_eol(bytes_of("a\nb\rc\r\n"), AttributeEol::Crlf);
t.expect_eq(to_string(bytes_of(crlf)), "a\r\nb\r\nc\r\n");
const auto cr = normalize_eol(bytes_of("a\nb\r\nc\r"), AttributeEol::Cr);
t.expect_eq(to_string(bytes_of(cr)), "a\rb\rc\r");
}
void test_attributes_edges(test::Context& t) {
const TempDir temp;
const std::string rules_text = "# leading comment\r\n"
"\r\n"
"*.c gen\r\n"
"*.c -gen\r\n"
"*.h txt\r\n"
"*.h -bin\r\n"
"*.log eol:crlf\r\n"
"*.log -eol\r\n"
"*.mac eol:cr\r\n"
"src/*.c gen\r\n"
"*.dat unknown-token bin\r\n";
t.expect(write_file(temp.path / ".vnattribs", bytes_of(rules_text)));
const AttributeRules rules = AttributeRules::load(temp.path);
t.expect(!rules.lookup("a.c").generated);
t.expect(rules.lookup("deep/a.c").generated == false);
t.expect(rules.lookup("a.h").text == AttributeText::Auto);
t.expect(rules.lookup("a.log").eol == AttributeEol::None);
t.expect(rules.lookup("a.mac").eol == AttributeEol::Cr);
t.expect(rules.lookup("src/main.c").generated);
t.expect(!rules.lookup("nested/src/main.c").generated);
t.expect(rules.lookup("assets/blob.dat").text == AttributeText::Binary);
const TempDir empty;
const AttributeRules none = AttributeRules::load(empty.path);
const auto plain = none.lookup("anything");
t.expect(!plain.generated);
t.expect(plain.text == AttributeText::Auto);
t.expect(plain.eol == AttributeEol::None);
}
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(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 / "a"));
t.expect_eq(basename_string(file), "file.txt");
}
} // namespace
int main() {
const std::vector<test::Suite> suites = {
{"unit",
{
{"varint", test_varint},
{"identifiers", test_identifiers},
{"blob_roundtrip", test_blob_roundtrip},
{"tree_roundtrip", test_tree_roundtrip},
{"commit_roundtrip", test_commit_roundtrip},
{"conflict_roundtrip", test_conflict_roundtrip},
{"view_roundtrip", test_view_roundtrip},
{"operation_roundtrip", test_operation_roundtrip},
{"pack_roundtrip", test_pack_roundtrip},
{"order_log_nodes", test_order_log_nodes},
{"pathspecs", test_pathspecs},
{"ignore_rules", test_ignore_rules},
{"attributes", test_attributes},
{"attributes_edges", test_attributes_edges},
{"file_helpers", test_file_helpers},
}},
};
return test::run_tests(suites);
}