upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_display.c
blob: 81f67a7c8c3df256b5aa3bbec1df4d12143dc7a7 (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
#include "test_framework.h"
#include <string.h>
#include <stdio.h>

static int escape_wifi_field(const char *src, char *dst, int dst_size) {
    int si = 0, di = 0;
    while (src[si] && di < dst_size - 2) {
        char c = src[si];
        if (c == '\\' || c == ';' || c == ':' || c == ',' || c == '"') {
            if (di + 2 >= dst_size) break;
            dst[di++] = '\\';
            dst[di++] = c;
        } else {
            dst[di++] = c;
        }
        si++;
    }
    dst[di] = '\0';
    return di;
}

static int test_escape_no_special(void) {
    char dst[64];
    int len = escape_wifi_field("HelloWorld", dst, sizeof(dst));
    ASSERT(strcmp(dst, "HelloWorld") == 0, "no special chars unchanged");
    ASSERT(len == 10, "no special chars length correct");
    return 0;
}

static int test_escape_semicolon(void) {
    char dst[64];
    int len = escape_wifi_field("Hello;World", dst, sizeof(dst));
    ASSERT(strcmp(dst, "Hello\\;World") == 0, "semicolon escaped");
    ASSERT(len == 12, "semicolon escaped length correct");
    return 0;
}

static int test_escape_colon(void) {
    char dst[64];
    int len = escape_wifi_field("Hello:World", dst, sizeof(dst));
    ASSERT(strcmp(dst, "Hello\\:World") == 0, "colon escaped");
    ASSERT(len == 12, "colon escaped length correct");
    return 0;
}

static int test_escape_backslash(void) {
    char dst[64];
    int len = escape_wifi_field("Hello\\World", dst, sizeof(dst));
    ASSERT(strcmp(dst, "Hello\\\\World") == 0, "backslash escaped");
    ASSERT(len == 12, "backslash escaped length correct");
    return 0;
}

static int test_escape_comma(void) {
    char dst[64];
    int len = escape_wifi_field("Hello,World", dst, sizeof(dst));
    ASSERT(strcmp(dst, "Hello\\,World") == 0, "comma escaped");
    ASSERT(len == 12, "comma escaped length correct");
    return 0;
}

static int test_escape_quote(void) {
    char dst[64];
    int len = escape_wifi_field("Hello\"World", dst, sizeof(dst));
    ASSERT(strcmp(dst, "Hello\\\"World") == 0, "quote escaped");
    ASSERT(len == 12, "quote escaped length correct");
    return 0;
}

static int test_escape_multiple(void) {
    char dst[64];
    int len = escape_wifi_field("a;b:c\\d", dst, sizeof(dst));
    ASSERT(strcmp(dst, "a\\;b\\:c\\\\d") == 0, "multiple special chars escaped");
    ASSERT(len == 10, "multiple special chars length correct");
    return 0;
}

static int test_escape_empty(void) {
    char dst[64];
    int len = escape_wifi_field("", dst, sizeof(dst));
    ASSERT(strcmp(dst, "") == 0, "empty string stays empty");
    ASSERT(len == 0, "empty string length is 0");
    return 0;
}

static int test_escape_overflow(void) {
    char dst[5];
    int len = escape_wifi_field("Hello;World", dst, sizeof(dst));
    ASSERT(len < (int)sizeof(dst), "output truncated on overflow");
    ASSERT(dst[len] == '\0', "still null-terminated after truncation");
    return 0;
}

static int test_escape_ssid_like(void) {
    char dst[64];
    int len = escape_wifi_field("TollGate-C0E9CA", dst, sizeof(dst));
    ASSERT(strcmp(dst, "TollGate-C0E9CA") == 0, "TollGate SSID no escaping needed");
    ASSERT(len == 15, "TollGate SSID length correct");
    return 0;
}

static int test_escape_all_special_in_one(void) {
    char dst[64];
    int len = escape_wifi_field("\\;:,\"", dst, sizeof(dst));
    ASSERT(strcmp(dst, "\\\\\\;\\:\\,\\\"") == 0, "all special chars in sequence");
    ASSERT(len == 10, "all special chars length correct");
    return 0;
}

int main(void) {
    int failed = 0;
    failed += test_escape_no_special();
    failed += test_escape_semicolon();
    failed += test_escape_colon();
    failed += test_escape_backslash();
    failed += test_escape_comma();
    failed += test_escape_quote();
    failed += test_escape_multiple();
    failed += test_escape_empty();
    failed += test_escape_overflow();
    failed += test_escape_ssid_like();
    failed += test_escape_all_special_in_one();

    if (failed == 0) {
        printf("\n=== ALL DISPLAY TESTS PASSED ===\n");
    }
    return failed;
}