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