Store each hash as its raw 32 bytes in a BLOB column (closes #134)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The head, tail and content columns of the files table are BLOB NOT NULL and hold the raw 32-byte SHA-256, half the size of the hex text, in the table and in the files_signature index. The hash phase, the records, report and trees carry each hash as a [sha256.Size]byte and compare it with ==; nothing is hex-encoded. Reading a record whose hash column is not 32 bytes is an error naming its path. In the tests, testHash is a stand-in hash: a string's bytes padded with zeros, so fixtures keep the sort order their strings give. Judgement call: a hash of the wrong length is refused on reading, not copied silently. Model: opus-5-5
This commit is contained in:
+46
-8
@@ -473,6 +473,43 @@ func TestLoadRowsCountAgreeDuringScan(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadRowsRefuseShortHash stores a record whose head is one byte,
|
||||
// not the 32 bytes of a SHA-256: loadFileRows and loadSignatureRows
|
||||
// must each fail on it with an error naming its path.
|
||||
func TestLoadRowsRefuseShortHash(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := openTestDB(t)
|
||||
|
||||
err := applyChanges(t.Context(), db, []scanRec{{path: "/a/f"}}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.ExecContext(t.Context(), "UPDATE files SET head = x'00'")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loads := map[string]func() error{
|
||||
"loadFileRows": func() error {
|
||||
return loadFileRows(t.Context(), db, func(int64) {},
|
||||
func(scanRec) {})
|
||||
},
|
||||
"loadSignatureRows": func() error {
|
||||
return loadSignatureRows(t.Context(), db, func(int64) {},
|
||||
func(scanRec) error { return nil })
|
||||
},
|
||||
}
|
||||
|
||||
for name, load := range loads {
|
||||
err := load()
|
||||
if !errors.Is(err, errHashSize) || !strings.Contains(err.Error(), "/a/f") {
|
||||
t.Errorf("%s: err = %v, want %v naming /a/f", name, err, errHashSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSignatureRowsNeedNoSort checks that SQLite reads signatureRowsSQL
|
||||
// from the files_signature index alone, with nothing to sort: the
|
||||
// first record comes at once, so report's read phase counts each
|
||||
@@ -527,12 +564,13 @@ func TestApplyChangesRoundTrip(t *testing.T) {
|
||||
// written.
|
||||
recs := []scanRec{
|
||||
{
|
||||
size: 2, mtime: time.Unix(20, 999_999_999), head: "h2", tail: "t2",
|
||||
content: "c2", path: "/a/tab\tnew\nline",
|
||||
size: 2, mtime: time.Unix(20, 999_999_999), head: testHash("h2"),
|
||||
tail: testHash("t2"), content: testHash("c2"),
|
||||
path: "/a/tab\tnew\nline",
|
||||
},
|
||||
{
|
||||
size: 1, mtime: time.Unix(10, 0), head: "h1", tail: "t1",
|
||||
content: "c1", path: "/a/x",
|
||||
size: 1, mtime: time.Unix(10, 0), head: testHash("h1"),
|
||||
tail: testHash("t1"), content: testHash("c1"), path: "/a/x",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -553,8 +591,8 @@ func TestApplyChangesRoundTrip(t *testing.T) {
|
||||
// An upsert for an existing path updates in place; a delete
|
||||
// removes exactly its path.
|
||||
upd := scanRec{
|
||||
size: 3, mtime: time.Unix(30, 0), head: "h3", tail: "t3", content: "c3",
|
||||
path: "/a/x",
|
||||
size: 3, mtime: time.Unix(30, 0), head: testHash("h3"),
|
||||
tail: testHash("t3"), content: testHash("c3"), path: "/a/x",
|
||||
}
|
||||
|
||||
err = applyChanges(t.Context(), db, []scanRec{upd},
|
||||
@@ -581,8 +619,8 @@ func TestApplyChangesBatching(t *testing.T) {
|
||||
recs := make([]scanRec, 0, n)
|
||||
for i := range n {
|
||||
recs = append(recs, scanRec{
|
||||
size: int64(i), mtime: time.Unix(1, 0), head: "h", tail: "t",
|
||||
path: fmt.Sprintf("/batch/%07d", i),
|
||||
size: int64(i), mtime: time.Unix(1, 0), head: testHash("h"),
|
||||
tail: testHash("t"), path: fmt.Sprintf("/batch/%07d", i),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user