1 package walletdb_test
2 3 import (
4 "fmt"
5 "os"
6 "testing"
7 8 "github.com/p9c/p9/pkg/walletdb"
9 _ "github.com/p9c/p9/pkg/walletdb/bdb"
10 )
11 12 var (
13 // ignoreDbTypes are types which should be ignored when running tests
14 // that iterate all supported DB types. This allows some tests to add
15 // bogus drivers for testing purposes while still allowing other tests
16 // to easily iterate all supported drivers.
17 ignoreDbTypes = map[string]bool{"createopenfail": true}
18 )
19 20 // TestAddDuplicateDriver ensures that adding a duplicate driver does not overwrite an existing one.
21 func TestAddDuplicateDriver(t *testing.T) {
22 supportedDrivers := walletdb.SupportedDrivers()
23 if len(supportedDrivers) == 0 {
24 t.Errorf("no backends to test")
25 return
26 }
27 dbType := supportedDrivers[0]
28 // bogusCreateDB is a function which acts as a bogus create and open driver function and intentionally returns a
29 // failure that can be detected if the interface allows a duplicate driver to overwrite an existing one.
30 bogusCreateDB := func(args ...interface{}) (walletdb.DB, error) {
31 return nil, fmt.Errorf(
32 "duplicate driver allowed for database "+
33 "type [%v]", dbType,
34 )
35 }
36 // Create a driver that tries to replace an existing one. Set its create and open functions to a function that
37 // causes a test failure if they are invoked.
38 driver := walletdb.Driver{
39 DbType: dbType,
40 Create: bogusCreateDB,
41 Open: bogusCreateDB,
42 }
43 e := walletdb.RegisterDriver(driver)
44 if e != walletdb.ErrDbTypeRegistered {
45 t.Errorf(
46 "unexpected duplicate driver registration error - "+
47 "got %v, want %v", e, walletdb.ErrDbTypeRegistered,
48 )
49 }
50 dbPath := "dupdrivertest.db"
51 db, e := walletdb.Create(dbType, dbPath)
52 if E.Chk(e) {
53 t.Errorf("failed to create database: %v", e)
54 return
55 }
56 if e := db.Close(); walletdb.E.Chk(e) {
57 }
58 _ = os.Remove(dbPath)
59 }
60 61 // TestCreateOpenFail ensures that errors which occur while opening or closing a database are handled properly.
62 func TestCreateOpenFail(t *testing.T) {
63 // bogusCreateDB is a function which acts as a bogus create and open driver function that intentionally returns a
64 // failure which can be detected.
65 dbType := "createopenfail"
66 openError := fmt.Errorf(
67 "failed to create or open database for "+
68 "database type [%v]", dbType,
69 )
70 bogusCreateDB := func(args ...interface{}) (walletdb.DB, error) {
71 return nil, openError
72 }
73 // Create and add driver that intentionally fails when created or opened to ensure errors on database open and
74 // create are handled properly.
75 driver := walletdb.Driver{
76 DbType: dbType,
77 Create: bogusCreateDB,
78 Open: bogusCreateDB,
79 }
80 e := walletdb.RegisterDriver(driver)
81 if E.Chk(e) {
82 }
83 // Ensure creating a database with the new type fails with the expected error.
84 _, e = walletdb.Create(dbType)
85 if e != openError {
86 t.Errorf(
87 "expected error not received - got: %v, want %v", e,
88 openError,
89 )
90 return
91 }
92 // Ensure opening a database with the new type fails with the expected
93 // error.
94 _, e = walletdb.Open(dbType)
95 if e != openError {
96 t.Errorf(
97 "expected error not received - got: %v, want %v", e,
98 openError,
99 )
100 return
101 }
102 }
103 104 // TestCreateOpenUnsupported ensures that attempting to create or open an unsupported database type is handled properly.
105 func TestCreateOpenUnsupported(t *testing.T) {
106 // Ensure creating a database with an unsupported type fails with the expected error.
107 dbType := "unsupported"
108 _, e := walletdb.Create(dbType)
109 if e != walletdb.ErrDbUnknownType {
110 t.Errorf(
111 "expected error not received - got: %v, want %v", e,
112 walletdb.ErrDbUnknownType,
113 )
114 return
115 }
116 // Ensure opening a database with the an unsupported type fails with the expected error.
117 _, e = walletdb.Open(dbType)
118 if e != walletdb.ErrDbUnknownType {
119 t.Errorf(
120 "expected error not received - got: %v, want %v", e,
121 walletdb.ErrDbUnknownType,
122 )
123 return
124 }
125 }
126