加密功能完成了 , 寫一個測試程式來試試看吧 !!
首先要說明 , 這個程式原作者是 Alvin , 我借用這個程式修改一下來測試 !!
這個程式會 Create /tmp/t01.db , 並且設定加密 key .
然後填寫一些固定的 data 到 db 中 , 並且dump , quary 等一些機本操作 !!
接著執行 rekey 功能 (移除 key ) , 如果不想移除 key 可以用 Ctrl + C 中斷 !!
然後用 cat 看看 t01.db 的內容 是否有加密 !!
執行完 rekey 後一樣有 dump , quary 功能 , 可以比對加密/ 沒加密是否有錯誤 !!
當然 rekey 後也可用 cat 看看 t01.db 的內容
增加方式如下:
A.增加一個資料夾 sql-test.並且建立 Andorid.mk file.
diff -Nuraw TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/Android.mk TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/Android.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/Android.mk 1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/Android.mk 2013-05-27 13:46:53.090414327 +0800
@@ -0,0 +1,32 @@
+##
+##
+## Build test program
+##
+##
+
+
+ifeq ($(CONFIG_PRJ_RELASE_TEST),y)
+
+LOCAL_PATH:= $(call my-dir)
+
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := sqlkey.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../dist
+
+LOCAL_SHARED_LIBRARIES := libsqlite
+
+LOCAL_CFLAGS += -I$(LOCAL_PATH) -DSQLITE_HAS_CODEC=1
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+
+LOCAL_MODULE_TAGS := debug
+
+LOCAL_MODULE := sqlkey
+
+include $(BUILD_EXECUTABLE)
+
+endif
+
B.建立一個 db.h , 這是程式程式要用 heard file.
diff -Nuraw TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/db.h TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/db.h
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/db.h 1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/db.h 2013-05-27 11:02:01.742571654 +0800
@@ -0,0 +1,38 @@
+#ifndef __DB_H__
+#define __DB_H__
+
+#include "i_basetype.h"
+#include <sqlite3.h>
+
+#define MAX_SQL_LEN 1024 //
+#define MAX_DB_CNT 50
+
+typedef struct {
+ sqlite3 *db;
+ SCHAR *cmd;
+ SCHAR *exe;
+ UINT opt;
+ SINT ret;
+ UINT *width;
+
+ int row;
+ int col;
+ UINT total;
+ FREE SCHAR **result;
+ SCHAR *errmsg;
+ SINT (*callback)(void *data, int argc, char **argv, char **columnNames);
+ VOID *callback_arg;
+} SQL_CMD;
+
+#define SQL_OPT_SHOW 0x01 // show result & message
+#define SQL_OPT_MFREE 0x80 // manually free
+
+
+extern sqlite3 *m_db;
+
+int sql_execx(SQL_CMD *sql, char *fmt, ...);
+void sql_freex(SQL_CMD *sql);
+sqlite3 *db_open(SCHAR* db_name);
+void db_close(sqlite3 *db);
+
+#endif // __DB_H__
C.建立 i_basetype.h , 主要定義一些基本的結構和資料 Type.
diff -Nuraw TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/i_basetype.h TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/i_basetype.h
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/i_basetype.h 1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/i_basetype.h 2013-05-27 11:01:46.815417060 +0800
@@ -0,0 +1,113 @@
+//
+#ifndef __I_BASETYPE_H__
+#define __I_BASETYPE_H__
+
+typedef char SCHAR; // 8-bit
+typedef short SSHORT; // 16-bit
+typedef int SINT; // 32-bit
+typedef long SLONG; // 32-bit
+typedef long long SLONGLONG; // 64-bit
+/*
+typedef signed char SCHAR; // 8-bit
+typedef signed short SSHORT; // 16-bit
+typedef signed int SINT; // 32-bit
+typedef signed long SLONG; // 32-bit
+typedef signed long long SLONGLONG; // 64-bit
+*/
+
+typedef unsigned char UCHAR; // 8-bit
+typedef unsigned short USHORT; // 16-bit
+typedef unsigned int UINT; // 32-bit
+typedef unsigned long ULONG; // 32-bit
+typedef unsigned long long ULONGLONG; // 64-bit
+
+#ifndef __BASE_TYPE_DEFINED__
+#define __BASE_TYPE_DEFINED__
+
+typedef unsigned int BOOL; // 32-bit
+
+typedef unsigned char UINT8; // 8-bit
+typedef unsigned short UINT16; // 16-bit
+typedef unsigned int UINT32; // 32-bit
+typedef unsigned long long UINT64; // 64-bit
+
+typedef signed char SINT8; // 8-bit
+typedef signed short SINT16; // 16-bit
+typedef signed int SINT32; // 32-bit
+typedef signed long long SINT64; // 64-bit
+
+#endif // __BASE_TYPE_DEFINED__
+
+typedef void VOID; // 32-bit
+typedef float FLOAT; // 32-bit
+typedef double DOUBLE; // 64-bit
+
+typedef struct {
+ union {
+ SCHAR c;
+ SSHORT s;
+ SINT i;
+ SLONG l;
+ SLONGLONG ll;
+ UCHAR b;
+ USHORT w;
+ UINT u;
+ ULONG q;
+ ULONGLONG lq;
+ FLOAT f;
+ DOUBLE d;
+ VOID *p;
+ } v;
+} VALUE; // 64-bit/8 bytes
+
+#ifndef IO
+ #define IO
+#endif
+#ifndef IN
+ #define IN
+#endif
+#ifndef OUT
+ #define OUT
+#endif
+
+#ifndef FREE
+ #define FREE
+#endif
+
+#ifndef TRUE
+ #define TRUE 1
+#endif
+
+#ifndef FALSE
+ #define FALSE 0
+#endif
+
+#ifndef MIN
+ #define MIN(x,y) ((x)<(y)?(x):(y))
+#endif
+
+#ifndef MAX
+ #define MAX(x,y) ((x)>(y)?(x):(y))
+#endif
+
+#ifndef ABS
+ #define ABS(x) (((x)>=0)? (x): -(x))
+#endif
+
+#ifndef DIM
+ #define DIM(var,type) (sizeof(var)/sizeof(type))
+#endif
+
+#ifndef OFFS
+ #define OFFS(type,item) ((UINT)&(((type*)0)->item))
+#endif
+
+#ifndef SIZE
+ #define SIZE(type,item) (sizeof(((type*)0)->item))
+#endif
+
+#ifndef BITMASK
+ #define BITMASK(b) ((ULONG)0x01UL << (b))
+#endif
+
+#endif // __I_BASETYPE_H__
D. 主要程式.
diff -Nuraw TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/sqlkey.c TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/sqlkey.c
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/external/sqlite/sql-test/sqlkey.c 1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/external/sqlite/sql-test/sqlkey.c 2013-05-27 10:56:06.118859064 +0800
@@ -0,0 +1,346 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#include "db.h"
+#include <sqlite3.h>
+
+
+#define DB_FILE "/tmp/t01.db"
+
+//========================
+sqlite3 *m_db=NULL;
+
+int sql_execx(SQL_CMD *sql, char *fmt, ...)
+{
+ static char cmd[PATH_MAX];
+ int rc=0, sel=0;
+ va_list args;
+
+ if(m_db==NULL)
+ {
+ printf("database not opened.\n");
+ return(-100);
+ }
+ sql->db=m_db;
+ sql->exe=cmd;
+ sql->ret=0;
+ sql->row=0;
+ sql->col=0;
+ sql->total=0;
+ sql->result=NULL;
+ sql->errmsg=NULL;
+ //
+ va_start(args, fmt);
+ cmd[PATH_MAX - 1] = '\0';
+ sprintf(cmd, fmt, args);
+ va_end(args);
+
+ if(strncmp(cmd, "select", 6)==0)
+ sel=1;
+ if(sel)
+ rc = sqlite3_get_table(sql->db, cmd, &sql->result, &sql->row, &sql->col, &sql->errmsg);
+ else
+ rc = sqlite3_exec(sql->db, cmd, NULL /* callback */, NULL /* callback_arg */, &sql->errmsg);
+ if(SQLITE_OK != rc)
+ {
+ if(sql->opt&SQL_OPT_SHOW)
+ printf("* SQL error! cmd='%s', err=%d (%s).\n", cmd, rc, sql->errmsg);
+ return(-rc);
+ }
+ if(sel)
+ {
+ int r, c;
+
+ rc=sql->row;
+ if(sql->opt&SQL_OPT_SHOW)
+ {
+ printf("--------------------------------------\n");
+ printf("SELECT: row=%d, col=%d, rc=%d.\n", sql->row, sql->col, rc);
+ printf("--------------------------------------\n");
+ for(r=0; r<=sql->row; r++)
+ {
+ for(c=0; c<sql->col; c++)
+ printf("%-*s", (sql->width? sql->width[c]: 16), sql->result[(r*sql->col)+c]);
+ if(sql->row)
+ printf("\n");
+ }
+ printf("--------------------------------------\n");
+ }
+ if((sql->opt&SQL_OPT_MFREE)==0)
+ {
+ sqlite3_free_table(sql->result);
+ sql->result=NULL;
+ }
+ }
+ return(sql->ret=rc);
+}
+
+void sql_freex(SQL_CMD *sql)
+{
+ if(sql->result)
+ {
+ sqlite3_free_table(sql->result);
+ sql->result=NULL;
+ }
+}
+
+sqlite3 *db_open(char* db_name)
+{
+ int rc=0;
+ sqlite3 *db=NULL;
+
+ //open a database, create it if doesn't exist.
+ if((rc=sqlite3_open(db_name, &db))!=SQLITE_OK)
+ {
+ printf("sqlite3_open() failed, db='%s', err=%d (%s).\n", db_name, rc, sqlite3_errmsg(db));
+ return(NULL);
+ }
+
+ m_db = db;
+ return(db);
+}
+
+void db_close(sqlite3 *db)
+{
+ if(db)
+ sqlite3_close(db);
+
+}
+
+void ap_initial(void)
+{
+ if((m_db=db_open(DB_FILE))==NULL)
+ printf("db_open() failed, file=%s\n", DB_FILE);
+}
+
+void ap_shutdown(void)
+{
+ if(m_db)
+ db_close(m_db);
+}
+
+int main(int argc, char* argv[])
+{
+ int rc=0;
+
+ SQL_CMD sql;
+
+ printf("=== SQLite test utility v0.1 - Alvin, 2009/03/08 ===\n");
+ ap_initial();
+
+ sqlite3_key(m_db,"1q2w3e4r",8);
+
+
+ memset(&sql, 0, sizeof(sql));
+ sql.db = m_db;
+ sql.opt = SQL_OPT_SHOW;
+
+/*
+create table dict_tbl(vocab text, sentence text, name text);
+*/
+
+ printf("Testing Create Table\n");
+ getchar();
+
+ if((rc=sql_execx(&sql, "create table dict_tbl(vocab text, sentence text, name text)"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("=== Done. ===\n");
+
+ printf("Testing Insert data\n");
+ getchar();
+
+
+
+//insert into dict_tbl values('a', 'This is an apple.', '陳奕迅先生');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('a', 'This is an apple.', '陳奕迅先生')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('book', 'This is a book.', '陳曉東先生');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('book', 'This is a book.', '陳曉東先生')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('he', 'He is a good student.', '張宇先生');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('he', 'He is a good student.', '張宇先生')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('let', "Let's go for shopping.", '張信哲先生');
+
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('let', 'Let-s go for shopping.','張信哲先生')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('sample', 'The sample is very easy.', '張學友先生');
+
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('sample', 'The sample is very easy.', '張學友先生')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('she', 'She is more beautiful than you.', '范瑋琪小姐');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('she', 'She is more beautiful than you.', '范瑋琪小姐')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('that', 'That is my desk.', '陳珊妮小姐');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('that', 'That is my desk.', '陳珊妮小姐')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+//insert into dict_tbl values('this', 'This is your sample.', '張惠妹小姐');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('this', 'This is your sample.', '張惠妹小姐')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+//insert into dict_tbl values('tomorrow', 'I will go to USA tomorrow.', '陳慧琳小姐');
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('tomorrow', 'I will go to USA tomorrow.', '陳慧琳小姐')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+//insert into dict_tbl values('you', 'You are so beautiful.', '張清芳小姐');
+
+ if((rc=sql_execx(&sql, "insert into dict_tbl values('you', 'You are so beautiful.', '張清芳小姐')"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("=== Done. ===\n");
+
+ printf("Testing dump data \n");
+ getchar();
+
+ if((rc=sql_execx(&sql, "select * from dict_tbl"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("=== Done. ===\n");
+
+ printf("Testing quary '*you*' data \n");
+ getchar();
+
+//select sentence from dict_tbl where sentence like '%you%';
+ if((rc=sql_execx(&sql, "select sentence from dict_tbl where sentence like '%%you%%'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("Testing quary '*is*you*' data \n");
+ getchar();
+
+//select sentence from dict_tbl where sentence like '%is%you%';
+ if((rc=sql_execx(&sql, "select sentence from dict_tbl where sentence like \"%%is%%you%%\""))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '*is*a*' data \n");
+ getchar();
+
+//select sentence from dict_tbl where sentence like '%is%a%';
+ if((rc=sql_execx(&sql, "select sentence from dict_tbl where sentence like '%%is%%a%%'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("=== Done. ===\n");
+ printf("Testing rekey\n");
+ getchar();
+
+ sqlite3_rekey(m_db,"",0);
+
+ printf("=== Done. ===\n");
+ printf("Testing dump data after rekey\n");
+ getchar();
+
+
+ if((rc=sql_execx(&sql, "select * from dict_tbl"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+ printf("=== Done. ===\n");
+
+ printf("Testing quary '陳*' and sentence like '*book*' data \n");
+ getchar();
+
+//select * from dict_tbl where name like '陳%' and sentence like '%book%';
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '陳%%' and sentence like '%%book%%'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '張*' data \n");
+ getchar();
+
+//select * from dict_tbl where name like '張%';
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '張%%'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '陳*先生' data \n");
+ getchar();
+
+//select * from dict_tbl where name like '陳%先生';
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '陳%%先生'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '張*小姐' data \n");
+ getchar();
+//select * from dict_tbl where name like '張%小姐';
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '張%%小姐'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '*小姐' data \n");
+ getchar();
+
+//select * from dict_tbl where name like '%小姐';
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '%%小姐'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+
+ printf("Testing quary '*先生' data \n");
+ getchar();
+
+//select * from dict_tbl where name like '%先生'; --> error
+
+ if((rc=sql_execx(&sql, "select * from dict_tbl where name like '%先生'"))<0)
+ {
+ printf("* SQL error, cmd=%s, rc=%d (%s).\n", sql.exe, rc, sql.errmsg);
+ }
+
+ printf("=== Done. ===\n");
+ getchar();
+
+ sql_freex(&sql);
+ ap_shutdown();
+
+ return 0;
+}
結果 ...... 正確沒有問題 , 打完收工 .......
沒有留言:
張貼留言