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