summaryrefslogtreecommitdiffstats
path: root/src/tests/safe-format-tests.c
blob: 0fe813e383cd143a5650b7efbd61f5c931508e52 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * This file originated in realmd
 *
 * Copyright 2012 Red Hat Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@redhat.com>
 */

#include "config.h"

#include "src/util/safe-format-string.h"

#include <check.h>
#include <popt.h>
#include <string.h>
#include <talloc.h>

#ifndef ck_assert_int_ge
#define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
#endif

#ifndef ck_assert_int_lt
#define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
#endif

typedef struct {
    const char *format;
    const char *args[8];
    const char *result;
} Fixture;

static const Fixture fixtures[] = {
    {
      /* Just a bog standard string */
      "%s", { "blah", NULL, },
      "blah"
    },
    {
      /* Empty to print */
      "%s", { "", NULL, },
      ""
    },
    {
      /* Nothing to print */
      "", { "blah", NULL, },
      ""
    },
    {
      /* Width right aligned */
      "%8s", { "blah", NULL, },
      "    blah"
    },
    {
      /* Width left aligned */
      "whoop %-8s doo", { "dee", NULL, },
      "whoop dee      doo"
    },
    {
      /* Width space aligned (ignored) */
      "whoop % 8s doo", { "dee", NULL, },
      "whoop      dee doo"
    },
    {
      /* Width left space aligned (ignored) */
      "whoop % -8s doo", { "dee", NULL, },
      "whoop dee      doo"
    },
    {
      /* Precision 1 digit */
      "whoop %.3s doo", { "deedle-dee", NULL, },
      "whoop dee doo"
    },
    {
      /* Precision, N digits */
      "whoop %.10s doo", { "deedle-dee-deedle-do-deedle-dum", NULL, },
      "whoop deedle-dee doo"
    },
    {
      /* Precision, zero digits */
      "whoop %.s doo", { "deedle", NULL, },
      "whoop  doo"
    },
    {
      /* Multiple simple arguments */
      "space %s %s", { "man", "dances", NULL, },
      "space man dances"
    },
    {
      /* Literal percent */
      "100%% of space folk dance", { NULL, },
      "100% of space folk dance"
    },
    {
      /* Multiple simple arguments */
      "space %2$s %1$s", { "dances", "man", NULL, },
      "space man dances"
    },
    {
      /* Skipping an argument (not supported by standard printf) */
      "space %2$s dances", { "dances", "man", NULL, },
      "space man dances"
    },

    /* Failures start here */

    {
      /* Unsupported conversion */
      "%x", { "blah", NULL, },
      NULL
    },
    {
      /* Bad positional argument */
      "space %55$s dances", { "dances", "man", NULL, },
      NULL
    },
    {
      /* Zero positional argument */
      "space %0$s dances", { "dances", "man", NULL, },
      NULL
    },
    {
      /* Too many args used */
      "%s %s dances", { "space", NULL, },
      NULL
    },
    {
      /* Too many digits used */
      "%1234567890s dances", { "space", NULL, },
      NULL
    },
};


static void
callback(void *data, const char *piece, size_t len)
{
    char **str = data;
    *str = talloc_strndup_append(*str, piece, len);
}

START_TEST(test_safe_format_string_cb)
{
    const Fixture *fixture;
    char *out;
    int num_args;
    int ret;
    void *mem_ctx;

    fixture = &fixtures[_i];
    mem_ctx = talloc_init("safe-printf");

    for (num_args = 0; fixture->args[num_args] != NULL; )
        num_args++;

    out = talloc_strdup(mem_ctx, "");
    ret = safe_format_string_cb(callback, &out, fixture->format,
                                (const char * const*)fixture->args, num_args);
    if (fixture->result) {
        ck_assert_int_ge(ret, 0);
        ck_assert_str_eq(out, fixture->result);
        ck_assert_int_eq(ret, strlen(out));
    } else {
        ck_assert_int_lt(ret, 0);
    }

    talloc_free(mem_ctx);
}
END_TEST

START_TEST(test_safe_format_string)
{
    char buffer[8];
    int ret;

    ret = safe_format_string(buffer, 8, "%s", "space", "man", NULL);
    ck_assert_int_eq(ret, 5);
    ck_assert_str_eq(buffer, "space");

    ret = safe_format_string(buffer, 8, "", "space", "man", NULL);
    ck_assert_int_eq(ret, 0);
    ck_assert_str_eq(buffer, "");

    ret = safe_format_string(buffer, 8, "the %s %s dances away", "space", "man", NULL);
    ck_assert_int_eq(ret, 25);
    ck_assert_str_eq(buffer, "the spa");

    ret = safe_format_string(NULL, 0, "the %s %s dances away", "space", "man", NULL);
    ck_assert_int_eq(ret, 25);

    ret = safe_format_string(buffer, 8, "%5$s", NULL);
    ck_assert_int_lt(ret, 0);
}
END_TEST

static Suite *
create_safe_format_suite(void)
{
    Suite *s = suite_create("safe-format");
    TCase *tc_format = tcase_create("safe-format-string");

    /* One for each fixture */
    tcase_add_loop_test(tc_format, test_safe_format_string_cb, 0,
                        (sizeof (fixtures) / sizeof (fixtures[0])));

    tcase_add_test(tc_format, test_safe_format_string);

    suite_add_tcase(s, tc_format);

    return s;
}

int
main(int argc, const char *argv[])
{
    int opt;
    poptContext pc;
    int failure_count;
    Suite *suite;
    SRunner *sr;

    struct poptOption long_options[] = {
        POPT_AUTOHELP
        POPT_TABLEEND
    };

    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    while((opt = poptGetNextOpt(pc)) != -1) {
        switch(opt) {
        default:
            fprintf(stderr, "\nInvalid option %s: %s\n\n",
                    poptBadOption(pc, 0), poptStrerror(opt));
            poptPrintUsage(pc, stderr, 0);
            return 1;
        }
    }
    poptFreeContext(pc);

    suite = create_safe_format_suite();
    sr = srunner_create(suite);
    /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    srunner_run_all(sr, CK_ENV);
    failure_count = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}