This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit 263955bf2b9d9e0ca2b8d187bef4b7592efa249c
Author: Boris Faure <[email protected]>
AuthorDate: Sun Nov 15 18:56:13 2026 +0000

    tests: add the program status unit test
    
    The shell scripts check the store end to end through the checksum;
    this one walks the parser one case at a time, where a script cannot
    say which check failed: the field limits, the grammar of state, id
    and app, the values that are absent or discard the whole report,
    and the eviction order.
    
    The record walks here also go over the chain by hand rather than
    through EINA_INLIST_FOREACH, which the undefined-behavior sanitizer
    rejects.
---
 src/bin/termptystatus.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termptystatus.h |   4 +
 src/bin/tytest.c        |  16 +++-
 src/bin/unit_tests.h    |   1 +
 4 files changed, 206 insertions(+), 4 deletions(-)

diff --git a/src/bin/termptystatus.c b/src/bin/termptystatus.c
index aa4edfda..8b1985f5 100644
--- a/src/bin/termptystatus.c
+++ b/src/bin/termptystatus.c
@@ -663,3 +663,192 @@ termpty_program_status_clear_all(Termpty *ty)
    ty->program_records_count = 0;
    ty->program_status_seen = EINA_FALSE;
 }
+
+#if defined(BINARY_TYTEST)
+#include <assert.h>
+
+static void
+_ty_ps_feed(Termpty *ty, const char *body)
+{
+   Eina_Unicode *uni = eina_unicode_utf8_to_unicode(body, NULL);
+
+   assert(uni);
+   termpty_program_status_handle(ty, uni);
+   free(uni);
+}
+
+static Termpty_Program_Record *
+_ty_ps_record_get(Termpty *ty, const char *id)
+{
+   Eina_Inlist *l;
+
+   for (l = ty->program_records; l; l = l->next)
+     {
+        Termpty_Program_Record *rec = _PS_REC(l);
+
+        if (strcmp(rec->id, id) == 0)
+          return rec;
+     }
+   return NULL;
+}
+
+int
+tytest_program_status_parse(void)
+{
+   Termpty ty;
+   Termpty_Program_Record *rec;
+   int i;
+
+   memset(&ty, 0, sizeof(ty));
+
+   /* a full report: progress, a decoded msg */
+   _ty_ps_feed(&ty, "state=working:progress=40:msg=Y2FyZ28gdGVzdA==");
+   rec = _ty_ps_record_get(&ty, "");
+   assert(rec);
+   assert(rec->state == TERMPTY_PROGRAM_STATE_WORKING);
+   assert(rec->progress == 40);
+   assert(rec->msg);
+   assert(strcmp(rec->msg, "cargo test") == 0);
+
+   /* the root record is replaced completely: the msg is gone */
+   _ty_ps_feed(&ty, "state=working");
+   rec = _ty_ps_record_get(&ty, "");
+   assert(rec);
+   assert(rec->msg == NULL);
+   assert(rec->title == NULL);
+
+   /* unpadded base64 */
+   _ty_ps_feed(&ty, "id=tf:state=blocked:kind=permission:app=terraform:"
+                    "msg=QXBwbHkgMyBjaGFuZ2VzPw");
+   rec = _ty_ps_record_get(&ty, "tf");
+   assert(rec);
+   assert(rec->state == TERMPTY_PROGRAM_STATE_BLOCKED);
+   assert(rec->kind == TERMPTY_PROGRAM_KIND_PERMISSION);
+   assert(rec->progress == -1);
+   assert(strcmp(rec->app, "terraform") == 0);
+   assert(rec->msg);
+   assert(strcmp(rec->msg, "Apply 3 changes?") == 0);
+
+   /* out of place or out of range values are absent, the rest applies */
+   _ty_ps_feed(&ty, "id=a:state=working:progress=101:kind=auth");
+   rec = _ty_ps_record_get(&ty, "a");
+   assert(rec);
+   assert(rec->progress == -1);
+   assert(rec->kind == TERMPTY_PROGRAM_KIND_NONE);
+   _ty_ps_feed(&ty, "id=b:state=done:progress=50:app=bad!app");
+   rec = _ty_ps_record_get(&ty, "b");
+   assert(rec);
+   assert(rec->progress == -1);
+   assert(rec->app[0] == '\0');
+   /* progress=42.5 parses as a value, but not as an integer */
+   _ty_ps_feed(&ty, "id=a:state=working:progress=42.5");
+   rec = _ty_ps_record_get(&ty, "a");
+   assert(rec);
+   assert(rec->progress == -1);
+   /* an empty progress is not a number either */
+   _ty_ps_feed(&ty, "id=a:state=working:progress=");
+   rec = _ty_ps_record_get(&ty, "a");
+   assert(rec);
+   assert(rec->progress == -1);
+   /* kind on a non-blocked state */
+   _ty_ps_feed(&ty, "id=b:state=done:kind=auth");
+   rec = _ty_ps_record_get(&ty, "b");
+   assert(rec);
+   assert(rec->kind == TERMPTY_PROGRAM_KIND_NONE);
+
+   /* whitespace, repeated keys (last wins), unknown keys */
+   _ty_ps_feed(&ty, " id = c : state=idle : state=error : colour=red ");
+   rec = _ty_ps_record_get(&ty, "c");
+   assert(rec);
+   assert(rec->state == TERMPTY_PROGRAM_STATE_ERROR);
+
+   /* a malformed pair is skipped, the rest applies */
+   _ty_ps_feed(&ty, "id=d:nonsense:STATE=x:state=done");
+   rec = _ty_ps_record_get(&ty, "d");
+   assert(rec);
+   assert(rec->state == TERMPTY_PROGRAM_STATE_DONE);
+   /* the root record, tf, a, b, c and d */
+   assert(ty.program_records_count == 6);
+
+   /* a non-ascii codepoint makes its pair malformed */
+   _ty_ps_feed(&ty, "id=e:state=working:id=\xc3\xa9:state=error");
+   rec = _ty_ps_record_get(&ty, "e");
+   assert(rec);
+   assert(strcmp(rec->id, "e") == 0);
+   assert(rec->state == TERMPTY_PROGRAM_STATE_ERROR);
+
+   /* U+202E is stripped from the text, the report still applies */
+   _ty_ps_feed(&ty, "id=f:state=done:msg=4oCuZXZpbA==");
+   rec = _ty_ps_record_get(&ty, "f");
+   assert(rec);
+   assert(rec->msg);
+   assert(strcmp(rec->msg, "evil") == 0);
+
+   /* reports that change nothing */
+   i = ty.program_records_count;
+   _ty_ps_feed(&ty, "id=x");                     /* no state */
+   _ty_ps_feed(&ty, "id=x:state=sleeping");      /* unknown state */
+   _ty_ps_feed(&ty, "id=a//b:state=idle");       /* empty id segment */
+   _ty_ps_feed(&ty, "id=a/b/c/d/e/f/g/h/i:state=idle"); /* 9 levels */
+   _ty_ps_feed(&ty, "id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:state=idle"); /* 33 bytes */
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=YQpi");  /* a newline */
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=YQli");  /* a tab */
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=Q");     /* 4n+1 characters */
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=Q==");
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=A===");  /* '=' before padding */
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=AB=CD");
+   _ty_ps_feed(&ty, "id=keep:state=idle:msg=YQ");    /* a decoded NUL */
+   _ty_ps_feed(&ty, "id=keep:state=idle:title=");    /* empty is fine */
+   rec = _ty_ps_record_get(&ty, "keep");
+   assert(rec);
+   assert(rec->msg == NULL);
+   assert(rec->title && rec->title[0] == '\0');
+   assert(ty.program_records_count == i + 1);
+   assert(ty.program_status_seen == EINA_TRUE);
+
+   /* clear with an id takes the record and its children; a sibling
+    * sharing the prefix stays */
+   _ty_ps_feed(&ty, "id=parent:state=done");
+   _ty_ps_feed(&ty, "id=parent/child:state=error");
+   _ty_ps_feed(&ty, "id=parentx:state=idle");
+   _ty_ps_feed(&ty, "id=parent:state=clear");
+   assert(_ty_ps_record_get(&ty, "parent") == NULL);
+   assert(_ty_ps_record_get(&ty, "parent/child") == NULL);
+   assert(_ty_ps_record_get(&ty, "parentx"));
+
+   /* a global clear may carry a title and a msg, which this path frees */
+   _ty_ps_feed(&ty, "state=clear:title=aGk=:msg=ZXZpbA==");
+   assert(ty.program_records_count == 0);
+
+   /* LRU eviction: the least recently updated record goes first */
+   termpty_program_status_clear_all(&ty);
+   for (i = 0; i < 260; i++)
+     {
+        char body[64];
+
+        snprintf(body, sizeof(body), "id=j%d:state=working", i);
+        _ty_ps_feed(&ty, body);
+     }
+   assert(ty.program_records_count == PS_RECORDS_MAX);
+   /* four records more than the cap: j0 to j3 were evicted */
+   assert(_ty_ps_record_get(&ty, "j0") == NULL);
+   assert(_ty_ps_record_get(&ty, "j3") == NULL);
+   assert(_ty_ps_record_get(&ty, "j4"));
+   assert(_ty_ps_record_get(&ty, "j259"));
+
+   /* updating a record makes it the most recently updated one */
+   _ty_ps_feed(&ty, "id=j4:state=done");
+   _ty_ps_feed(&ty, "id=new:state=idle");
+   assert(_ty_ps_record_get(&ty, "j4"));
+   assert(_ty_ps_record_get(&ty, "j5") == NULL);
+   assert(_ty_ps_record_get(&ty, "new"));
+   assert(ty.program_records_count == PS_RECORDS_MAX);
+
+   /* clear_all resets the seen flag too */
+   termpty_program_status_clear_all(&ty);
+   assert(ty.program_records_count == 0);
+   assert(ty.program_status_seen == EINA_FALSE);
+
+   return 0;
+}
+#endif
diff --git a/src/bin/termptystatus.h b/src/bin/termptystatus.h
index 49e834d6..49255d73 100644
--- a/src/bin/termptystatus.h
+++ b/src/bin/termptystatus.h
@@ -56,4 +56,8 @@ void termpty_program_status_summary_get(const Termpty *ty,
 
 void termpty_program_status_clear_all(Termpty *ty);
 
+#if defined(BINARY_TYTEST)
+int tytest_program_status_parse(void);
+#endif
+
 #endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 7c58bda1..9ebd1902 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -83,6 +83,7 @@ static struct {
        { "osc9_notify", tytest_osc9_notify},
        { "osc9_progress", tytest_osc9_progress},
        { "osc9_progress_cb", tytest_osc9_progress_cb},
+       { "program_status_parse", tytest_program_status_parse},
        { "notify_meta_parse", tytest_notify_meta_parse},
        { "notify_pending_cap", tytest_notify_pending_cap},
        { "notify_query_gated", tytest_notify_query_gated},
@@ -700,11 +701,15 @@ _tytest_checksum_hex(Termpty *ty, char *md5out)
     * so tests that send none are unchanged. */
    if (ty->program_records)
      {
-        const Termpty_Program_Record *rec;
+        const Eina_Inlist *l;
         char line[TERMPTY_PROGRAM_STATUS_LINE_MAX];
 
-        EINA_INLIST_FOREACH(ty->program_records, rec)
+        /* no EINA_INLIST_FOREACH: it trips the undefined-behavior sanitizer */
+        for (l = ty->program_records; l; l = l->next)
           {
+             const Termpty_Program_Record *rec =
+                EINA_INLIST_CONTAINER_GET(l, Termpty_Program_Record);
+
              snprintf(line, sizeof(line), "%s|%d|%d|%d|%s|%s|%s\n",
                       rec->id, (int)rec->state, (int)rec->kind,
                       (int)rec->progress, rec->app,
@@ -996,10 +1001,13 @@ _tytest_dump(Termpty *ty)
           (int)ty->progress.state, (int)ty->progress.value);
    if (ty->program_records)
      {
-        const Termpty_Program_Record *rec;
+        const Eina_Inlist *l;
 
-        EINA_INLIST_FOREACH(ty->program_records, rec)
+        for (l = ty->program_records; l; l = l->next)
           {
+             const Termpty_Program_Record *rec =
+                EINA_INLIST_CONTAINER_GET(l, Termpty_Program_Record);
+
              printf("program_status id=%s state=%d kind=%d progress=%d "
                     "app=%s title=%s msg=%s\n",
                     rec->id, (int)rec->state, (int)rec->kind,
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index f5c9b6ea..ec6da929 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -56,6 +56,7 @@ int tytest_notify_gated(void);
 int tytest_osc9_notify(void);
 int tytest_osc9_progress(void);
 int tytest_osc9_progress_cb(void);
+int tytest_program_status_parse(void);
 int tytest_notify_meta_parse(void);
 int tytest_notify_pending_cap(void);
 int tytest_notify_query_gated(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to