summaryrefslogtreecommitdiff
path: root/tests/test_mains_proxy.c
blob: b95a9044236679323cccd610b035d0864c67e872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "hybbx/mains_proxy.h"
#include "hybbx/util.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static unsigned g_failures;

static void check_str(const char *label, const char *got, const char *want)
{
    if (got == NULL || want == NULL || strcmp(got, want) != 0) {
        fprintf(stderr, "FAIL %s: got '%s' want '%s'\n",
                label, got != NULL ? got : "(null)", want);
        g_failures++;
    }
}

static void check_int(const char *label, int got, int want)
{
    if (got != want) {
        fprintf(stderr, "FAIL %s: got %d want %d\n", label, got, want);
        g_failures++;
    }
}

static void check_uint(const char *label, unsigned got, unsigned want)
{
    if (got != want) {
        fprintf(stderr, "FAIL %s: got %u want %u\n", label, got, want);
        g_failures++;
    }
}

static void parse_one_peer(const char *section,
                           hybbx_mains_proxy_peer_config_t *out)
{
    hybbx_result_t rc = hybbx_mains_proxy_peer_parse(section, out);
    if (rc != HYBBX_OK) {
        fprintf(stderr, "FAIL peer_parse returned %d for '%s'\n", (int)rc, section);
        g_failures++;
    }
}

int main(void)
{
    hybbx_mains_proxy_peer_config_t peer;
    const char *multi;
    const char *sep;
    char section_buf[1024];

    /* 1. semicolon-separated section (serialized by hybbx_config_format_section) */
    parse_one_peer(
        "peer_id=alpha;circuit_host=127.0.0.1;circuit_port=7323;"
        "link_id=alpha-main;link_password=secret;enabled=yes;wire=circuit;"
        "duplex=full;use_secondary=yes",
        &peer);
    check_str("semicolon peer_id", peer.peer_id, "alpha");
    check_str("semicolon circuit_host", peer.circuit_host, "127.0.0.1");
    check_uint("semicolon circuit_port", peer.circuit_port, 7323u);
    check_str("semicolon link_id", peer.link_id, "alpha-main");
    check_str("semicolon link_password", peer.link_password, "secret");
    check_int("semicolon enabled", peer.enabled, 1);
    check_int("semicolon wire", (int)peer.wire, (int)HYBBX_MAINS_PROXY_WIRE_CIRCUIT);
    check_int("semicolon duplex", (int)peer.duplex, (int)HYBBX_MAINS_PROXY_DUPLEX_FULL);
    check_int("semicolon use_secondary", peer.use_secondary, 1);

    /* 2. newline-separated section */
    parse_one_peer(
        "peer_id=beta\ncircuit_host=127.0.0.2\ncircuit_port=7324\n"
        "link_id=beta-main\nlink_password=other\nenabled=no\n"
        "wire=ax25\nduplex=half\nuse_secondary=no",
        &peer);
    check_str("newline peer_id", peer.peer_id, "beta");
    check_str("newline circuit_host", peer.circuit_host, "127.0.0.2");
    check_uint("newline circuit_port", peer.circuit_port, 7324u);
    check_str("newline link_id", peer.link_id, "beta-main");
    check_str("newline link_password", peer.link_password, "other");
    check_int("newline enabled", peer.enabled, 0);
    check_int("newline wire", (int)peer.wire, (int)HYBBX_MAINS_PROXY_WIRE_AX25);
    check_int("newline duplex", (int)peer.duplex, (int)HYBBX_MAINS_PROXY_DUPLEX_HALF);
    check_int("newline use_secondary", peer.use_secondary, 0);

    /* 3. multiple peers separated by instance separator */
    multi =
        "peer_id=first;circuit_host=127.0.0.1;enabled=yes\x1e"
        "peer_id=second;circuit_host=127.0.0.2;enabled=yes\x1e"
        "enabled=no\x1e"
        "peer_id=fourth;circuit_host=127.0.0.4;enabled=yes";

    sep = multi;
    for (unsigned i = 0; i < 4; i++) {
        const char *next = strchr(sep, HYBBX_MAINS_PROXY_INSTANCE_SEP);
        size_t len;

        if (next != NULL) {
            len = (size_t)(next - sep);
        } else {
            len = strlen(sep);
        }

        if (len >= sizeof(section_buf)) {
            len = sizeof(section_buf) - 1;
        }
        memcpy(section_buf, sep, len);
        section_buf[len] = '\0';

        parse_one_peer(section_buf, &peer);

        switch (i) {
        case 0:
            check_str("multi[0] peer_id", peer.peer_id, "first");
            check_str("multi[0] circuit_host", peer.circuit_host, "127.0.0.1");
            check_int("multi[0] enabled", peer.enabled, 1);
            break;
        case 1:
            check_str("multi[1] peer_id", peer.peer_id, "second");
            check_str("multi[1] circuit_host", peer.circuit_host, "127.0.0.2");
            check_int("multi[1] enabled", peer.enabled, 1);
            break;
        case 2:
            /* section with only enabled=no: disabled peer preserves defaults */
            check_int("multi[2] enabled", peer.enabled, 0);
            check_str("multi[2] peer_id", peer.peer_id, "");
            check_str("multi[2] circuit_host", peer.circuit_host, "");
            break;
        case 3:
            check_str("multi[3] peer_id", peer.peer_id, "fourth");
            check_str("multi[3] circuit_host", peer.circuit_host, "127.0.0.4");
            check_int("multi[3] enabled", peer.enabled, 1);
            break;
        }

        if (next == NULL) {
            break;
        }
        sep = next + 1;
    }

    /* 4. missing credentials: empty peer_id, empty link_password, invalid port */
    parse_one_peer(
        "circuit_host=127.0.0.1;circuit_port=99999;link_id=main;enabled=yes",
        &peer);
    check_str("missing peer_id", peer.peer_id, "");
    check_str("missing link_password", peer.link_password, "");
    /* invalid port should fall back to default */
    check_uint("invalid port default", peer.circuit_port,
               (unsigned)HYBBX_CIRCUIT_DEFAULT_PORT);

    /* 5. legacy host/port keys: host maps to circuit_host, port is ignored */
    parse_one_peer(
        "peer_id=legacy;circuit_host=127.0.0.1;host=127.0.0.9;port=9999;"
        "link_id=main;link_password=secret;enabled=yes",
        &peer);
    check_str("legacy host ignored", peer.circuit_host, "127.0.0.1");
    check_uint("legacy port ignored", peer.port, 9999u);

    /* 6. empty section should yield defaults */
    parse_one_peer("", &peer);
    check_str("empty peer_id", peer.peer_id, "");
    check_int("empty enabled default", peer.enabled, 1);
    check_int("empty wire default", (int)peer.wire,
              (int)HYBBX_MAINS_PROXY_WIRE_CIRCUIT);
    check_int("empty duplex default", (int)peer.duplex,
              (int)HYBBX_MAINS_PROXY_DUPLEX_FULL);
    check_int("empty use_secondary default", peer.use_secondary, 1);


    /* 7. mesh active: stopped mesh reports inactive */
    hybbx_mains_proxy_mesh_stop(NULL);
    check_int("mesh_active when stopped", hybbx_mains_proxy_mesh_active(), 0);

    /* 8. peer parse leaves legacy host intact when circuit_host is set */
    parse_one_peer(
        "peer_id=explicit;circuit_host=127.0.0.1;host=127.0.0.9;"
        "link_id=main;link_password=secret;enabled=yes",
        &peer);
    check_str("circuit_host wins over host", peer.circuit_host, "127.0.0.1");

    if (g_failures != 0) {
        fprintf(stderr, "%u failure(s)\n", g_failures);
        return 1;
    }

    puts("test_mains_proxy: ok");
    return 0;
}

/* Test kept separate above; this file ends after main() in the original. */
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com