navid/veins / b314db4a57679115094595d043fcc5b30d359768e2abd8eb72be3a14203a1f26 / xveins/tests/unit/protocol_tests.cpp

protocol_tests.cpp

← xveins/tests/unit · 15943 bytes · raw · download

#include "test_support.h"

#include "core/common.h"
#include "git/git_internal.h"
#include "protocol/protocol.h"
#include "protocol/wire.h"
#include "unit/unit_helpers.h"

#include <array>
#include <cstring>
#include <string>
#include <vector>

namespace vn::unit {
namespace {

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{0xff}}) {
            auto copy = body;
            copy[i] = static_cast<std::uint8_t>(copy[i] ^ mask);
            (void)decode(bytes_of(copy));
        }
    }
}

void test_protocol_roundtrips(test::Context& t) {
    proto::Hello hello;
    hello.min_version = 1;
    hello.max_version = 2;
    hello.capabilities = {"zstd", "auth"};
    hello.repo = "repo";
    hello.client_nonce = "nonce";
    auto hello_bytes = proto::encode_hello(hello);
    const auto decoded_hello = proto::decode_hello(bytes_of(hello_bytes));
    t.expect(decoded_hello.has_value());
    if (decoded_hello) {
        t.expect_eq(decoded_hello->max_version, 2);
        t.expect_eq(decoded_hello->capabilities.size(), 2u);
        t.expect_eq(decoded_hello->repo, "repo");
        t.expect_eq(decoded_hello->client_nonce, "nonce");
    }
    expect_all_prefixes_rejected(t, hello_bytes,
                                 [](Bytes bytes) { return proto::decode_hello(bytes); });
    exercise_mutations(hello_bytes, [](Bytes bytes) { return proto::decode_hello(bytes); });

    proto::HelloOk hello_ok;
    hello_ok.version = 1;
    hello_ok.capabilities = {"auth"};
    hello_ok.server_nonce = "server";
    hello_ok.auth_required = 1;
    auto hello_ok_bytes = proto::encode_hello_ok(hello_ok);
    const auto decoded_hello_ok = proto::decode_hello_ok(bytes_of(hello_ok_bytes));
    t.expect(decoded_hello_ok.has_value());
    if (decoded_hello_ok) {
        t.expect_eq(decoded_hello_ok->version, 1);
        t.expect_eq(decoded_hello_ok->capabilities.size(), 1u);
        t.expect_eq(decoded_hello_ok->auth_required, 1);
    }
    expect_all_prefixes_rejected(t, hello_ok_bytes,
                                 [](Bytes bytes) { return proto::decode_hello_ok(bytes); });
    exercise_mutations(hello_ok_bytes, [](Bytes bytes) { return proto::decode_hello_ok(bytes); });

    proto::Auth auth;
    auth.public_key = "key";
    auth.signature = "signature";
    auto auth_bytes = proto::encode_auth(auth);
    const auto decoded_auth = proto::decode_auth(bytes_of(auth_bytes));
    t.expect(decoded_auth.has_value());
    if (decoded_auth)
        t.expect_eq(decoded_auth->public_key, "key");
    expect_all_prefixes_rejected(t, auth_bytes,
                                 [](Bytes bytes) { return proto::decode_auth(bytes); });
    exercise_mutations(auth_bytes, [](Bytes bytes) { return proto::decode_auth(bytes); });

    proto::AuthOk auth_ok;
    auth_ok.role = 2;
    auth_ok.fingerprint = "fingerprint";
    auto auth_ok_bytes = proto::encode_auth_ok(auth_ok);
    const auto decoded_auth_ok = proto::decode_auth_ok(bytes_of(auth_ok_bytes));
    t.expect(decoded_auth_ok.has_value());
    if (decoded_auth_ok)
        t.expect_eq(decoded_auth_ok->role, 2);
    expect_all_prefixes_rejected(t, auth_ok_bytes,
                                 [](Bytes bytes) { return proto::decode_auth_ok(bytes); });
    exercise_mutations(auth_ok_bytes, [](Bytes bytes) { return proto::decode_auth_ok(bytes); });

    std::vector<proto::RefInfo> refs = {
        proto::RefInfo{"refs/heads/master", make_id(1), "change", 0},
        proto::RefInfo{"refs/tags/v1", make_id(2), "tag", 1},
    };
    auto refs_bytes = proto::encode_refs(refs);
    const auto decoded_refs = proto::decode_refs(bytes_of(refs_bytes));
    t.expect(decoded_refs.has_value());
    if (decoded_refs)
        t.expect_eq(decoded_refs->size(), 2u);
    expect_all_prefixes_rejected(t, refs_bytes,
                                 [](Bytes bytes) { return proto::decode_refs(bytes); });
    exercise_mutations(refs_bytes, [](Bytes bytes) { return proto::decode_refs(bytes); });

    const std::vector<Id> ids = {make_id(1), make_id(2)};
    auto ids_bytes = proto::encode_ids(ids);
    const auto decoded_ids = proto::decode_ids(bytes_of(ids_bytes), 10);
    t.expect(decoded_ids.has_value());
    if (decoded_ids)
        t.expect_eq(decoded_ids->size(), 2u);
    expect_all_prefixes_rejected(t, ids_bytes,
                                 [](Bytes bytes) { return proto::decode_ids(bytes, 10); });
    exercise_mutations(ids_bytes, [](Bytes bytes) { return proto::decode_ids(bytes, 10); });

    auto negotiate_bytes = proto::encode_negotiate({ids[0]}, {ids[1]});
    const auto decoded_negotiate = proto::decode_negotiate(bytes_of(negotiate_bytes));
    t.expect(decoded_negotiate.has_value());
    if (decoded_negotiate) {
        t.expect_eq(decoded_negotiate->wants.size(), 1u);
        t.expect_eq(decoded_negotiate->haves.size(), 1u);
    }
    expect_all_prefixes_rejected(t, negotiate_bytes,
                                 [](Bytes bytes) { return proto::decode_negotiate(bytes); });
    exercise_mutations(negotiate_bytes, [](Bytes bytes) { return proto::decode_negotiate(bytes); });

    auto negotiate_ok_bytes = proto::encode_negotiate_ok({1, 0, 1}, 1);
    const auto decoded_negotiate_ok = proto::decode_negotiate_ok(bytes_of(negotiate_ok_bytes));
    t.expect(decoded_negotiate_ok.has_value());
    if (decoded_negotiate_ok)
        t.expect_eq(decoded_negotiate_ok->size(), 3u);
    expect_all_prefixes_rejected(t, negotiate_ok_bytes,
                                 [](Bytes bytes) { return proto::decode_negotiate_ok(bytes); });
    exercise_mutations(negotiate_ok_bytes,
                       [](Bytes bytes) { return proto::decode_negotiate_ok(bytes); });

    auto bitmap_bytes = proto::encode_bitmap({1, 0, 1, 1});
    const auto decoded_bitmap = proto::decode_bitmap(bytes_of(bitmap_bytes));
    t.expect(decoded_bitmap.has_value());
    if (decoded_bitmap)
        t.expect_eq(decoded_bitmap->size(), 4u);
    expect_all_prefixes_rejected(t, bitmap_bytes,
                                 [](Bytes bytes) { return proto::decode_bitmap(bytes); });
    exercise_mutations(bitmap_bytes, [](Bytes bytes) { return proto::decode_bitmap(bytes); });

    std::vector<proto::UpdateRef> updates = {
        proto::UpdateRef{"refs/heads/master", std::nullopt, ids[0]},
        proto::UpdateRef{"refs/tags/v1", ids[1], std::nullopt},
    };
    auto updates_bytes = proto::encode_update_refs(updates);
    const auto decoded_updates = proto::decode_update_refs(bytes_of(updates_bytes));
    t.expect(decoded_updates.has_value());
    if (decoded_updates)
        t.expect_eq(decoded_updates->size(), 2u);
    expect_all_prefixes_rejected(t, updates_bytes,
                                 [](Bytes bytes) { return proto::decode_update_refs(bytes); });
    exercise_mutations(updates_bytes, [](Bytes bytes) { return proto::decode_update_refs(bytes); });

    t.expect_eq(proto::role_name(2), "write");
    t.expect_eq(proto::role_name(1), "read");
    t.expect_eq(proto::role_name(0), "anonymous");
    const std::string message =
        proto::auth_message(1, "repo", bytes_of("server"), bytes_of("client"), bytes_of("binding"));
    t.expect(!message.empty());
}

void test_protocol_limits(test::Context& t) {
    ByteWriter huge_ids;
    huge_ids.varint(1'000'000);
    t.expect(!proto::decode_ids(huge_ids.view(), 1'000'000).has_value());
    t.expect(!proto::decode_negotiate(huge_ids.view()).has_value());
    t.expect(!proto::decode_refs(huge_ids.view()).has_value());
    t.expect(!proto::decode_bitmap(huge_ids.view()).has_value());
    t.expect(!proto::decode_update_refs(huge_ids.view()).has_value());

    ByteWriter invalid_control;
    invalid_control.varint(0);
    t.expect(proto::decode_ids(invalid_control.view(), 1).has_value());
}

void test_wire_records(test::Context& t) {
    const Id id = make_id(3);
    const std::string body = "record body";
    const auto encoded = wire::make_record(id, TYPE_BLOB, bytes_of(body));
    const auto record = wire::parse_record(bytes_of(encoded));
    t.expect(record.has_value());
    if (record) {
        t.expect_eq(std::memcmp(record->id.data(), id.data(), ID_LEN), 0);
        t.expect_eq(record->kind, TYPE_BLOB);
        t.expect_eq(to_string(bytes_of(record->body)), body);
    }
    expect_all_prefixes_rejected(t, encoded, [](Bytes bytes) { return wire::parse_record(bytes); });
    exercise_mutations(encoded, [](Bytes bytes) { return wire::parse_record(bytes); });

    ByteWriter claimed_huge;
    claimed_huge.raw(Bytes(id.data(), id.size()));
    claimed_huge.u8(TYPE_BLOB);
    claimed_huge.u8(0);
    claimed_huge.varint(64u * 1024u * 1024u);
    claimed_huge.varint(0);
    t.expect(!wire::parse_record(claimed_huge.view()).has_value());

    t.expect_eq(wire::read_error(Bytes{}), wire::ERR_INTERNAL);
    const std::uint8_t error_bytes[] = {wire::ERR_NOT_FOUND, 0};
    t.expect_eq(wire::read_error(Bytes(error_bytes, 1)), wire::ERR_NOT_FOUND);
    t.expect(wire::error_message(Bytes{}).empty());
    t.expect_eq(wire::error_code_name(wire::ERR_NOT_FOUND), "object not found");
    t.expect_eq(wire::error_code_name(250), "unknown server error");
}

void test_url_parsing(test::Context& t) {
    auto file = proto::parse_url("file:///tmp/repo");
    t.expect(file.has_value());
    if (file) {
        t.expect(!file->tls);
        t.expect(!file->ssh);
        t.expect_eq(file->path, "/tmp/repo");
    }
    auto ssh = proto::parse_url("ssh://git@example.com:2222/repo.git");
    t.expect(ssh.has_value());
    if (ssh) {
        t.expect(ssh->ssh);
        t.expect_eq(ssh->host, "example.com");
        t.expect_eq(ssh->port, 2222);
        t.expect_eq(ssh->path, "repo.git");
    }
    auto tls = proto::parse_url("veins://example.com/repo");
    t.expect(tls.has_value());
    if (tls) {
        t.expect(tls->tls);
        t.expect_eq(tls->host, "example.com");
        t.expect_eq(tls->port, 7419);
        t.expect_eq(tls->path, "repo");
    }
    auto port = proto::parse_url("veins://example.com:1/repo");
    t.expect(port.has_value());
    if (port)
        t.expect_eq(port->port, 1);
    t.expect(!proto::parse_url("").has_value());
    t.expect(!proto::parse_url("http://example.com/repo").has_value());
    t.expect(!proto::parse_url("veins://example.com").has_value());
    t.expect(!proto::parse_url("ssh://example.com").has_value());
    t.expect_eq(proto::url_repo_name("veins://example.com/repo"), "repo");
    t.expect(proto::url_repo_name("file:///tmp/repo").empty());
    t.expect(proto::url_repo_name("not a url").empty());
}

void test_git_parsers(test::Context& t) {
    const auto hello_oid = git::object_hash(git::KIND_BLOB, bytes_of("hello"));
    t.expect(hello_oid.has_value());
    if (hello_oid)
        t.expect_eq(git::oid_hex(*hello_oid), "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0");
    t.expect(!git::object_hash(99, bytes_of("x")).has_value());
    t.expect(git::oid_from_hex(std::string(40, '0')).has_value());
    t.expect(!git::oid_from_hex("xyz").has_value());

    const auto sig = git::parse_sig("Name <mail@example.com> 1234567890 +0530");
    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, 1234567890);
        t.expect_eq(sig->tz_min, 330);
    }
    const auto negative = git::parse_sig("Name <mail@example.com> -12 -0130");
    t.expect(negative.has_value());
    if (negative)
        t.expect_eq(negative->tz_min, -90);
    t.expect(!git::parse_sig("Name <mail@example.com> 1 +000").has_value());
    t.expect(!git::parse_sig("Name <mail@example.com> nope +0000").has_value());
    t.expect(!git::parse_sig("Name mail@example.com 1 +0000").has_value());
    t.expect(!git::parse_sig("Name <mail@example.com> 999999999999999999999999 +0000").has_value());

    git::ParsedCommit commit;
    const auto tree_oid = git::oid_from_hex(std::string(40, '1'));
    const auto parent_oid = git::oid_from_hex(std::string(40, '2'));
    t.expect(tree_oid.has_value() && parent_oid.has_value());
    if (!tree_oid || !parent_oid)
        return;
    commit.tree = *tree_oid;
    commit.parents = {*parent_oid};
    commit.author = Sig{"Author", "author@example.com", 1, 0};
    commit.committer = Sig{"Committer", "committer@example.com", 2, -60};
    commit.message = "message\n";
    commit.extras = {KeyValue{"encoding", "UTF-8"}};
    const auto commit_body = git::serialize_commit(commit);
    const auto parsed_commit = git::parse_commit(bytes_of(commit_body));
    t.expect(parsed_commit.has_value());
    if (parsed_commit) {
        t.expect_eq(parsed_commit->parents.size(), 1u);
        t.expect_eq(parsed_commit->message, commit.message);
        t.expect_eq(parsed_commit->extras.size(), 1u);
        t.expect(parsed_commit->canonical);
    }
    for (std::size_t length = 0; length < commit_body.size(); ++length)
        (void)git::parse_commit(bytes_of(commit_body).first(length));
    exercise_mutations(commit_body, [](Bytes bytes) { return git::parse_commit(bytes); });

    std::vector<std::uint8_t> tree_body;
    const std::string mode = "100644";
    tree_body.insert(tree_body.end(), mode.begin(), mode.end());
    tree_body.push_back(' ');
    const std::string name = "a.txt";
    tree_body.insert(tree_body.end(), name.begin(), name.end());
    tree_body.push_back(0);
    const auto blob_oid = git::oid_from_hex(std::string(40, '3'));
    t.expect(blob_oid.has_value());
    if (blob_oid)
        tree_body.insert(tree_body.end(), blob_oid->begin(), blob_oid->end());
    const auto parsed_tree = git::parse_tree(bytes_of(tree_body));
    t.expect(parsed_tree.has_value());
    if (parsed_tree) {
        t.expect_eq(parsed_tree->entries.size(), 1u);
        t.expect(parsed_tree->canonical);
    }
    for (std::size_t length = 0; length < tree_body.size(); ++length)
        (void)git::parse_tree(bytes_of(tree_body).first(length));
    exercise_mutations(tree_body, [](Bytes bytes) { return git::parse_tree(bytes); });

    const std::string tag_text = "object " + git::oid_hex(*tree_oid) + "\ntype commit\n\nmessage\n";
    const auto parsed_tag = git::parse_tag(bytes_of(tag_text));
    t.expect(parsed_tag.has_value());
    if (parsed_tag) {
        t.expect_eq(git::oid_hex(parsed_tag->target), git::oid_hex(*tree_oid));
        t.expect_eq(parsed_tag->target_kind, git::KIND_COMMIT);
    }
    exercise_mutations(bytes_from(tag_text), [](Bytes bytes) { return git::parse_tag(bytes); });

    t.expect_eq(git::mode_for_kind(KIND_FILE), "100644");
    t.expect_eq(git::mode_for_kind(KIND_EXECUTABLE), "100755");
    t.expect_eq(git::mode_for_kind(KIND_SYMLINK), "120000");
    t.expect_eq(git::mode_for_kind(KIND_SUBTREE), "40000");
    t.expect_eq(git::mode_for_kind(KIND_SUBMODULE), "160000");
    t.expect_eq(git::mode_kind("100755"), KIND_EXECUTABLE);
    t.expect_eq(git::mode_kind("40000"), KIND_SUBTREE);
    t.expect_eq(git::mode_kind("040000"), KIND_SUBTREE);
    t.expect_eq(git::mode_kind("nonsense"), KIND_FILE);
}

} // namespace

test::Suite protocol_suite() {
    return {"unit-protocol",
            {
                {"protocol_roundtrips", test_protocol_roundtrips},
                {"protocol_limits", test_protocol_limits},
                {"wire_records", test_wire_records},
                {"url_parsing", test_url_parsing},
                {"git_parsers", test_git_parsers},
            }};
}

} // namespace vn::unit