1 /* $OpenBSD: test_strdelim.c,v 1.2 2021/05/21 03:59:01 djm Exp $ */ 2 /* 3 * Regress test for misc strdelim() and co 4 * 5 * Placed in the public domain. 6 */ 7 8 #include "includes.h" 9 10 #include <sys/types.h> 11 #include <sys/param.h> 12 #include <stdio.h> 13 #ifdef HAVE_STDINT_H 14 #include <stdint.h> 15 #endif 16 #include <stdlib.h> 17 #include <string.h> 18 19 #include "../test_helper/test_helper.h" 20 21 #include "log.h" 22 #include "misc.h" 23 #include "xmalloc.h" 24 25 void test_strdelim(void); 26 27 void 28 test_strdelim(void) 29 { 30 char *orig, *str, *cp; 31 32 #define START_STRING(x) orig = str = xstrdup(x) 33 #define DONE_STRING() free(orig) 34 35 TEST_START("empty"); 36 START_STRING(""); 37 cp = strdelim(&str); 38 ASSERT_STRING_EQ(cp, ""); /* XXX arguable */ 39 cp = strdelim(&str); 40 ASSERT_PTR_EQ(cp, NULL); 41 DONE_STRING(); 42 TEST_DONE(); 43 44 TEST_START("whitespace"); 45 START_STRING(" "); 46 cp = strdelim(&str); 47 ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */ 48 ASSERT_STRING_EQ(str, ""); 49 DONE_STRING(); 50 TEST_DONE(); 51 52 TEST_START("trivial"); 53 START_STRING("blob"); 54 cp = strdelim(&str); 55 ASSERT_STRING_EQ(cp, "blob"); 56 cp = strdelim(&str); 57 ASSERT_PTR_EQ(cp, NULL); 58 ASSERT_PTR_EQ(str, NULL); 59 DONE_STRING(); 60 TEST_DONE(); 61 62 TEST_START("trivial whitespace"); 63 START_STRING("blob "); 64 cp = strdelim(&str); 65 ASSERT_STRING_EQ(cp, "blob"); 66 ASSERT_STRING_EQ(str, ""); 67 cp = strdelim(&str); 68 ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */ 69 ASSERT_PTR_EQ(str, NULL); 70 DONE_STRING(); 71 TEST_DONE(); 72 73 TEST_START("multi"); 74 START_STRING("blob1 blob2"); 75 cp = strdelim(&str); 76 ASSERT_STRING_EQ(cp, "blob1"); 77 ASSERT_STRING_EQ(str, "blob2"); 78 cp = strdelim(&str); 79 ASSERT_STRING_EQ(cp, "blob2"); 80 ASSERT_PTR_EQ(str, NULL); 81 cp = strdelim(&str); 82 ASSERT_PTR_EQ(cp, NULL); 83 DONE_STRING(); 84 TEST_DONE(); 85 86 TEST_START("multi whitespace"); 87 START_STRING("blob1 blob2 "); 88 cp = strdelim(&str); 89 ASSERT_STRING_EQ(cp, "blob1"); 90 ASSERT_STRING_EQ(str, "blob2 "); 91 cp = strdelim(&str); 92 ASSERT_STRING_EQ(cp, "blob2"); 93 cp = strdelim(&str); 94 ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */ 95 ASSERT_PTR_EQ(str, NULL); 96 DONE_STRING(); 97 TEST_DONE(); 98 99 TEST_START("multi equals"); 100 START_STRING("blob1=blob2"); 101 cp = strdelim(&str); 102 ASSERT_STRING_EQ(cp, "blob1"); 103 ASSERT_STRING_EQ(str, "blob2"); 104 cp = strdelim(&str); 105 ASSERT_STRING_EQ(cp, "blob2"); 106 ASSERT_PTR_EQ(str, NULL); 107 cp = strdelim(&str); 108 ASSERT_PTR_EQ(cp, NULL); 109 DONE_STRING(); 110 TEST_DONE(); 111 112 TEST_START("multi too many equals"); 113 START_STRING("blob1==blob2"); 114 cp = strdelim(&str); 115 ASSERT_STRING_EQ(cp, "blob1"); /* XXX better returning NULL early */ 116 ASSERT_STRING_EQ(str, "=blob2"); 117 cp = strdelim(&str); 118 ASSERT_STRING_EQ(cp, ""); 119 ASSERT_STRING_EQ(str, "blob2"); 120 cp = strdelim(&str); 121 ASSERT_STRING_EQ(cp, "blob2"); /* XXX should (but can't) reject */ 122 ASSERT_PTR_EQ(str, NULL); 123 DONE_STRING(); 124 TEST_DONE(); 125 126 TEST_START("multi equals strdelimw"); 127 START_STRING("blob1=blob2"); 128 cp = strdelimw(&str); 129 ASSERT_STRING_EQ(cp, "blob1=blob2"); 130 ASSERT_PTR_EQ(str, NULL); 131 cp = strdelimw(&str); 132 ASSERT_PTR_EQ(cp, NULL); 133 DONE_STRING(); 134 TEST_DONE(); 135 136 TEST_START("quoted"); 137 START_STRING("\"blob\""); 138 cp = strdelim(&str); 139 ASSERT_STRING_EQ(cp, "blob"); 140 cp = strdelim(&str); 141 ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */ 142 ASSERT_PTR_EQ(str, NULL); 143 DONE_STRING(); 144 TEST_DONE(); 145 146 TEST_START("quoted multi"); 147 START_STRING("\"blob1\" blob2"); 148 cp = strdelim(&str); 149 ASSERT_STRING_EQ(cp, "blob1"); 150 ASSERT_STRING_EQ(str, "blob2"); 151 cp = strdelim(&str); 152 ASSERT_STRING_EQ(cp, "blob2"); 153 ASSERT_PTR_EQ(str, NULL); 154 cp = strdelim(&str); 155 ASSERT_PTR_EQ(cp, NULL); 156 DONE_STRING(); 157 TEST_DONE(); 158 159 TEST_START("quoted multi reverse"); 160 START_STRING("blob1 \"blob2\""); 161 cp = strdelim(&str); 162 ASSERT_STRING_EQ(cp, "blob1"); 163 ASSERT_STRING_EQ(str, "\"blob2\""); 164 cp = strdelim(&str); 165 ASSERT_STRING_EQ(cp, "blob2"); 166 ASSERT_STRING_EQ(str, ""); 167 cp = strdelim(&str); 168 ASSERT_STRING_EQ(cp, ""); /* XXX better as NULL */ 169 ASSERT_PTR_EQ(str, NULL); 170 DONE_STRING(); 171 TEST_DONE(); 172 173 TEST_START("quoted multi middle"); 174 START_STRING("blob1 \"blob2\" blob3"); 175 cp = strdelim(&str); 176 ASSERT_STRING_EQ(cp, "blob1"); 177 cp = strdelim(&str); 178 ASSERT_STRING_EQ(cp, "blob2"); 179 cp = strdelim(&str); 180 ASSERT_STRING_EQ(cp, "blob3"); 181 cp = strdelim(&str); 182 ASSERT_PTR_EQ(cp, NULL); 183 DONE_STRING(); 184 TEST_DONE(); 185 186 TEST_START("badquote"); 187 START_STRING("\"blob"); 188 cp = strdelim(&str); 189 ASSERT_PTR_EQ(cp, NULL); 190 DONE_STRING(); 191 TEST_DONE(); 192 193 TEST_START("oops quote"); 194 START_STRING("\"blob\\\""); 195 cp = strdelim(&str); 196 ASSERT_STRING_EQ(cp, "blob\\"); /* XXX wrong */ 197 cp = strdelim(&str); 198 ASSERT_STRING_EQ(cp, ""); 199 DONE_STRING(); 200 TEST_DONE(); 201 202 } 203