mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-12-23 13:01:08 +00:00
Test libc strlcat() and strlcpy().
On systems that have strlcat() and strlcpy() in libc, run the tests twice (once with our implementation and once with the system's) to verify that our tests are correct.
This commit is contained in:
parent
d80dc09f3c
commit
882312950d
2 changed files with 58 additions and 10 deletions
|
@ -35,11 +35,12 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#undef HAVE_STRLCAT
|
||||
#include <cryb/strlcat.h>
|
||||
|
||||
#include <cryb/test.h>
|
||||
|
||||
typedef size_t (*strlcat_f)(char *, const char *, size_t);
|
||||
|
||||
#define T_MAGIC_STR "squeamish ossifrage"
|
||||
#define T_MAGIC_LEN (sizeof(T_MAGIC_STR) - 1)
|
||||
#define T_BUFSIZE (T_MAGIC_LEN + 1 + T_MAGIC_LEN + 1)
|
||||
|
@ -126,16 +127,15 @@ static struct t_case t_cases[] = {
|
|||
* Test function
|
||||
*/
|
||||
static int
|
||||
t_strlcat(char **desc CRYB_UNUSED, void *arg)
|
||||
t_strlcat(strlcat_f func, const struct t_case *t)
|
||||
{
|
||||
struct t_case *t = arg;
|
||||
char buf[T_BUFSIZE + 1];
|
||||
size_t sz;
|
||||
int ret;
|
||||
|
||||
memcpy(buf, t->buf, sizeof t->buf);
|
||||
buf[T_BUFSIZE] = T_CANARY;
|
||||
sz = strlcat(buf, t->in, T_BUFSIZE);
|
||||
sz = func(buf, t->in, T_BUFSIZE);
|
||||
if (buf[T_BUFSIZE] != T_CANARY) {
|
||||
t_printv("buffer overflow\n");
|
||||
return (0);
|
||||
|
@ -146,6 +146,24 @@ t_strlcat(char **desc CRYB_UNUSED, void *arg)
|
|||
return (ret);
|
||||
}
|
||||
|
||||
static int
|
||||
t_cryb_strlcat(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
const struct t_case *t = arg;
|
||||
|
||||
return (t_strlcat(cryb_strlcat, t));
|
||||
}
|
||||
|
||||
#if HAVE_STRLCAT
|
||||
static int
|
||||
t_libc_strlcat(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
const struct t_case *t = arg;
|
||||
|
||||
return (t_strlcat(strlcat, t));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
* Boilerplate
|
||||
|
@ -160,7 +178,13 @@ t_prepare(int argc, char *argv[])
|
|||
(void)argv;
|
||||
n = sizeof t_cases / sizeof t_cases[0];
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_strlcat, &t_cases[i], "%s", t_cases[i].desc);
|
||||
t_add_test(t_cryb_strlcat, &t_cases[i],
|
||||
"%s (cryb)", t_cases[i].desc);
|
||||
#if HAVE_STRLCAT
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_libc_strlcat, &t_cases[i],
|
||||
"%s (libc)", t_cases[i].desc);
|
||||
#endif
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
|
@ -35,11 +35,12 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#undef HAVE_STRLCPY
|
||||
#include <cryb/strlcpy.h>
|
||||
|
||||
#include <cryb/test.h>
|
||||
|
||||
typedef size_t (*strlcpy_f)(char *, const char *, size_t);
|
||||
|
||||
#define T_MAGIC_STR "squeamish ossifrage"
|
||||
#define T_MAGIC_LEN (sizeof(T_MAGIC_STR) - 1)
|
||||
#define T_BUFSIZE (T_MAGIC_LEN + 1 + T_MAGIC_LEN + 1)
|
||||
|
@ -86,14 +87,13 @@ static struct t_case t_cases[] = {
|
|||
* Test function
|
||||
*/
|
||||
static int
|
||||
t_strlcpy(char **desc CRYB_UNUSED, void *arg)
|
||||
t_strlcpy(strlcpy_f func, const struct t_case *t)
|
||||
{
|
||||
struct t_case *t = arg;
|
||||
char buf[T_BUFSIZE + 1];
|
||||
size_t sz;
|
||||
|
||||
memset(buf, T_CANARY, sizeof buf);
|
||||
sz = strlcpy(buf, t->in, T_BUFSIZE);
|
||||
sz = func(buf, t->in, T_BUFSIZE);
|
||||
if (buf[T_BUFSIZE] != T_CANARY) {
|
||||
t_printv("buffer overflow\n");
|
||||
return (0);
|
||||
|
@ -101,6 +101,24 @@ t_strlcpy(char **desc CRYB_UNUSED, void *arg)
|
|||
return (t_compare_sz(t->sz, sz) & t_compare_str(t->out, buf));
|
||||
}
|
||||
|
||||
static int
|
||||
t_cryb_strlcpy(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
const struct t_case *t = arg;
|
||||
|
||||
return (t_strlcpy(cryb_strlcpy, t));
|
||||
}
|
||||
|
||||
#if HAVE_STRLCPY
|
||||
static int
|
||||
t_libc_strlcpy(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
const struct t_case *t = arg;
|
||||
|
||||
return (t_strlcpy(strlcpy, t));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
* Boilerplate
|
||||
|
@ -115,7 +133,13 @@ t_prepare(int argc, char *argv[])
|
|||
(void)argv;
|
||||
n = sizeof t_cases / sizeof t_cases[0];
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_strlcpy, &t_cases[i], "%s", t_cases[i].desc);
|
||||
t_add_test(t_cryb_strlcpy, &t_cases[i],
|
||||
"%s (cryb)", t_cases[i].desc);
|
||||
#if HAVE_STRLCPY
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_libc_strlcpy, &t_cases[i],
|
||||
"%s (libc)", t_cases[i].desc);
|
||||
#endif
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue