/* Copyright (c) 2001-2018 Dovecot authors, see the included COPYING file */
/* Unit tests for printf-format-fix helper */
#include "test-lib.h"
#include "printf-format-fix.h"
#include <string.h>
struct format_fix_rewrites {
const char *input;
const char *output;
};
static void test_unchanged()
{
static const char *tests[] = {
"Hello world",
"Embedded %%, %u, %f, %s, etc. are OK",
"Allow %#0- +s flags",
"duplicate flags in different args %0-123s %0-123s",
"Minimum length %9999s",
"Minimum length parameter %*s",
"Precision %.9999s",
"Precision %1.9999s",
"Precision parameter %1.*s %.*s",
"Floating precisions such as %.0f %0.4f %-4.0f",
"Length modifiers %hd %hhd %ld %lld %Lg %jd %zd %td",
"Specifiers %s %u %d %c %i %x %X %p %o %e %E %f %F %g %G %a %A",
"%%doesn't cause confusion in %%m and %%n",
};
unsigned int i;
test_begin("printf_format_fix(safe)");
for (i = 0; i < N_ELEMENTS(tests); ++i) {
T_BEGIN {
== tests[i], i);
== tests[i], i);
} T_END;
}
test_end();
}
static void test_ok_changes()
{
static const char *tests[] = {
"OK to have a trailing %m",
"%m can appear at the start too",
"Even %m in the middle with a confusing %%m elsewhere is OK",
};
unsigned int i;
const char *needle;
unsigned int needlen;
test_begin("printf_format_fix(rewrites)");
for (i = 0; i < N_ELEMENTS(tests); ++i) {
char const *chgd;
char const *insert;
unsigned int offs;
T_BEGIN {
} T_END;
}
test_end();
}
void test_printf_format_fix()
{
}
/* Want to test the panics too? go for it! */
{
static const struct {
const char *format;
const char *expected_fatal;
} fatals[] = {
{ "no no no %n's", "%n modifier used" },
{ "no no no %-1234567890123n's with extra stuff", "Too large minimum field width" },
{ "%m allowed once, but not twice: %m", "%m used twice" },
{ "%m must not obscure a later %n", "%n modifier used" },
{ "definitely can't have a tailing %", "Missing % specifier" },
{ "Evil %**%n", "Unsupported 0x2a specifier" },
{ "Evil %*#%99999$s", "Unsupported 0x23 specifier" },
{ "No weird %% with %0%", "Unsupported 0x25 specifier" },
{ "No duplicate modifiers %00s", "Duplicate % flag '0'" },
{ "Minimum length can't be too long %10000s", "Too large minimum field width" },
{ "Minimum length doesn't support %*1$s", "Unsupported 0x31 specifier" },
{ "Precision can't be too long %.10000s", "Too large precision" },
{ "Precision can't be too long %1.10000s", "Too large precision" },
{ "Precision doesn't support %1.-1s", "Unsupported 0x2d specifier" },
};
test_end();
return FATAL_TEST_FINISHED;
}
if(stage == 0)
test_begin("fatal_printf_format_fix");
/* let's crash! */
return FATAL_TEST_FAILURE;
}