654 lines
23 KiB
Ziran
654 lines
23 KiB
Ziran
#import "mesh_client/async_client"
|
|
#import "mesh_client/async_sync"
|
|
#import "mesh_client/async_account"
|
|
#import "mesh_client/client"
|
|
#import "mesh_client/sync"
|
|
#import "mesh_client/auth"
|
|
#import "net_http"
|
|
#import "text_buffer"
|
|
#import "c_string"
|
|
#import "json_scan"
|
|
#import "mesh_client/sync_flow"
|
|
#import "mesh_client/wire"
|
|
#import "mesh_client/transaction"
|
|
#import "byte_text_linux"
|
|
c :: #system_library "c";
|
|
TestAllocate :: (size: usize) -> *void #foreign c "malloc";
|
|
TestFree :: (pointer: *void) #foreign c "free";
|
|
|
|
calls: s32;
|
|
challenges: s32;
|
|
logins: s32;
|
|
registrations: s32;
|
|
deletions: s32;
|
|
delete_status: s32;
|
|
syncs: s32;
|
|
polls: s32;
|
|
cancels: s32;
|
|
account_signs: s32;
|
|
device_signs: s32;
|
|
nonces: s32;
|
|
status: s32;
|
|
reject_syncs: s32;
|
|
bad_request: bool;
|
|
active: bool;
|
|
fail_transport: bool;
|
|
flow_mode: bool;
|
|
flow_failure: s32;
|
|
flow_trial: s32;
|
|
flow_pages: s32;
|
|
flow_applied: s32;
|
|
flow_bytes: s64;
|
|
flow_state: SyncPageState;
|
|
flow_download_only: bool;
|
|
missing_device: bool;
|
|
reject_registration: bool;
|
|
replay_syncs: s32;
|
|
|
|
|
|
Contains :: (text: string, part: string) -> bool {
|
|
if part.count > text.count {
|
|
return false
|
|
}
|
|
offset: s64 = 0
|
|
while offset <= cast(s64)text.count - cast(s64)part.count {
|
|
index: s64 = 0
|
|
while index < cast(s64)part.count && text[offset + index] == part[index] {
|
|
index += 1
|
|
}
|
|
if index == cast(s64)part.count {
|
|
return true
|
|
}
|
|
offset += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
HexFill :: (output: []u8, count: s32, byte: u8) -> bool {
|
|
if cast(s64)output.count <= cast(s64)count {
|
|
return false
|
|
}
|
|
index: s32 = 0
|
|
while index < count {
|
|
output[index] = byte
|
|
index += 1
|
|
}
|
|
output[count] = cast(u8)0
|
|
return true
|
|
}
|
|
|
|
Digest :: (body: string, output: []u8) -> bool {
|
|
return body.count > 0 && HexFill(output, 64, cast(u8)102)
|
|
}
|
|
|
|
SignAccount :: (context: *void, message: string, output: []u8) -> bool {
|
|
unused context
|
|
account_signs += 1
|
|
if !Contains(message, "mesh-") {
|
|
bad_request = true
|
|
}
|
|
return HexFill(output, 4840, cast(u8)97)
|
|
}
|
|
|
|
SignDevice :: (context: *void, message: string, output: []u8) -> bool {
|
|
unused context
|
|
device_signs += 1
|
|
if !Contains(message, "mesh-tx-v1\n6\n") {
|
|
bad_request = true
|
|
}
|
|
return HexFill(output, 128, cast(u8)100)
|
|
}
|
|
|
|
Nonce :: (context: *void, output: []u8) -> bool {
|
|
unused context
|
|
nonces += 1
|
|
return HexFill(output, 64, cast(u8)(48 + nonces % 10))
|
|
}
|
|
|
|
Start :: (context: *void, request: HttpRequest, output: []u8) -> bool {
|
|
unused context
|
|
if active {
|
|
bad_request = true
|
|
return false
|
|
}
|
|
active = true
|
|
calls += 1
|
|
polls = 0
|
|
status = 200
|
|
if Contains(request.url, "/sync/challenge?") {
|
|
challenges += 1
|
|
if request.method != "GET" {
|
|
bad_request = true
|
|
}
|
|
return CopyCString("{\"nonce\":\"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\"}", output)
|
|
}
|
|
if Contains(request.url, "/sync/login") {
|
|
logins += 1
|
|
if request.headers.count != 2 || request.headers[1].value.count != 4840 {
|
|
bad_request = true
|
|
}
|
|
return CopyCString("{\"auth_token\":\"token\",\"expires_in_seconds\":1800,\"server_time\":1700000000}", output)
|
|
}
|
|
if Contains(request.url, "/account/delete") {
|
|
deletions += 1
|
|
if request.method != "POST" || request.token.count != 0 ||
|
|
request.headers.count != 2 ||
|
|
request.headers[0].name != "X-Mesh-User" ||
|
|
request.headers[1].name != "X-Mesh-Signature" ||
|
|
request.headers[1].value.count != 4840 ||
|
|
!Contains(request.body, "\"public_key\":\"public-key\"") {
|
|
bad_request = true
|
|
}
|
|
status = delete_status
|
|
return CopyCString("{}", output)
|
|
}
|
|
if request.token != "token" || request.method != "POST" {
|
|
bad_request = true
|
|
}
|
|
if Contains(request.url, "/account/devices") {
|
|
registrations += 1
|
|
if !Contains(request.body, "\"app_id\":\"inbe\"") ||
|
|
!Contains(request.body, "\"expires_at\":1700000300") {
|
|
bad_request = true
|
|
}
|
|
if reject_registration {
|
|
status = 409
|
|
} else {
|
|
missing_device = false
|
|
}
|
|
return CopyCString("{}", output)
|
|
}
|
|
syncs += 1
|
|
if replay_syncs > 0 {
|
|
replay_syncs -= 1
|
|
status = 409
|
|
return CopyCString("{\"error\":\"signed transaction replay\"}", output)
|
|
}
|
|
if flow_mode {
|
|
flow_failure = 0
|
|
if !Contains(request.body, "\"encrypted_records_only\":true") {
|
|
flow_failure = TRANSFER_FAILURE_CAPACITY
|
|
return CopyCString("", output)
|
|
}
|
|
if Contains(request.body, "bootstrap") || Contains(request.body, "full_sync_requested") ||
|
|
Contains(request.body, "last_server_state_hash") || request.headers.count != 3 {
|
|
bad_request = true
|
|
}
|
|
if flow_applied < 20 && (!flow_download_only || !Contains(request.body, "\"encrypted_records\":[]")) {
|
|
bad_request = true
|
|
}
|
|
if flow_applied >= 20 && (flow_download_only || !Contains(request.body, "\"encrypted_records\":[{}]")) {
|
|
bad_request = true
|
|
}
|
|
if flow_applied >= 20 {
|
|
return CopyCString("{\"protocol_version\":6,\"encrypted_records_only\":true,\"server_version\":50,\"changes_complete\":true,\"changes\":{\"habits\":[],\"habit_days\":[],\"sessions\":[],\"meditation_logs\":[],\"social_cache\":[]}}", output)
|
|
}
|
|
if flow_trial == 1 && flow_applied == 1 && flow_pages < 3 {
|
|
flow_failure = TRANSFER_FAILURE_TRANSPORT
|
|
}
|
|
flow_pages += 1
|
|
if flow_trial == 4 {
|
|
return CopyCString("{\"protocol_version\":6,\"encrypted_records_only\":true,\"server_version\":50,\"changes_complete\":false,\"record_cursor\":{\"version\":1,\"collection\":\"notes\",\"id\":\"same\"},\"changes\":{\"encrypted_records\":[{}]}}", output)
|
|
}
|
|
writer := Writer.{.output = output, .valid = true}
|
|
writer = WriteText(writer, "{\"protocol_version\":6,\"encrypted_records_only\":true,\"server_version\":50,\"changes_complete\":")
|
|
if flow_applied < 19 {
|
|
writer = WriteText(writer, "false,\"record_cursor\":{\"version\":")
|
|
writer = WriteDecimal(writer, cast(s64)flow_applied + 1)
|
|
writer = WriteText(writer, ",\"collection\":\"notes\",\"id\":\"note\"},")
|
|
} else {
|
|
writer = WriteText(writer, "true,")
|
|
}
|
|
writer = WriteText(writer, "\"changes\":{\"encrypted_records\":[{\"ciphertext\":\"")
|
|
index: s32 = 0
|
|
while index < 1048576 {
|
|
writer = WriteByte(writer, cast(u8)97)
|
|
index += 1
|
|
}
|
|
writer = WriteText(writer, "\"}]}}\n")
|
|
return writer.valid
|
|
}
|
|
if request.headers.count != 3 || request.headers[2].name != "X-Mesh-Tx" ||
|
|
!Contains(request.headers[2].value, "\"protocol_version\":6") ||
|
|
!Contains(request.headers[2].value, "\"device_signature\":\"dddd") ||
|
|
request.body != "{\"protocol_version\":6}" {
|
|
bad_request = true
|
|
}
|
|
if reject_syncs > 0 {
|
|
reject_syncs -= 1
|
|
status = 401
|
|
return CopyCString("{}", output)
|
|
}
|
|
if missing_device {
|
|
status = 401
|
|
return CopyCString("{\"error\":\"device key not registered\"}", output)
|
|
}
|
|
return CopyCString("{\"server_version\":7}", output)
|
|
}
|
|
|
|
Poll :: (context: *void, output_status: *s32) -> s32 {
|
|
unused context
|
|
if !active {
|
|
bad_request = true
|
|
return 0
|
|
}
|
|
polls += 1
|
|
if polls == 1 {
|
|
return -1
|
|
}
|
|
active = false
|
|
output_status[0] = status
|
|
return ifx (fail_transport || (flow_mode && flow_failure != 0)) then (0) else (1)
|
|
}
|
|
|
|
Cancel :: (context: *void) {
|
|
unused context
|
|
if !active {
|
|
bad_request = true
|
|
}
|
|
active = false
|
|
cancels += 1
|
|
}
|
|
|
|
Reset :: () {
|
|
calls = 0
|
|
challenges = 0
|
|
logins = 0
|
|
registrations = 0
|
|
deletions = 0
|
|
delete_status = 200
|
|
syncs = 0
|
|
polls = 0
|
|
cancels = 0
|
|
account_signs = 0
|
|
device_signs = 0
|
|
nonces = 0
|
|
status = 0
|
|
reject_syncs = 0
|
|
bad_request = false
|
|
active = false
|
|
fail_transport = false
|
|
flow_mode = false
|
|
flow_failure = 0
|
|
flow_pages = 0
|
|
flow_applied = 0
|
|
flow_bytes = 0
|
|
flow_state = SyncPageState.{}
|
|
flow_download_only = false
|
|
missing_device = false
|
|
reject_registration = false
|
|
replay_syncs = 0
|
|
}
|
|
|
|
RunCachedSync :: (client: Client, session: *Session, device: Device, transport: AsyncTransport) -> AuthResult {
|
|
pending: PendingSync
|
|
output: [256]u8
|
|
if !BeginSync(*pending, client, session, device, 1000, null, Nonce, transport,
|
|
"{\"protocol_version\":6}", output[:]) {
|
|
return cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
steps: s32 = 0
|
|
while !PollSync(*pending) && steps < 60 {
|
|
steps += 1
|
|
}
|
|
if steps >= 60 || bad_request || active {
|
|
return cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED
|
|
}
|
|
return pending.result
|
|
}
|
|
|
|
CheckRegistrationCache :: (client: Client, device: Device, transport: AsyncTransport) -> s32 {
|
|
Reset()
|
|
session: Session
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_OK || registrations != 1 {
|
|
return 80
|
|
}
|
|
Reset()
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_OK ||
|
|
registrations != 0 || calls != 1 || account_signs != 1 || device_signs != 1 || nonces != 2 {
|
|
return 81
|
|
}
|
|
saved: [1536]u8
|
|
restored: Registration
|
|
if !WriteRegistration(*session.registration, saved[:]) || !ReadRegistration(TextUntilNul(saved[:]), *restored) {
|
|
return 82
|
|
}
|
|
session.registration = restored
|
|
if !RegistrationCurrent(client, *session, device, 1000) || RegistrationCurrent(client, *session, device, 4600) ||
|
|
RegistrationCurrent(client, *session, device, -1) {
|
|
return 83
|
|
}
|
|
for field: cast(s32)0..5 {
|
|
other := client
|
|
changed := device
|
|
if field == 0 { other.server_url = "http://127.0.0.1:8081" }
|
|
if field == 1 { other.identity.account_id = "other-account" }
|
|
if field == 2 { other.app_id = "other-app" }
|
|
if field == 3 { other.identity.client_id = "other-client" }
|
|
if field == 4 { changed.key_id = "other-device" }
|
|
if field == 5 { changed.public_key_hex = "other-public-key" }
|
|
if RegistrationCurrent(other, *session, changed, 1000) { return 84 }
|
|
}
|
|
Reset()
|
|
replay_syncs = 1
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_OK ||
|
|
registrations != 0 || syncs != 2 || nonces != 4 {
|
|
return 87
|
|
}
|
|
Reset()
|
|
replay_syncs = 5
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED ||
|
|
registrations != 0 || syncs != 3 || nonces != 6 {
|
|
return 88
|
|
}
|
|
Reset()
|
|
missing_device = true
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_OK ||
|
|
registrations != 1 || syncs != 3 || calls > 8 {
|
|
return 85
|
|
}
|
|
Reset()
|
|
missing_device = true
|
|
reject_registration = true
|
|
if RunCachedSync(client, *session, device, transport) != cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED ||
|
|
registrations != 1 || syncs != 2 || calls > 8 {
|
|
return 86
|
|
}
|
|
return 0
|
|
}
|
|
|
|
FlowFailure :: (context: *void) -> s32 {
|
|
unused context
|
|
return flow_failure
|
|
}
|
|
|
|
FlowLoad :: (context: *void, state: *SyncPageState) -> bool {
|
|
unused context
|
|
state.* = flow_state
|
|
return true
|
|
}
|
|
|
|
FlowSave :: (context: *void, state: *SyncPageState) -> bool {
|
|
unused context
|
|
if flow_trial == 2 { return false }
|
|
flow_state = state.*
|
|
return true
|
|
}
|
|
|
|
FlowPrepare :: (context: *void, maximum_records: s32) -> string {
|
|
unused context
|
|
if maximum_records < 0 || maximum_records > 200 { bad_request = true }
|
|
flow_download_only = maximum_records == 0
|
|
// Even a host that leaves its queued records in the body cannot extend
|
|
// the restore indefinitely: the library excludes them during downloads.
|
|
return "{\"protocol_version\":6,\"app_id\":\"inbe\",\"since_server_version\":0,\"encrypted_records\":[{}],\"bootstrap\":true,\"full_sync_requested\":true,\"last_server_state_hash\":\"hash\"}"
|
|
}
|
|
|
|
FlowAccept :: (context: *void, response: string, state: *SyncPageState) -> bool {
|
|
unused context
|
|
if flow_trial == 3 && flow_applied == 1 { return false }
|
|
flow_bytes += cast(s64)response.count
|
|
flow_applied += 1
|
|
flow_state = state.*
|
|
return true
|
|
}
|
|
|
|
CheckFlow :: (client: Client, device: Device, transport: AsyncTransport) -> s32 {
|
|
memory := TestAllocate(1050000)
|
|
if memory == null { return 59 }
|
|
defer { TestFree(memory) }
|
|
output := BytesFromPointer(cast(*u8)memory, 1050000)
|
|
trial: s32 = 0
|
|
while trial < 5 {
|
|
Reset()
|
|
flow_trial = trial
|
|
flow_mode = true
|
|
host := transport
|
|
host.failure = FlowFailure
|
|
flow := SyncFlow.{.load = FlowLoad, .prepare = FlowPrepare, .save = FlowSave, .accept = FlowAccept}
|
|
session: Session
|
|
pending: PendingSync
|
|
if !BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, host, "", output[:], flow) { return 60 }
|
|
steps: s32 = 0
|
|
while !PollSync(*pending) && steps < 250 { steps += 1 }
|
|
if steps >= 250 || bad_request || active || registrations != 1 { return 61 }
|
|
if trial < 2 {
|
|
if pending.result != cast(AuthResult)AuthResult.AUTH_OK || flow_applied != 20 ||
|
|
flow_bytes < 20000000 || flow_state.pending || flow_state.cursor[0] != cast(u8)0 {
|
|
return 62 + trial
|
|
}
|
|
} else if pending.result != cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED {
|
|
return 64 + trial
|
|
}
|
|
if trial == 2 && (flow_state.enabled || flow_applied != 0) { return 69 }
|
|
if trial == 3 {
|
|
// The rejected second page leaves only the first cursor committed.
|
|
if flow_applied != 1 || !flow_state.pending { return 70 }
|
|
flow_trial = 0
|
|
pending = PendingSync.{}
|
|
unused BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, host, "", output[:], flow)
|
|
// Cancel while a page is in flight; durable progress is unchanged.
|
|
while pending.stage < 3 && !PollSync(*pending) {}
|
|
CancelSync(*pending)
|
|
if flow_applied != 1 || !flow_state.pending || active { return 71 }
|
|
pending = PendingSync.{}
|
|
unused BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, host, "", output[:], flow)
|
|
steps = 0
|
|
while !PollSync(*pending) && steps < 250 { steps += 1 }
|
|
if pending.result != cast(AuthResult)AuthResult.AUTH_OK || flow_applied != 20 ||
|
|
flow_state.pending { return 72 }
|
|
}
|
|
if trial == 0 {
|
|
pending = PendingSync.{}
|
|
unused BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, host, "", output[:], flow)
|
|
steps = 0
|
|
while !PollSync(*pending) && steps < 30 { steps += 1 }
|
|
if pending.result != cast(AuthResult)AuthResult.AUTH_OK || flow_download_only ||
|
|
flow_applied != 21 { return 74 }
|
|
}
|
|
if trial == 4 && flow_applied != 1 { return 73 }
|
|
trial += 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
CheckEmptyRecordPages :: () -> bool {
|
|
state := SyncPageState.{.enabled = true}
|
|
next: SyncPageState
|
|
body := "{\"since_server_version\":50}"
|
|
// Mesh omits encrypted_records when no records changed (omitempty).
|
|
prefix := "{\"protocol_version\":6,\"encrypted_records_only\":true,\"server_version\":50,\"changes_complete\":true,\"changes\":"
|
|
response: [512]u8
|
|
writer := Writer.{.output = response[:], .valid = true}
|
|
writer = WriteText(writer, prefix)
|
|
writer = WriteText(writer, "{\"habits\":[],\"habit_days\":[],\"sessions\":[],\"meditation_logs\":[],\"social_cache\":[]}}")
|
|
if !writer.valid || !AcceptSyncResponse(body, TextUntilNul(response[:]), *state, *next, 64) || next.pending {
|
|
return false
|
|
}
|
|
invalid: [3]string = .["{\"encrypted_records\":null}}", "{\"encrypted_records\":{}}}", "null}"]
|
|
for suffix: invalid {
|
|
writer = Writer.{.output = response[:], .valid = true}
|
|
writer = WriteText(writer, prefix)
|
|
writer = WriteText(writer, suffix)
|
|
if !writer.valid || AcceptSyncResponse(body, TextUntilNul(response[:]), *state, *next, 64) {
|
|
return false
|
|
}
|
|
}
|
|
return !AcceptSyncResponse(body,
|
|
"{\"protocol_version\":6,\"encrypted_records_only\":true,\"server_version\":50,\"changes_complete\":false,\"record_cursor\":{\"version\":50,\"collection\":\"notes\",\"id\":\"note\"},\"changes\":{}}",
|
|
*state, *next, 64)
|
|
}
|
|
|
|
// A cursor saved by a download that began before the app's position is one
|
|
// the node refuses: the next page starts again at the app's position.
|
|
CheckStaleRecordCursor :: () -> bool {
|
|
body := "{\"protocol_version\":6,\"since_server_version\":42341}"
|
|
stale := SyncPageState.{.enabled = true, .pending = true}
|
|
if !CopyCString("{\"version\":37105,\"collection\":\"notes\",\"id\":\"note\"}", stale.cursor[:]) ||
|
|
!RestartStaleSyncPage(body, *stale) || stale.pending || !stale.enabled || stale.cursor[0] != cast(u8)0 {
|
|
return false
|
|
}
|
|
request: [1024]u8
|
|
if !BuildSyncPageRequest(body, *stale, 64, request[:], true) ||
|
|
FieldAt(TextUntilNul(request[:]), 0, "record_cursor") >= 0 {
|
|
return false
|
|
}
|
|
// A cursor at or after the position continues its download.
|
|
current := SyncPageState.{.enabled = true, .pending = true}
|
|
if !CopyCString("{\"version\":42341,\"collection\":\"notes\",\"id\":\"note\"}", current.cursor[:]) ||
|
|
RestartStaleSyncPage(body, *current) || !current.pending || current.cursor[0] == cast(u8)0 {
|
|
return false
|
|
}
|
|
if !BuildSyncPageRequest(body, *current, 64, request[:], true) ||
|
|
FieldAt(TextUntilNul(request[:]), 0, "record_cursor") < 0 {
|
|
return false
|
|
}
|
|
// A first page has no cursor to drop.
|
|
first := SyncPageState.{.enabled = true}
|
|
return !RestartStaleSyncPage(body, *first) && !first.pending
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
if !CheckEmptyRecordPages() {
|
|
return 75
|
|
}
|
|
if !CheckStaleRecordCursor() {
|
|
return 76
|
|
}
|
|
account: [65]u8
|
|
device_key: [65]u8
|
|
unused HexFill(account[:], 64, cast(u8)97)
|
|
unused HexFill(device_key[:], 64, cast(u8)98)
|
|
client: Client
|
|
client.server_url = "http://127.0.0.1:8080"
|
|
client.app_id = "inbe"
|
|
client.identity.account_id = TextUntilNul(account[:])
|
|
client.identity.public_key = "public-key"
|
|
client.identity.client_id = "client-1"
|
|
client.digest = Digest
|
|
client.signer = SignAccount
|
|
device: Device
|
|
device.key_id = TextUntilNul(device_key[:])
|
|
device.public_key_hex = TextUntilNul(device_key[:])
|
|
device.signer = SignDevice
|
|
transport: AsyncTransport
|
|
transport.start = Start
|
|
transport.poll = Poll
|
|
transport.cancel = Cancel
|
|
cache_result := CheckRegistrationCache(client, device, transport)
|
|
if cache_result != 0 { return cache_result }
|
|
session: Session
|
|
pending: PendingSync
|
|
output: [256]u8
|
|
trial: s32 = 0
|
|
while trial < 3 {
|
|
Reset()
|
|
reject_syncs = trial
|
|
session = Session.{}
|
|
pending = PendingSync.{}
|
|
if !BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, transport, "{\"protocol_version\":6}", output[:]) {
|
|
return 1
|
|
}
|
|
if PollSync(*pending) || calls != 1 {
|
|
return 2
|
|
}
|
|
limit: s32 = 0
|
|
while !PollSync(*pending) && limit < 30 {
|
|
limit += 1
|
|
}
|
|
if limit >= 30 || bad_request || active || registrations != 1 ||
|
|
device_signs != 1 || nonces != 3 {
|
|
return 3
|
|
}
|
|
if trial == 0 {
|
|
if pending.result != cast(AuthResult)AuthResult.AUTH_OK ||
|
|
pending.status != 200 || calls != 4 || account_signs != 3 ||
|
|
TextUntilNul(output[:]) != "{\"server_version\":7}" {
|
|
return 4
|
|
}
|
|
} else {
|
|
if challenges != 2 || logins != 2 || syncs != 2 || calls != 7 {
|
|
return 5
|
|
}
|
|
if trial == 1 && pending.result != cast(AuthResult)AuthResult.AUTH_OK {
|
|
return 6
|
|
}
|
|
if trial == 2 && (pending.result != cast(AuthResult)AuthResult.AUTH_FAILED ||
|
|
pending.status != 401 || session.token[0] != cast(u8)0) {
|
|
return 7
|
|
}
|
|
}
|
|
trial += 1
|
|
}
|
|
Reset()
|
|
session = Session.{}
|
|
pending = PendingSync.{}
|
|
unused BeginSync(*pending, client, *session, device, 1000, null,
|
|
Nonce, transport, "{\"protocol_version\":6}", output[:])
|
|
CancelSync(*pending)
|
|
if cancels != 1 || active || !PollSync(*pending) || bad_request ||
|
|
pending.result != cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED {
|
|
return 8
|
|
}
|
|
trial = 0
|
|
while trial < 3 {
|
|
Reset()
|
|
if trial == 1 {
|
|
delete_status = 403
|
|
}
|
|
if trial == 2 {
|
|
delete_status = 500
|
|
}
|
|
deletion: PendingDeletion
|
|
if !BeginDeletion(*deletion, client, transport) ||
|
|
BeginDeletion(*deletion, client, transport) ||
|
|
PollDeletion(*deletion) || calls != 1 {
|
|
return 9
|
|
}
|
|
steps: s32 = 0
|
|
while !PollDeletion(*deletion) && steps < 10 {
|
|
steps += 1
|
|
}
|
|
if steps >= 10 || bad_request || active || calls != 2 ||
|
|
challenges != 1 || deletions != 1 || logins != 0 ||
|
|
account_signs != 1 {
|
|
return 10
|
|
}
|
|
expected := cast(AuthResult)AuthResult.AUTH_OK
|
|
if trial == 1 {
|
|
expected = cast(AuthResult)AuthResult.AUTH_FAILED
|
|
}
|
|
if trial == 2 {
|
|
expected = cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED
|
|
}
|
|
if deletion.result != expected {
|
|
return 20 + trial * 10 + cast(s32)deletion.result
|
|
}
|
|
trial += 1
|
|
}
|
|
trial = 0
|
|
while trial < 2 {
|
|
Reset()
|
|
deletion: PendingDeletion
|
|
unused BeginDeletion(*deletion, client, transport)
|
|
if trial == 1 {
|
|
unused PollDeletion(*deletion)
|
|
unused PollDeletion(*deletion)
|
|
}
|
|
CancelDeletion(*deletion)
|
|
CancelDeletion(*deletion)
|
|
if cancels != 1 || active || !PollDeletion(*deletion) ||
|
|
bad_request || deletion.result != cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED {
|
|
return 12
|
|
}
|
|
trial += 1
|
|
}
|
|
return CheckFlow(client, device, transport)
|
|
}
|