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