SQLite Android Bindings
Check-in [0b5fd0b0d3]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add extra SEE tests. And fix problems revealed by the same.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0b5fd0b0d3a7dccf95bb35e5569e24e0768d863d
User & Date: dan 2013-12-24 18:51:53
Context
2013-12-24
19:16
Add a test to check that SQLiteOpenHelper works with SEE. check-in: 60c548bff9 user: dan tags: trunk
18:51
Add extra SEE tests. And fix problems revealed by the same. check-in: 0b5fd0b0d3 user: dan tags: trunk
12:07
In SQLITE_HAS_CODEC builds, do not initialize the LOCALIZED collation automatically (as if the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag was set). Require apps to call the enableLocalizedCollators() method to explicitly initialize it. This gives the app an opportunity to execute a PRAGMA statement to configure an encryption key before the database is first accessed. check-in: 9c4a073c3b user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to jni/Android.mk.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	android_database_SQLiteDebug.cpp      \
	JNIHelp.cpp JniConstants.cpp

#
# For a SEE build, add the SEE sources to the tree and uncomment the first
# two of the following three lines.
#
LOCAL_SRC_FILES += sqlite3-see.c
LOCAL_CFLAGS    += -DSQLITE_HAS_CODEC
# LOCAL_SRC_FILES += sqlite3.c

LOCAL_C_INCLUDES += nativehelper/

LOCAL_MODULE:= libsqliteX
LOCAL_LDLIBS += -ldl -llog 

include $(BUILD_SHARED_LIBRARY)








|
|
|








22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	android_database_SQLiteDebug.cpp      \
	JNIHelp.cpp JniConstants.cpp

#
# For a SEE build, add the SEE sources to the tree and uncomment the first
# two of the following three lines.
#
#    LOCAL_SRC_FILES += sqlite3-see.c
#    LOCAL_CFLAGS    += -DSQLITE_HAS_CODEC
LOCAL_SRC_FILES += sqlite3.c

LOCAL_C_INCLUDES += nativehelper/

LOCAL_MODULE:= libsqliteX
LOCAL_LDLIBS += -ldl -llog 

include $(BUILD_SHARED_LIBRARY)

Changes to src/org/sqlite/app/customsqlite/CustomSqlite.java.

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
..
62
63
64
65
66
67
68





















69
70
71
72
73
74
75
..
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
...
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
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;



import java.lang.InterruptedException;

import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;


import android.database.Cursor;

/*
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
*/









public class CustomSqlite extends Activity
{
  private TextView myTV;          /* Text view widget */
  private int myNTest;            /* Number of tests attempted */
  private int myNErr;             /* Number of tests failed */

................................................................................
    } else {
      myNErr++;
      myTV.append("FAILED\n");
      myTV.append("   res=     \"" + res + "\"\n");
      myTV.append("   expected=\"" + expected + "\"\n");
    }
  }






















  /*
  ** Test that a database connection may be accessed from a second thread.
  */
  public void thread_test_1(){
    SQLiteDatabase.deleteDatabase(DB_PATH);
    final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
................................................................................
    } catch (InterruptedException e) {
    }
  }

  /*
  ** Use a Cursor to loop through the results of a SELECT query.
  */
  public void csr_test_1(){
    SQLiteDatabase.deleteDatabase(DB_PATH);
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    String res = "";

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
................................................................................
      for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){
        String x = c.getString(0);
        res = res + "." + x;
      }
    }else{
      test_warning("csr_test_1", "c==NULL");
    }



    test_result("csr_test_1", res, ".one.two.three");













  }

  /*
  ** Check that using openSeeDatabase() creates encrypted databases. 
  */
  public void see_test_1(){


    SQLiteDatabase.deleteDatabase(DB_PATH);
    String res = "";
    
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    db.execSQL("PRAGMA key = 'secretkey'");

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
    Cursor c = db.rawQuery("SELECT x FROM t1", null);
    if( c!=null ){
      boolean bRes;
      for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){

        String x = c.getString(0);
        res = res + "." + x;
      }

    }else{
      test_warning("see_test_1", "c==NULL");


    }











    test_result("see_test_1", res, ".one.two.three");














  }

  public void run_the_tests(View view){
    System.loadLibrary("sqliteX");
    DB_PATH = getApplicationContext().getDatabasePath("test.db");
    DB_PATH.mkdirs();








>
>





>







>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







>

>
|
>
>
>
>
>
>
>
>
>
>
>
>
>





|
>
>









|
|
|
<
>
|
<
|
>
|
|
>
>
|
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
..
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
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
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
217
218
219
220
221
222
223
224
225
226
227
228
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;

import java.lang.InterruptedException;

import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;
import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException;

import android.database.Cursor;

/*
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
*/

import org.sqlite.database.DatabaseErrorHandler;
class DoNotDeleteErrorHandler implements DatabaseErrorHandler {
  private static final String TAG = "DoNotDeleteErrorHandler";
  public void onCorruption(SQLiteDatabase dbObj) {
    Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
  }
}

public class CustomSqlite extends Activity
{
  private TextView myTV;          /* Text view widget */
  private int myNTest;            /* Number of tests attempted */
  private int myNErr;             /* Number of tests failed */

................................................................................
    } else {
      myNErr++;
      myTV.append("FAILED\n");
      myTV.append("   res=     \"" + res + "\"\n");
      myTV.append("   expected=\"" + expected + "\"\n");
    }
  }

  /*
  ** Test if the database at DB_PATH is encrypted or not. The db
  ** is assumed to be encrypted if the first 6 bytes are anything
  ** other than "SQLite".
  **
  ** If the test reveals that the db is encrypted, return the string
  ** "encrypted". Otherwise, "unencrypted".
  */
  public String db_is_encrypted() throws Exception {
    FileInputStream in = new FileInputStream(DB_PATH);

    byte[] buffer = new byte[6];
    in.read(buffer, 0, 6);

    String res = "encrypted";
    if( Arrays.equals(buffer, (new String("SQLite")).getBytes()) ){
      res = "unencrypted";
    }
    return res;
  }

  /*
  ** Test that a database connection may be accessed from a second thread.
  */
  public void thread_test_1(){
    SQLiteDatabase.deleteDatabase(DB_PATH);
    final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
................................................................................
    } catch (InterruptedException e) {
    }
  }

  /*
  ** Use a Cursor to loop through the results of a SELECT query.
  */
  public void csr_test_1() throws Exception {
    SQLiteDatabase.deleteDatabase(DB_PATH);
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    String res = "";

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
................................................................................
      for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){
        String x = c.getString(0);
        res = res + "." + x;
      }
    }else{
      test_warning("csr_test_1", "c==NULL");
    }
    test_result("csr_test_1.1", res, ".one.two.three");

    db.close();
    test_result("csr_test_1.2", db_is_encrypted(), "unencrypted");
  }

  public String string_from_t1_x(SQLiteDatabase db){
    String res = "";

    Cursor c = db.rawQuery("SELECT x FROM t1", null);
    boolean bRes;
    for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){
      String x = c.getString(0);
      res = res + "." + x;
    }

    return res;
  }

  /*
  ** Check that using openSeeDatabase() creates encrypted databases. 
  */
  public void see_test_1() throws Exception {
    if( !SQLiteDatabase.hasCodec() ) return;

    SQLiteDatabase.deleteDatabase(DB_PATH);
    String res = "";
    
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
    db.execSQL("PRAGMA key = 'secretkey'");

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
    res = string_from_t1_x(db);
    test_result("see_test_1.1", res, ".one.two.three");
    db.close();


    test_result("see_test_1.2", db_is_encrypted(), "encrypted");


    db = SQLiteDatabase.openOrCreateDatabase(DB_PATH.getPath(), null, new DoNotDeleteErrorHandler());
    db.execSQL("PRAGMA key = 'secretkey'");
    res = string_from_t1_x(db);
    test_result("see_test_1.3", res, ".one.two.three");
    db.close();

    res = "unencrypted";
    try {
      db = SQLiteDatabase.openOrCreateDatabase(
	  DB_PATH.getPath(), null, new DoNotDeleteErrorHandler()
      );
      string_from_t1_x(db);
    } catch ( SQLiteDatabaseCorruptException e ){
      res = "encrypted";
    } finally {
      db.close();
    }
    test_result("see_test_1.4", res, "encrypted");

    res = "unencrypted";
    try {
      db = SQLiteDatabase.openOrCreateDatabase(
	  DB_PATH.getPath(), null, new DoNotDeleteErrorHandler()
      );
      db.execSQL("PRAGMA key = 'otherkey'");
      string_from_t1_x(db);
    } catch ( SQLiteDatabaseCorruptException e ){
      res = "encrypted";
    } finally {
      db.close();
    }
    test_result("see_test_1.5", res, "encrypted");
  }

  public void run_the_tests(View view){
    System.loadLibrary("sqliteX");
    DB_PATH = getApplicationContext().getDatabasePath("test.db");
    DB_PATH.mkdirs();

Changes to src/org/sqlite/database/sqlite/SQLiteConnection.java.

212
213
214
215
216
217
218
219
220
221
222

223
224
225
226
227
228
229
    private void open() {
        mConnectionPtr = nativeOpen(mConfiguration.path, mConfiguration.openFlags,
                mConfiguration.label,
                SQLiteDebug.DEBUG_SQL_STATEMENTS, SQLiteDebug.DEBUG_SQL_TIME);

        setPageSize();
        setForeignKeyModeFromConfiguration();
        setWalModeFromConfiguration();
        setJournalSizeLimit();
        setAutoCheckpointInterval();
	if( !nativeHasCodec() ){

          setLocaleFromConfiguration();
	}

        // Register custom functions.
        final int functionCount = mConfiguration.customFunctions.size();
        for (int i = 0; i < functionCount; i++) {
            SQLiteCustomFunction function = mConfiguration.customFunctions.get(i);







<

|

>







212
213
214
215
216
217
218

219
220
221
222
223
224
225
226
227
228
229
    private void open() {
        mConnectionPtr = nativeOpen(mConfiguration.path, mConfiguration.openFlags,
                mConfiguration.label,
                SQLiteDebug.DEBUG_SQL_STATEMENTS, SQLiteDebug.DEBUG_SQL_TIME);

        setPageSize();
        setForeignKeyModeFromConfiguration();

        setJournalSizeLimit();
	setAutoCheckpointInterval();
	if( !nativeHasCodec() ){
	  setWalModeFromConfiguration();
          setLocaleFromConfiguration();
	}

        // Register custom functions.
        final int functionCount = mConfiguration.customFunctions.size();
        for (int i = 0; i < functionCount; i++) {
            SQLiteCustomFunction function = mConfiguration.customFunctions.get(i);