2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 22:35:10 +00:00

Fix final test with a format > 2048 bytes.

Keep track of tests run in the syslog() stub so we can
detect if the stub is not being called.
This commit is contained in:
Todd C. Miller
2017-02-13 20:30:45 -07:00
parent 6263cc55a5
commit f59327bc5c

View File

@@ -44,6 +44,7 @@ __dso_public int main(int argc, char *argv[]);
*/ */
static char *expected_result; static char *expected_result;
static int errors; static int errors;
static int ntests;
/* /*
* Dummy version of syslog to verify the message * Dummy version of syslog to verify the message
@@ -62,6 +63,8 @@ syslog(int priority, const char *fmt, ...)
if (strcmp(msg, expected_result) != 0) { if (strcmp(msg, expected_result) != 0) {
sudo_warnx_nodebug("Expected \"%s\", got \"%s\"", expected_result, msg); sudo_warnx_nodebug("Expected \"%s\", got \"%s\"", expected_result, msg);
errors++; errors++;
} else {
ntests++;
} }
va_end(ap); va_end(ap);
} }
@@ -79,7 +82,6 @@ test_vsyslog(int priority, const char *fmt, ...)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ntests = 0;
char buf1[1024 * 16], buf2[1024 * 16]; char buf1[1024 * 16], buf2[1024 * 16];
initprogname(argc > 0 ? argv[0] : "vsyslog_test"); initprogname(argc > 0 ? argv[0] : "vsyslog_test");
@@ -89,7 +91,6 @@ main(int argc, char *argv[])
"%s: %s : TTY=%s ; PWD=%s ; USER=%s ; TSID=%s ; COMMAND=%s", "%s: %s : TTY=%s ; PWD=%s ; USER=%s ; TSID=%s ; COMMAND=%s",
"sudo", "millert", "ttypa", "/etc/mail", "root", "000AB0", "sudo", "millert", "ttypa", "/etc/mail", "root", "000AB0",
"/usr/sbin/newaliases"); "/usr/sbin/newaliases");
ntests++;
/* Test small buffer w/ errno. */ /* Test small buffer w/ errno. */
snprintf(buf1, sizeof(buf1), snprintf(buf1, sizeof(buf1),
@@ -97,14 +98,12 @@ main(int argc, char *argv[])
expected_result = buf1; expected_result = buf1;
errno = ENOENT; errno = ENOENT;
test_vsyslog(0, "unable to open %s: %m", "/var/log/sudo-io/seq"); test_vsyslog(0, "unable to open %s: %m", "/var/log/sudo-io/seq");
ntests++;
/* Test large buffer > 8192 bytes. */ /* Test large buffer > 8192 bytes. */
memset(buf1, 'a', 8192); memset(buf1, 'a', 8192);
buf1[8192] = '\0'; buf1[8192] = '\0';
expected_result = buf1; expected_result = buf1;
test_vsyslog(0, "%s", buf1); test_vsyslog(0, "%s", buf1);
ntests++;
/* Test large buffer w/ errno > 8192 bytes. */ /* Test large buffer w/ errno > 8192 bytes. */
memset(buf1, 'b', 8184); memset(buf1, 'b', 8184);
@@ -113,21 +112,20 @@ main(int argc, char *argv[])
expected_result = buf2; expected_result = buf2;
errno = EINVAL; errno = EINVAL;
test_vsyslog(0, "%s: %m", buf1); test_vsyslog(0, "%s: %m", buf1);
ntests++;
/* Test large buffer w/ errno > 8192 bytes. */ /* Test large format string > 8192 bytes, expect truncation to 2048. */
memset(buf1, 'b', 8184); memset(buf1, 'b', 8184);
buf1[8184] = '\0'; buf1[8184] = '\0';
snprintf(buf2, sizeof(buf2), "%s: %s", buf1, strerror(EINVAL)); snprintf(buf2, sizeof(buf2), "%.*s", 2047, buf1);
expected_result = buf2; expected_result = buf2;
errno = EINVAL;
strlcat(buf1, ": %m", sizeof(buf1));
test_vsyslog(0, buf1); test_vsyslog(0, buf1);
ntests++;
if (ntests != 0) { if (ntests != 0) {
printf("%s: %d tests run, %d errors, %d%% success rate\n", printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
} else {
printf("%s: error, no tests run!\n", getprogname());
errors = 1;
} }
exit(errors); exit(errors);
} }