mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2026-11-24 23:39:41 +08:00
feat: validate synchronized ASN prefix snapshots
This commit is contained in:
1 parent
a32a1067d3
commit
577871a7df
20 files changed
+8987
-2983
No files matched your search
@@ -2,8 +2,13 @@ package backtrace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -16,20 +21,61 @@ func TestParseASNPrefixLinesNormalizesAndDeduplicates(t *testing.T) {
|
||||
|
||||
func TestLoadASNPrefixRegistryUsesRawAfterCDNFailure(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Path == "/bad/as23764.txt" {
|
||||
http.Error(writer, "bad", http.StatusBadGateway)
|
||||
path := strings.TrimPrefix(request.URL.Path, "/")
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) != 2 {
|
||||
http.NotFound(writer, request)
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write([]byte("2409:8000\n"))
|
||||
base, file := parts[0], parts[1]
|
||||
snapshot := []byte("2409:8000\n")
|
||||
if base == "bad" && file == "as23764.txt.manifest.json" {
|
||||
_, _ = writer.Write([]byte(`{}`))
|
||||
return
|
||||
}
|
||||
if strings.HasSuffix(file, ".manifest.json") {
|
||||
hash := sha256.Sum256(snapshot)
|
||||
manifest, _ := json.Marshal(prefixManifest{Schema: ASNPrefixRegistrySchema, File: strings.TrimSuffix(file, ".manifest.json"), Count: 1, SHA256: hex.EncodeToString(hash[:]), GeneratedAt: "2026-01-01T00:00:00Z"})
|
||||
_, _ = writer.Write(manifest)
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write(snapshot)
|
||||
}))
|
||||
defer server.Close()
|
||||
sources := []ASNPrefixRegistrySource{{Name: "cdn", Base: server.URL + "/bad"}, {Name: "raw", Base: server.URL + "/good"}}
|
||||
sources := []ASNPrefixRegistrySource{{Name: "cdn", Base: server.URL + "/bad", ValidateManifest: true}, {Name: "raw", Base: server.URL + "/good", ValidateManifest: true}}
|
||||
loaded, err := LoadASNPrefixRegistry(context.Background(), server.Client(), sources)
|
||||
if err != nil || loaded.Source != "raw" || !loaded.Fallback || len(loaded.Prefixes) != len(knownPrefixASNs) {
|
||||
t.Fatalf("unexpected load: %+v, %v", loaded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePrefixManifestRejectsHashAndCountMismatch(t *testing.T) {
|
||||
snapshot := []byte("2409:8000\n")
|
||||
manifest := prefixManifest{Schema: ASNPrefixRegistrySchema, File: "as23764.txt", Count: 2, SHA256: strings.Repeat("0", 64), GeneratedAt: "2026-01-01T00:00:00Z"}
|
||||
data, err := json.Marshal(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validatePrefixManifest(data, snapshot, "as23764.txt"); err == nil {
|
||||
t.Fatal("invalid manifest unexpectedly accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEmbeddedPrefixManifests(t *testing.T) {
|
||||
if err := validateEmbeddedPrefixManifests(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbeddedManifestFilesArePresent(t *testing.T) {
|
||||
for _, asn := range knownPrefixASNs {
|
||||
name := strings.ToLower(asn) + ".txt.manifest.json"
|
||||
if _, err := embeddedPrefixManifestFS.ReadFile(filepath.Join("prefix", name)); err != nil {
|
||||
t.Fatalf("missing embedded manifest %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadASNPrefixRegistryEmbeddedFallback(t *testing.T) {
|
||||
loaded, err := LoadASNPrefixRegistry(context.Background(), nil, nil)
|
||||
if err != nil || loaded.Source != "embedded" || !loaded.Fallback || len(loaded.Prefixes) == 0 {
|
||||
|
||||
Reference in new issue
Block a user