upstream: unit test for misc.c:strdelim() that mostly servces to
highlight its inconsistencies OpenBSD-Regress-ID: 8d2bf970fcc01ccc6e36a5065f89b9c7fa934195
This commit is contained in:
parent
7a3a1dd2c7
commit
39f6cd2078
|
@ -648,7 +648,8 @@ UNITTESTS_TEST_MISC_OBJS=\
|
||||||
regress/unittests/misc/test_parse.o \
|
regress/unittests/misc/test_parse.o \
|
||||||
regress/unittests/misc/test_expand.o \
|
regress/unittests/misc/test_expand.o \
|
||||||
regress/unittests/misc/test_convtime.o \
|
regress/unittests/misc/test_convtime.o \
|
||||||
regress/unittests/misc/test_argv.o
|
regress/unittests/misc/test_argv.o \
|
||||||
|
regress/unittests/misc/test_strdelim.o
|
||||||
|
|
||||||
regress/unittests/misc/test_misc$(EXEEXT): \
|
regress/unittests/misc/test_misc$(EXEEXT): \
|
||||||
${UNITTESTS_TEST_MISC_OBJS} \
|
${UNITTESTS_TEST_MISC_OBJS} \
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# $OpenBSD: Makefile,v 1.6 2021/03/19 04:23:50 djm Exp $
|
# $OpenBSD: Makefile,v 1.7 2021/05/21 03:48:07 djm Exp $
|
||||||
|
|
||||||
PROG=test_misc
|
PROG=test_misc
|
||||||
SRCS=tests.c
|
SRCS=tests.c
|
||||||
|
@ -6,6 +6,7 @@ SRCS+= test_convtime.c
|
||||||
SRCS+= test_expand.c
|
SRCS+= test_expand.c
|
||||||
SRCS+= test_parse.c
|
SRCS+= test_parse.c
|
||||||
SRCS+= test_argv.c
|
SRCS+= test_argv.c
|
||||||
|
SRCS+= test_strdelim.c
|
||||||
|
|
||||||
# From usr.bin/ssh/Makefile.inc
|
# From usr.bin/ssh/Makefile.inc
|
||||||
SRCS+= sshbuf.c
|
SRCS+= sshbuf.c
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
/* $OpenBSD: test_strdelim.c,v 1.1 2021/05/21 03:48:07 djm Exp $ */
|
||||||
|
/*
|
||||||
|
* Regress test for misc strdelim() and co
|
||||||
|
*
|
||||||
|
* Placed in the public domain.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "includes.h"
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "../test_helper/test_helper.h"
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
|
#include "misc.h"
|
||||||
|
#include "xmalloc.h"
|
||||||
|
|
||||||
|
void test_strdelim(void);
|
||||||
|
|
||||||
|
void
|
||||||
|
test_strdelim(void)
|
||||||
|
{
|
||||||
|
char *orig, *str, *cp;
|
||||||
|
|
||||||
|
#define START_STRING(x) orig = str = xstrdup(x)
|
||||||
|
#define DONE_STRING() free(orig)
|
||||||
|
|
||||||
|
TEST_START("empty");
|
||||||
|
START_STRING("");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("whitespace");
|
||||||
|
START_STRING(" ");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("trivial");
|
||||||
|
START_STRING("blob");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("trivial whitespace");
|
||||||
|
START_STRING("blob ");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("multi");
|
||||||
|
START_STRING("blob1 blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("multi whitespace");
|
||||||
|
START_STRING("blob1 blob2 ");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("multi equals");
|
||||||
|
START_STRING("blob1=blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("multi too many equals");
|
||||||
|
START_STRING("blob1==blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1"); /* XXX better returning NULL early */
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "");
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("multi equals strdelimw");
|
||||||
|
START_STRING("blob1=blob2");
|
||||||
|
cp = strdelimw(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1=blob2");
|
||||||
|
cp = strdelimw(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("quoted");
|
||||||
|
START_STRING("\"blob\"");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("quoted multi");
|
||||||
|
START_STRING("\"blob1\" blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("quoted multi reverse");
|
||||||
|
START_STRING("blob1 \"blob2\"");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("quoted multi middle");
|
||||||
|
START_STRING("blob1 \"blob2\" blob3");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob1");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob2");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob3");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("badquote");
|
||||||
|
START_STRING("\"blob");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_PTR_EQ(cp, NULL);
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
TEST_START("oops quote");
|
||||||
|
START_STRING("\"blob\\\"");
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "blob\\"); /* XXX wrong */
|
||||||
|
cp = strdelim(&str);
|
||||||
|
ASSERT_STRING_EQ(cp, "");
|
||||||
|
DONE_STRING();
|
||||||
|
TEST_DONE();
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: tests.c,v 1.6 2021/03/19 04:23:50 djm Exp $ */
|
/* $OpenBSD: tests.c,v 1.7 2021/05/21 03:48:07 djm Exp $ */
|
||||||
/*
|
/*
|
||||||
* Regress test for misc helper functions.
|
* Regress test for misc helper functions.
|
||||||
*
|
*
|
||||||
|
@ -23,6 +23,7 @@ void test_parse(void);
|
||||||
void test_convtime(void);
|
void test_convtime(void);
|
||||||
void test_expand(void);
|
void test_expand(void);
|
||||||
void test_argv(void);
|
void test_argv(void);
|
||||||
|
void test_strdelim(void);
|
||||||
|
|
||||||
void
|
void
|
||||||
tests(void)
|
tests(void)
|
||||||
|
@ -31,4 +32,5 @@ tests(void)
|
||||||
test_convtime();
|
test_convtime();
|
||||||
test_expand();
|
test_expand();
|
||||||
test_argv();
|
test_argv();
|
||||||
|
test_strdelim();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue