Skip to content

Commit ce50fe4

Browse files
Merge branch 'brodybits-closed-and-corrupt-db-test'
2 parents 95c3910 + aaf255d commit ce50fe4

File tree

7 files changed

+442
-16
lines changed

7 files changed

+442
-16
lines changed

AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package="net.zetetic" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
44
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="21"/>
55
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="ZeteticApplication">
6-
<activity android:name=".TestSuiteActivity">
6+
<activity android:name=".TestSuiteActivity" android:screenOrientation="portrait">
77
<intent-filter>
88
<action android:name="android.intent.action.MAIN"/>
99
<category android:name="android.intent.category.LAUNCHER"/>

assets/corrupt.db

321 Bytes
Binary file not shown.

src/main/java/net/zetetic/tests/AttachDatabaseTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package net.zetetic.tests;
22

3-
43
import net.sqlcipher.database.SQLiteDatabase;
54
import net.zetetic.QueryHelper;
5+
66
import net.zetetic.ZeteticApplication;
77

8-
import javax.management.Query;
98
import java.io.File;
109

1110
public class AttachDatabaseTest extends SQLCipherTest {
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
package net.zetetic.tests;
2+
3+
import android.util.Log;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import java.util.Locale;
9+
10+
import net.sqlcipher.database.SQLiteDatabase;
11+
import net.sqlcipher.database.SQLiteException;
12+
import net.zetetic.ZeteticApplication;
13+
14+
public class ClosedDatabaseTest extends SQLCipherTest {
15+
16+
@Override
17+
public TestResult run() {
18+
19+
TestResult result = new TestResult(getName(), false);
20+
try {
21+
result.setResult(execute(null));
22+
SQLiteDatabase.releaseMemory();
23+
} catch (Exception e) {
24+
Log.v(ZeteticApplication.TAG, e.toString());
25+
}
26+
return result;
27+
}
28+
29+
@Override
30+
public boolean execute(SQLiteDatabase null_database_ignored) {
31+
32+
File testDatabasePath = ZeteticApplication.getInstance().getDatabasePath("closed-db-test.db");
33+
34+
boolean status = false;
35+
36+
try {
37+
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(testDatabasePath, "", null);
38+
39+
database.close();
40+
41+
status = execute_closed_database_tests(database);
42+
} catch (Exception e) {
43+
// Uncaught [unexpected] exception:
44+
Log.e(ZeteticApplication.TAG, "Unexpected exception", e);
45+
return false;
46+
}
47+
finally {
48+
testDatabasePath.delete();
49+
}
50+
51+
return status;
52+
}
53+
54+
boolean execute_closed_database_tests(SQLiteDatabase database) {
55+
try {
56+
/* operations that check if db is closed (and throw IllegalStateException): */
57+
try {
58+
// should throw IllegalStateException:
59+
database.beginTransaction();
60+
61+
// should not get here:
62+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.beginTransaction() did NOT throw exception on closed database");
63+
return false;
64+
} catch (IllegalStateException e) {
65+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.beginTransaction() did throw exception on closed database OK", e);
66+
}
67+
68+
try {
69+
// should throw IllegalStateException:
70+
database.endTransaction();
71+
72+
// should not get here:
73+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.endTransaction() did NOT throw exception on closed database");
74+
return false;
75+
} catch (IllegalStateException e) {
76+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.endTransaction() did throw exception on closed database OK", e);
77+
}
78+
79+
try {
80+
// should throw IllegalStateException:
81+
database.setTransactionSuccessful();
82+
83+
// should not get here:
84+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setTransactionSuccessful() did NOT throw throw exception on closed database");
85+
return false;
86+
} catch (IllegalStateException e) {
87+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setTransactionSuccessful() did throw exception on closed database OK", e);
88+
}
89+
90+
try {
91+
// should throw IllegalStateException:
92+
database.getVersion();
93+
94+
// should not get here:
95+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getVersion() did NOT throw exception on closed database");
96+
return false;
97+
} catch (IllegalStateException e) {
98+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getVersion() did throw exception on closed database OK", e);
99+
}
100+
101+
try {
102+
// should throw IllegalStateException:
103+
database.setVersion(111);
104+
105+
// should not get here:
106+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setVersion() did NOT throw exception on closed database");
107+
return false;
108+
} catch (IllegalStateException e) {
109+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setVersion() did throw exception on closed database OK", e);
110+
}
111+
112+
try {
113+
// should throw IllegalStateException:
114+
database.getMaximumSize();
115+
116+
// should not get here:
117+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getMaximumSize() did NOT throw exception on closed database");
118+
return false;
119+
} catch (IllegalStateException e) {
120+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getMaximumSize() did throw exception on closed database OK", e);
121+
}
122+
123+
try {
124+
// should throw IllegalStateException:
125+
database.setMaximumSize(111);
126+
127+
// should not get here:
128+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setMaximumSize() did NOT throw exception on closed database");
129+
return false;
130+
} catch (IllegalStateException e) {
131+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setMaximumSize() did throw exception on closed database OK", e);
132+
}
133+
134+
try {
135+
// should throw IllegalStateException:
136+
database.getPageSize();
137+
138+
// should not get here:
139+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getPageSize() did NOT throw exception on closed database");
140+
return false;
141+
} catch (IllegalStateException e) {
142+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getPageSize() did throw exception on closed database OK", e);
143+
}
144+
145+
try {
146+
// should throw IllegalStateException:
147+
database.setPageSize(111);
148+
149+
// should not get here:
150+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setPageSize() did NOT throw exception on closed database");
151+
return false;
152+
} catch (IllegalStateException e) {
153+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setPageSize() did throw exception on closed database OK", e);
154+
}
155+
156+
try {
157+
// should throw IllegalStateException:
158+
database.compileStatement("SELECT 1;");
159+
160+
// should not get here:
161+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.compileStatement() did NOT throw exception on closed database");
162+
return false;
163+
} catch (IllegalStateException e) {
164+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.compileStatement() did throw exception on closed database OK", e);
165+
}
166+
167+
try {
168+
// should throw IllegalStateException:
169+
database.query("t1", new String[]{"a", "b"}, null, null, null, null, null);
170+
171+
// should not get here:
172+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.query() did NOT throw exception on closed database");
173+
return false;
174+
} catch (IllegalStateException e) {
175+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.query() did throw exception on closed database OK", e);
176+
}
177+
178+
// TODO: cover more query functions
179+
180+
try {
181+
// should throw IllegalStateException:
182+
database.execSQL("SELECT 1;");
183+
184+
// should not get here:
185+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String) did NOT throw exception on closed database");
186+
return false;
187+
} catch (IllegalStateException e) {
188+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String) did throw exception on closed database OK", e);
189+
}
190+
191+
try {
192+
// should throw IllegalStateException:
193+
database.execSQL("SELECT 1;", new Object[1]);
194+
195+
// should not get here:
196+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String, Object[]) did NOT throw exception on closed database");
197+
return false;
198+
} catch (IllegalStateException e) {
199+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String, Object[]) did throw exception on closed database OK", e);
200+
}
201+
202+
try {
203+
// should throw IllegalStateException:
204+
database.rawExecSQL("SELECT 1;");
205+
206+
// should not get here:
207+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.rawExecSQL() did NOT throw exception on closed database");
208+
return false;
209+
} catch (IllegalStateException e) {
210+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.rawExecSQL() did throw exception on closed database OK", e);
211+
}
212+
213+
/* operations that do not explicitly check if db is closed
214+
* ([should] throw SQLiteException on a closed database): */
215+
216+
try {
217+
// should throw IllegalStateException:
218+
database.setLocale(Locale.getDefault());
219+
220+
// should not get here:
221+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setLocale() did NOT throw exception on closed database");
222+
return false;
223+
} catch (SQLiteException e) {
224+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setLocale() did throw exception on closed database OK", e);
225+
}
226+
227+
try {
228+
// [should] throw an exception on a closed database:
229+
database.changePassword("new-password");
230+
231+
// should not get here:
232+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(String) did NOT throw exception on closed database");
233+
return false;
234+
} catch (SQLiteException e) {
235+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(String) did throw exception on closed database OK", e);
236+
}
237+
238+
try {
239+
// [should] throw an exception on a closed database:
240+
database.changePassword("new-password".toCharArray());
241+
242+
// should not get here:
243+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(char []) did NOT throw exception on closed database");
244+
return false;
245+
} catch (SQLiteException e) {
246+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(char []) did throw exception on closed database OK", e);
247+
}
248+
249+
try {
250+
// [should] throw an exception on a closed database:
251+
database.markTableSyncable("aa", "bb");
252+
253+
// should not get here:
254+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String) did NOT throw exception on closed database");
255+
return false;
256+
} catch (SQLiteException e) {
257+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String) did throw exception on closed database OK", e);
258+
}
259+
260+
// TBD SQLiteDatabase.markTableSyncable(String, String, String) does NOT throw exception on closed database:
261+
try {
262+
// NOTE: does not (yet) throw an exception on a closed database:
263+
database.markTableSyncable("aa", "bb", "cc");
264+
265+
// ...
266+
// should not get here:
267+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String, String) did NOT throw exception on closed database");
268+
return false;
269+
} catch (SQLiteException e) {
270+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String, String) did throw exception on closed database OK", e);
271+
272+
// SIGNAL that this test must be updated:
273+
//Log.e(ZeteticApplication.TAG, "BEHAVIOR CHANGED - please update the test");
274+
//return false;
275+
}
276+
277+
try {
278+
// should throw IllegalStateException [since it calls getVersion()]:
279+
database.needUpgrade(111);
280+
281+
// should not get here:
282+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.needUpgrade() did NOT throw exception on closed database");
283+
return false;
284+
} catch (IllegalStateException e) {
285+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.needUpgrade() did throw exception on closed database OK", e);
286+
}
287+
288+
/* operations that are NOT expected to throw an exception if the database is closed ([should] not crash) */
289+
290+
291+
/* XXX TODO: these functions should check the db state,
292+
* TBD either throw or simply return false if the db is closed */
293+
database.yieldIfContended();
294+
database.yieldIfContendedSafely();
295+
database.yieldIfContendedSafely(100);
296+
297+
database.setLockingEnabled(false);
298+
database.setLockingEnabled(true);
299+
300+
database.close();
301+
302+
database.isReadOnly();
303+
database.isOpen();
304+
305+
database.isInCompiledSqlCache("SELECT 1;");
306+
database.purgeFromCompiledSqlCache("SELECT 1;");
307+
database.resetCompiledSqlCache();
308+
309+
database.getMaxSqlCacheSize();
310+
311+
try {
312+
// should throw IllegalStateException:
313+
database.setMaxSqlCacheSize(111);
314+
315+
// should not get here:
316+
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setMaxSqlCacheSize() did NOT throw exception on closed database");
317+
return false;
318+
} catch (IllegalStateException e) {
319+
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setMaxSqlCacheSize() did throw exception on closed database OK", e);
320+
}
321+
322+
} catch (Exception e) {
323+
// Uncaught [unexpected] exception:
324+
Log.e(ZeteticApplication.TAG, "Unexpected exception", e);
325+
return false;
326+
}
327+
328+
return true;
329+
}
330+
331+
@Override
332+
public String getName() {
333+
return "Closed Database Test";
334+
}
335+
}

0 commit comments

Comments
 (0)