1 // Copyright (c) 2015-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
11 "github.com/btcsuite/btcd/database"
12 _ "github.com/btcsuite/btcd/database/ffldb"
16 // ignoreDbTypes are types which should be ignored when running tests
17 // that iterate all supported DB types. This allows some tests to add
18 // bogus drivers for testing purposes while still allowing other tests
19 // to easily iterate all supported drivers.
20 ignoreDbTypes = map[string]bool{"createopenfail": true}
23 // checkDbError ensures the passed error is a database.Error with an error code
24 // that matches the passed error code.
25 func checkDbError(t *testing.T, testName string, gotErr error, wantErrCode database.ErrorCode) bool {
26 dbErr, ok := gotErr.(database.Error)
28 t.Errorf("%s: unexpected error type - got %T, want %T",
29 testName, gotErr, database.Error{})
32 if dbErr.ErrorCode != wantErrCode {
33 t.Errorf("%s: unexpected error code - got %s (%s), want %s",
34 testName, dbErr.ErrorCode, dbErr.Description,
42 // TestAddDuplicateDriver ensures that adding a duplicate driver does not
43 // overwrite an existing one.
44 func TestAddDuplicateDriver(t *testing.T) {
45 supportedDrivers := database.SupportedDrivers()
46 if len(supportedDrivers) == 0 {
47 t.Errorf("no backends to test")
50 dbType := supportedDrivers[0]
52 // bogusCreateDB is a function which acts as a bogus create and open
53 // driver function and intentionally returns a failure that can be
54 // detected if the interface allows a duplicate driver to overwrite an
56 bogusCreateDB := func(args ...interface{}) (database.DB, error) {
57 return nil, fmt.Errorf("duplicate driver allowed for database "+
61 // Create a driver that tries to replace an existing one. Set its
62 // create and open functions to a function that causes a test failure if
64 driver := database.Driver{
66 Create: bogusCreateDB,
69 testName := "duplicate driver registration"
70 err := database.RegisterDriver(driver)
71 if !checkDbError(t, testName, err, database.ErrDbTypeRegistered) {
76 // TestCreateOpenFail ensures that errors which occur while opening or closing
77 // a database are handled properly.
78 func TestCreateOpenFail(t *testing.T) {
79 // bogusCreateDB is a function which acts as a bogus create and open
80 // driver function that intentionally returns a failure which can be
82 dbType := "createopenfail"
83 openError := fmt.Errorf("failed to create or open database for "+
84 "database type [%v]", dbType)
85 bogusCreateDB := func(args ...interface{}) (database.DB, error) {
89 // Create and add driver that intentionally fails when created or opened
90 // to ensure errors on database open and create are handled properly.
91 driver := database.Driver{
93 Create: bogusCreateDB,
96 database.RegisterDriver(driver)
98 // Ensure creating a database with the new type fails with the expected
100 _, err := database.Create(dbType)
101 if err != openError {
102 t.Errorf("expected error not received - got: %v, want %v", err,
107 // Ensure opening a database with the new type fails with the expected
109 _, err = database.Open(dbType)
110 if err != openError {
111 t.Errorf("expected error not received - got: %v, want %v", err,
117 // TestCreateOpenUnsupported ensures that attempting to create or open an
118 // unsupported database type is handled properly.
119 func TestCreateOpenUnsupported(t *testing.T) {
120 // Ensure creating a database with an unsupported type fails with the
122 testName := "create with unsupported database type"
123 dbType := "unsupported"
124 _, err := database.Create(dbType)
125 if !checkDbError(t, testName, err, database.ErrDbUnknownType) {
129 // Ensure opening a database with the an unsupported type fails with the
131 testName = "open with unsupported database type"
132 _, err = database.Open(dbType)
133 if !checkDbError(t, testName, err, database.ErrDbUnknownType) {