-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlcipher_test.go
More file actions
232 lines (219 loc) · 5.22 KB
/
sqlcipher_test.go
File metadata and controls
232 lines (219 loc) · 5.22 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package sqlite3_test
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"testing"
sqlite3 "github.com/thinkgos/go-sqlcipher"
)
var (
testDb *sql.DB
testDir = "go-sqlcipher_test"
tables = `
CREATE TABLE KeyValueStore (
KeyEntry TEXT NOT NULL UNIQUE,
ValueEntry TEXT NOT NULL
);`
)
func init() {
// create DB
key := url.QueryEscape("passphrase")
tmpdir, err := os.MkdirTemp("", testDir)
if err != nil {
panic(err)
}
dbname := filepath.Join(tmpdir, "sqlcipher_test")
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=%s&_pragma_cipher_page_size=4096", key)
testDb, err = sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
panic(err)
}
_, err = testDb.Exec(tables)
if err != nil {
panic(err)
}
testDb.Close()
// make sure DB is encrypted
encrypted, err := sqlite3.IsEncrypted(dbname)
if err != nil {
panic(err)
}
if !encrypted {
panic(errors.New("go-sqlcipher: DB not encrypted"))
}
// open DB for testing
testDb, err = sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
panic(err)
}
_, err = testDb.Exec("SELECT count(*) FROM sqlite_master;")
if err != nil {
panic(err)
}
}
var mapping = map[string]string{
"foo": "one",
"bar": "two",
"baz": "three",
}
func TestSQLCipherParallelInsert(t *testing.T) {
t.Parallel()
insertValueQuery, err := testDb.Prepare("INSERT INTO KeyValueStore (KeyEntry, ValueEntry) VALUES (?, ?);")
noError(t, err)
for key, value := range mapping {
_, err := insertValueQuery.Exec(key, value)
noError(t, err)
}
}
func TestSQLCipherParallelSelect(t *testing.T) {
t.Parallel()
getValueQuery, err := testDb.Prepare("SELECT ValueEntry FROM KeyValueStore WHERE KeyEntry=?;")
if err != nil {
t.Fatal(err)
}
for key, value := range mapping {
var val string
err := getValueQuery.QueryRow(key).Scan(&val)
if err != sql.ErrNoRows {
if noError(t, err) {
if value != val {
t.Fatal("should not be equal")
}
}
}
}
}
func TestSQLCipherIsEncryptedFalse(t *testing.T) {
tmpdir, err := os.MkdirTemp("", testDir)
noError(t, err)
defer os.RemoveAll(tmpdir)
dbname := filepath.Join(tmpdir, "unencrypted.sqlite")
testDb, err := sql.Open("sqlite3", dbname)
noError(t, err)
defer testDb.Close()
_, err = testDb.Exec(tables)
noError(t, err)
encrypted, err := sqlite3.IsEncrypted(dbname)
if noError(t, err) {
if encrypted {
t.Fatal("should not be encrypted")
}
}
}
func TestSQLCipherIsEncryptedTrue(t *testing.T) {
tmpdir, err := os.MkdirTemp("", testDir)
noError(t, err)
defer os.RemoveAll(tmpdir)
dbname := filepath.Join(tmpdir, "encrypted.sqlite")
var key [32]byte
_, err = io.ReadFull(rand.Reader, key[:])
noError(t, err)
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=x'%s'",
hex.EncodeToString(key[:]))
testDb, err := sql.Open("sqlite3", dbnameWithDSN)
noError(t, err)
defer testDb.Close()
_, err = testDb.Exec(tables)
noError(t, err)
encrypted, err := sqlite3.IsEncrypted(dbname)
if noError(t, err) {
if !encrypted {
t.Fatal("should be encrypted")
}
}
}
// // BUG: Failed use sqlcipher3
// func TestSQLCipher3DB(t *testing.T) {
// dbname := filepath.Join("testdata", "sqlcipher3.sqlite3")
// dbnameWithDSN := dbname + "?_pragma_key=passphrase&_pragma_cipher_page_size=4096"
// // make sure DB is encrypted
// encrypted, err := sqlite3.IsEncrypted(dbname)
// if err != nil {
// t.Fatal(err)
// }
// if !encrypted {
// t.Fatal("go-sqlcipher: DB not encrypted")
// }
// // open DB for testing
// testDb, err := sql.Open("sqlite3", dbnameWithDSN)
// if err != nil {
// t.Fatal(err)
// }
// // should fail
// _, err = testDb.Exec("SELECT count(*) FROM sqlite_master;")
// if err == nil {
// t.Fatal(errors.New("opening a SQLCipher 3 database with SQLCipher 4 should fail"))
// }
// }
func TestSQLCipher4DB(t *testing.T) {
dbname := filepath.Join("testdata", "sqlcipher4.sqlite3")
dbnameWithDSN := dbname + "?_pragma_key=passphrase&_pragma_cipher_page_size=4096"
// make sure DB is encrypted
encrypted, err := sqlite3.IsEncrypted(dbname)
if err != nil {
t.Fatal(err)
}
if !encrypted {
t.Fatal("go-sqlcipher: DB not encrypted")
}
// open DB for testing
testDb, err := sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
t.Fatal(err)
}
// should succeed
_, err = testDb.Exec("SELECT count(*) FROM sqlite_master;")
if err != nil {
t.Fatal(err)
}
}
func ExampleIsEncrypted() {
// create random key
var key [32]byte
_, err := io.ReadFull(rand.Reader, key[:])
if err != nil {
log.Fatal(err)
}
// set DB name
dbname := "go-sqlcipher.sqlite"
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=x'%s'",
hex.EncodeToString(key[:]))
// create encrypted DB file
testDb, err := sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
log.Fatal(err)
}
defer os.Remove(dbname)
defer testDb.Close()
// create table
_, err = testDb.Exec("CREATE TABLE t(x INTEGER);")
if err != nil {
log.Fatal(err)
}
// make sure database is encrypted
encrypted, err := sqlite3.IsEncrypted(dbname)
if err != nil {
log.Fatal(err)
}
if encrypted {
fmt.Println("DB is encrypted")
} else {
fmt.Println("DB is unencrypted")
}
// Output:
// DB is encrypted
}
func noError(t *testing.T, err error) bool {
if err != nil {
t.Fatal(err)
return false
}
return true
}