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