1*cdfa2a7eSchristos /* $NetBSD: msyslog.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */
2067f5680Schristos
3f17b710fSchristos #include "config.h"
4f17b710fSchristos
5f17b710fSchristos #include "ntp_stdlib.h"
6f17b710fSchristos
7f17b710fSchristos #include "unity.h"
8f17b710fSchristos
9f17b710fSchristos #ifndef VSNPRINTF_PERCENT_M
10f17b710fSchristos // format_errmsg() is normally private to msyslog.c
11f17b710fSchristos void format_errmsg(char *, size_t, const char *, int);
12f17b710fSchristos #endif
13f17b710fSchristos
14f17b710fSchristos
154c290c01Schristos void setUp(void);
16a6f3f22fSchristos void test_msnprintf(void);
17a6f3f22fSchristos void test_msnprintfLiteralPercentm(void);
18a6f3f22fSchristos void test_msnprintfBackslashLiteralPercentm(void);
19a6f3f22fSchristos void test_msnprintfBackslashPercent(void);
20a6f3f22fSchristos void test_msnprintfHangingPercent(void);
21a6f3f22fSchristos void test_format_errmsgHangingPercent(void);
22a6f3f22fSchristos void test_msnprintfNullTarget(void);
23a6f3f22fSchristos void test_msnprintfTruncate(void);
24f17b710fSchristos
25a6f3f22fSchristos
26a6f3f22fSchristos void
setUp(void)274c290c01Schristos setUp(void)
284c290c01Schristos {
294c290c01Schristos init_lib();
304c290c01Schristos
314c290c01Schristos return;
324c290c01Schristos }
334c290c01Schristos
344c290c01Schristos
354c290c01Schristos void
test_msnprintf(void)36a6f3f22fSchristos test_msnprintf(void) {
37f17b710fSchristos #define FMT_PREFIX "msyslog.cpp ENOENT: "
38f17b710fSchristos char exp_buf[512];
39f17b710fSchristos char act_buf[512];
40f17b710fSchristos int exp_cnt;
41f17b710fSchristos int act_cnt;
42f17b710fSchristos
43f17b710fSchristos exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
44f17b710fSchristos strerror(ENOENT));
45f17b710fSchristos errno = ENOENT;
46f17b710fSchristos act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
47a6f3f22fSchristos
48f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
49f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
50f17b710fSchristos }
51f17b710fSchristos
52f17b710fSchristos void
test_msnprintfLiteralPercentm(void)53f17b710fSchristos test_msnprintfLiteralPercentm(void)
54f17b710fSchristos {
55f17b710fSchristos char exp_buf[32];
56f17b710fSchristos char act_buf[32];
57f17b710fSchristos int exp_cnt;
58f17b710fSchristos int act_cnt;
59f17b710fSchristos
60f17b710fSchristos exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
61f17b710fSchristos errno = ENOENT;
62f17b710fSchristos act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
63a6f3f22fSchristos
64f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
65f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
66f17b710fSchristos }
67f17b710fSchristos
68f17b710fSchristos void
test_msnprintfBackslashLiteralPercentm(void)69f17b710fSchristos test_msnprintfBackslashLiteralPercentm(void) {
70f17b710fSchristos char exp_buf[32];
71f17b710fSchristos char act_buf[32];
72f17b710fSchristos int exp_cnt;
73f17b710fSchristos int act_cnt;
74f17b710fSchristos
75f17b710fSchristos exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
76f17b710fSchristos errno = ENOENT;
77f17b710fSchristos act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
78a6f3f22fSchristos
79f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
80f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
81f17b710fSchristos }
82f17b710fSchristos
83f17b710fSchristos void
test_msnprintfBackslashPercent(void)84f17b710fSchristos test_msnprintfBackslashPercent(void) {
85f17b710fSchristos char exp_buf[32];
86f17b710fSchristos char act_buf[32];
87f17b710fSchristos int exp_cnt;
88f17b710fSchristos int act_cnt;
89f17b710fSchristos
90f17b710fSchristos exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
91f17b710fSchristos strerror(ENOENT));
92f17b710fSchristos errno = ENOENT;
93f17b710fSchristos act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
94a6f3f22fSchristos
95f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
96f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
97f17b710fSchristos }
98f17b710fSchristos
99f17b710fSchristos void
test_msnprintfHangingPercent(void)100f17b710fSchristos test_msnprintfHangingPercent(void) {
101f17b710fSchristos static char fmt[] = "percent then nul term then non-nul %\0oops!";
102f17b710fSchristos char exp_buf[64];
103f17b710fSchristos char act_buf[64];
104f17b710fSchristos int exp_cnt;
105f17b710fSchristos int act_cnt;
106f17b710fSchristos
107f17b710fSchristos ZERO(exp_buf);
108f17b710fSchristos ZERO(act_buf);
109f17b710fSchristos exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
110f17b710fSchristos act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
111a6f3f22fSchristos
112f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
113f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
114f17b710fSchristos TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
115f17b710fSchristos }
116f17b710fSchristos
117f17b710fSchristos void
test_format_errmsgHangingPercent(void)118f17b710fSchristos test_format_errmsgHangingPercent(void) {
119f17b710fSchristos #ifndef VSNPRINTF_PERCENT_M
120f17b710fSchristos static char fmt[] = "percent then nul term then non-nul %\0oops!";
121f17b710fSchristos char act_buf[64];
122f17b710fSchristos
123f17b710fSchristos ZERO(act_buf);
124f17b710fSchristos format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
125a6f3f22fSchristos
126f17b710fSchristos TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
127f17b710fSchristos TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
128f17b710fSchristos #else
129f17b710fSchristos TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
130f17b710fSchristos #endif
131f17b710fSchristos }
132f17b710fSchristos
133f17b710fSchristos void
test_msnprintfNullTarget(void)134f17b710fSchristos test_msnprintfNullTarget(void) {
135f17b710fSchristos int exp_cnt;
136f17b710fSchristos int act_cnt;
137f17b710fSchristos
138f17b710fSchristos exp_cnt = snprintf(NULL, 0, "%d", 123);
139f17b710fSchristos errno = ENOENT;
140f17b710fSchristos act_cnt = msnprintf(NULL, 0, "%d", 123);
141a6f3f22fSchristos
142f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
143f17b710fSchristos }
144f17b710fSchristos
145f17b710fSchristos void
test_msnprintfTruncate(void)146f17b710fSchristos test_msnprintfTruncate(void) {
147f17b710fSchristos char undist[] = "undisturbed";
148f17b710fSchristos char exp_buf[512];
149f17b710fSchristos char act_buf[512];
150f17b710fSchristos int exp_cnt;
151f17b710fSchristos int act_cnt;
152f17b710fSchristos
153f17b710fSchristos memcpy(exp_buf + 3, undist, sizeof(undist));
154f17b710fSchristos memcpy(act_buf + 3, undist, sizeof(undist));
155f17b710fSchristos exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
156f17b710fSchristos errno = ENOENT;
157f17b710fSchristos act_cnt = msnprintf(act_buf, 3, "%m");
158a6f3f22fSchristos
159f17b710fSchristos TEST_ASSERT_EQUAL('\0', exp_buf[2]);
160f17b710fSchristos TEST_ASSERT_EQUAL('\0', act_buf[2]);
161f17b710fSchristos TEST_ASSERT_TRUE(act_cnt > 0);
162f17b710fSchristos TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
163f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
164f17b710fSchristos TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
165f17b710fSchristos TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);
166f17b710fSchristos }
167