-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_tests.sh
More file actions
executable file
·216 lines (171 loc) · 5.62 KB
/
cli_tests.sh
File metadata and controls
executable file
·216 lines (171 loc) · 5.62 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
#!/bin/bash
# Runs basic tests of the CLI tool.
set -e
# For verbose output (useful for debugging), run as: env V=1 ./cli_tests.sh
test "$V" = 1 && set -o xtrace
SQLFUSE=${SQLFUSE:-./sqlfuse}
FUSERMOUNT=${FUSERMOUNT:-$(which fusermount)}
if [ ! -x "${SQLFUSE}" ]; then
echo "sqlfuse binary (${SQLFUSE}) not found or not executable."
exit 1
fi
function sqlfuse() {
"${SQLFUSE}" "$@"
}
if [ ! -x "${FUSERMOUNT}" ]; then
echo "fusermount binary (${FUSERMOUNT}) not found or not executable."
exit 1
fi
function mount() {
CHILD_PID=$(${SQLFUSE} mount --print_pid "$@")
}
function unmount() {
fusermount -u "$1"
# fusermount sends a signal to the child process, but does not wait for it to
# exit. This causes a race condition where the database may still be locked if
# we try to re-open it immediately after fusermount returns. The command below
# waits for the child process to exit, which means it has released its locks.
tail --pid=${CHILD_PID} -f /dev/null
}
function fusermount() {
"${FUSERMOUNT}" "$@"
}
function cleanup() {
if [ $? -ne 0 ]; then
echo 'Test failed! For verbose output, rerun as: env V=1 ./cli_tests.sh'
fi
for dir in "${MNTDIR}" "${MNTDIR2}"; do
if [ -d "${dir}" ]; then
if mountpoint -q "${dir}"; then
unmount "${dir}" || echo "Couldn't unmount ${dir}"
fi
rmdir "${dir}" || echo "Couldn't remove mount dir ${dir}"
fi
done
rm -f "${DBFILE}" || echo "Couldn't remove db file ${DBFILE}"
rmdir "${TMPDIR}" || echo "Couldn't remove temp dir ${TMPDIR}"
}
TMPDIR=$(mktemp -d)
trap cleanup EXIT
MNTDIR=${TMPDIR}/mnt
mkdir "${MNTDIR}"
MNTDIR2=${TMPDIR}/mnt2
mkdir "${MNTDIR2}"
DBFILE=${TMPDIR}/db
function create_files() {
mkdir -p "$1/dir/subdir"
echo bla >"$1/dir/subdir/file"
touch "$1/empty"
}
function verify_files() {
test -f "$1/empty"
test ! -s "$1/empty"
test -d "$1/dir"
test -d "$1/dir/subdir"
test -s "$1/dir/subdir/file"
test "$(cat "$1/dir/subdir/file")" = bla
}
#
# Basic tests without a password
#
echo 'Testing without a password.'
# Create a test db
sqlfuse create --no_password "${DBFILE}"
# Mount it
mount --no_password -s "${DBFILE}" "${MNTDIR}"
mountpoint -q "${MNTDIR}"
# Fill it with some test files
create_files "${MNTDIR}"
verify_files "${MNTDIR}"
# Unmount should work
unmount "${MNTDIR}"
! mountpoint -q "${MNTDIR}"
test ! -e "${MNTDIR}"/dir
test ! -e "${MNTDIR}"/empty
# Compaction.
sqlfuse compact --no_password "${DBFILE}"
# Remount readonly. Files should still be there.
mount --no_password -s "${DBFILE}" "${MNTDIR}" -o ro
verify_files "${MNTDIR}"
! touch "${MNTDIR}/newfile" 2>/dev/null
# Can mount the DB twice in readonly mode.
mount --no_password -s "${DBFILE}" "${MNTDIR2}" -o ro
mountpoint -q "${MNTDIR2}"
verify_files "${MNTDIR2}"
unmount "${MNTDIR2}"
# Cannot mount a second time in writable mode.
! mount --no_password -s "${DBFILE}" "${MNTDIR2}" 2>/dev/null
! mountpoint -q "${MNTDIR2}"
unmount "${MNTDIR}"
# Cannot mount twice in writable mode.
mount --no_password -s "${DBFILE}" "${MNTDIR}"
! mount --no_password -s "${DBFILE}" "${MNTDIR2}" 2>/dev/null
unmount "${MNTDIR}"
rm "${DBFILE}"
#
# Basic tests with a password
#
echo 'Testing with a password.'
# Create a test db
sqlfuse create --insecure_password=password1 "${DBFILE}"
# Mount it
mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR}"
mountpoint -q "${MNTDIR}"
# Fill it with some test files
create_files "${MNTDIR}"
verify_files "${MNTDIR}"
# Unmount should work
unmount "${MNTDIR}"
! mountpoint -q "${MNTDIR}"
test ! -e "${MNTDIR}"/dir
test ! -e "${MNTDIR}"/empty
# Compaction.
! sqlfuse compact --insecure_password=password2 "${DBFILE}" 2>/dev/null
sqlfuse compact --insecure_password=password1 "${DBFILE}"
# Remount readonly. Files should still be there.
mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR}" -o ro
verify_files "${MNTDIR}"
! touch "${MNTDIR}/newfile" 2>/dev/null
# Can mount the DB twice in readonly mode.
mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR2}" -o ro
mountpoint -q "${MNTDIR2}"
verify_files "${MNTDIR2}"
unmount "${MNTDIR2}"
# Cannot mount a second time in writable mode.
! mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR2}" 2>/dev/null
! mountpoint -q "${MNTDIR2}"
unmount "${MNTDIR}"
# Cannot mount twice in writable mode
mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR}"
! mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR2}" 2>/dev/null
unmount "${MNTDIR}"
# Rekey.
! sqlfuse rekey --old_insecure_password=wrong --new_insecure_password=irrelevant "${DBFILE}" 2>/dev/null
sqlfuse rekey --old_insecure_password=password1 --new_insecure_password=password2 "${DBFILE}"
# Remount after rekeying.
! mount --insecure_password=password1 -s "${DBFILE}" "${MNTDIR}" 2>/dev/null
mount --insecure_password=password2 -s "${DBFILE}" "${MNTDIR}"
mountpoint -q "${MNTDIR}"
verify_files "${MNTDIR}"
unmount "${MNTDIR}"
rm "${DBFILE}"
#
# Backward compatibility: test opening v3 database.
#
mount --insecure_password=test -s testdata/v3.db "${MNTDIR}" -o ro
test "$(cat "${MNTDIR}/hello.txt")" = 'Hello, world!'
unmount "${MNTDIR}"
#
# Backward compatibility: migrate v3 to v4.
#
cp testdata/v3.db "${DBFILE}"
if ! sqlfuse cipher_migrate --insecure_password=test "${DBFILE}"; then
# If we're using SQLCipher v3, then we cannot migrate to v4.
# If migration fails for this reason, we won't consider than a test failure.
sqlfuse cipher_migrate --insecure_password=test "${DBFILE}" 2>&1 | grep -q 'SQLCipher version .* too low'
else
mount --insecure_password=test -s "${DBFILE}" "${MNTDIR}" -o ro
test "$(cat "${MNTDIR}/hello.txt")" = 'Hello, world!'
unmount "${MNTDIR}"
fi
echo 'All tests passed.'