1290001Sglebius#include "config.h"
2290001Sglebius
3290001Sglebius#include "ntp_stdlib.h"
4290001Sglebius
5290001Sglebius#include "unity.h"
6290001Sglebius
7290001Sglebius#ifndef VSNPRINTF_PERCENT_M
8290001Sglebius// format_errmsg() is normally private to msyslog.c
9290001Sglebiusvoid format_errmsg(char *, size_t, const char *, int);
10290001Sglebius#endif
11290001Sglebius
12290001Sglebius
13293896Sglebiusvoid setUp(void);
14290001Sglebiusvoid test_msnprintf(void);
15290001Sglebiusvoid test_msnprintfLiteralPercentm(void);
16290001Sglebiusvoid test_msnprintfBackslashLiteralPercentm(void);
17290001Sglebiusvoid test_msnprintfBackslashPercent(void);
18290001Sglebiusvoid test_msnprintfHangingPercent(void);
19290001Sglebiusvoid test_format_errmsgHangingPercent(void);
20290001Sglebiusvoid test_msnprintfNullTarget(void);
21290001Sglebiusvoid test_msnprintfTruncate(void);
22290001Sglebius
23290001Sglebius
24290001Sglebiusvoid
25293896SglebiussetUp(void)
26293896Sglebius{
27293896Sglebius	init_lib();
28293896Sglebius
29293896Sglebius	return;
30293896Sglebius}
31293896Sglebius
32293896Sglebius
33293896Sglebiusvoid
34290001Sglebiustest_msnprintf(void) {
35290001Sglebius#define FMT_PREFIX "msyslog.cpp ENOENT: "
36290001Sglebius	char	exp_buf[512];
37290001Sglebius	char	act_buf[512];
38290001Sglebius	int	exp_cnt;
39290001Sglebius	int	act_cnt;
40290001Sglebius
41290001Sglebius	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
42290001Sglebius			   strerror(ENOENT));
43290001Sglebius	errno = ENOENT;
44290001Sglebius	act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
45290001Sglebius
46290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
47290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
48290001Sglebius}
49290001Sglebius
50290001Sglebiusvoid
51290001Sglebiustest_msnprintfLiteralPercentm(void)
52290001Sglebius{
53290001Sglebius	char	exp_buf[32];
54290001Sglebius	char	act_buf[32];
55290001Sglebius	int	exp_cnt;
56290001Sglebius	int	act_cnt;
57290001Sglebius
58290001Sglebius	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
59290001Sglebius	errno = ENOENT;
60290001Sglebius	act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
61290001Sglebius
62290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
63290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
64290001Sglebius}
65290001Sglebius
66290001Sglebiusvoid
67290001Sglebiustest_msnprintfBackslashLiteralPercentm(void) {
68290001Sglebius	char	exp_buf[32];
69290001Sglebius	char	act_buf[32];
70290001Sglebius	int	exp_cnt;
71290001Sglebius	int	act_cnt;
72290001Sglebius
73290001Sglebius	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
74290001Sglebius	errno = ENOENT;
75290001Sglebius	act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
76290001Sglebius
77290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
78290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
79290001Sglebius}
80290001Sglebius
81290001Sglebiusvoid
82290001Sglebiustest_msnprintfBackslashPercent(void) {
83290001Sglebius	char	exp_buf[32];
84290001Sglebius	char	act_buf[32];
85290001Sglebius	int	exp_cnt;
86290001Sglebius	int	act_cnt;
87290001Sglebius
88290001Sglebius	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
89290001Sglebius			   strerror(ENOENT));
90290001Sglebius	errno = ENOENT;
91290001Sglebius	act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
92290001Sglebius
93290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
94290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
95290001Sglebius}
96290001Sglebius
97290001Sglebiusvoid
98290001Sglebiustest_msnprintfHangingPercent(void) {
99290001Sglebius	static char fmt[] = "percent then nul term then non-nul %\0oops!";
100290001Sglebius	char exp_buf[64];
101290001Sglebius	char act_buf[64];
102290001Sglebius	int	exp_cnt;
103290001Sglebius	int	act_cnt;
104290001Sglebius
105290001Sglebius	ZERO(exp_buf);
106290001Sglebius	ZERO(act_buf);
107290001Sglebius	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
108290001Sglebius	act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
109290001Sglebius
110290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
111290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
112290001Sglebius	TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
113290001Sglebius}
114290001Sglebius
115290001Sglebiusvoid
116290001Sglebiustest_format_errmsgHangingPercent(void) {
117290001Sglebius#ifndef VSNPRINTF_PERCENT_M
118290001Sglebius	static char fmt[] = "percent then nul term then non-nul %\0oops!";
119290001Sglebius	char act_buf[64];
120290001Sglebius
121290001Sglebius	ZERO(act_buf);
122290001Sglebius	format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
123290001Sglebius
124290001Sglebius	TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
125290001Sglebius	TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
126290001Sglebius#else
127290001Sglebius	TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
128290001Sglebius#endif
129290001Sglebius}
130290001Sglebius
131290001Sglebiusvoid
132290001Sglebiustest_msnprintfNullTarget(void) {
133290001Sglebius	int	exp_cnt;
134290001Sglebius	int	act_cnt;
135290001Sglebius
136290001Sglebius	exp_cnt = snprintf(NULL, 0, "%d", 123);
137290001Sglebius	errno = ENOENT;
138290001Sglebius	act_cnt = msnprintf(NULL, 0, "%d", 123);
139290001Sglebius
140290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
141290001Sglebius}
142290001Sglebius
143290001Sglebiusvoid
144290001Sglebiustest_msnprintfTruncate(void) {
145290001Sglebius	char	undist[] = "undisturbed";
146290001Sglebius	char	exp_buf[512];
147290001Sglebius	char	act_buf[512];
148290001Sglebius	int	exp_cnt;
149290001Sglebius	int	act_cnt;
150290001Sglebius
151290001Sglebius	memcpy(exp_buf + 3, undist, sizeof(undist));
152290001Sglebius	memcpy(act_buf + 3, undist, sizeof(undist));
153290001Sglebius	exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
154290001Sglebius	errno = ENOENT;
155290001Sglebius	act_cnt = msnprintf(act_buf, 3, "%m");
156290001Sglebius
157290001Sglebius	TEST_ASSERT_EQUAL('\0', exp_buf[2]);
158290001Sglebius	TEST_ASSERT_EQUAL('\0', act_buf[2]);
159290001Sglebius	TEST_ASSERT_TRUE(act_cnt > 0);
160290001Sglebius	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
161290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
162290001Sglebius	TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
163290001Sglebius	TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);
164290001Sglebius}
165