204 lines
12 KiB
Ziran
204 lines
12 KiB
Ziran
// Two devices of one account share entity records through a node: run with
|
|
// ENTITY_DATA set to the device's folder and ENTITY_RECORDS_STEP naming the
|
|
// step (scripts/records_sync_test.py starts a private node and the steps).
|
|
#import "account_store"
|
|
#import "entity_records"
|
|
#import "recovery"
|
|
#import "model"
|
|
#import "native_files"
|
|
#import "std/json_scan"
|
|
#import "std/c_string"
|
|
#import "std/timer_linux"
|
|
#import "std/text_buffer"
|
|
#import "std/byte_text_linux"
|
|
#import "mesh_client/keys"
|
|
#import "mesh_client/async_client"
|
|
#import "native_management"
|
|
#import "shared_account"
|
|
Auth :: #import "mesh_client/auth";
|
|
|
|
backup_observer: ManagementProvider;
|
|
|
|
BackupSettle :: (provider: *ManagementProvider) -> bool {
|
|
for round: 0..1200 {
|
|
ProviderBackupStatusPump(provider)
|
|
if !provider.backup_status_pending && provider.backup_status_next > 0 { return true }
|
|
unused SleepMilliseconds(25)
|
|
}
|
|
return false
|
|
}
|
|
|
|
BackupOperation :: (remove: bool) -> bool {
|
|
if remove { ProviderBackupOnlineRemove(*account_provider) }
|
|
else { ProviderBackupOnline(*account_provider, "Fixture password 1") }
|
|
if !account_provider.asynchronous { return false }
|
|
for round: 0..1200 {
|
|
if PollRequest(*account_provider.pending_request) {
|
|
success := account_provider.pending_request.status == 200
|
|
ProviderBackupOnlineFinished(*account_provider)
|
|
account_provider.asynchronous = false
|
|
return success
|
|
}
|
|
unused SleepMilliseconds(25)
|
|
}
|
|
return false
|
|
}
|
|
|
|
SyncField :: (reply: string, name: string, expected: string) -> bool {
|
|
value: [128]u8
|
|
length := ReadStringText(reply, FieldAt(reply, 0, name), value[:])
|
|
return length >= 0 && TextView(value[0:length]) == expected
|
|
}
|
|
|
|
// Pumps until a sync has reached the node's latest version and nothing
|
|
// waits to upload. The app first names its kind, as an app's list does.
|
|
SyncSettle :: () -> bool {
|
|
unused EntityRecordsAnswer("{\"operation\":\"list\",\"kind\":\"levels\"}")
|
|
unused EntityRecordsAnswer("{\"operation\":\"list\",\"kind\":\"game-progress\"}")
|
|
records_due = 0
|
|
start := records_completed
|
|
for round: 0..1200 {
|
|
EntityRecordsPump()
|
|
status := EntityRecordsAnswer("{\"operation\":\"status\"}")
|
|
if SyncField(status, "sync", "failed") { return false }
|
|
if records_completed > start && SyncField(status, "sync", "synced") { return true }
|
|
if records_completed > start && !records_running { records_due = 0 }
|
|
unused SleepMilliseconds(25)
|
|
}
|
|
return false
|
|
}
|
|
|
|
SyncTitle :: (id: string, expected: string) -> bool {
|
|
request: [128]u8
|
|
length: s64 = 0
|
|
for byte: "{\"operation\":\"get\",\"kind\":\"levels\",\"id\":\"" { request[length] = byte; length += 1 }
|
|
for byte: id { request[length] = byte; length += 1 }
|
|
for byte: "\"}" { request[length] = byte; length += 1 }
|
|
reply := EntityRecordsAnswer(TextView(request[0:length]))
|
|
document := FieldAt(reply, 0, "document")
|
|
if document < 0 { return false }
|
|
title: [64]u8
|
|
title_length := ReadStringText(reply, FieldAt(reply, document, "title"), title[:])
|
|
return title_length >= 0 && TextView(title[0:title_length]) == expected
|
|
}
|
|
|
|
#program_export
|
|
Check :: () -> s32 {
|
|
root := NativeEnvironment("ENTITY_DATA")
|
|
step := NativeEnvironment("ENTITY_RECORDS_STEP")
|
|
if step == "create" {
|
|
recovery: Recovery
|
|
if CreateAccountKeys(*recovery.keys) != 1 || !NativeDirectory(root) { return 1 }
|
|
text: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8
|
|
path: [4096]u8
|
|
if !RecoveryDeviceText(*recovery, text[:]) || !NativeWrite(NativeJoin(path[:], root, "account.key"), TextUntilNul(text[:])) { return 2 }
|
|
state := EntityDefault()
|
|
unused CopyCString(TextUntilNul(recovery.keys.public_id[:]), state.owner_id[:])
|
|
unused CopyCString(NativeEnvironment("ENTITY_RECORDS_NODE"), state.nodes[0][:])
|
|
document := EntityEncode(state)
|
|
if !NativeWrite(NativeJoin(path[:], root, "entity.json"), document) { return 3 }
|
|
return 0
|
|
}
|
|
if root == "" || !AccountStoreStartAt(root, "entity-test") { return 4 }
|
|
one := "0123456789abcdef0123456789abcdef"
|
|
two := "fedcba9876543210fedcba9876543210"
|
|
if step == "local" {
|
|
// No account yet: kept on this device until one is connected.
|
|
if account_standalone_ready { return 5 }
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"game-progress\",\"schema\":\"entity.game-progress.v1\",\"document\":{\"game\":\"shovey.classic\",\"unlocked\":5,\"completed\":\"001\",\"state\":\"local\"}}"), "status", "done") { return 26 }
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"levels\",\"schema\":\"entity.levels.v1\",\"id\":\"00000000000000000000000000000003\",\"document\":{\"title\":\"Offline\"}}"), "status", "done") { return 6 }
|
|
return ifx SyncField(EntityRecordsAnswer("{\"operation\":\"status\"}"), "sync", "local") then 0 else 7
|
|
}
|
|
if !account_standalone_ready { return 8 }
|
|
if step == "backup" {
|
|
backup_observer = ManagementProvider.{.snapshot = AccountStoreSnapshot, .action = AccountStoreAction, .client = AccountStoreClient}
|
|
ProviderBackedUpPump(*backup_observer)
|
|
if !BackupSettle(*backup_observer) || backup_observer.backup_status_request.status != 200 || ProviderState(*backup_observer).backed_up { return 30 }
|
|
if !BackupOperation(false) { return 31 }
|
|
backup_observer.backup_status_next = 0
|
|
if !BackupSettle(*backup_observer) || backup_observer.backup_status_request.status != 200 ||
|
|
backup_observer.backup_status_session.token[0] == cast(u8)0 || !ProviderState(*backup_observer).backed_up { return 32 }
|
|
if ProviderBackupStatusApply(*backup_observer, "{\"stored\":false} trailing") || !ProviderState(*backup_observer).backed_up { return 35 }
|
|
// A cached result for a different node or identity is never evidence.
|
|
old_node := account_profile.nodes[0]
|
|
backup_observer.backup_status_pending = true
|
|
backup_observer.backup_status_request = PendingRequest.{.done = true, .result = Auth.AuthResult.AUTH_OK, .status = 200}
|
|
unused CopyCString("{\"stored\":true}", backup_observer.backup_status_reply[:])
|
|
unused CopyCString("http://127.0.0.1:1", account_profile.nodes[0][:])
|
|
if ProviderState(*backup_observer).backed_up { return 36 }
|
|
ProviderBackupStatusPump(*backup_observer)
|
|
if backup_observer.backup_status_stored { return 40 }
|
|
account_profile.nodes[0] = old_node
|
|
ProviderBackupStatusPump(*backup_observer)
|
|
if !BackupSettle(*backup_observer) || !ProviderState(*backup_observer).backed_up { return 41 }
|
|
old_owner := account_recovery.keys.public_id
|
|
account_recovery.keys.public_id[0] = ifx old_owner[0] == #char "b" then #char "c" else #char "b"
|
|
if ProviderState(*backup_observer).backed_up { return 37 }
|
|
account_recovery.keys.public_id = old_owner
|
|
if !BackupOperation(true) { return 33 }
|
|
backup_observer.backup_status_next = 0
|
|
if !BackupSettle(*backup_observer) || ProviderState(*backup_observer).backed_up { return 34 }
|
|
// A file backup remains valid when the node says it has none.
|
|
if !AccountBackedUpWrite(root, TextUntilNul(account_recovery.keys.public_id[:]), true) { return 38 }
|
|
backup_observer.backup_local_next = 0
|
|
ProviderBackedUpPump(*backup_observer)
|
|
if !ProviderState(*backup_observer).backed_up { return 39 }
|
|
ProviderClose(*backup_observer)
|
|
return 0
|
|
}
|
|
if step == "first" {
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"game-progress\",\"schema\":\"entity.game-progress.v1\",\"document\":{\"game\":\"shovey.classic\",\"unlocked\":40,\"completed\":\"110\",\"state\":\"advanced\"}}"), "status", "done") { return 22 }
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"levels\",\"schema\":\"entity.levels.v1\",\"id\":\"0123456789abcdef0123456789abcdef\",\"document\":{\"format\":\"shovey-levels-1\",\"edition\":\"classic\",\"title\":\"Garden\",\"levels\":[]}}"), "status", "done") ||
|
|
!SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"levels\",\"schema\":\"entity.levels.v1\",\"id\":\"fedcba9876543210fedcba9876543210\",\"document\":{\"format\":\"shovey-levels-1\",\"edition\":\"island\",\"title\":\"Shore\",\"levels\":[]}}"), "status", "done") { return 9 }
|
|
packed: Vec(u8)
|
|
BuilderPrint(*packed, "{\"operation\":\"put\",\"kind\":\"levels\",\"schema\":\"entity.levels.v1\",\"id\":\"00000000000000000000000000000004\",\"document\":{\"title\":\"Compressed record\",\"note\":\"")
|
|
for index: 0..255 { BuilderPrint(*packed, "abcdefghijklmnopqrstuvwxyz012345") }
|
|
BuilderPrint(*packed, "\"}}")
|
|
packed_request := BuilderFinish(packed)
|
|
defer { EntityFree(packed_request) }
|
|
if !SyncField(EntityRecordsAnswer(packed_request), "status", "done") { return 27 }
|
|
return ifx SyncSettle() then 0 else 10
|
|
}
|
|
if step == "second" {
|
|
// This installation has never fetched the account. Its newer local
|
|
// default must not overwrite the account's level forty.
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"game-progress\",\"schema\":\"entity.game-progress.v1\",\"document\":{\"game\":\"shovey.classic\",\"unlocked\":5,\"completed\":\"001\",\"state\":\"fresh\"}}"), "status", "done") { return 23 }
|
|
if !SyncSettle() { return 11 }
|
|
if !SyncProgress() { return 24 }
|
|
packed_reply := EntityRecordsAnswer("{\"operation\":\"get\",\"kind\":\"levels\",\"id\":\"00000000000000000000000000000004\"}")
|
|
packed_document := FieldAt(packed_reply, 0, "document")
|
|
packed_note: [8193]u8
|
|
if packed_document < 0 || ReadStringText(packed_reply, FieldAt(packed_reply, packed_document, "note"), packed_note[:]) != 8192 { return 28 }
|
|
pattern := "abcdefghijklmnopqrstuvwxyz012345"
|
|
for index: 0..8191 { if packed_note[index] != pattern[index % pattern.count] { return 29 } }
|
|
if !SyncTitle(one, "Garden") || !SyncTitle(two, "Shore") { return 12 }
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"put\",\"kind\":\"levels\",\"schema\":\"entity.levels.v1\",\"id\":\"0123456789abcdef0123456789abcdef\",\"document\":{\"format\":\"shovey-levels-1\",\"edition\":\"classic\",\"title\":\"Garden at night\",\"levels\":[]}}"), "status", "done") ||
|
|
!SyncField(EntityRecordsAnswer("{\"operation\":\"delete\",\"kind\":\"levels\",\"id\":\"fedcba9876543210fedcba9876543210\"}"), "status", "done") { return 13 }
|
|
return ifx SyncSettle() then 0 else 14
|
|
}
|
|
if step == "third" {
|
|
if !SyncSettle() { return 15 }
|
|
if !SyncProgress() { return 25 }
|
|
if !SyncTitle(one, "Garden at night") { return 16 }
|
|
if !SyncField(EntityRecordsAnswer("{\"operation\":\"get\",\"kind\":\"levels\",\"id\":\"fedcba9876543210fedcba9876543210\"}"), "status", "missing") { return 17 }
|
|
if !SyncTitle("00000000000000000000000000000003", "Offline") { return 18 }
|
|
return 0
|
|
}
|
|
if step == "adopt" {
|
|
// The records kept before the account now belong to it and upload.
|
|
if !SyncTitle("00000000000000000000000000000003", "Offline") { return 19 }
|
|
return ifx SyncSettle() then 0 else 20
|
|
}
|
|
return 21
|
|
}
|
|
|
|
SyncProgress :: () -> bool {
|
|
reply := EntityRecordsAnswer("{\"operation\":\"get\",\"kind\":\"game-progress\",\"id\":\"fd65a3d7ac3d5ceb2d4d96cc5224a740\"}")
|
|
at := FieldAt(reply, 0, "document")
|
|
if at < 0 { return false }
|
|
document := reply[at:ValueEnd(reply, at, 0)]
|
|
unlocked := ReadNumber(document, FieldAt(document, 0, "unlocked"))
|
|
return unlocked.valid && unlocked.value == 40.0 && SyncField(document, "completed", "111") &&
|
|
FieldAt(document, 0, "state") < 0
|
|
}
|