diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-02 16:56:16 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-02 16:56:16 -0600 |
commit | ca83d171256885ea33508a51e855c7666fc3a140 (patch) | |
tree | 3bc60b4b73295b839588555cbe6760a1a439d86c /db_test.go | |
parent | Merge pull request #112 from benbjohnson/perf-stats (diff) | |
download | dedo-ca83d171256885ea33508a51e855c7666fc3a140.tar.gz dedo-ca83d171256885ea33508a51e855c7666fc3a140.tar.xz |
Add meta page checksums.
This commit adds checksums to the meta pages on every write. When the
database loads, it verifies the checksums on the meta pages and returns
an error if either one is corrupt.
In the future, it should fallback to the previous meta page but for right
now it just hard fails. This is at least preferable to opening the database
and getting a random error or further corruption.
Fixes #25.
Diffstat (limited to 'db_test.go')
-rw-r--r-- | db_test.go | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -117,10 +117,43 @@ func TestDBCorruptMeta0(t *testing.T) { // Open the database. _, err = Open(path, 0666) - assert.Equal(t, err, errors.New("meta error: invalid database")) + assert.Equal(t, err, errors.New("meta0 error: invalid database")) }) } +// Ensure that a corrupt meta page checksum causes the open to fail. +func TestDBMetaChecksumError(t *testing.T) { + for i := 0; i < 2; i++ { + withTempPath(func(path string) { + db, err := Open(path, 0600) + pageSize := db.pageSize + db.Update(func(tx *Tx) error { + return tx.CreateBucket("widgets") + }) + db.Update(func(tx *Tx) error { + return tx.CreateBucket("woojits") + }) + db.Close() + + // Change a single byte in the meta page. + f, _ := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600) + f.WriteAt([]byte{1}, int64((i*pageSize)+(pageHeaderSize+12))) + f.Sync() + f.Close() + + // Reopen the database. + _, err = Open(path, 0600) + if assert.Error(t, err) { + if i == 0 { + assert.Equal(t, "meta0 error: checksum error", err.Error()) + } else { + assert.Equal(t, "meta1 error: checksum error", err.Error()) + } + } + }) + } +} + // Ensure that a database cannot open a transaction when it's not open. func TestDBTxErrDatabaseNotOpen(t *testing.T) { var db DB |