|
| 1 | +package net.zetetic.database.sqlcipher_cts; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.is; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import android.database.Cursor; |
| 7 | + |
| 8 | +import net.zetetic.database.sqlcipher.SQLiteDatabase; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | + |
| 14 | +public class ImportUnencryptedDatabaseTest extends AndroidSQLCipherTestCase { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void shouldImportUnencryptedPlaintextDatabase(){ |
| 18 | + String a = null, b = null; |
| 19 | + File unencryptedDatabasePath = null; |
| 20 | + File encryptedDatabasePath = context.getDatabasePath("encrypted.db"); |
| 21 | + try { |
| 22 | + database.close(); |
| 23 | + unencryptedDatabasePath = extractAssetToDatabaseDirectory("unencrypted.db"); |
| 24 | + database = SQLiteDatabase.openDatabase(unencryptedDatabasePath.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE); |
| 25 | + database.execSQL("ATTACH DATABASE ? as encrypted KEY ?;", |
| 26 | + new Object[]{encryptedDatabasePath.getAbsolutePath(), "foo"}); |
| 27 | + database.execSQL("SELECT sqlcipher_export('encrypted');"); |
| 28 | + database.execSQL("DETACH DATABASE encrypted;"); |
| 29 | + database.close(); |
| 30 | + |
| 31 | + database = SQLiteDatabase.openDatabase(encryptedDatabasePath.getAbsolutePath(), "foo", |
| 32 | + null, SQLiteDatabase.OPEN_READWRITE, null); |
| 33 | + Cursor cursor = database.rawQuery("select * from t1", new String[]{}); |
| 34 | + if(cursor != null && cursor.moveToFirst()) { |
| 35 | + a = cursor.getString(0); |
| 36 | + b = cursor.getString(1); |
| 37 | + cursor.close(); |
| 38 | + } |
| 39 | + database.close(); |
| 40 | + assertThat(a, is("one for the money")); |
| 41 | + assertThat(b, is("two for the show")); |
| 42 | + |
| 43 | + } catch (Exception ex){ |
| 44 | + |
| 45 | + } finally { |
| 46 | + delete(unencryptedDatabasePath); |
| 47 | + delete(encryptedDatabasePath); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments