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