1152251Srwatson/*	$NetBSD: msyslog.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2152251Srwatson
3152251Srwatson#include "config.h"
4152251Srwatson
5152251Srwatson#include "ntp_stdlib.h"
6152251Srwatson
7152251Srwatson#include "unity.h"
8152251Srwatson
9152251Srwatson#ifndef VSNPRINTF_PERCENT_M
10152251Srwatson// format_errmsg() is normally private to msyslog.c
11152251Srwatsonvoid format_errmsg(char *, size_t, const char *, int);
12152251Srwatson#endif
13152251Srwatson
14152251Srwatson
15152251Srwatsonvoid setUp(void);
16152251Srwatsonvoid test_msnprintf(void);
17152251Srwatsonvoid test_msnprintfLiteralPercentm(void);
18152251Srwatsonvoid test_msnprintfBackslashLiteralPercentm(void);
19152251Srwatsonvoid test_msnprintfBackslashPercent(void);
20152251Srwatsonvoid test_msnprintfHangingPercent(void);
21152251Srwatsonvoid test_format_errmsgHangingPercent(void);
22152251Srwatsonvoid test_msnprintfNullTarget(void);
23152251Srwatsonvoid test_msnprintfTruncate(void);
24152251Srwatson
25152251Srwatson
26152251Srwatsonvoid
27152251SrwatsonsetUp(void)
28152251Srwatson{
29152251Srwatson	init_lib();
30152251Srwatson
31152251Srwatson	return;
32152251Srwatson}
33152251Srwatson
34152251Srwatson
35152251Srwatsonvoid
36152251Srwatsontest_msnprintf(void) {
37152251Srwatson#define FMT_PREFIX "msyslog.cpp ENOENT: "
38152251Srwatson	char	exp_buf[512];
39152251Srwatson	char	act_buf[512];
40152251Srwatson	int	exp_cnt;
41152251Srwatson	int	act_cnt;
42152251Srwatson
43152251Srwatson	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
44152251Srwatson			   strerror(ENOENT));
45152251Srwatson	errno = ENOENT;
46152251Srwatson	act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
47152251Srwatson
48152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
49152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
50152251Srwatson}
51152251Srwatson
52152251Srwatsonvoid
53152251Srwatsontest_msnprintfLiteralPercentm(void)
54152251Srwatson{
55152251Srwatson	char	exp_buf[32];
56152251Srwatson	char	act_buf[32];
57152251Srwatson	int	exp_cnt;
58152251Srwatson	int	act_cnt;
59152251Srwatson
60152251Srwatson	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
61152251Srwatson	errno = ENOENT;
62152251Srwatson	act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
63152251Srwatson
64152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
65152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
66152251Srwatson}
67152251Srwatson
68152251Srwatsonvoid
69152251Srwatsontest_msnprintfBackslashLiteralPercentm(void) {
70152251Srwatson	char	exp_buf[32];
71152251Srwatson	char	act_buf[32];
72152251Srwatson	int	exp_cnt;
73152251Srwatson	int	act_cnt;
74152251Srwatson
75152251Srwatson	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
76152251Srwatson	errno = ENOENT;
77152251Srwatson	act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
78152251Srwatson
79152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
80152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
81152251Srwatson}
82152251Srwatson
83152251Srwatsonvoid
84152251Srwatsontest_msnprintfBackslashPercent(void) {
85152251Srwatson	char	exp_buf[32];
86152251Srwatson	char	act_buf[32];
87152251Srwatson	int	exp_cnt;
88152251Srwatson	int	act_cnt;
89152251Srwatson
90152251Srwatson	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
91152251Srwatson			   strerror(ENOENT));
92152251Srwatson	errno = ENOENT;
93152251Srwatson	act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
94152251Srwatson
95152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
96152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
97152251Srwatson}
98152251Srwatson
99152251Srwatsonvoid
100152251Srwatsontest_msnprintfHangingPercent(void) {
101152251Srwatson	static char fmt[] = "percent then nul term then non-nul %\0oops!";
102152251Srwatson	char exp_buf[64];
103152251Srwatson	char act_buf[64];
104152251Srwatson	int	exp_cnt;
105152251Srwatson	int	act_cnt;
106152251Srwatson
107152251Srwatson	ZERO(exp_buf);
108152251Srwatson	ZERO(act_buf);
109152251Srwatson	exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt);
110152251Srwatson	act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
111152251Srwatson
112152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
113152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
114152251Srwatson	TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
115152251Srwatson}
116152251Srwatson
117152251Srwatsonvoid
118152251Srwatsontest_format_errmsgHangingPercent(void) {
119152251Srwatson#ifndef VSNPRINTF_PERCENT_M
120152251Srwatson	static char fmt[] = "percent then nul term then non-nul %\0oops!";
121152251Srwatson	char act_buf[64];
122152251Srwatson
123152251Srwatson	ZERO(act_buf);
124152251Srwatson	format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
125152251Srwatson
126152251Srwatson	TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
127152251Srwatson	TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
128152251Srwatson#else
129152251Srwatson	TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
130152251Srwatson#endif
131152251Srwatson}
132152251Srwatson
133152251Srwatsonvoid
134152251Srwatsontest_msnprintfNullTarget(void) {
135152251Srwatson	int	exp_cnt;
136152251Srwatson	int	act_cnt;
137152251Srwatson
138152251Srwatson	exp_cnt = snprintf(NULL, 0, "%d", 123);
139152251Srwatson	errno = ENOENT;
140152251Srwatson	act_cnt = msnprintf(NULL, 0, "%d", 123);
141152251Srwatson
142152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
143152251Srwatson}
144152251Srwatson
145152251Srwatsonvoid
146152251Srwatsontest_msnprintfTruncate(void) {
147152251Srwatson	char	undist[] = "undisturbed";
148152251Srwatson	char	exp_buf[512];
149152251Srwatson	char	act_buf[512];
150152251Srwatson	int	exp_cnt;
151152251Srwatson	int	act_cnt;
152152251Srwatson
153152251Srwatson	memcpy(exp_buf + 3, undist, sizeof(undist));
154152251Srwatson	memcpy(act_buf + 3, undist, sizeof(undist));
155152251Srwatson	exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
156152251Srwatson	errno = ENOENT;
157152251Srwatson	act_cnt = msnprintf(act_buf, 3, "%m");
158152251Srwatson
159152251Srwatson	TEST_ASSERT_EQUAL('\0', exp_buf[2]);
160152251Srwatson	TEST_ASSERT_EQUAL('\0', act_buf[2]);
161152251Srwatson	TEST_ASSERT_TRUE(act_cnt > 0);
162152251Srwatson	TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
163152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
164152251Srwatson	TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
165152251Srwatson	TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);
166152251Srwatson}
167152251Srwatson