44 lines
4.5 KiB
Java
44 lines
4.5 KiB
Java
|
|
package cn.ucvl.zhaodisplay;
|
||
|
|
|
||
|
|
import android.app.*;
|
||
|
|
import android.os.Bundle;
|
||
|
|
import android.database.Cursor;
|
||
|
|
import org.json.*;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
|
||
|
|
/** Isolated test package only. No GPS permission, family token or production data. */
|
||
|
|
public final class QueueInstrumentation extends Instrumentation {
|
||
|
|
int assertions=0;
|
||
|
|
void check(boolean condition){assertions++;if(!condition)throw new AssertionError("Assertion "+assertions);}
|
||
|
|
JSONObject position(double at)throws Exception{return new JSONObject().put("latitude",12.345678).put("longitude",45.678901).put("accuracy",7).put("at",at);}
|
||
|
|
JSONObject receipt(LocationQueue.Batch b,String state)throws Exception{return new JSONObject().put("batchId",b.id).put("sha256",b.digest).put("count",b.count).put("state",state);}
|
||
|
|
public void onCreate(Bundle args){super.onCreate(args);start();}
|
||
|
|
public void onStart(){Bundle result=new Bundle();LocationQueue queue=null;try{
|
||
|
|
getTargetContext().deleteDatabase("location-outbox.db");queue=new LocationQueue(getTargetContext());String owner=LocationQueue.digest("synthetic-owner"),other=LocationQueue.digest("synthetic-other"),session="a1111111111111111111111111111111";
|
||
|
|
for(int i=0;i<130;i++)check(queue.append(owner,session,position(1700000000+i)));
|
||
|
|
check(queue.append(other,session,position(1700000000)));
|
||
|
|
check(queue.stats(owner)[0]==130);check(queue.stats(other)[0]==1);
|
||
|
|
try(Cursor c=queue.getReadableDatabase().rawQuery("SELECT payload FROM points LIMIT 1",null)){c.moveToFirst();check(!new String(c.getBlob(0),StandardCharsets.UTF_8).contains("latitude"));}
|
||
|
|
LocationQueue.Batch batch=queue.next(owner,Long.MAX_VALUE,System.currentTimeMillis());check(batch.count==128);check(new JSONObject(batch.payload).getJSONArray("points").length()==128);
|
||
|
|
String digest=batch.digest,id=batch.id;queue.close();queue=new LocationQueue(getTargetContext());
|
||
|
|
batch=queue.next(owner,Long.MAX_VALUE,System.currentTimeMillis());check(batch.id.equals(id));check(batch.digest.equals(digest));check(queue.stats(owner)[0]==130);
|
||
|
|
try{queue.acknowledge(owner,batch,receipt(batch,"verified").put("sha256","f"));throw new AssertionError("Accepted corrupt receipt");}catch(SecurityException expected){assertions++;}
|
||
|
|
try{queue.acknowledge(owner,batch,receipt(batch,"verified").put("count",127));throw new AssertionError("Accepted partial receipt");}catch(SecurityException expected){assertions++;}
|
||
|
|
check(queue.stats(owner)[0]==130);check(!queue.acknowledge(owner,batch,receipt(batch,"accepted")));check(queue.stats(owner)[0]==130);
|
||
|
|
check(queue.append(owner,session,position(1700000500)));check(queue.acknowledge(owner,batch,receipt(batch,"verified")));check(queue.stats(owner)[0]==3);check(queue.stats(owner)[2]==128);check(queue.stats(other)[0]==1);
|
||
|
|
check(queue.next(owner,0,System.currentTimeMillis())==null);
|
||
|
|
batch=queue.next(owner,Long.MAX_VALUE,System.currentTimeMillis());check(batch.count==3);check(queue.acknowledge(owner,batch,receipt(batch,"verified")));check(queue.stats(owner)[0]==0);check(queue.stats(owner)[2]==131);check(queue.stats(owner)[3]>0);
|
||
|
|
queue.getWritableDatabase().beginTransaction();try{
|
||
|
|
for(int i=0;i<LocationQueue.MAX_POINTS-1;i++)queue.getWritableDatabase().execSQL("INSERT INTO points VALUES (?,?,?,0,?,NULL)",new Object[]{"cap-"+i,owner,session,new byte[16]});
|
||
|
|
queue.getWritableDatabase().setTransactionSuccessful();
|
||
|
|
}finally{queue.getWritableDatabase().endTransaction();}
|
||
|
|
check(queue.total()==LocationQueue.MAX_POINTS);check(!queue.append(owner,session,position(1700000600)));check(queue.total()==LocationQueue.MAX_POINTS);
|
||
|
|
queue.discard(owner);check(queue.stats(other)[0]==1);check(queue.stats(owner)[0]==0);
|
||
|
|
check(queue.append(owner,session,position(1700000700)));
|
||
|
|
queue.getWritableDatabase().execSQL("UPDATE points SET payload=zeroblob(40) WHERE owner=?",new Object[]{owner});
|
||
|
|
try{queue.next(owner,Long.MAX_VALUE,System.currentTimeMillis());throw new AssertionError("Decrypted corrupt payload");}catch(javax.crypto.AEADBadTagException expected){assertions++;}
|
||
|
|
check(queue.stats(owner)[0]==1);
|
||
|
|
result.putString("stream","PASS: "+assertions+" assertions; encrypted queue, restart, retry, receipts, owner isolation, capacity and tamper checks\n");finish(Activity.RESULT_OK,result);
|
||
|
|
}catch(Throwable error){result.putString("stream","FAIL: "+error.getClass().getSimpleName()+": "+error.getMessage()+"\n");finish(Activity.RESULT_CANCELED,result);}finally{if(queue!=null)queue.close();}}
|
||
|
|
}
|