LCOV - code coverage report
Current view: top level - tests/unit - test_wizard.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 48 48
Test Date: 2026-04-15 21:12:52 Functions: 100.0 % 2 2

            Line data    Source code
       1              : #include "test_helpers.h"
       2              : #include "setup_wizard.h"
       3              : #include "raii.h"
       4              : #include <stdio.h>
       5              : #include <string.h>
       6              : #include <stdlib.h>
       7              : #include <unistd.h>
       8              : 
       9            5 : static void config_cleanup(void *ptr) {
      10            5 :     Config **cfg = (Config **)ptr;
      11            5 :     if (cfg && *cfg) {
      12            3 :         config_free(*cfg);
      13            3 :         *cfg = NULL;
      14              :     }
      15            5 : }
      16              : 
      17            1 : void test_wizard(void) {
      18              :     // 1. Full valid input
      19              :     {
      20            1 :         const char *input = "imaps://imap.test.com\ntest@user.com\nsecretpass\nMYFOLDER\n";
      21            2 :         RAII_FILE FILE *stream = fmemopen((void*)input, strlen(input), "r");
      22            1 :         ASSERT(stream != NULL, "fmemopen should succeed");
      23              : 
      24            2 :         RAII_WITH_CLEANUP(config_cleanup) Config *cfg = setup_wizard_run_internal(stream);
      25            1 :         ASSERT(cfg != NULL, "setup_wizard_run_internal should return config");
      26            1 :         ASSERT(strcmp(cfg->host, "imaps://imap.test.com") == 0, "Host should match input");
      27            1 :         ASSERT(strcmp(cfg->user, "test@user.com") == 0, "User should match input");
      28            1 :         ASSERT(strcmp(cfg->pass, "secretpass") == 0, "Pass should match input");
      29            1 :         ASSERT(strcmp(cfg->folder, "MYFOLDER") == 0, "Folder should match input");
      30              :     }
      31              : 
      32              :     // 2. Empty folder defaults to INBOX
      33              :     {
      34            1 :         const char *input2 = "h\nu\np\n\n";
      35            2 :         RAII_FILE FILE *stream = fmemopen((void*)input2, strlen(input2), "r");
      36            2 :         RAII_WITH_CLEANUP(config_cleanup) Config *cfg = setup_wizard_run_internal(stream);
      37            1 :         ASSERT(cfg != NULL, "setup_wizard should return config with empty folder");
      38            1 :         ASSERT(strcmp(cfg->folder, "INBOX") == 0, "Folder should default to INBOX");
      39              :     }
      40              : 
      41              :     // 3. EOF after host: user field missing → should return NULL
      42              :     {
      43            1 :         const char *input3 = "imaps://imap.test.com\n";
      44            2 :         RAII_FILE FILE *stream = fmemopen((void*)input3, strlen(input3), "r");
      45            1 :         ASSERT(stream != NULL, "fmemopen should succeed for partial input");
      46            2 :         RAII_WITH_CLEANUP(config_cleanup) Config *cfg = setup_wizard_run_internal(stream);
      47            1 :         ASSERT(cfg == NULL, "setup_wizard should return NULL when user input is missing");
      48              :     }
      49              : 
      50              :     // 4. EOF after host+user: password field missing → should return NULL
      51              :     {
      52            1 :         const char *input4 = "imaps://imap.test.com\ntest@user.com\n";
      53            2 :         RAII_FILE FILE *stream = fmemopen((void*)input4, strlen(input4), "r");
      54            1 :         ASSERT(stream != NULL, "fmemopen should succeed for partial input");
      55            2 :         RAII_WITH_CLEANUP(config_cleanup) Config *cfg = setup_wizard_run_internal(stream);
      56            1 :         ASSERT(cfg == NULL, "setup_wizard should return NULL when password is missing");
      57              :     }
      58              : 
      59              :     // 5. Test setup_wizard_run() (stdin path) via pipe redirect
      60              :     {
      61            1 :         const char *input5 = "imaps://imap.stdin.com\nstdin@user.com\nstdinpass\nSTDIN\n";
      62            1 :         int pipefd[2];
      63            1 :         ASSERT(pipe(pipefd) == 0, "pipe() should succeed");
      64            1 :         ssize_t written = write(pipefd[1], input5, strlen(input5));
      65            1 :         ASSERT(written > 0, "write to pipe should succeed");
      66            1 :         close(pipefd[1]);
      67              : 
      68            1 :         int saved_stdin = dup(STDIN_FILENO);
      69            1 :         dup2(pipefd[0], STDIN_FILENO);
      70            1 :         close(pipefd[0]);
      71              : 
      72            2 :         RAII_WITH_CLEANUP(config_cleanup) Config *cfg = setup_wizard_run();
      73              : 
      74            1 :         dup2(saved_stdin, STDIN_FILENO);
      75            1 :         close(saved_stdin);
      76              : 
      77            1 :         ASSERT(cfg != NULL, "setup_wizard_run should return config via pipe");
      78            1 :         ASSERT(strcmp(cfg->host, "imaps://imap.stdin.com") == 0, "Host should match stdin input");
      79            1 :         ASSERT(strcmp(cfg->user, "stdin@user.com") == 0, "User should match stdin input");
      80            1 :         ASSERT(strcmp(cfg->pass, "stdinpass") == 0, "Pass should match stdin input");
      81            1 :         ASSERT(strcmp(cfg->folder, "STDIN") == 0, "Folder should match stdin input");
      82              :     }
      83              : }
        

Generated by: LCOV version 2.0-1