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