feat: validate synchronized ASN prefix snapshots

This commit is contained in:
spiritlhl committed 2026-07-21 03:18:31 +08:00
1 parent a32a1067d3
commit 577871a7df
20 files changed
+8987 -2983

No files matched your search

+38
View File
@@ -1,6 +1,10 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"os"
"path/filepath"
@@ -30,3 +34,37 @@ func TestWritePrefixFileProtectsLargeCountDrop(t *testing.T) {
t.Fatalf("count drop error = %v", err)
}
}
func TestWritePrefixManifestPreservesGeneratedAtWhenSnapshotIsUnchanged(t *testing.T) {
directory := t.TempDir()
snapshotPath := filepath.Join(directory, "as64500.txt")
if err := os.WriteFile(snapshotPath, []byte("2001:db8::/48\n"), 0o644); err != nil {
t.Fatal(err)
}
manifestPath := snapshotPath + ".manifest.json"
old := prefixManifest{Schema: "backtrace.asn-prefixes/v1", File: "as64500.txt", Count: 1, SHA256: "", GeneratedAt: "2020-01-02T03:04:05Z"}
data, err := os.ReadFile(snapshotPath)
if err != nil {
t.Fatal(err)
}
// Seed a valid manifest with an old timestamp. The updater must retain it.
hash := sha256.Sum256(data)
old.SHA256 = hex.EncodeToString(hash[:])
encoded, err := json.Marshal(old)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(manifestPath, append(encoded, '\n'), 0o644); err != nil {
t.Fatal(err)
}
if err := writePrefixManifest(snapshotPath); err != nil {
t.Fatal(err)
}
actual, err := os.ReadFile(manifestPath)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(actual, []byte(`"generated_at":"2020-01-02T03:04:05Z"`)) {
t.Fatalf("generated_at was not preserved: %s", actual)
}
}