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