package bdb_test import ( "fmt" "os" "reflect" "testing" "github.com/p9c/p9/pkg/walletdb" "github.com/p9c/p9/pkg/walletdb/bdb" _ "github.com/p9c/p9/pkg/walletdb/bdb" ) // 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) { var e error // Ensure that attempting to open a database that doesn't exist returns the expected error. wantErr := walletdb.ErrDbDoesNotExist if _, e = walletdb.Open(dbType, "noexist.db"); e != wantErr { t.Errorf("Open: did not receive expected error - got %v, "+ "want %v", e, 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 "+ "database path", dbType, ) if _, e = walletdb.Open(dbType, 1, 2, 3); e != nil && e.Error() != wantErr.Error() { t.Errorf("Open: did not receive expected error - got %v, "+ "want %v", e, 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, ) if _, e = walletdb.Open(dbType, 1); e != nil && e.Error() != wantErr.Error() { t.Errorf("Open: did not receive expected error - got %v, "+ "want %v", e, 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 "+ "database path", dbType, ) if _, e = walletdb.Create(dbType, 1, 2, 3); e != nil && e.Error() != wantErr.Error() { t.Errorf("Create: did not receive expected error - got %v, "+ "want %v", e, 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, ) if _, e = walletdb.Create(dbType, 1); e != nil && e.Error() != wantErr.Error() { t.Errorf("Create: did not receive expected error - got %v, "+ "want %v", e, wantErr, ) return } // Ensure operations against a closed database return the expected error. dbPath := "createfail.db" db, e := walletdb.Create(dbType, dbPath) if e != nil { t.Errorf("Create: unexpected error: %v", e) return } defer func() { if e = os.Remove(dbPath); bdb.E.Chk(e) { } }() if e = db.Close(); bdb.E.Chk(e) { } wantErr = walletdb.ErrDbNotOpen if _, e = db.BeginReadTx(); e != wantErr { t.Errorf("Namespace: did not receive expected error - got %v, "+ "want %v", e, 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" db, e := walletdb.Create(dbType, dbPath) if e != nil { t.Errorf("Failed to create test database (%s) %v", dbType, e) return } defer func() { if e = os.Remove(dbPath); bdb.E.Chk(e) { } }() defer func() { if e = db.Close(); bdb.E.Chk(e) { } }() // 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") e = walletdb.Update(db, func(tx walletdb.ReadWriteTx) (e error) { ns1, e := tx.CreateTopLevelBucket(ns1Key) if e != nil { return e } for k, v := range storeValues { if e := ns1.Put([]byte(k), []byte(v)); E.Chk(e) { return fmt.Errorf("put: unexpected error: %v", e) } } return nil }, ) if e != nil { t.Errorf("ns1 Update: unexpected error: %v", e) return } // Close and reopen the database to ensure the values persist. if e = db.Close(); bdb.E.Chk(e) { } db, e = walletdb.Open(dbType, dbPath) if e != nil { t.Errorf("failed to open test database (%s) %v", dbType, e) return } defer func() { if e = db.Close(); bdb.E.Chk(e) { } }() // Ensure the values previously stored in the 3rd namespace still exist and are correct. e = walletdb.View(db, func(tx walletdb.ReadTx) (e error) { ns1 := tx.ReadBucket(ns1Key) if ns1 == nil { return fmt.Errorf("ReadTx.ReadBucket: unexpected nil root bucket") } for k, v := range storeValues { gotVal := ns1.Get([]byte(k)) 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 e != nil { t.Errorf("ns1 View: unexpected error: %v", e) return } }