bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2001-2018 Dovecot authors, see the included COPYING file */
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody/* Unit tests for printf-format-fix helper */
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "Hello world",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "Embedded %%, %u, %f, %s, etc. are OK",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "Allow %#0- +s flags",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "duplicate flags in different args %0-123s %0-123s",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "Minimum length %9999s",
85174bd658f999cb3aa145885fd85674f8595fbbTimo Sirainen "Minimum length parameter %*s",
92cb1ef4f68f48c4b58d3949edb952158024af9dTimo Sirainen "Precision %.9999s",
92cb1ef4f68f48c4b58d3949edb952158024af9dTimo Sirainen "Precision %1.9999s",
85174bd658f999cb3aa145885fd85674f8595fbbTimo Sirainen "Precision parameter %1.*s %.*s",
9bf221d642eba214b81346abd9ba2f9b9f2eb937Phil Carmody "Floating precisions such as %.0f %0.4f %-4.0f",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "Length modifiers %hd %hhd %ld %lld %Lg %jd %zd %td",
c398eca6b0fc6583687bd6fe2ee2dbcca2ae9387Timo Sirainen "Specifiers %s %u %d %c %i %x %X %p %o %e %E %f %F %g %G %a %A",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody "%%doesn't cause confusion in %%m and %%n",
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody unsigned int i;
1d940afbc02516d8c3d016780e1223a779844a1ePhil Carmody test_assert_idx(printf_format_fix_get_len(tests[i], &len)
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 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)
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen static const struct {
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "no no no %-1234567890123n's with extra stuff", "Too large minimum field width" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "%m allowed once, but not twice: %m", "%m used twice" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "%m must not obscure a later %n", "%n modifier used" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "definitely can't have a tailing %", "Missing % specifier" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Evil %**%n", "Unsupported 0x2a specifier" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Evil %*#%99999$s", "Unsupported 0x23 specifier" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "No weird %% with %0%", "Unsupported 0x25 specifier" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "No duplicate modifiers %00s", "Duplicate % flag '0'" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Minimum length can't be too long %10000s", "Too large minimum field width" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Minimum length doesn't support %*1$s", "Unsupported 0x31 specifier" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Precision can't be too long %.10000s", "Too large precision" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Precision can't be too long %1.10000s", "Too large precision" },
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen { "Precision doesn't support %1.-1s", "Unsupported 0x2d specifier" },
8bec19723b39071a1794e76dec35d151473cae5fPhil Carmody /* let's crash! */
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen test_expect_fatal_string(fatals[stage].expected_fatal);