]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_msyslog.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_msyslog.cpp
1 #include "g_libntptest.h"
2
3 extern "C" {
4 #include <stdio.h>
5 #include <string.h>
6 #include <errno.h>
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 class msyslogTest : public libntptest {
14 };
15
16 // msnprintf()
17 TEST_F(msyslogTest, msnprintf)
18 {
19 #define FMT_PREFIX "msyslog.cpp ENOENT: "
20         char    exp_buf[512];
21         char    act_buf[512];
22         int     exp_cnt;
23         int     act_cnt;
24
25         exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s",
26                            strerror(ENOENT));
27         errno = ENOENT;
28         act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m");
29         EXPECT_EQ(exp_cnt, act_cnt);
30         EXPECT_STREQ(exp_buf, act_buf);
31 }
32
33 TEST_F(msyslogTest, msnprintfLiteralPercentm)
34 {
35         char    exp_buf[32];
36         char    act_buf[32];
37         int     exp_cnt;
38         int     act_cnt;
39
40         exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m");
41         errno = ENOENT;
42         act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m");
43         EXPECT_EQ(exp_cnt, act_cnt);
44         EXPECT_STREQ(exp_buf, act_buf);
45 }
46
47 TEST_F(msyslogTest, msnprintfBackslashLiteralPercentm)
48 {
49         char    exp_buf[32];
50         char    act_buf[32];
51         int     exp_cnt;
52         int     act_cnt;
53
54         exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m");
55         errno = ENOENT;
56         act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m");
57         EXPECT_EQ(exp_cnt, act_cnt);
58         EXPECT_STREQ(exp_buf, act_buf);
59 }
60
61 TEST_F(msyslogTest, msnprintfBackslashPercent)
62 {
63         char    exp_buf[32];
64         char    act_buf[32];
65         int     exp_cnt;
66         int     act_cnt;
67
68         exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s",
69                            strerror(ENOENT));
70         errno = ENOENT;
71         act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m");
72         EXPECT_EQ(exp_cnt, act_cnt);
73         EXPECT_STREQ(exp_buf, act_buf);
74 }
75
76 TEST_F(msyslogTest, msnprintfHangingPercent)
77 {
78         static char fmt[] = "percent then nul term then non-nul %\0oops!";
79         char exp_buf[64];
80         char act_buf[64];
81         int     exp_cnt;
82         int     act_cnt;
83
84         ZERO(exp_buf);
85         ZERO(act_buf);
86         exp_cnt = snprintf(exp_buf, sizeof(exp_buf), fmt);
87         act_cnt = msnprintf(act_buf, sizeof(act_buf), fmt);
88         EXPECT_EQ(exp_cnt, act_cnt);
89         EXPECT_STREQ(exp_buf, act_buf);
90         EXPECT_STREQ("", act_buf + 1 + strlen(act_buf));
91 }
92
93 #ifndef VSNPRINTF_PERCENT_M
94 TEST_F(msyslogTest, format_errmsgHangingPercent)
95 {
96         static char fmt[] = "percent then nul term then non-nul %\0oops!";
97         char act_buf[64];
98
99         ZERO(act_buf);
100         format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT);
101         EXPECT_STREQ(fmt, act_buf);
102         EXPECT_STREQ("", act_buf + 1 + strlen(act_buf));
103 }
104 #endif
105
106 TEST_F(msyslogTest, msnprintfNullTarget)
107 {
108         int     exp_cnt;
109         int     act_cnt;
110
111         exp_cnt = snprintf(NULL, 0, "%d", 123);
112         errno = ENOENT;
113         act_cnt = msnprintf(NULL, 0, "%d", 123);
114         EXPECT_EQ(exp_cnt, act_cnt);
115 }
116
117 TEST_F(msyslogTest, msnprintfTruncate)
118 {
119         char    undist[] = "undisturbed";
120         char    exp_buf[512];
121         char    act_buf[512];
122         int     exp_cnt;
123         int     act_cnt;
124
125         memcpy(exp_buf + 3, undist, sizeof(undist));
126         memcpy(act_buf + 3, undist, sizeof(undist));
127         exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT));
128         errno = ENOENT;
129         act_cnt = msnprintf(act_buf, 3, "%m");
130         EXPECT_EQ('\0', exp_buf[2]);
131         EXPECT_EQ('\0', act_buf[2]);
132         EXPECT_TRUE(act_cnt > 0);
133         EXPECT_EQ(exp_cnt, act_cnt);
134         EXPECT_STREQ(exp_buf, act_buf);
135         EXPECT_STREQ(exp_buf + 3, undist);
136         EXPECT_STREQ(act_buf + 3, undist);
137 }