Files

76 lines
2.5 KiB
Go

package storage
import (
"strings"
"testing"
)
// ClearAllMetadata wipes BOTH databases: votes, signers and groups in truth;
// merged records, the cid lookup, roots, search rows, the claim index and the
// local files in the cache. Service scratch keys are not metadata and survive.
func TestClearAllMetadataWipesTruthAndCache(t *testing.T) {
c := newTestClient(t)
a := signer(t, 1)
mustAccept(t, c,
vote(t, a, "bafkone", "title", "One", 1),
vote(t, a, "bafkone", "cids/bafkonefull", "true", 1),
vote(t, a, "bafktwo", "title", "Two", 1),
)
if _, _, err := c.RegisterFileTuple("/files/watch/a.mkv", 10, 20, "bafkmid"); err != nil {
t.Fatal(err)
}
for k, v := range map[string]string{"btmagnet:abc": "magnet:?xt=urn:btih:abc", "searchcov:tmdb:1": "1"} {
if err := c.SetServiceValue(k, v); err != nil {
t.Fatalf("seed %s: %v", k, err)
}
}
deleted, err := c.ClearAllMetadata()
if err != nil {
t.Fatalf("ClearAllMetadata: %v", err)
}
if deleted != 3 {
t.Errorf("deleted = %d, want 3 records", deleted)
}
for _, k := range allKeys(t, c) {
for _, ns := range []string{recordNS, CIDIndexPrefix, rootsNS, ftsNS} {
if strings.HasPrefix(k, ns) {
t.Errorf("key survived the clear: %q", k)
}
}
}
for _, table := range []string{"fts", "claim_by_pid", "local_file"} {
var n int
if err := c.Store().DB().QueryRow(`SELECT count(*) FROM ` + table).Scan(&n); err != nil || n != 0 {
t.Errorf("%s rows after clear = %d (err %v)", table, n, err)
}
}
if n, err := c.CountClaims(); err != nil || n != 0 {
t.Errorf("claims after clear = %d (err %v)", n, err)
}
if got := c.ResolveRoot("bafkone"); got != "bafkone" {
t.Errorf("post-clear ResolveRoot = %q, want the hash unchanged", got)
}
for _, k := range []string{"btmagnet:abc", "searchcov:tmdb:1"} {
if _, _, ok, _ := c.GetKeyInfo(k); !ok {
t.Errorf("clear deleted service key %q", k)
}
}
// The store keeps working after a wipe.
mustAccept(t, c, vote(t, a, "bafkone", "title", "Again", 1))
if got := record(t, c, "bafkone")["title"]; got != "Again" {
t.Errorf("title after re-write = %q", got)
}
}
// Raw writes are limited to service scratch keys: merged records and
// bookkeeping cannot be written around the claims.
func TestServiceValueRefusesRecordKeys(t *testing.T) {
c := newTestClient(t)
for _, k := range []string{"file:R1/title", "cid:bafk", "idx:roots/R1", "ev:meta:events/1", "meta:applied_seq"} {
if err := c.SetServiceValue(k, "x"); err == nil {
t.Errorf("SetServiceValue(%q) accepted", k)
}
}
}