2 # codec.test developed by Stephen Lombardo (Zetetic LLC)
3 # sjlombardo at zetetic dot net
6 # Copyright (c) 2018, ZETETIC LLC
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the ZETETIC LLC nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # This file implements regression tests for SQLite library. The
30 # focus of this script is testing code cipher features.
32 # NOTE: tester.tcl has overridden the definition of sqlite3 to
33 # automatically pass in a key value. Thus tests in this file
34 # should explicitly close and open db with sqlite_orig in order
35 # to bypass default key assignment.
37 set testdir [file dirname $argv0]
38 source $testdir/tester.tcl
39 source $testdir/sqlcipher.tcl
41 # backup from plaintext to plaintext
43 do_test sqlcipher-backup-plain-plain {
44 sqlite_orig db test.db
48 INSERT INTO t1 VALUES(1, randstr(16384,16384));
51 set md5a [execsql {SELECT md5sum(a,b) FROM t1}]
52 sqlite_orig db2 backup.db
53 sqlite3_backup B db2 main db main
54 lappend rc [B step -1]
60 sqlite_orig db backup.db
62 set md5b [execsql {SELECT md5sum(a,b) FROM t1}]
64 lappend rc [ execsql {
65 PRAGMA integrity_check;
68 lappend rc [string equal $md5a $md5b]
69 } {SQLITE_DONE SQLITE_OK ok 1}
71 file delete -force test.db
72 file delete -force backup.db
74 # backup from encrypted to encrypted
76 do_test sqlcipher-backup-encrypted-encrypted {
77 sqlite_orig db test.db
80 PRAGMA key = 'testkey';
82 INSERT INTO t1 VALUES(1, randstr(16384,16384));
84 set md5a [execsql {SELECT md5sum(a,b) FROM t1}]
86 sqlite_orig db2 backup.db
87 execsql { PRAGMA key = 'testkey' } db2;
89 sqlite3_backup B db2 main db main
90 lappend rc [B step -1]
96 sqlite_orig db backup.db
97 execsql { PRAGMA key = 'testkey' };
99 set md5b [execsql {SELECT md5sum(a,b) FROM t1}]
101 lappend rc [ execsql {
102 PRAGMA integrity_check;
103 PRAGMA cipher_integrity_check;
106 lappend rc [string equal $md5a $md5b]
108 } {SQLITE_DONE SQLITE_OK ok 1}
110 file delete -force test.db
111 file delete -force backup.db
113 # backup from plaintext to encrypted
115 do_test sqlcipher-backup-plain-encrypted {
116 sqlite_orig db test.db
119 CREATE TABLE t1(a,b);
120 INSERT INTO t1 VALUES(1, randstr(16384,16384));
123 sqlite_orig db2 backup.db
124 execsql { PRAGMA key = 'testkey' } db2;
126 lappend rc [catch {sqlite3_backup B db2 main db main}]
127 lappend rc [sqlite3_errcode db2]
128 lappend rc [sqlite3_errmsg db2]
129 } {1 SQLITE_ERROR {backup is not supported with encrypted databases}}
132 file delete -force test.db
133 file delete -force backup.db
135 # backup from encrypted to plaintext
137 do_test sqlcipher-backup-encrypted-plain {
138 sqlite_orig db test.db
141 PRAGMA key = 'testkey';
142 CREATE TABLE t1(a,b);
143 INSERT INTO t1 VALUES(1, randstr(16384,16384));
146 sqlite_orig db2 backup.db
148 lappend rc [catch {sqlite3_backup B db2 main db main}]
149 lappend rc [sqlite3_errcode db2]
150 lappend rc [sqlite3_errmsg db2]
151 } {1 SQLITE_ERROR {backup is not supported with encrypted databases}}
154 file delete -force test.db
155 file delete -force backup.db