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
|
#include "config.h"
#include "ntp_stdlib.h"
#include "unity.h"
#ifndef VSNPRINTF_PERCENT_M
// format_errmsg() is normally private to msyslog.c
void format_errmsg (char *, size_t, const char *, int);
#endif
void test_msnprintf(void) {
#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");
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
}
void
test_msnprintfLiteralPercentm(void)
{
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");
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
}
void
test_msnprintfBackslashLiteralPercentm(void) {
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");
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
}
void
test_msnprintfBackslashPercent(void) {
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");
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
}
void
test_msnprintfHangingPercent(void) {
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), "%s", fmt);
act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt);
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
}
void
test_format_errmsgHangingPercent(void) {
#ifndef VSNPRINTF_PERCENT_M
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);
TEST_ASSERT_EQUAL_STRING(fmt, act_buf);
TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf));
#else
TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined")
#endif
}
void
test_msnprintfNullTarget(void) {
int exp_cnt;
int act_cnt;
exp_cnt = snprintf(NULL, 0, "%d", 123);
errno = ENOENT;
act_cnt = msnprintf(NULL, 0, "%d", 123);
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
}
void
test_msnprintfTruncate(void) {
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");
TEST_ASSERT_EQUAL('\0', exp_buf[2]);
TEST_ASSERT_EQUAL('\0', act_buf[2]);
TEST_ASSERT_TRUE(act_cnt > 0);
TEST_ASSERT_EQUAL(exp_cnt, act_cnt);
TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf);
TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist);
TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist);
}
|