Record the digest algorithm in the index snapshot
Snapshot schema 4 stores digestAlgorithm inside the digested payload. Restore accepts schemas 1-4, verifies the digest for 3 and 4, and classifies an unknown algorithm as incompatible instead of corrupt so a future digest change is not misread as corruption. Registered tests cover the migration and the rejection.
This commit is contained in:
1 parent
98ddf08eaf
commit
67ca5ea275
3 files changed
+42
-9
No files matched your search
@@ -134,7 +134,7 @@ VOID_TEST(SymbolIndex, rejects_incompatible_or_corrupt_snapshots) {
|
||||
return std::string{std::istreambuf_iterator<char>{stream}, {}};
|
||||
}();
|
||||
auto incompatible = bytes;
|
||||
const auto schema = incompatible.find("\"schema\":3");
|
||||
const auto schema = incompatible.find("\"schema\":4");
|
||||
require(schema != std::string::npos);
|
||||
incompatible.replace(schema, 10, "\"schema\":9");
|
||||
{
|
||||
@@ -210,7 +210,7 @@ VOID_TEST(SymbolIndex, quarantines_incompatible_snapshots_and_rebuilds) {
|
||||
std::ifstream stream{path, std::ios::binary};
|
||||
return std::string{std::istreambuf_iterator<char>{stream}, {}};
|
||||
}();
|
||||
const auto schema = bytes.find("\"schema\":3");
|
||||
const auto schema = bytes.find("\"schema\":4");
|
||||
require(schema != std::string::npos);
|
||||
bytes.replace(schema, 10, "\"schema\":9");
|
||||
{
|
||||
@@ -350,7 +350,7 @@ std::string downgrade(std::string bytes) {
|
||||
const auto end = bytes.find('"', digest + 12);
|
||||
require(end != std::string::npos);
|
||||
bytes.erase(digest, end + 1 - digest);
|
||||
const auto schema = bytes.find("\"schema\":3");
|
||||
const auto schema = bytes.find("\"schema\":4");
|
||||
require(schema != std::string::npos);
|
||||
bytes.replace(schema, 10, "\"schema\":1");
|
||||
return bytes;
|
||||
@@ -373,7 +373,7 @@ VOID_TEST(SymbolIndex, snapshot_schema_migration_is_reported_and_rewritten) {
|
||||
const auto upgraded = temp.path() / "upgraded.json";
|
||||
require(loaded.save(upgraded));
|
||||
const auto bytes = file_text(upgraded);
|
||||
expect(bytes.find("\"schema\":3") != std::string::npos);
|
||||
expect(bytes.find("\"schema\":4") != std::string::npos);
|
||||
expect(bytes.find("\"digest\":\"") != std::string::npos);
|
||||
SymbolIndex reread;
|
||||
expect(reread.load(upgraded));
|
||||
@@ -394,7 +394,7 @@ VOID_TEST(SymbolIndex, generation_restore_persists_the_migrated_schema) {
|
||||
expect(restored->migrated);
|
||||
expect(count(loaded.find({.name = "a"})) == 1);
|
||||
const auto bytes = file_text(root / "snapshot.json");
|
||||
expect(bytes.find("\"schema\":3") != std::string::npos);
|
||||
expect(bytes.find("\"schema\":4") != std::string::npos);
|
||||
expect(bytes.find("\"digest\":\"") != std::string::npos);
|
||||
expect(fs::is_regular_file(root / "snapshot.prev.json"));
|
||||
SymbolIndex reread;
|
||||
@@ -482,7 +482,7 @@ VOID_TEST(SymbolIndex, schema_2_snapshots_migrate_with_their_records) {
|
||||
require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)})));
|
||||
require(index.save(path));
|
||||
auto bytes = file_text(path);
|
||||
const auto schema = bytes.find("\"schema\":3");
|
||||
const auto schema = bytes.find("\"schema\":4");
|
||||
require(schema != std::string::npos);
|
||||
bytes.replace(schema, 10, "\"schema\":2");
|
||||
put_text(path, bytes);
|
||||
@@ -493,7 +493,7 @@ VOID_TEST(SymbolIndex, schema_2_snapshots_migrate_with_their_records) {
|
||||
expect(restored->migrated);
|
||||
expect(count(loaded.find({.name = "f"})) == 1);
|
||||
require(loaded.save(path));
|
||||
expect(file_text(path).find("\"schema\":3") != std::string::npos);
|
||||
expect(file_text(path).find("\"schema\":4") != std::string::npos);
|
||||
}
|
||||
|
||||
VOID_TEST(SymbolIndex, rejects_an_oversized_signature) {
|
||||
@@ -529,3 +529,20 @@ VOID_TEST(SymbolIndex, selector_records_the_previous_generation_name) {
|
||||
SymbolIndex rejected;
|
||||
expect(!rejected.restore_generation(root).has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(SymbolIndex, schema_4_rejects_an_unknown_digest_algorithm) {
|
||||
Void::FileSystem::TempDir temp{"vls_symbol_index_"};
|
||||
const auto path = temp.path() / "index.json";
|
||||
SymbolIndex index;
|
||||
require(index.publish(context("debug:unit", {record("f", "/project/unit.cpp", 3)})));
|
||||
require(index.save(path));
|
||||
auto bytes = file_text(path);
|
||||
const auto algorithm = bytes.find("\"digestAlgorithm\":\"sha256\"");
|
||||
require(algorithm != std::string::npos);
|
||||
bytes.replace(algorithm, 26, "\"digestAlgorithm\":\"blake3\"");
|
||||
put_text(path, bytes);
|
||||
const auto restored = index.restore(path);
|
||||
require(restored.has_value());
|
||||
expect(restored->status == SymbolIndex::LoadStatus::incompatible);
|
||||
expect(restored->quarantine.has_value());
|
||||
}
|
||||
Reference in new issue
Block a user