feat(location): durable offline GPS outbox and hourly daily uploads v0.1.50

This commit is contained in:
Codex
2026-10-19 00:57:33 +08:00
parent cd3ed420a2
commit e3afb2e07f
26 changed files with 697 additions and 135 deletions
@@ -5,7 +5,7 @@ public class LocationCadenceTest {
static void require(boolean value){if(!value)throw new AssertionError();}
public static void main(String[] args){
int assertions=0;
for(int seconds:new int[]{10,60,300}){
for(int seconds:new int[]{10,60,300,3600,7200,10800,14400,18000,23*3600,86400,172800,259200,30*86400}){
require(LocationCadence.seconds(seconds)==seconds);assertions++;
require(LocationCadence.due(100000,0,seconds,false));assertions++;
require(!LocationCadence.due(1000+seconds*1000L-1,1000,seconds,false));assertions++;
@@ -13,7 +13,10 @@ public class LocationCadenceTest {
require(!LocationCadence.due(10999,1000,seconds,true));assertions++;
require(LocationCadence.due(11000,1000,seconds,true));assertions++;
}
for(int invalid:new int[]{-1,0,9,59,301}){require(LocationCadence.seconds(invalid)==300);assertions++;}
for(int invalid:new int[]{-1,0,9,59,301,3601,31*86400,Integer.MAX_VALUE}){require(LocationCadence.seconds(invalid)==300);assertions++;}
require(LocationCadence.custom(3,true)==259200);assertions++;
require(LocationCadence.custom(5,false)==18000);assertions++;
for(int invalid:new int[]{-1,0,721,Integer.MAX_VALUE}){try{LocationCadence.custom(invalid,false);throw new AssertionError();}catch(IllegalArgumentException expected){assertions++;}}
System.out.println("Location cadence: "+assertions+" assertions passed");
}
}
@@ -0,0 +1,43 @@
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();}}
}
@@ -0,0 +1,36 @@
param([string]$ToolRoot='C:\Users\Administrator\Documents\ChatGPT\优创未来\services\toplc-vpn-android\.tools')
$ErrorActionPreference='Stop'
$tests=Split-Path -Parent $MyInvocation.MyCommand.Path
$source=Split-Path -Parent $tests
$stage=Join-Path 'C:\CodexBuild' ('ZhaoQueueCheck-'+[guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $stage,"$stage\classes","$stage\dex" | Out-Null
$jdk=Join-Path $ToolRoot 'jdk-staging\jdk-21.0.12.1+1'
$sdk=Join-Path $ToolRoot 'android-sdk'
$bt=Join-Path $sdk 'build-tools\36.0.0'
$android=Join-Path $sdk 'platforms\android-37.0\android.jar'
$env:JAVA_HOME=$jdk
$env:Path="$jdk\bin;$env:Path"
@'
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.ucvl.zhaodisplay.queuecheck" android:versionCode="1" android:versionName="1">
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="30"/>
<application android:label="轨迹队列隔离测试" android:allowBackup="false"/>
<instrumentation android:name="cn.ucvl.zhaodisplay.QueueInstrumentation" android:targetPackage="cn.ucvl.zhaodisplay.queuecheck"/>
</manifest>
'@ | Set-Content -LiteralPath "$stage\AndroidManifest.xml" -Encoding utf8
& "$bt\aapt2.exe" link -o "$stage\unsigned.apk" -I $android --manifest "$stage\AndroidManifest.xml"
if($LASTEXITCODE){throw 'Test resource linking failed'}
& "$jdk\bin\javac.exe" -encoding UTF-8 -source 8 -target 8 -cp $android -d "$stage\classes" "$source\src\cn\ucvl\zhaodisplay\LocationQueue.java" "$tests\QueueInstrumentation.java"
if($LASTEXITCODE){throw 'Test Java compile failed'}
& "$jdk\bin\jar.exe" cf "$stage\classes.jar" -C "$stage\classes" .
& "$bt\d8.bat" --lib $android --min-api 26 --output "$stage\dex" "$stage\classes.jar"
if($LASTEXITCODE){throw 'Test Dex compile failed'}
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip=[IO.Compression.ZipFile]::Open("$stage\unsigned.apk",[IO.Compression.ZipArchiveMode]::Update)
try{[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$stage\dex\classes.dex",'classes.dex',[IO.Compression.CompressionLevel]::Optimal) | Out-Null}finally{$zip.Dispose()}
& "$bt\zipalign.exe" -f 4 "$stage\unsigned.apk" "$stage\aligned.apk"
$keystore=Join-Path $env:LOCALAPPDATA 'ZhaoDisplay\display-signing.jks'
& "$bt\apksigner.bat" sign --ks $keystore --ks-pass pass:android --key-pass pass:android --ks-key-alias zhaodisplay --out "$source\build\QueueCheck.apk" "$stage\aligned.apk"
if($LASTEXITCODE){throw 'Test APK signing failed'}
& "$bt\apksigner.bat" verify "$source\build\QueueCheck.apk"
if($LASTEXITCODE){throw 'Test APK verification failed'}
Write-Output "$source\build\QueueCheck.apk"