test-printf-format-fix.c revision 957d34edbe3599fbb4e7c0bcf3785bd7fd4862c4
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2001-2016 Dovecot authors, see the included COPYING file */
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody/* Unit tests for printf-format-fix helper */
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody static const char *tests[] = {
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "Hello world",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "Embedded %%, %u, %f, etc. are OK",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "%%doesn't cause confusion in %%m and %%n",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int i;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int len;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(printf_format_fix_get_len(tests[i], &len)
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody static const char *tests[] = {
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "OK to have a trailing %m",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "%m can appear at the start too",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "Even %m in the middle with a confusing %%m elsewhere is OK",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int i;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int needlen;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int len;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody char const *chgd;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int offs;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(memcmp(chgd, tests[i], offs) == 0, i);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(memcmp(chgd+offs, needle, needlen) == 0, i);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(strcmp(chgd+offs+needlen, tests[i]+offs+2) == 0, i);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody chgd = printf_format_fix_get_len(tests[i], &len);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(memcmp(chgd, tests[i], offs) == 0, i);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(memcmp(chgd+offs, needle, needlen) == 0, i);
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(memcmp(chgd+offs+needlen, tests[i]+offs+2, len-needlen-offs) == 0, i);
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody/* Want to test the panics too? go for it! */
957d34edbe3599fbb4e7c0bcf3785bd7fd4862c4Timo Sirainenenum fatal_test_state fatal_printf_format_fix(unsigned int stage)
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody static const char *fatals[] = {
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody "no no no %n's",
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody "%m allowed once, but not twice: %m",
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody "%m must not obscure a later %n",
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody "definitely can't have a tailing %",
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody /* let's crash! */