2015-12-01 19:44:58 +01:00
|
|
|
// Copyright (c) 2014 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-11-10 01:34:40 +01:00
|
|
|
|
|
|
|
package bdb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-15 07:11:11 +02:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
2014-11-10 01:34:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// dbType is the database type name for this driver.
|
|
|
|
const dbType = "bdb"
|
|
|
|
|
|
|
|
// TestCreateOpenFail ensures that errors related to creating and opening a
|
|
|
|
// database are handled properly.
|
|
|
|
func TestCreateOpenFail(t *testing.T) {
|
|
|
|
// Ensure that attempting to open a database that doesn't exist returns
|
|
|
|
// the expected error.
|
|
|
|
wantErr := walletdb.ErrDbDoesNotExist
|
2020-01-15 13:51:15 +01:00
|
|
|
if _, err := walletdb.Open(dbType, "noexist.db", true); err != wantErr {
|
2014-11-10 01:34:40 +01:00
|
|
|
t.Errorf("Open: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that attempting to open a database with the wrong number of
|
|
|
|
// parameters returns the expected error.
|
|
|
|
wantErr = fmt.Errorf("invalid arguments to %s.Open -- expected "+
|
2020-01-15 13:51:15 +01:00
|
|
|
"database path and no-freelist-sync option", dbType)
|
2014-11-10 01:34:40 +01:00
|
|
|
if _, err := walletdb.Open(dbType, 1, 2, 3); err.Error() != wantErr.Error() {
|
|
|
|
t.Errorf("Open: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that attempting to open a database with an invalid type for
|
|
|
|
// the first parameter returns the expected error.
|
|
|
|
wantErr = fmt.Errorf("first argument to %s.Open is invalid -- "+
|
|
|
|
"expected database path string", dbType)
|
2020-01-15 13:51:15 +01:00
|
|
|
if _, err := walletdb.Open(dbType, 1, true); err.Error() != wantErr.Error() {
|
2014-11-10 01:34:40 +01:00
|
|
|
t.Errorf("Open: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that attempting to create a database with the wrong number of
|
|
|
|
// parameters returns the expected error.
|
|
|
|
wantErr = fmt.Errorf("invalid arguments to %s.Create -- expected "+
|
2020-01-15 13:51:15 +01:00
|
|
|
"database path and no-freelist-sync option", dbType)
|
2014-11-10 01:34:40 +01:00
|
|
|
if _, err := walletdb.Create(dbType, 1, 2, 3); err.Error() != wantErr.Error() {
|
|
|
|
t.Errorf("Create: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that attempting to open a database with an invalid type for
|
|
|
|
// the first parameter returns the expected error.
|
|
|
|
wantErr = fmt.Errorf("first argument to %s.Create is invalid -- "+
|
|
|
|
"expected database path string", dbType)
|
2020-01-15 13:51:15 +01:00
|
|
|
if _, err := walletdb.Create(dbType, 1, true); err.Error() != wantErr.Error() {
|
2014-11-10 01:34:40 +01:00
|
|
|
t.Errorf("Create: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure operations against a closed database return the expected
|
|
|
|
// error.
|
|
|
|
dbPath := "createfail.db"
|
2020-01-15 13:51:15 +01:00
|
|
|
db, err := walletdb.Create(dbType, dbPath, true)
|
2014-11-10 01:34:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Create: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(dbPath)
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
wantErr = walletdb.ErrDbNotOpen
|
2017-01-20 01:07:56 +01:00
|
|
|
if _, err := db.BeginReadTx(); err != wantErr {
|
2014-11-10 01:34:40 +01:00
|
|
|
t.Errorf("Namespace: did not receive expected error - got %v, "+
|
|
|
|
"want %v", err, wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPersistence ensures that values stored are still valid after closing and
|
|
|
|
// reopening the database.
|
|
|
|
func TestPersistence(t *testing.T) {
|
|
|
|
// Create a new database to run tests against.
|
|
|
|
dbPath := "persistencetest.db"
|
2020-01-15 13:51:15 +01:00
|
|
|
db, err := walletdb.Create(dbType, dbPath, true)
|
2014-11-10 01:34:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create test database (%s) %v", dbType, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(dbPath)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a namespace and put some values into it so they can be tested
|
|
|
|
// for existence on re-open.
|
|
|
|
storeValues := map[string]string{
|
|
|
|
"ns1key1": "foo1",
|
|
|
|
"ns1key2": "foo2",
|
|
|
|
"ns1key3": "foo3",
|
|
|
|
}
|
|
|
|
ns1Key := []byte("ns1")
|
2017-01-20 01:07:56 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns1, err := tx.CreateTopLevelBucket(ns1Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-10 01:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range storeValues {
|
2017-01-20 01:07:56 +01:00
|
|
|
if err := ns1.Put([]byte(k), []byte(v)); err != nil {
|
2014-11-10 01:34:40 +01:00
|
|
|
return fmt.Errorf("Put: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ns1 Update: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close and reopen the database to ensure the values persist.
|
|
|
|
db.Close()
|
2019-10-02 19:14:59 +02:00
|
|
|
db, err = walletdb.Open(dbType, dbPath, true)
|
2014-11-10 01:34:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database (%s) %v", dbType, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Ensure the values previously stored in the 3rd namespace still exist
|
|
|
|
// and are correct.
|
2017-01-20 01:07:56 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns1 := tx.ReadBucket(ns1Key)
|
|
|
|
if ns1 == nil {
|
|
|
|
return fmt.Errorf("ReadTx.ReadBucket: unexpected nil root bucket")
|
2014-11-10 01:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range storeValues {
|
2017-01-20 01:07:56 +01:00
|
|
|
gotVal := ns1.Get([]byte(k))
|
2014-11-10 01:34:40 +01:00
|
|
|
if !reflect.DeepEqual(gotVal, []byte(v)) {
|
|
|
|
return fmt.Errorf("Get: key '%s' does not "+
|
|
|
|
"match expected value - got %s, want %s",
|
|
|
|
k, gotVal, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ns1 View: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|