29 lines
2.5 KiB
Java
29 lines
2.5 KiB
Java
package cn.ucvl.zhaodisplay;
|
|
import android.content.*;import android.database.sqlite.*;import org.json.*;
|
|
/** Production SQLite/Keystore logic runs under the fixture's separate UID and database. */
|
|
final class QueueStorageChecks {
|
|
static void check(boolean v,String msg){if(!v)throw new AssertionError(msg);}
|
|
static void run(Context c)throws Exception{
|
|
c.deleteDatabase("storage-check.db");SQLiteDatabase old=c.openOrCreateDatabase("storage-check.db",0,null);
|
|
old.execSQL("CREATE TABLE points(id TEXT PRIMARY KEY,owner TEXT NOT NULL,session TEXT NOT NULL,at INTEGER,payload BLOB NOT NULL,batch TEXT)");
|
|
old.execSQL("CREATE TABLE batches(id TEXT PRIMARY KEY,owner TEXT,session TEXT,payload BLOB,digest TEXT,count INTEGER,next_try INTEGER)");
|
|
old.execSQL("CREATE TABLE totals(owner TEXT PRIMARY KEY,uploaded INTEGER NOT NULL,freed INTEGER NOT NULL)");
|
|
old.execSQL("INSERT INTO points VALUES ('old','legacy','old-session',1,X'01',NULL)");old.setVersion(1);old.close();
|
|
StoredLocationQueue q=new StoredLocationQueue(c);
|
|
check(q.stats("legacy")[0]==1,"v1 pending rows survive migration");
|
|
check(q.getReadableDatabase().getVersion()==2,"database migrated to v2");
|
|
q.discard("legacy");String owner=StoredLocationQueue.digest("synthetic"),session="test-session";
|
|
JSONObject point=new JSONObject().put("latitude",0).put("longitude",0).put("accuracy",5).put("at",System.currentTimeMillis()/1000d);
|
|
q.latest(owner,point);check(q.latest(owner).getDouble("latitude")==0,"encrypted latest round trip");
|
|
check(q.append(owner,session,point),"encrypted sample persisted");
|
|
StoredLocationQueue.Batch batch=q.next(owner,System.currentTimeMillis()+1000,System.currentTimeMillis());
|
|
check(batch!=null,"batch created");JSONObject accepted=new JSONObject().put("batchId",batch.id).put("sha256",batch.digest).put("count",batch.count).put("state","accepted");
|
|
check(!q.acknowledge(owner,batch,accepted)&&q.stats(owner)[0]==1,"accepted receipt keeps local data");
|
|
try{q.acknowledge(owner,batch,new JSONObject(accepted.toString()).put("sha256","wrong").put("state","verified"));throw new AssertionError("mismatched receipt accepted");}catch(SecurityException expected){}
|
|
check(q.stats(owner)[0]==1,"invalid receipt keeps pending row");
|
|
check(q.acknowledge(owner,batch,accepted.put("state","verified")),"verified receipt clears batch");
|
|
check(q.stats(owner)[0]==0&&q.stats(owner)[2]==1,"only verified point removed");
|
|
q.clearLatest(owner);check(q.latest(owner)==null,"latest removed on stop");q.close();c.deleteDatabase("storage-check.db");
|
|
}
|
|
}
|