Files
entity/tests/alias_test.zi
T

234 lines
9.6 KiB
Ziran
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Setting an account alias through the standalone account's provider, the
// way the Alias page does, against a private node: run with ENTITY_DATA set
// to a device's folder, ALIAS_STEP naming the step and ALIAS_NODE the node
// (scripts/alias_test.py starts the node and the steps).
#import "account_store"
#import "picture_publish"
#import "native_management"
#import "management"
#import "recovery"
#import "model"
#import "friends_native"
#import "std/json_scan"
#import "native_files"
#import "std/c_string"
#import "std/timer_linux"
#import "std/text_buffer"
#import "std/byte_text_linux"
#import "mesh_client/keys"
alias_result: [32]u8;
alias_message: [256]u8;
// Asks for an alias as the Alias page's Save does and waits for the answer.
SaveAlias :: (alias: string) -> bool {
request: ManagementRequest
request.sequence = account_provider.queue.sequence + 1
request.owner_id = account_recovery.keys.public_id
if !CopyCString("alias", request.operation[:]) || !CopyCString(alias, request.value[:]) { return false }
encoded := ManagementRequestEncode(request)
defer { EntityFree(encoded) }
if !ProviderEnqueue(*account_provider, encoded) { return false }
for round: 0..2400 {
ProviderPump(*account_provider)
if !account_provider.queue.pending {
alias_result = account_provider.queue.result
alias_message = account_provider.queue.message
return true
}
unused SleepMilliseconds(25)
}
return false
}
Answered :: (result: string, message: string) -> bool {
return TextUntilNul(alias_result[:]) == result && TextUntilNul(alias_message[:]) == message
}
saved_alias: [64]u8;
// The alias the device keeps for the account, as its alias file holds it.
SavedAlias :: () -> string {
path: [4096]u8
saved_alias = .{}
if NativeRead(AccountStoreAliasPath(path[:]), saved_alias[:]) <= 0 { return "" }
return TextUntilNul(saved_alias[:])
}
FriendCountTest :: () -> s32 {
document := TextUntilNul(friend_list[:])
return ArrayCount(document, FieldAt(document, 0, "friends"))
}
RequestCountTest :: (key: string) -> s32 {
document := TextUntilNul(friend_requests[:])
return ArrayCount(document, FieldAt(document, 0, key))
}
FirstRequestIdTest :: (output: []u8) -> bool {
document := TextUntilNul(friend_requests[:])
item := ElementAt(document, FieldAt(document, 0, "incoming"), 0)
return ReadStringText(document, FieldAt(document, item, "id"), output) > 0
}
FirstFriendIdTest :: (output: []u8) -> bool {
document := TextUntilNul(friend_list[:])
item := ElementAt(document, FieldAt(document, 0, "friends"), 0)
return ReadStringText(document, FieldAt(document, item, "user_id_hash"), output) == 64
}
FirstFriendIconTest :: () -> s32 {
document := TextUntilNul(friend_list[:])
item := ElementAt(document, FieldAt(document, 0, "friends"), 0)
icon := ReadNumber(document, FieldAt(document, item, "profile_icon"))
return ifx icon.valid then cast(s32)icon.value else -1
}
FirstFriendPictureTest :: () -> s32 { return EntityPictureFromPublicIcon(FirstFriendIconTest()) }
FriendsWait :: () -> bool {
for round: 0..1200 {
EntityFriendsPump()
if !friend_queued && !friend_refresh && friend_stage == 0 { return !friend_error }
unused SleepMilliseconds(25)
}
return false
}
FriendsRefreshTest :: () -> bool { unused EntityFriendsAnswer("refresh"); return FriendsWait() }
FriendsActionTest :: (operation: string, target: string) -> bool {
out: Vec(u8)
BuilderPrint(*out, "{\"owner\":\"%\",\"operation\":\"%\",\"target\":\"%\"}",
TextUntilNul(account_recovery.keys.public_id[:]), operation, target)
request := BuilderFinish(out)
accepted := EntityFriendsAnswer(request) == "queued"
EntityFree(request)
return accepted && FriendsWait()
}
SaveTestPicture :: (picture: s32, kind: s32 = 0) -> bool {
state := account_profile
state.picture_kind = kind
if kind == 1 { state.abstract_picture = picture }
else { state.picture = picture }
if !AccountStoreSaveProfile(state) { return false }
for round: 0..1200 {
AccountStorePump()
if round > 0 && !picture_dirty && !picture_publisher.active { return true }
unused SleepMilliseconds(25)
}
return false
}
#program_export
Check :: () -> s32 {
root := NativeEnvironment("ENTITY_DATA")
step := NativeEnvironment("ALIAS_STEP")
node := NativeEnvironment("ALIAS_NODE")
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(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") || !account_standalone_ready { return 4 }
if step == "first" {
// A phone keyboard's capital, an @ and spaces still save, as the
// node stores the alias.
if !SaveAlias(" @Live_Tester ") { return 5 }
if !Answered("ok", "Account alias saved") { return 6 }
if TextUntilNul(account_alias[:]) != "live_tester" || SavedAlias() != "live_tester" { return 7 }
// Too short is refused on the device, before any request.
if !SaveAlias("Ab") || !Answered("error", "Use 4–32 lowercase letters, numbers or underscores") { return 8 }
// Changing it keeps the new one.
if !SaveAlias("live_tester_one") || !Answered("ok", "Account alias saved") { return 9 }
if SavedAlias() != "live_tester_one" { return 10 }
if !SaveAlias("Live_Tester_One") || !Answered("ok", "Account alias saved") || SavedAlias() != "Live_Tester_One" { return 80 }
if !SaveAlias("live_tester_one") || !Answered("ok", "Account alias saved") { return 81 }
return 0
}
if step == "picture" {
state := account_profile
state.picture = 7
if !AccountStoreSaveProfile(state) { return 20 }
for round: 0..1200 {
AccountStorePump()
if round > 0 && !picture_dirty && !picture_publisher.active { return 0 }
unused SleepMilliseconds(25)
}
return 21
}
if step == "picture_restore" {
for round: 0..1200 {
AccountStorePump()
if account_profile.picture == 7 { return 0 }
unused SleepMilliseconds(25)
}
return 22
}
if step == "restore" {
for round: 0..600 {
AccountStoreAliasPump()
if TextUntilNul(account_alias[:]) == "live_tester_one" { return ifx SavedAlias() == "live_tester_one" then 0 else 19 }
unused SleepMilliseconds(25)
}
return 18
}
if step == "second" {
// Another account cannot take it, and is told so.
if !SaveAlias("LIVE_TESTER_ONE") { return 11 }
if !Answered("error", "That alias is already taken; choose another") { return 12 }
if SavedAlias() != "" { return 13 }
// The first account's earlier alias is free again.
if !SaveAlias("live_tester") || !Answered("ok", "Account alias saved") || SavedAlias() != "live_tester" { return 14 }
return 0
}
if step == "friends_send" {
if !FriendsRefreshTest() || FriendCountTest() != 0 { return 60 }
if !FriendsActionTest("add", " @LIVE_TESTER ") || RequestCountTest("outgoing") != 1 { return 62 }
return 0
}
if step == "friends_accept" {
if !SaveTestPicture(12) || !FriendsRefreshTest() || RequestCountTest("incoming") != 1 { return 64 }
id: [129]u8
if !FirstRequestIdTest(id[:]) || !FriendsActionTest("accept", TextUntilNul(id[:])) { return 67 }
return ifx FriendCountTest() == 1 && FirstFriendPictureTest() == 7 then 0 else 69
}
if step == "friends_check" || step == "friends_changed" {
EntityFriendsGame("wurm")
if !FriendsRefreshTest() || FriendCountTest() != 1 { return 70 }
expected := ifx step == "friends_check" then 12 else 24
if FirstFriendPictureTest() != expected { return 72 }
if step == "friends_check" && !SaveTestPicture(24) { return 73 }
return 0
}
if step == "friends_remove" {
if !FriendsRefreshTest() { return 74 }
id: [65]u8
if !FirstFriendIdTest(id[:]) || !FriendsActionTest("remove", TextUntilNul(id[:])) { return 76 }
if FriendCountTest() != 0 { return 77 }
if FriendsActionTest("add", "@no_such_friend_account") || !friend_error { return 78 }
return 0
}
if step == "friends_abstract" { return ifx SaveTestPicture(2, 1) then 0 else 84 }
if step == "friends_abstract_check" {
if !FriendsRefreshTest() || FriendCountTest() != 1 { return 85 }
return ifx FirstFriendIconTest() == 129 then 0 else 86
}
if step == "restore_abstract" {
account_profile.picture_kind = 0
for round: 0..1200 {
AccountStorePump()
if account_profile.picture_kind == 1 && account_profile.abstract_picture == 2 { return 0 }
unused SleepMilliseconds(25)
}
return 87
}
if step == "offline" {
// A node that cannot be reached says so.
if !SaveAlias("live_offline") { return 15 }
if !Answered("error", "Could not reach the node; check the connection and try again") { return 16 }
return 0
}
return 17
}