-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
repo_test.go
92 lines (71 loc) · 2.43 KB
/
repo_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package encrepo
import (
"context"
"path/filepath"
"testing"
"github.com/ipfs/go-datastore"
config "github.com/ipfs/kubo/config"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
func TestRepo(t *testing.T) {
key := testingKey(t)
salt := testingSalt(t)
opts := SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"}
dbPath := filepath.Join(t.TempDir(), "db.sqlite")
isInit, err := IsInitialized(dbPath, key, opts)
require.NoError(t, err)
require.False(t, isInit)
err = Init(dbPath, key, opts, &config.Config{})
require.NoError(t, err)
isInit, err = IsInitialized(dbPath, key, opts)
require.NoError(t, err)
require.True(t, isInit)
err = Init(dbPath, key, opts, &config.Config{})
require.NoError(t, err)
r, err := Open(dbPath, key, SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"})
require.NoError(t, err)
defer requireClose(t, r)
ds := r.Datastore()
require.NotNil(t, ds)
ks := r.Keystore()
require.NotNil(t, ks)
}
func TestSetAPIAddrTwice(t *testing.T) {
key := testingKey(t)
salt := testingSalt(t)
opts := SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"}
dbPath := filepath.Join(t.TempDir(), "db.sqlite")
err := Init(dbPath, key, opts, &config.Config{})
require.NoError(t, err)
r, err := Open(dbPath, key, SQLCipherDatastoreOptions{PlaintextHeader: true, Salt: salt, JournalMode: "WAL"})
require.NoError(t, err)
defer requireClose(t, r)
require.NoError(t, r.SetAPIAddr(ma.StringCast("/ip4/127.0.0.1")))
require.NoError(t, r.SetAPIAddr(ma.StringCast("/ip4/127.0.0.42")))
}
func TestNewSQLiteDatastoreTwice(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "db.sqlite")
ds, err := NewSQLiteDatastore("sqlite3", dbPath, "data")
require.NoError(t, err)
require.NoError(t, ds.Close())
ds, err = NewSQLiteDatastore("sqlite3", dbPath, "data")
require.NoError(t, err)
require.NoError(t, ds.Close())
}
func TestGetSize(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dbPath := filepath.Join(t.TempDir(), "db.sqlite")
ds, err := NewSQLiteDatastore("sqlite3", dbPath, "data")
require.NoError(t, err)
defer requireClose(t, ds)
buf := []byte("42\x0042")
key := datastore.NewKey("key")
err = ds.Put(ctx, key, buf)
require.NoError(t, err)
sz, err := ds.GetSize(ctx, key)
require.NoError(t, err)
require.Equal(t, len(buf), sz)
require.NoError(t, ds.Close())
}