Line data Source code
1 : #include "test_helpers.h"
2 : #include "config_store.h"
3 : #include "fs_util.h"
4 : #include "logger.h"
5 : #include "raii.h"
6 : #include <string.h>
7 : #include <stdlib.h>
8 : #include <unistd.h>
9 :
10 4 : static void config_cleanup(void *ptr) {
11 4 : Config **cfg = (Config **)ptr;
12 4 : if (cfg && *cfg) {
13 2 : config_free(*cfg);
14 2 : *cfg = NULL;
15 : }
16 4 : }
17 :
18 1 : void test_config_store(void) {
19 1 : char *old_home = getenv("HOME");
20 1 : char *old_xdg = getenv("XDG_CONFIG_HOME");
21 1 : setenv("HOME", "/tmp/tg-cli-test-home", 1);
22 1 : unsetenv("XDG_CONFIG_HOME"); /* ensure we use the HOME-based path */
23 1 : fs_mkdir_p("/tmp/tg-cli-test-home/.config/tg-cli", 0700);
24 :
25 : // 1. Test Save
26 : {
27 1 : Config cfg = {0};
28 1 : cfg.api_base = strdup("https://api.telegram.org");
29 1 : cfg.token = strdup("123456:ABC-DEF");
30 :
31 1 : int res = config_save_to_store(&cfg);
32 1 : ASSERT(res == 0, "config_save_to_store should return 0");
33 :
34 1 : free(cfg.api_base);
35 1 : free(cfg.token);
36 : }
37 :
38 : // 2. Test Load - full config
39 : {
40 2 : RAII_WITH_CLEANUP(config_cleanup) Config *loaded = config_load_from_store();
41 1 : ASSERT(loaded != NULL, "config_load_from_store should not return NULL");
42 1 : ASSERT(strcmp(loaded->token, "123456:ABC-DEF") == 0, "Token should match");
43 1 : ASSERT(loaded->api_base != NULL, "api_base should not be NULL");
44 : }
45 :
46 : // 3. Test Load - incomplete config (missing TOKEN) → should return NULL
47 : {
48 1 : FILE *fp = fopen("/tmp/tg-cli-test-home/.config/tg-cli/config.ini", "w");
49 1 : if (fp) {
50 1 : fprintf(fp, "API_BASE=https://api.telegram.org\n");
51 1 : fclose(fp);
52 : }
53 : /* logger not initialised → logger_log() returns early, no output */
54 2 : RAII_WITH_CLEANUP(config_cleanup) Config *loaded = config_load_from_store();
55 1 : ASSERT(loaded == NULL, "config_load_from_store should return NULL for incomplete config");
56 : }
57 :
58 : // 4. Test Load - no config file → should return NULL
59 : {
60 1 : unlink("/tmp/tg-cli-test-home/.config/tg-cli/config.ini");
61 2 : RAII_WITH_CLEANUP(config_cleanup) Config *loaded = config_load_from_store();
62 1 : ASSERT(loaded == NULL, "config_load_from_store should return NULL when file is missing");
63 : }
64 :
65 : // 5. Test Save/Load with ssl_no_verify=1
66 : {
67 1 : Config cfg2 = {0};
68 1 : cfg2.api_base = strdup("https://api.telegram.org");
69 1 : cfg2.token = strdup("123456:ABC-DEF");
70 1 : cfg2.ssl_no_verify = 1;
71 :
72 1 : int res = config_save_to_store(&cfg2);
73 1 : ASSERT(res == 0, "config_save_to_store with ssl_no_verify=1 should return 0");
74 :
75 2 : RAII_WITH_CLEANUP(config_cleanup) Config *loaded2 = config_load_from_store();
76 1 : ASSERT(loaded2 != NULL, "config_load_from_store should not return NULL");
77 1 : ASSERT(loaded2->ssl_no_verify == 1, "ssl_no_verify should be 1 after load");
78 :
79 1 : free(cfg2.api_base);
80 1 : free(cfg2.token);
81 : }
82 :
83 : // 6. Test config_free with NULL (should not crash)
84 1 : config_free(NULL);
85 :
86 1 : if (old_home) setenv("HOME", old_home, 1);
87 0 : else unsetenv("HOME");
88 1 : if (old_xdg) setenv("XDG_CONFIG_HOME", old_xdg, 1);
89 : }
|