2021-12-14 21:25:27 +00:00
|
|
|
/* $OpenBSD: test_strdelim.c,v 1.3 2021/12/14 21:25:27 deraadt Exp $ */
|
2021-05-21 03:48:07 +00:00
|
|
|
/*
|
|
|
|
* Regress test for misc strdelim() and co
|
|
|
|
*
|
|
|
|
* Placed in the public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
2021-08-16 21:59:27 +00:00
|
|
|
#ifdef HAVE_STDINT_H
|
2021-05-21 03:48:07 +00:00
|
|
|
#include <stdint.h>
|
2021-08-16 21:59:27 +00:00
|
|
|
#endif
|
2021-05-21 03:48:07 +00:00
|
|
|
#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);
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(cp, ""); /* XXX arguable */
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_PTR_EQ(cp, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("whitespace");
|
|
|
|
START_STRING(" ");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "");
|
2021-05-21 03:48:07 +00:00
|
|
|
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);
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("trivial whitespace");
|
|
|
|
START_STRING("blob ");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("multi");
|
|
|
|
START_STRING("blob1 blob2");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob1");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "blob2");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
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");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "blob2 ");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("multi equals");
|
|
|
|
START_STRING("blob1=blob2");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob1");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "blob2");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
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 */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "=blob2");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "blob2");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2"); /* XXX should (but can't) reject */
|
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("multi equals strdelimw");
|
|
|
|
START_STRING("blob1=blob2");
|
|
|
|
cp = strdelimw(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob1=blob2");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
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 */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
DONE_STRING();
|
|
|
|
TEST_DONE();
|
|
|
|
|
|
|
|
TEST_START("quoted multi");
|
|
|
|
START_STRING("\"blob1\" blob2");
|
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob1");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "blob2");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
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");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "\"blob2\"");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, "blob2");
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_STRING_EQ(str, "");
|
2021-05-21 03:48:07 +00:00
|
|
|
cp = strdelim(&str);
|
|
|
|
ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
|
2021-05-21 03:59:01 +00:00
|
|
|
ASSERT_PTR_EQ(str, NULL);
|
2021-05-21 03:48:07 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
}
|