Files
breeze/tests/telegram_account_ui_test.c
waozi ef0de86e02
Cloudflare Pages / Build and verify the site (push) Successful in 3m29s
Integrate Breeze desktop, Android, and app updates
Use the verified dependency lock across native hosts and bundled apps. Preserve saved styles, local app reactivation, and offline packages while integrating the latest practice, Habits, Lumi, and figure preview updates.
2026-11-20 02:24:34 -03:00

420 lines
19 KiB
C

#include "telegram_account.h"
#include "zir_string.h"
#include "telegram_account_ui_backend.h"
#include "text_buffers.h"
#include "metrics.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
static const char secret[] = "Recovery-phrase-sentinel";
static bool contains(String text, const char *needle)
{
size_t length = strlen(needle);
for (size_t i = 0; i + length <= text.length; ++i) {
if (memcmp(text.data + i, needle, length) == 0) return true;
}
return false;
}
int32_t MeasureGlyphWidth(String text, int32_t font, String face)
{
(void)face;
return (int32_t)text.length * font / 2;
}
int32_t MeasureGlyphLineHeight(int32_t font, String face)
{
(void)face;
return font;
}
void RasterRoundedRectangle(Rectangle bounds, float radius, int32_t segments, Color color)
{ (void)bounds; (void)radius; (void)segments; (void)color; assert(0); }
void RasterRoundedRectangleOutline(Rectangle bounds, float radius, int32_t segments, float width, Color color)
{ (void)bounds; (void)radius; (void)segments; (void)width; (void)color; assert(0); }
void RasterText(String value, int32_t x, int32_t y, int32_t font, Color color)
{ (void)value; (void)x; (void)y; (void)font; (void)color; assert(0); }
void RasterTextClipped(String value, int32_t x, int32_t y, int32_t font, Color color, Rectangle clip)
{ (void)value; (void)x; (void)y; (void)font; (void)color; (void)clip; assert(0); }
void RasterTextRotated(String value, int32_t x, int32_t y, int32_t font, Color color, Rectangle clip,
Vector2 origin, float rotation, Rectangle outer_clip, bool outer_clipped, bool emphasis, bool italic)
{ (void)value; (void)x; (void)y; (void)font; (void)color; (void)clip; (void)origin; (void)rotation;
(void)outer_clip; (void)outer_clipped; (void)emphasis; (void)italic; assert(0); }
void RasterTriangles(Slice points, Color color) { (void)points; (void)color; assert(0); }
int32_t ImageWidth(String path) { (void)path; return 0; }
int32_t ImageHeight(String path) { (void)path; return 0; }
void RasterLine(Rectangle line, Color color) { (void)line; (void)color; assert(0); }
void RasterImage(String asset_path, uint32_t texture_id, Rectangle source,
Rectangle destination, Rectangle clip, Vector2 origin,
float rotation, float radius, Color tint)
{
(void)asset_path; (void)texture_id; (void)source; (void)destination;
(void)clip; (void)origin; (void)rotation; (void)radius; (void)tint;
assert(0);
}
void RasterImageMesh(String path, uint32_t texture, Rectangle source,
Slice points, int32_t columns, int32_t rows, Rectangle clip, Color tint) {
(void)path; (void)texture; (void)source; (void)points;
(void)columns; (void)rows; (void)clip; (void)tint;
assert(0);
}
int32_t GetCharPressed(void) { return 0; }
bool IsKeyPressed(int32_t key) { (void)key; return false; }
bool IsKeyPressedRepeat(int32_t key) { (void)key; return false; }
bool IsKeyDown(int32_t key) { (void)key; return false; }
char *GetClipboardText(void) { return ""; }
void SetClipboardText(char *text) { (void)text; assert(0); }
int32_t SystemClipboardOpenHost(void) { return -1; }
int32_t SystemClipboardByteHost(int32_t index) { (void)index; return -1; }
bool SystemClipboardWriteHost(String text) { (void)text; assert(0); return false; }
void *asset_entry_at(size_t index) { (void)index; return NULL; }
size_t asset_entry_total(void) { return 0; }
unsigned char *LoadFileData(char *path, int32_t *size) { (void)path; *size = 0; return NULL; }
void UnloadFileData(unsigned char *data) { (void)data; }
static int32_t render(InnerBreeze *app, TelegramAccountView *view)
{
PaintClear(app->ui.session);
FrameTextReset(&app->ui.frame_text);
TreeStart(app->ui.session, 1, (Rectangle){0, 0, app->ui.view_width, app->ui.view_height});
BeginScrollFrame(app->ui.session, (Vector2){0, 0}, false, false, false, 0, 1.0);
int32_t action = telegram_account_DrawTelegramAccount(app, view);
assert(TreeFinish(app->ui.session));
for (int32_t i = 0; i < PendingPaintCount(app->ui.session); ++i) {
PaintCommand paint = PendingPaintAt(app->ui.session, i);
assert(!contains(paint.value, secret));
}
return action;
}
static TreeEntry node(Session session, int32_t focus)
{
for (int32_t i = 0; i < TreeCount(session); ++i) {
TreeEntry found = TreeNodeAt(session, i);
if (found.focus_id == focus) return found;
}
assert(0);
return (TreeEntry){0};
}
static void click(Session session, TreeEntry entry)
{
float x = entry.bounds.x + entry.bounds.width / 2;
float y = entry.bounds.y + entry.bounds.height / 2;
TreePointerUpdate(session, (PointerFrame){x, y, true, true, false});
TreePointerUpdate(session, (PointerFrame){x, y, false, false, true});
TreePointerUpdate(session, (PointerFrame){x, y, false, false, false});
}
static bool hex_digit(unsigned char value)
{
return (value >= '0' && value <= '9') ||
(value >= 'a' && value <= 'f') ||
(value >= 'A' && value <= 'F');
}
static void assert_visible_password_label(InnerBreeze *app)
{
TreeEntry field = node(app->ui.session, 76602);
bool visible = false;
for (int32_t i = 0; i < PendingPaintCount(app->ui.session); ++i) {
PaintCommand paint = PendingPaintAt(app->ui.session, i);
if (paint.value.length != 19 ||
memcmp(paint.value.data, "Recovery passphrase", 19) != 0) {
continue;
}
assert(paint.y >= paint.clip.y);
assert(paint.y + paint.font <= field.bounds.y);
assert(paint.x >= paint.clip.x);
assert(paint.x + MeasureGlyphWidth(paint.value, paint.font, StringView("", 0)) <=
paint.clip.x + paint.clip.width);
visible = true;
}
assert(visible);
}
static void assert_grouped_review_text(void)
{
static const char fingerprint[] =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
unsigned char output[8193];
memset(output, 0xff, sizeof(output));
assert(telegram_account_TelegramAccountReviewText(
StringView(fingerprint, 64), 320, (Slice){output, sizeof(output)}));
assert(strlen((char *)output) == 79);
assert(output[4] == ' ');
size_t digits = 0;
for (size_t i = 0; output[i]; ++i) {
if (output[i] != ' ') {
assert(output[i] == (unsigned char)fingerprint[digits++]);
}
}
assert(digits == 64);
assert(!telegram_account_TelegramAccountReviewText(
StringView(fingerprint, 64), 320, (Slice){output, 64}));
assert(output[0] == 0);
assert(!telegram_account_TelegramAccountReviewText(
StringView("bad\0review", 10), 320, (Slice){output, sizeof(output)}));
assert(output[0] == 0);
assert(!telegram_account_TelegramAccountReviewText(
StringView("\xff", 1), 320, (Slice){output, sizeof(output)}));
assert(output[0] == 0);
assert(!telegram_account_TelegramAccountReviewText(
StringView(fingerprint, 64), 1, (Slice){output, sizeof(output)}));
assert(output[0] == 0);
char maximum[4097];
memset(maximum, 'a', sizeof(maximum));
assert(telegram_account_TelegramAccountReviewText(
StringView(maximum, 4096), 320, (Slice){output, sizeof(output)}));
assert(strlen((char *)output) == 5119);
assert(!telegram_account_TelegramAccountReviewText(
StringView(maximum, sizeof(maximum)), 320, (Slice){output, sizeof(output)}));
assert(output[0] == 0);
}
static void assert_complete_fingerprint_paint(InnerBreeze *app, TelegramAccountView *view)
{
static const char *fingerprints[] = {
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"89abcdef0123456789abcdef0123456789abcdef0123456789abcdef01234567",
"ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789"
};
static const struct {
int32_t width;
int32_t font;
} layouts[] = {
{320, 16}, {400, 24}, {900, 16}, {180, 48}
};
view->password = NULL;
view->allowed_actions = 0;
view->busy = false;
view->restore_failed = false;
app->ui.view_height = 2000;
for (size_t layout = 0; layout < sizeof(layouts) / sizeof(layouts[0]); ++layout) {
app->ui.view_width = layouts[layout].width;
AppMetricsConfigureFont(layouts[layout].font);
for (size_t key = 0; key < sizeof(fingerprints) / sizeof(fingerprints[0]); ++key) {
view->details = StringView(fingerprints[key], 64);
assert(render(app, view) == 0);
assert(view->details.length == 64);
assert(memcmp(view->details.data, fingerprints[key], 64) == 0);
assert(strlen((char *)view->seen_review_details) == 64);
assert(memcmp(view->seen_review_details, fingerprints[key], 64) == 0);
assert(strlen((char *)view->seen_review_id) == view->review_id.length);
assert(memcmp(view->seen_review_id, view->review_id.data, view->review_id.length) == 0);
size_t digits = 0;
for (int32_t i = 0; i < PendingPaintCount(app->ui.session); ++i) {
PaintCommand paint = PendingPaintAt(app->ui.session, i);
bool fingerprint_line = paint.value.length > 0;
for (size_t j = 0; j < paint.value.length; ++j) {
if (!hex_digit((unsigned char)paint.value.data[j]) && paint.value.data[j] != ' ') {
fingerprint_line = false;
}
}
if (!fingerprint_line) {
continue;
}
assert(paint.x >= paint.clip.x);
assert(paint.x + MeasureGlyphWidth(paint.value, paint.font, StringView("", 0)) <=
paint.clip.x + paint.clip.width);
assert(paint.y >= paint.clip.y);
assert(paint.y + paint.font <= paint.clip.y + paint.clip.height);
for (size_t j = 0; j < paint.value.length; ++j) {
if (paint.value.data[j] != ' ') {
assert(digits < 64);
assert(paint.value.data[j] == fingerprints[key][digits++]);
}
}
}
assert(digits == 64);
}
}
AppMetricsConfigureFont(16);
}
static void assert_scrolled_review_paint(InnerBreeze *app, TelegramAccountView *view)
{
static const char fingerprint[] =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
char details[1024];
int count = snprintf(details, sizeof(details),
"App: inbe\nAccount: %s\nBot: 123456 Telegram user: 700001\n"
"Node: %s\nAddress: https://api.waozi.xyz\n"
"Read and write only: private.inbe.v2.lumi\n"
"Request expires at (Unix seconds): 1791529031\n\n"
"Signing key fingerprint: %s\nEncryption key fingerprint: %s",
fingerprint, fingerprint, fingerprint, fingerprint);
assert(count > 0 && (size_t)count < sizeof(details));
static const struct {
int32_t width;
int32_t height;
int32_t font;
float scale;
} layouts[] = {
{320, 360, 16, 1.0f},
{400, 360, 24, 1.0f},
{900, 500, 24, 1.6f}
};
view->password = NULL;
view->allowed_actions = (1u << 4) | (1u << 6);
view->review_complete = true;
view->busy = false;
view->restore_failed = false;
view->details = StringView(details, (size_t)count);
for (size_t layout = 0; layout < sizeof(layouts) / sizeof(layouts[0]); ++layout) {
app->ui.view_width = layouts[layout].width;
app->ui.view_height = layouts[layout].height;
AppMetricsConfigure(layouts[layout].scale);
AppMetricsConfigureFont(layouts[layout].font);
view->review_seen = false;
view->scroll_offset = 0;
assert(render(app, view) == 0);
assert(!view->review_seen);
assert(node(app->ui.session, 76616).disabled);
int32_t review_node = -1;
TreeEntry review_entry = {0};
TreeEntry scroll_entry = {0};
for (int32_t index = 0; index < TreeCount(app->ui.session); ++index) {
TreeEntry entry = TreeNodeAt(app->ui.session, index);
if (entry.key == 76601) {
scroll_entry = entry;
}
if (contains(entry.semantic_label, "Signing key fingerprint:")) {
review_node = index;
review_entry = entry;
}
}
assert(review_node >= 0);
assert(scroll_entry.clips_children);
assert(scroll_entry.child_clip.width < scroll_entry.bounds.width);
assert(review_entry.bounds.x + review_entry.bounds.width <=
scroll_entry.child_clip.x + scroll_entry.child_clip.width);
size_t digits = 0;
for (int32_t index = 0; index < PendingPaintCount(app->ui.session); ++index) {
PaintCommand paint = PendingPaintAt(app->ui.session, index);
if (paint.node != review_node || paint.value.length == 0) {
continue;
}
assert(paint.x >= scroll_entry.child_clip.x);
assert(paint.x + MeasureGlyphWidth(paint.value, paint.font, StringView("", 0)) <=
scroll_entry.child_clip.x + scroll_entry.child_clip.width);
size_t offset = 0;
while (offset < paint.value.length) {
size_t end = offset;
while (end < paint.value.length && paint.value.data[end] != ' ') {
++end;
}
bool group = end - offset == 4;
for (size_t byte = offset; byte < end; ++byte) {
group = group && hex_digit((unsigned char)paint.value.data[byte]);
}
if (group) {
for (size_t byte = offset; byte < end; ++byte) {
assert(digits < 256);
assert(paint.value.data[byte] == fingerprint[digits % 64]);
++digits;
}
}
offset = end + 1;
}
}
assert(digits == 256);
assert(view->details.length == (size_t)count);
assert(memcmp(view->details.data, details, (size_t)count) == 0);
assert(strcmp((char *)view->seen_review_details, details) == 0);
view->scroll_offset = 100000;
assert(render(app, view) == 0);
assert(view->review_seen);
TreeEntry approve = node(app->ui.session, 76616);
assert(!approve.disabled);
assert(approve.bounds.x + approve.bounds.width <= approve.clip.x + approve.clip.width);
TreeEntry cancel = node(app->ui.session, 76614);
assert(cancel.bounds.x + cancel.bounds.width <= cancel.clip.x + cancel.clip.width);
}
AppMetricsConfigure(1.0f);
AppMetricsConfigureFont(16);
}
int main(void)
{
TelegramAccountPassword password = {0};
unsigned char mask[257] = {0};
memcpy(password.value, "a\xc3\xa9\xe6\x97\xa5", 6);
assert(telegram_account_TelegramAccountPasswordMask(&password, (Slice){mask, sizeof(mask)}));
assert(strcmp((char *)mask, "***") == 0);
password.value[0] = 0xff;
assert(!telegram_account_TelegramAccountPasswordMask(&password, (Slice){mask, sizeof(mask)}));
assert(mask[0] == 0);
telegram_account_TelegramAccountClearPassword(&password);
assert(password.value[0] == 0);
memcpy(password.value, secret, sizeof(secret));
static InnerBreeze app = {0};
app.ui.session = SessionOpen();
app.ui.view_width = 400;
app.ui.view_height = 360;
AppMetricsConfigure(1.0f);
assert_grouped_review_text();
TelegramAccountView view = {0};
assert(telegram_account_DrawTelegramAccount(&app, &view) == 0);
assert(!telegram_account_TelegramAccountActionAllowed(&view, 0));
assert(!telegram_account_TelegramAccountActionAllowed(&view, 8));
view.visible = true;
view.review_complete = true;
view.review_id = StringView("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 32);
view.allowed_actions = (1u << 4) | (1u << 6);
view.password = &password;
view.password_label = StringView("Recovery passphrase", 19);
static const char details[] =
"Verified account identity\nAccount hash\nBot numeric identifier\nTelegram numeric user\n"
"Issuing node identity\nIssuing audience\nDelegate installation\nLumi exact collection\n"
"Read and write only\nPrivate key generation\nFinite grant expiry\nSigning public fingerprint\n"
"Encryption public fingerprint\nCompare both fingerprints on your other device\n";
view.details = StringView(details, sizeof(details) - 1);
assert(render(&app, &view) == 0);
assert(!view.review_seen);
assert(node(app.ui.session, 76616).disabled);
TreeEntry field = node(app.ui.session, 76602);
assert(field.secure && field.has_text && field.text_bytes == strlen(secret));
for (int32_t i = 0; i < TreeCount(app.ui.session); ++i) {
TreeEntry found = TreeNodeAt(app.ui.session, i);
if (found.focus_id == 76602) {
unsigned char bytes[257];
assert(TreeTextRange(app.ui.session, i, 0, (int32_t)strlen(secret),
(Slice){bytes, sizeof(bytes)}).length == 0);
}
}
view.scroll_offset = 100000;
assert(render(&app, &view) == 0);
assert(view.review_seen);
assert_visible_password_label(&app);
assert(!node(app.ui.session, 76616).disabled);
click(app.ui.session, node(app.ui.session, 76616));
assert(render(&app, &view) == 6);
view.review_id = StringView("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 32);
assert(render(&app, &view) == 0);
assert(!view.review_seen && node(app.ui.session, 76616).disabled);
view.scroll_offset = 100000;
view.busy = true;
assert(render(&app, &view) == 0);
assert(node(app.ui.session, 76616).disabled);
assert(!node(app.ui.session, 76614).disabled);
click(app.ui.session, node(app.ui.session, 76614));
assert(render(&app, &view) == 4);
for (size_t i = 0; i < sizeof(password.value); ++i) assert(password.value[i] == 0);
view.details = StringView(details + 1, sizeof(details) - 2);
view.review_seen = true;
view.review_complete = false;
render(&app, &view);
assert(!view.review_seen);
assert(view.scroll_offset == 0);
view.details = StringView("", 0);
view.review_seen = true;
view.busy = false;
assert(!telegram_account_TelegramAccountApprovalReady(&view, true));
view.details = StringView(details, sizeof(details) - 1);
assert(!telegram_account_TelegramAccountApprovalReady(&view, false));
view.review_complete = false;
assert(!telegram_account_TelegramAccountApprovalReady(&view, true));
assert_complete_fingerprint_paint(&app, &view);
assert_scrolled_review_paint(&app, &view);
assert(SessionClose(app.ui.session));
puts("Telegram account visible label, complete fingerprints, masked passphrase, consent and cancel checks passed");
return 0;
}