forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsqlite3_cipher_test.go
112 lines (104 loc) · 2.74 KB
/
sqlite3_cipher_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2023 Jonathan Giannuzzi <jonathan@giannuzzi.me>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build cgo && !libsqlite3
// +build cgo,!libsqlite3
package sqlite3
import (
"database/sql"
"os"
"testing"
)
func TestCipher(t *testing.T) {
ciphers := []string{
"",
"aes128cbc",
"aes256cbc",
"chacha20",
"sqlcipher",
"rc4",
}
keys := []string{
// Passphrase with Key Derivation
"passphrase",
// Passphrase with Key Derivation starting with a digit
"1passphrase",
// Raw Key Data (Without Key Derivation)
"x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'",
// Raw Key Data with Explicit Salt (Without Key Derivation)
"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648101010101010101010101010101010101'",
}
for _, cipher := range ciphers {
for _, key := range keys {
fname := TempFilename(t)
uri := "file:" + fname + "?_key=" + key
if cipher != "" {
uri += "&_cipher=" + cipher
}
db, err := sql.Open("sqlite3", uri)
if err != nil {
os.Remove(fname)
t.Errorf("sql.Open(\"sqlite3\", %q): %v", uri, err)
continue
}
_, err = db.Exec("CREATE TABLE test (id int)")
if err != nil {
db.Close()
os.Remove(fname)
t.Errorf("failed creating test table for %q: %v", uri, err)
continue
}
_, err = db.Exec("INSERT INTO test VALUES (1)")
db.Close()
if err != nil {
os.Remove(fname)
t.Errorf("failed inserting value into test table for %q: %v", uri, err)
continue
}
db, err = sql.Open("sqlite3", "file:"+fname)
if err != nil {
os.Remove(fname)
t.Errorf("sql.Open(\"sqlite3\", %q): %v", "file:"+fname, err)
continue
}
_, err = db.Exec("SELECT id FROM test")
db.Close()
if err == nil {
os.Remove(fname)
t.Errorf("didn't expect to be able to access the encrypted database %q without a passphrase", fname)
continue
}
badUri := "file:" + fname + "?_key=bogus"
if cipher != "" {
badUri += "&_cipher=" + cipher
}
db, err = sql.Open("sqlite3", badUri)
if err != nil {
os.Remove(fname)
t.Errorf("sql.Open(\"sqlite3\", %q): %v", badUri, err)
continue
}
_, err = db.Exec("SELECT id FROM test")
db.Close()
if err == nil {
os.Remove(fname)
t.Errorf("didn't expect to be able to access the encrypted database %q with a bogus passphrase", fname)
continue
}
db, err = sql.Open("sqlite3", uri)
if err != nil {
os.Remove(fname)
t.Errorf("sql.Open(\"sqlite3\", %q): %v", uri, err)
continue
}
_, err = db.Exec("SELECT id FROM test")
db.Close()
os.Remove(fname)
if err != nil {
t.Errorf("unable to query test table for %q: %v", uri, err)
continue
}
}
}
}