1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* vector test suite.
*
* The canonical version of this file is maintained in the rra-c-util package,
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Written by Russ Allbery <eagle@eyrie.org>
*
* The authors hereby relinquish any claim to any copyright that they may have
* in this work, whether granted under contract or by operation of law or
* international treaty, and hereby commit to the public, at large, that they
* shall not, at any time in the future, seek to enforce any copyright in this
* work against any person or entity, or prevent any person or entity from
* copying, publishing, distributing or creating derivative works of this
* work.
*/
#include <config.h>
#include <portable/system.h>
#include <sys/wait.h>
#include <tests/tap/basic.h>
#include <tests/tap/string.h>
#include <util/vector.h>
#include <util/xmalloc.h>
int
main(void)
{
struct vector *vector;
struct cvector *cvector;
char *command, *string;
char *p;
pid_t child;
char empty[] = "";
static const char cstring[] = "This is a\ttest. ";
static const char nulls1[] = "This\0is\0a\0test.";
static const char nulls2[] = "This is a\t\0es\0. ";
static const char tabs[] = "test\t\ting\t";
/* Set up the plan. */
plan(119);
/* Be sure that freeing NULL doesn't cause a NULL pointer dereference. */
vector_free(NULL);
cvector_free(NULL);
/* Test basic add and resize functionality for vectors. */
vector = vector_new();
ok(vector != NULL, "vector_new returns non-NULL");
if (vector == NULL)
bail("vector_new returned NULL");
vector_add(vector, cstring);
is_int(1, vector->count, "vector_add increases count");
ok(vector->strings[0] != cstring, "...and allocated new memory");
vector_resize(vector, 4);
is_int(4, vector->allocated, "vector_resize works");
vector_add(vector, cstring);
vector_add(vector, cstring);
vector_add(vector, cstring);
is_int(4, vector->allocated, "...and no reallocation when adding strings");
is_int(4, vector->count, "...and the count matches");
is_string(cstring, vector->strings[0], "added the right string");
is_string(cstring, vector->strings[1], "added the right string");
is_string(cstring, vector->strings[2], "added the right string");
is_string(cstring, vector->strings[3], "added the right string");
/* Verify that adding the same string creates new copies. */
ok(vector->strings[1] != vector->strings[2], "each pointer is different");
ok(vector->strings[2] != vector->strings[3], "each pointer is different");
ok(vector->strings[3] != vector->strings[0], "each pointer is different");
ok(vector->strings[0] != cstring, "each pointer is different");
/* Test vector_clear. */
vector_clear(vector);
is_int(0, vector->count, "vector_clear works");
is_int(4, vector->allocated, "...but doesn't free the allocation");
/* Test that resizing the vector shrinks it and frees the string. */
string = xstrdup(cstring);
vector_add(vector, cstring);
vector_add(vector, string);
is_int(2, vector->count, "added two strings to the vector");
ok(vector->strings[1] != string, "...and the pointers are different");
vector_resize(vector, 1);
is_int(1, vector->count, "vector_resize shrinks the vector");
ok(vector->strings[0] != cstring, "...and the pointer is different");
free(string);
/* Test vector_addn. */
vector_addn(vector, cstring, 4);
is_int(2, vector->count, "vector_addn increments count");
is_string("This", vector->strings[1], "...and adds the right data");
vector_free(vector);
/* Test basic add and resize functionality for cvectors. */
cvector = cvector_new();
ok(cvector != NULL, "cvector_new returns non-NULL");
if (cvector == NULL)
bail("cvector_new returned NULL");
cvector_add(cvector, cstring);
is_int(1, cvector->count, "cvector_add adds a string");
ok(cvector->strings[0] == cstring, "...and keeps the same pointer");
cvector_resize(cvector, 4);
is_int(4, cvector->allocated, "cvector_resize works");
cvector_add(cvector, cstring);
cvector_add(cvector, cstring);
cvector_add(cvector, cstring);
is_int(4, cvector->allocated, "...and subsequent adds don't resize");
is_int(4, cvector->count, "...and the count is right");
ok(cvector->strings[1] == cvector->strings[2], "all pointers match");
ok(cvector->strings[2] == cvector->strings[3], "all pointers match");
ok(cvector->strings[3] == cvector->strings[0], "all pointers match");
ok(cvector->strings[0] == cstring, "all pointers match");
/* Test cvector_clear. */
cvector_clear(cvector);
is_int(0, cvector->count, "cvector_clear works");
is_int(4, cvector->allocated, "...but doesn't free the allocation");
/* Test that resizing the vector shrinks it. */
string = xstrdup(cstring);
cvector_add(cvector, cstring);
cvector_add(cvector, string);
is_int(2, cvector->count, "added two strings to the vector");
ok(cvector->strings[1] == string, "...and the pointers match");
cvector_resize(cvector, 1);
is_int(1, cvector->count, "cvector_resize shrinks the vector");
ok(cvector->strings[0] == cstring, "...and the pointers match");
cvector_free(cvector);
free(string);
/* Test vector_split_space. */
vector = vector_split_space("This is a\ttest. ", NULL);
is_int(4, vector->count, "vector_split_space returns right count");
is_int(4, vector->allocated, "...and allocation");
is_string("This", vector->strings[0], "...first string");
is_string("is", vector->strings[1], "...second string");
is_string("a", vector->strings[2], "...third string");
is_string("test.", vector->strings[3], "...fourth string");
/* Test that vector_add increases the size of the vector by one. */
vector_add(vector, cstring);
is_string(cstring, vector->strings[4], "...and can add another");
ok(vector->strings[4] != cstring, "allocates a new pointer");
is_int(5, vector->allocated, "allocation goes up by one");
/* Test vector_split with vector reuse. */
vector = vector_split(cstring, 't', vector);
is_int(3, vector->count, "resplitting returns the right count");
is_int(5, vector->allocated, "...but doesn't change allocation");
is_string("This is a\t", vector->strings[0], "...first string");
is_string("es", vector->strings[1], "...second string");
is_string(". ", vector->strings[2], "...third string");
ok(vector->strings[0] != cstring, "...and allocated new string");
/* Test vector_join. */
p = vector_join(vector, "fe");
is_string("This is a\tfeesfe. ", p, "vector_join works");
free(p);
vector_free(vector);
/* Test cvector_split_space. */
string = xstrdup(cstring);
cvector = cvector_split_space(string, NULL);
is_int(4, cvector->count, "cvector_split_space returns right count");
is_int(4, cvector->allocated, "...and allocation");
is_string("This", cvector->strings[0], "...first string");
is_string("is", cvector->strings[1], "...second string");
is_string("a", cvector->strings[2], "...third string");
is_string("test.", cvector->strings[3], "...fourth string");
ok(memcmp(string, nulls1, 16) == 0, "original string modified in place");
/* Test that cvector_add increases the size of the vector by one. */
cvector_add(cvector, cstring);
ok(cvector->strings[4] == cstring, "cvector_add then works");
is_int(5, cvector->allocated, "...and allocation increases by one");
free(string);
/* Test cvector_split with vector reuse. */
string = xstrdup(cstring);
cvector = cvector_split(string, 't', cvector);
is_int(3, cvector->count, "cvector_split into same cvector works");
is_int(5, cvector->allocated, "...and doesn't lower allocation");
is_string("This is a\t", cvector->strings[0], "...first string");
is_string("es", cvector->strings[1], "...second string");
is_string(". ", cvector->strings[2], "...third string");
ok(cvector->strings[0] == string, "no new memory is allocated");
ok(memcmp(string, nulls2, 18) == 0, "...and string is modified in place");
/* Test cvector_join. */
p = cvector_join(cvector, "oo");
is_string("This is a\tooesoo. ", p, "cvector_join works");
free(p);
cvector_free(cvector);
free(string);
/* Test vector_split and cvector_split on empty string. */
vector = vector_split("", ' ', NULL);
is_int(1, vector->count, "vector_split on empty string");
is_string("", vector->strings[0], "...returns only empty string");
vector_free(vector);
cvector = cvector_split(empty, ' ', NULL);
is_int(1, cvector->count, "cvector_split on empty string");
is_string("", cvector->strings[0], "...returns only empty string");
cvector_free(cvector);
/* Test vector_split_space and cvector_split_space on empty string. */
vector = vector_split_space("", NULL);
is_int(0, vector->count, "vector_split_space on empty string");
p = vector_join(vector, "mumble");
is_string("", p, "vector_join with an empty vector works");
free(p);
vector_free(vector);
cvector = cvector_split_space(empty, NULL);
is_int(0, cvector->count, "cvector_split_space on empty string");
p = cvector_join(cvector, "mumble");
is_string("", p, "cvector_join with an empty vector works");
free(p);
cvector_free(cvector);
/* Test vector_split again and then join with an empty string. */
vector = vector_split(tabs, '\t', NULL);
is_int(4, vector->count, "vector_split on tab string");
is_string("test", vector->strings[0], "...first string");
is_string("", vector->strings[1], "...second string");
is_string("ing", vector->strings[2], "...third string");
is_string("", vector->strings[3], "...fourth string");
p = vector_join(vector, "");
is_string("testing", p, "vector_join with an empty string works");
free(p);
vector_free(vector);
/* Test cvector_split again and then join with an empty string. */
string = xstrdup(tabs);
cvector = cvector_split(string, '\t', NULL);
is_int(4, cvector->count, "cvector_split on tab string");
is_string("test", cvector->strings[0], "...first string");
is_string("", cvector->strings[1], "...second string");
is_string("ing", cvector->strings[2], "...third string");
is_string("", cvector->strings[3], "...fourth string");
p = cvector_join(cvector, "");
is_string("testing", p, "cvector_join with an empty string works");
free(p);
cvector_free(cvector);
free(string);
/* Test that newline is not space for (c)vector_split_space. */
vector = vector_split_space("foo\nbar", NULL);
is_int(1, vector->count, "newline is not space for vector_split_space");
is_string("foo\nbar", vector->strings[0], "...first string");
vector_free(vector);
string = xstrdup("foo\nbar");
cvector = cvector_split_space(string, NULL);
is_int(1, cvector->count, "newline is not space for cvector_split_space");
is_string("foo\nbar", cvector->strings[0], "...first string");
cvector_free(cvector);
free(string);
/* Test (c)vector_split_space with leading and trailing delimiters. */
vector = vector_split_space(" \t foo\t", NULL);
is_int(1, vector->count, "extra whitespace in vector_split_space");
is_string("foo", vector->strings[0], "...first string");
vector_free(vector);
string = xstrdup(" \t foo\t");
cvector = cvector_split_space(string, NULL);
is_int(1, cvector->count, "extra whitespace in cvector_split_space");
is_string("foo", cvector->strings[0], "...first string");
cvector_free(cvector);
free(string);
/* Test (c)vector_split_space on a string that's all delimiters. */
vector = vector_split_space(" \t ", NULL);
is_int(0, vector->count, "vector_split_space on all whitespace string");
vector_free(vector);
string = xstrdup(" \t ");
cvector = cvector_split_space(string, NULL);
is_int(0, cvector->count, "cvector_split_space on all whitespace string");
cvector_free(cvector);
free(string);
/* Test vector_split_multi. */
vector = vector_split_multi("foo, bar, baz", ", ", NULL);
is_int(3, vector->count, "vector_split_multi returns right count");
is_string("foo", vector->strings[0], "...first string");
is_string("bar", vector->strings[1], "...second string");
is_string("baz", vector->strings[2], "...third string");
vector_free(vector);
vector = vector_split_multi("", ", ", NULL);
is_int(0, vector->count, "vector_split_multi reuse with empty string");
vector = vector_split_multi(",,, foo, ", ", ", vector);
is_int(1, vector->count, "vector_split_multi with extra separators");
is_string("foo", vector->strings[0], "...first string");
vector = vector_split_multi(", , ", ", ", vector);
is_int(0, vector->count, "vector_split_multi with only separators");
vector_free(vector);
/* Test cvector_split_multi. */
string = xstrdup("foo, bar, baz");
cvector = cvector_split_multi(string, ", ", NULL);
is_int(3, cvector->count, "cvector_split_multi returns right count");
is_string("foo", cvector->strings[0], "...first string");
is_string("bar", cvector->strings[1], "...second string");
is_string("baz", cvector->strings[2], "...third string");
free(string);
cvector_free(cvector);
cvector = cvector_split_multi(empty, ", ", NULL);
is_int(0, cvector->count, "cvector_split_multi reuse with empty string");
string = xstrdup(",,, foo, ");
cvector = cvector_split_multi(string, ", ", cvector);
is_int(1, cvector->count, "cvector_split_multi with extra separators");
is_string("foo", cvector->strings[0], "...first string");
free(string);
string = xstrdup(", , ");
cvector = cvector_split_multi(string, ", ", cvector);
is_int(0, cvector->count, "cvector_split_multi with only separators");
cvector_free(cvector);
free(string);
/*
* Test vector_exec. We mess with testnum here since the child outputs
* the okay message.
*/
vector = vector_new();
vector_add(vector, "/bin/sh");
vector_add(vector, "-c");
basprintf(&command, "echo ok %lu - vector_exec", testnum++);
vector_add(vector, command);
child = fork();
if (child < 0)
sysbail("unable to fork");
else if (child == 0)
if (vector_exec("/bin/sh", vector) < 0)
sysdiag("unable to exec /bin/sh");
waitpid(child, NULL, 0);
vector_free(vector);
free(command);
/* Test cvector_exec the same way. */
cvector = cvector_new();
cvector_add(cvector, "/bin/sh");
cvector_add(cvector, "-c");
basprintf(&command, "echo ok %lu - cvector_exec", testnum++);
cvector_add(cvector, command);
child = fork();
if (child < 0)
sysbail("unable to fork");
else if (child == 0)
if (cvector_exec("/bin/sh", cvector) < 0)
sysdiag("unable to exec /bin/sh");
waitpid(child, NULL, 0);
cvector_free(cvector);
free(command);
/* All done. */
return 0;
}
|