navid/veins / b314db4a57679115094595d043fcc5b30d359768e2abd8eb72be3a14203a1f26 / xveins/tests/unit/object_tests.cpp
object_tests.cpp
← xveins/tests/unit · 13977 bytes · raw · download
#include "test_support.h"
#include "core/common.h"
#include "object/object.h"
#include "unit/unit_helpers.h"
#include <algorithm>
#include <cstring>
#include <functional>
#include <string>
#include <vector>
namespace vn::unit {
namespace {
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;
}
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;
}
Conflict make_conflict() {
Conflict conflict;
conflict.adds = {
ConflictValue{KIND_FILE, text_id("one")},
ConflictValue{KIND_FILE, text_id("two")},
};
conflict.removes = {ConflictValue{KIND_FILE, text_id("three")}};
return conflict;
}
Operation make_operation() {
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"}};
return op;
}
template <typename Decode>
void expect_all_prefixes_rejected(test::Context& t, const std::vector<std::uint8_t>& body,
Decode decode) {
for (std::size_t length = 0; length < body.size(); ++length) {
const auto result = decode(bytes_of(body).first(length));
t.expect(!result.has_value());
}
}
template <typename Decode>
void exercise_mutations(const std::vector<std::uint8_t>& body, Decode decode) {
for (std::size_t i = 0; i < body.size(); ++i) {
for (const std::uint8_t mask :
{std::uint8_t{0x01}, std::uint8_t{0x80}, std::uint8_t{0xff}}) {
auto copy = body;
copy[i] = static_cast<std::uint8_t>(copy[i] ^ mask);
(void)decode(bytes_of(copy));
}
}
}
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);
}
expect_all_prefixes_rejected(t, *encoded, [](Bytes bytes) { return decode_blob(bytes); });
exercise_mutations(*encoded, [](Bytes bytes) { return decode_blob(bytes); });
const std::string large(200'000, 'x');
const BlobBuild build = build_blob(bytes_of(large));
t.expect(!build.objects.empty());
t.expect(build.root != Id{});
const auto root = decode_blob(bytes_of(build.objects.back().body));
t.expect(root.has_value());
if (root)
t.expect(root->form != BlobForm::Inline);
expect_all_prefixes_rejected(t, build.objects.back().body,
[](Bytes bytes) { return decode_blob(bytes); });
exercise_mutations(build.objects.back().body, [](Bytes bytes) { return decode_blob(bytes); });
t.expect_eq(fastcdc_next(bytes_of(std::string(1024, 'a'))), 1024u);
t.expect_eq(fastcdc_next(Bytes{}), 0u);
const std::size_t chunk = fastcdc_next(bytes_of(large));
t.expect(chunk >= 16384);
t.expect(chunk <= 262144);
}
void test_blob_invalid_values(test::Context& t) {
const std::uint8_t bad_form[] = {3};
t.expect(!decode_blob(Bytes(bad_form, 1)).has_value());
ByteWriter empty_leaf;
empty_leaf.u8(static_cast<std::uint8_t>(BlobForm::Leaf));
empty_leaf.varint(0);
t.expect(!decode_blob(empty_leaf.view()).has_value());
ByteWriter oversized_chunk;
oversized_chunk.u8(static_cast<std::uint8_t>(BlobForm::Leaf));
oversized_chunk.varint(1);
oversized_chunk.id(make_id(1));
oversized_chunk.varint(MAX_CHUNK_LEN + 1);
t.expect(!decode_blob(oversized_chunk.view()).has_value());
ByteWriter zero_chunk;
zero_chunk.u8(static_cast<std::uint8_t>(BlobForm::Leaf));
zero_chunk.varint(1);
zero_chunk.id(make_id(1));
zero_chunk.varint(0);
t.expect(!decode_blob(zero_chunk.view()).has_value());
ByteWriter overflowing_total;
overflowing_total.u8(static_cast<std::uint8_t>(BlobForm::Interior));
overflowing_total.varint(2);
overflowing_total.id(make_id(1));
overflowing_total.varint(0xffffffffffffffffull);
overflowing_total.id(make_id(2));
overflowing_total.varint(1);
t.expect(!decode_blob(overflowing_total.view()).has_value());
}
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);
}
}
}
expect_all_prefixes_rejected(t, *body, [](Bytes bytes) { return decode_tree(bytes); });
exercise_mutations(*body, [](Bytes bytes) { return decode_tree(bytes); });
}
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());
t.expect(!encode_tree({TreeEntry{"", KIND_FILE, first}}).has_value());
t.expect(!encode_tree({TreeEntry{"a", static_cast<std::uint8_t>(200), first}}).has_value());
ByteWriter huge_count;
huge_count.varint(1'000'000);
t.expect(!decode_tree(huge_count.view()).has_value());
}
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);
expect_all_prefixes_rejected(t, body, [](Bytes bytes) { return decode_commit(bytes); });
exercise_mutations(body, [](Bytes bytes) { return decode_commit(bytes); });
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());
Commit invalid_state = commit;
invalid_state.renames_state = 2;
t.expect(!decode_commit(bytes_of(encode_commit(invalid_state))).has_value());
ByteWriter huge_parents;
huge_parents.u8(1);
huge_parents.varint(1'000'000);
t.expect(!decode_commit(huge_parents.view()).has_value());
}
void test_conflict_roundtrip(test::Context& t) {
const Conflict conflict = make_conflict();
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);
}
expect_all_prefixes_rejected(t, body, [](Bytes bytes) { return decode_conflict(bytes); });
exercise_mutations(body, [](Bytes bytes) { return decode_conflict(bytes); });
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());
Conflict too_many;
for (int i = 0; i < 100; ++i)
too_many.adds.push_back(ConflictValue{KIND_FILE, text_id("a" + std::to_string(i))});
for (int i = 0; i < 99; ++i)
too_many.removes.push_back(ConflictValue{KIND_FILE, text_id("r" + std::to_string(i))});
t.expect(!decode_conflict(bytes_of(encode_conflict(too_many))).has_value());
}
void test_view_roundtrip(test::Context& t) {
const View view = make_view();
const auto body = encode_view(view);
const auto decoded = decode_view(bytes_of(body));
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);
expect_all_prefixes_rejected(t, body, [](Bytes bytes) { return decode_view(bytes); });
exercise_mutations(body, [](Bytes bytes) { return decode_view(bytes); });
View invalid = view;
invalid.bookmarks[0].targets.adds.clear();
t.expect(!decode_view(bytes_of(encode_view(invalid))).has_value());
View duplicate_heads = view;
duplicate_heads.heads.push_back(duplicate_heads.heads.front());
t.expect(!decode_view(bytes_of(encode_view(duplicate_heads))).has_value());
View empty_head_list;
t.expect(decode_view(bytes_of(encode_view(empty_head_list))).has_value());
ByteWriter huge_heads;
huge_heads.varint(1'000'000);
t.expect(!decode_view(huge_heads.view()).has_value());
}
void test_operation_roundtrip(test::Context& t) {
const Operation op = make_operation();
const auto body = encode_operation(op);
const auto decoded = decode_operation(bytes_of(body));
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);
expect_all_prefixes_rejected(t, body, [](Bytes bytes) { return decode_operation(bytes); });
exercise_mutations(body, [](Bytes bytes) { return decode_operation(bytes); });
Operation empty_key = op;
empty_key.tags = {KeyValue{"", "value"}};
t.expect(!decode_operation(bytes_of(encode_operation(empty_key))).has_value());
ByteWriter huge_tags;
huge_tags.varint(0);
huge_tags.id(make_id(1));
huge_tags.time_ms(Reader::TimeMs{0, 0});
huge_tags.time_ms(Reader::TimeMs{0, 0});
huge_tags.length_prefixed("host");
huge_tags.length_prefixed("user");
huge_tags.length_prefixed("description");
huge_tags.varint(1'000'000);
t.expect(!decode_operation(huge_tags.view()).has_value());
}
} // namespace
test::Suite object_suite() {
return {"unit-object",
{
{"blob_roundtrip", test_blob_roundtrip},
{"blob_invalid_values", test_blob_invalid_values},
{"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},
}};
}
} // namespace vn::unit