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