-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathds_test.go
74 lines (62 loc) · 2.16 KB
/
ds_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package encrepo
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/stretchr/testify/require"
)
func TestCase(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
key := testingKey(t)
salt := testingSalt(t)
opts := SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"}
ds, err := NewSQLCipherDatastore("sqlite3", filepath.Join(t.TempDir(), "test.sqlite"), "blocks", key, opts)
require.NoError(t, err)
require.NoError(t, ds.Put(ctx, datastore.KeyWithNamespaces([]string{"A", "B"}), ([]byte)("42")))
qr, err := ds.Query(ctx, query.Query{Prefix: "a"})
require.NoError(t, err)
vals, err := qr.Rest()
require.NoError(t, err)
require.Len(t, vals, 0)
qr, err = ds.Query(ctx, query.Query{Prefix: "A"})
require.NoError(t, err)
vals, err = qr.Rest()
require.NoError(t, err)
require.Len(t, vals, 1)
require.Equal(t, "/A/B", vals[0].Key)
require.Equal(t, ([]byte)("42"), vals[0].Value)
require.NoError(t, ds.Close())
}
const sqliteHeaderMagic = "SQLite format 3\000"
const walModeMagic = uint8(2)
const walModeWriteMagicOffset = 18
const walModeReadMagicOffset = 19
func TestDBHeader(t *testing.T) {
key := testingKey(t)
salt := testingSalt(t)
opts := SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"}
dbPath := filepath.Join(t.TempDir(), "test.sqlite")
// create datastore
ds, err := NewSQLCipherDatastore("sqlite3", dbPath, "blocks", key, opts)
require.NoError(t, err)
require.NoError(t, ds.Close())
// open db file
reader, err := os.Open(dbPath)
require.NoError(t, err)
// read db header
headerBytes := make([]byte, walModeReadMagicOffset+1)
n, err := reader.Read(headerBytes)
require.NoError(t, err)
require.Equal(t, walModeReadMagicOffset+1, n)
// check for db plaintext header mode
require.Equal(t, sqliteHeaderMagic, string(headerBytes[:len(sqliteHeaderMagic)]))
// check for db WAL mode
require.Equal(t, walModeMagic, headerBytes[walModeWriteMagicOffset])
require.Equal(t, walModeMagic, headerBytes[walModeReadMagicOffset])
// close db file
require.NoError(t, reader.Close())
}