github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/storage/boltdb_store_test.go (about) 1 package storage 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" 10 "github.com/stretchr/testify/require" 11 "go.etcd.io/bbolt" 12 ) 13 14 func newBoltStoreForTesting(t testing.TB) Store { 15 d := t.TempDir() 16 testFileName := filepath.Join(d, "test_bolt_db") 17 boltDBStore, err := NewBoltDBStore(dbconfig.BoltDBOptions{FilePath: testFileName}) 18 require.NoError(t, err) 19 return boltDBStore 20 } 21 22 func TestROBoltDB(t *testing.T) { 23 d := t.TempDir() 24 testFileName := filepath.Join(d, "test_ro_bolt_db") 25 cfg := dbconfig.BoltDBOptions{ 26 FilePath: testFileName, 27 ReadOnly: true, 28 } 29 30 // If DB doesn't exist, then error should be returned. 31 _, err := NewBoltDBStore(cfg) 32 require.Error(t, err) 33 34 // Create the DB and try to open it in RO mode. 35 cfg.ReadOnly = false 36 store, err := NewBoltDBStore(cfg) 37 require.NoError(t, err) 38 require.NoError(t, store.Close()) 39 cfg.ReadOnly = true 40 41 store, err = NewBoltDBStore(cfg) 42 require.NoError(t, err) 43 // Changes must be prohibited. 44 putErr := store.PutChangeSet(map[string][]byte{"one": []byte("one")}, nil) 45 require.ErrorIs(t, putErr, bbolt.ErrDatabaseReadOnly) 46 require.NoError(t, store.Close()) 47 48 // Create the DB without buckets and try to open it in RO mode, an error is expected. 49 tmp := t.TempDir() 50 cfg.FilePath = filepath.Join(tmp, "clean_ro_bolt") 51 db, err := bbolt.Open(cfg.FilePath, os.FileMode(0600), nil) 52 require.NoError(t, err) 53 require.NoError(t, db.Close()) 54 55 _, err = NewBoltDBStore(cfg) 56 require.Error(t, err) 57 require.True(t, strings.Contains(err.Error(), "root bucket does not exist")) 58 }