1*1323ec57SEd Maste /* $OpenBSD: test_strdelim.c,v 1.3 2021/12/14 21:25:27 deraadt Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Regress test for misc strdelim() and co
419261079SEd Maste *
519261079SEd Maste * Placed in the public domain.
619261079SEd Maste */
719261079SEd Maste
819261079SEd Maste #include "includes.h"
919261079SEd Maste
1019261079SEd Maste #include <sys/types.h>
1119261079SEd Maste #include <stdio.h>
1219261079SEd Maste #ifdef HAVE_STDINT_H
1319261079SEd Maste #include <stdint.h>
1419261079SEd Maste #endif
1519261079SEd Maste #include <stdlib.h>
1619261079SEd Maste #include <string.h>
1719261079SEd Maste
1819261079SEd Maste #include "../test_helper/test_helper.h"
1919261079SEd Maste
2019261079SEd Maste #include "log.h"
2119261079SEd Maste #include "misc.h"
2219261079SEd Maste #include "xmalloc.h"
2319261079SEd Maste
2419261079SEd Maste void test_strdelim(void);
2519261079SEd Maste
2619261079SEd Maste void
test_strdelim(void)2719261079SEd Maste test_strdelim(void)
2819261079SEd Maste {
2919261079SEd Maste char *orig, *str, *cp;
3019261079SEd Maste
3119261079SEd Maste #define START_STRING(x) orig = str = xstrdup(x)
3219261079SEd Maste #define DONE_STRING() free(orig)
3319261079SEd Maste
3419261079SEd Maste TEST_START("empty");
3519261079SEd Maste START_STRING("");
3619261079SEd Maste cp = strdelim(&str);
3719261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX arguable */
3819261079SEd Maste cp = strdelim(&str);
3919261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
4019261079SEd Maste DONE_STRING();
4119261079SEd Maste TEST_DONE();
4219261079SEd Maste
4319261079SEd Maste TEST_START("whitespace");
4419261079SEd Maste START_STRING(" ");
4519261079SEd Maste cp = strdelim(&str);
4619261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
4719261079SEd Maste ASSERT_STRING_EQ(str, "");
4819261079SEd Maste DONE_STRING();
4919261079SEd Maste TEST_DONE();
5019261079SEd Maste
5119261079SEd Maste TEST_START("trivial");
5219261079SEd Maste START_STRING("blob");
5319261079SEd Maste cp = strdelim(&str);
5419261079SEd Maste ASSERT_STRING_EQ(cp, "blob");
5519261079SEd Maste cp = strdelim(&str);
5619261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
5719261079SEd Maste ASSERT_PTR_EQ(str, NULL);
5819261079SEd Maste DONE_STRING();
5919261079SEd Maste TEST_DONE();
6019261079SEd Maste
6119261079SEd Maste TEST_START("trivial whitespace");
6219261079SEd Maste START_STRING("blob ");
6319261079SEd Maste cp = strdelim(&str);
6419261079SEd Maste ASSERT_STRING_EQ(cp, "blob");
6519261079SEd Maste ASSERT_STRING_EQ(str, "");
6619261079SEd Maste cp = strdelim(&str);
6719261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
6819261079SEd Maste ASSERT_PTR_EQ(str, NULL);
6919261079SEd Maste DONE_STRING();
7019261079SEd Maste TEST_DONE();
7119261079SEd Maste
7219261079SEd Maste TEST_START("multi");
7319261079SEd Maste START_STRING("blob1 blob2");
7419261079SEd Maste cp = strdelim(&str);
7519261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
7619261079SEd Maste ASSERT_STRING_EQ(str, "blob2");
7719261079SEd Maste cp = strdelim(&str);
7819261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
7919261079SEd Maste ASSERT_PTR_EQ(str, NULL);
8019261079SEd Maste cp = strdelim(&str);
8119261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
8219261079SEd Maste DONE_STRING();
8319261079SEd Maste TEST_DONE();
8419261079SEd Maste
8519261079SEd Maste TEST_START("multi whitespace");
8619261079SEd Maste START_STRING("blob1 blob2 ");
8719261079SEd Maste cp = strdelim(&str);
8819261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
8919261079SEd Maste ASSERT_STRING_EQ(str, "blob2 ");
9019261079SEd Maste cp = strdelim(&str);
9119261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
9219261079SEd Maste cp = strdelim(&str);
9319261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
9419261079SEd Maste ASSERT_PTR_EQ(str, NULL);
9519261079SEd Maste DONE_STRING();
9619261079SEd Maste TEST_DONE();
9719261079SEd Maste
9819261079SEd Maste TEST_START("multi equals");
9919261079SEd Maste START_STRING("blob1=blob2");
10019261079SEd Maste cp = strdelim(&str);
10119261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
10219261079SEd Maste ASSERT_STRING_EQ(str, "blob2");
10319261079SEd Maste cp = strdelim(&str);
10419261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
10519261079SEd Maste ASSERT_PTR_EQ(str, NULL);
10619261079SEd Maste cp = strdelim(&str);
10719261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
10819261079SEd Maste DONE_STRING();
10919261079SEd Maste TEST_DONE();
11019261079SEd Maste
11119261079SEd Maste TEST_START("multi too many equals");
11219261079SEd Maste START_STRING("blob1==blob2");
11319261079SEd Maste cp = strdelim(&str);
11419261079SEd Maste ASSERT_STRING_EQ(cp, "blob1"); /* XXX better returning NULL early */
11519261079SEd Maste ASSERT_STRING_EQ(str, "=blob2");
11619261079SEd Maste cp = strdelim(&str);
11719261079SEd Maste ASSERT_STRING_EQ(cp, "");
11819261079SEd Maste ASSERT_STRING_EQ(str, "blob2");
11919261079SEd Maste cp = strdelim(&str);
12019261079SEd Maste ASSERT_STRING_EQ(cp, "blob2"); /* XXX should (but can't) reject */
12119261079SEd Maste ASSERT_PTR_EQ(str, NULL);
12219261079SEd Maste DONE_STRING();
12319261079SEd Maste TEST_DONE();
12419261079SEd Maste
12519261079SEd Maste TEST_START("multi equals strdelimw");
12619261079SEd Maste START_STRING("blob1=blob2");
12719261079SEd Maste cp = strdelimw(&str);
12819261079SEd Maste ASSERT_STRING_EQ(cp, "blob1=blob2");
12919261079SEd Maste ASSERT_PTR_EQ(str, NULL);
13019261079SEd Maste cp = strdelimw(&str);
13119261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
13219261079SEd Maste DONE_STRING();
13319261079SEd Maste TEST_DONE();
13419261079SEd Maste
13519261079SEd Maste TEST_START("quoted");
13619261079SEd Maste START_STRING("\"blob\"");
13719261079SEd Maste cp = strdelim(&str);
13819261079SEd Maste ASSERT_STRING_EQ(cp, "blob");
13919261079SEd Maste cp = strdelim(&str);
14019261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
14119261079SEd Maste ASSERT_PTR_EQ(str, NULL);
14219261079SEd Maste DONE_STRING();
14319261079SEd Maste TEST_DONE();
14419261079SEd Maste
14519261079SEd Maste TEST_START("quoted multi");
14619261079SEd Maste START_STRING("\"blob1\" blob2");
14719261079SEd Maste cp = strdelim(&str);
14819261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
14919261079SEd Maste ASSERT_STRING_EQ(str, "blob2");
15019261079SEd Maste cp = strdelim(&str);
15119261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
15219261079SEd Maste ASSERT_PTR_EQ(str, NULL);
15319261079SEd Maste cp = strdelim(&str);
15419261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
15519261079SEd Maste DONE_STRING();
15619261079SEd Maste TEST_DONE();
15719261079SEd Maste
15819261079SEd Maste TEST_START("quoted multi reverse");
15919261079SEd Maste START_STRING("blob1 \"blob2\"");
16019261079SEd Maste cp = strdelim(&str);
16119261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
16219261079SEd Maste ASSERT_STRING_EQ(str, "\"blob2\"");
16319261079SEd Maste cp = strdelim(&str);
16419261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
16519261079SEd Maste ASSERT_STRING_EQ(str, "");
16619261079SEd Maste cp = strdelim(&str);
16719261079SEd Maste ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */
16819261079SEd Maste ASSERT_PTR_EQ(str, NULL);
16919261079SEd Maste DONE_STRING();
17019261079SEd Maste TEST_DONE();
17119261079SEd Maste
17219261079SEd Maste TEST_START("quoted multi middle");
17319261079SEd Maste START_STRING("blob1 \"blob2\" blob3");
17419261079SEd Maste cp = strdelim(&str);
17519261079SEd Maste ASSERT_STRING_EQ(cp, "blob1");
17619261079SEd Maste cp = strdelim(&str);
17719261079SEd Maste ASSERT_STRING_EQ(cp, "blob2");
17819261079SEd Maste cp = strdelim(&str);
17919261079SEd Maste ASSERT_STRING_EQ(cp, "blob3");
18019261079SEd Maste cp = strdelim(&str);
18119261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
18219261079SEd Maste DONE_STRING();
18319261079SEd Maste TEST_DONE();
18419261079SEd Maste
18519261079SEd Maste TEST_START("badquote");
18619261079SEd Maste START_STRING("\"blob");
18719261079SEd Maste cp = strdelim(&str);
18819261079SEd Maste ASSERT_PTR_EQ(cp, NULL);
18919261079SEd Maste DONE_STRING();
19019261079SEd Maste TEST_DONE();
19119261079SEd Maste
19219261079SEd Maste TEST_START("oops quote");
19319261079SEd Maste START_STRING("\"blob\\\"");
19419261079SEd Maste cp = strdelim(&str);
19519261079SEd Maste ASSERT_STRING_EQ(cp, "blob\\"); /* XXX wrong */
19619261079SEd Maste cp = strdelim(&str);
19719261079SEd Maste ASSERT_STRING_EQ(cp, "");
19819261079SEd Maste DONE_STRING();
19919261079SEd Maste TEST_DONE();
20019261079SEd Maste
20119261079SEd Maste }
202