navid/veins / 6169bc0f55b5c2ca7a3d19b3405cfaa6fa103a02cf0867e5daa279487e292194 / xveins/tests/unit/store_tests.cpp

store_tests.cpp

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

#include "test_support.h"

#include "core/common.h"
#include "object/object.h"
#include "store/store.h"
#include "unit/unit_helpers.h"

#include <algorithm>
#include <cstring>
#include <string>
#include <vector>

namespace vn::unit {
namespace {

std::vector<RawObject> sample_objects() {
    std::vector<RawObject> objects;
    const auto tree_body = encode_tree({TreeEntry{"a.txt", KIND_FILE, text_id("a")}});
    if (tree_body)
        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 std::string large(200'000, 'q');
    const BlobBuild build = build_blob(bytes_of(large));
    for (const auto& object : build.objects) {
        const Id id = object.id ? *object.id : object_id(object.type, bytes_of(object.body));
        objects.push_back(RawObject{object.type, object.body, id});
    }
    return objects;
}

void test_pack_roundtrip(test::Context& t) {
    const auto objects = sample_objects();
    const PackOutput output = encode_pack(objects);
    t.expect_eq(output.count, objects.size());
    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(), objects.size());
    t.expect_eq(index->ids.size(), objects.size());
    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_pack_corruption(test::Context& t) {
    (void)t;
    const PackOutput output = encode_pack(sample_objects());
    const std::size_t body_size = output.pack.size() - 32;

    for (std::size_t length = 0; length <= body_size; ++length) {
        auto copy = output.pack;
        copy.resize(length);
        fix_blake3_trailer(copy);
        if (auto decoded = decode_pack(copy)) {
            for (std::size_t i = 0; i < decoded->records.size(); ++i)
                (void)decoded->get(i);
        }
    }

    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 = output.pack;
            copy[i] = static_cast<std::uint8_t>(copy[i] ^ mask);
            fix_blake3_trailer(copy);
            if (auto decoded = decode_pack(copy)) {
                for (std::size_t record = 0; record < decoded->records.size(); ++record)
                    (void)decoded->get(record);
            }
        }
    }

    std::vector<std::uint8_t> garbage(200);
    for (std::size_t i = 0; i < garbage.size(); ++i)
        garbage[i] = static_cast<std::uint8_t>(i * 37);
    fix_blake3_trailer(garbage);
    (void)decode_pack(garbage);
}

void test_index_corruption(test::Context& t) {
    const PackOutput output = encode_pack(sample_objects());
    const auto pack = decode_pack(output.pack);
    t.expect(pack.has_value());
    if (!pack)
        return;

    std::vector<RecordMeta> records;
    for (const auto& record : pack->records)
        records.push_back(RecordMeta{record.id, record.record_off, record.type, record.raw_len});
    std::sort(records.begin(), records.end(), [](const RecordMeta& a, const RecordMeta& b) {
        return std::memcmp(a.id.data(), b.id.data(), ID_LEN) < 0;
    });
    const auto index_bytes = encode_index(records, pack->trailer);
    t.expect(decode_index(index_bytes).has_value());

    const std::size_t body_size = index_bytes.size() - 32;
    for (std::size_t length = 0; length <= body_size; ++length) {
        auto copy = index_bytes;
        copy.resize(length);
        fix_blake3_trailer(copy);
        if (auto decoded = decode_index(copy)) {
            for (const auto& id : decoded->ids)
                (void)decoded->find(id);
        }
    }

    for (std::size_t i = 0; i < body_size; ++i) {
        auto copy = index_bytes;
        copy[i] = static_cast<std::uint8_t>(copy[i] ^ 0xff);
        fix_blake3_trailer(copy);
        if (auto decoded = decode_index(copy)) {
            for (const auto& id : decoded->ids)
                (void)decoded->find(id);
        }
    }
}

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_repository_roundtrip(test::Context& t) {
    const TempDir temp;
    const fs::path repo = temp.path / "repo";
    const auto initialized = init_repo(repo);
    t.expect(initialized.has_value());
    if (!initialized)
        return;
    t.expect(path_is_file(repo / ".veins" / "format"));
    t.expect(discover_repo(repo).has_value());

    fs::create_directories(repo / "nested");
    t.expect(discover_repo(repo / "nested").has_value());
    t.expect(!discover_repo(temp.path).has_value());

    Store store = Store::open_all(repo);
    t.expect(!current_view(repo, store).has_value());
    t.expect(!store.read_body(make_id(1), TYPE_COMMIT).has_value());
    t.expect_eq(read_heads(repo).size(), 0u);

    auto tx = Txn::begin(repo, store);
    t.expect(tx.has_value());
    if (!tx)
        return;
    t.expect(tx->view().heads.empty());
    const auto status = tx->finish("unit transaction");
    t.expect(status.has_value());
    if (!status)
        return;

    Store refreshed = Store::open_all(repo);
    t.expect_eq(read_heads(repo).size(), 1u);
    const auto view = current_view(repo, refreshed);
    t.expect(view.has_value());
    if (view)
        t.expect(view->heads.empty());
}

} // namespace

test::Suite store_suite() {
    return {"unit-store",
            {
                {"pack_roundtrip", test_pack_roundtrip},
                {"pack_corruption", test_pack_corruption},
                {"index_corruption", test_index_corruption},
                {"order_log_nodes", test_order_log_nodes},
                {"repository_roundtrip", test_repository_roundtrip},
            }};
}

} // namespace vn::unit