diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2011-03-13 22:12:38 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2011-03-13 22:12:38 +0000 |
commit | 2c8b13a194717b959549c7063f797b71811d193e (patch) | |
tree | 7802c9f10a30f73450077e8680325dbccf863dca /regress | |
parent | 4c36a4d80be5a3be316adb18302fd30d7a24b79a (diff) |
add a regress test for the vis and unvis functions. after finding one
bug, this then found a 2nd bug..
worked on with guenther
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libc/vis/vis_test.c | 94 |
1 files changed, 90 insertions, 4 deletions
diff --git a/regress/lib/libc/vis/vis_test.c b/regress/lib/libc/vis/vis_test.c index 054befd44f8..74113e7a615 100644 --- a/regress/lib/libc/vis/vis_test.c +++ b/regress/lib/libc/vis/vis_test.c @@ -1,23 +1,109 @@ -/* $OpenBSD: vis_test.c,v 1.2 2009/06/21 00:38:22 martynas Exp $ */ +/* $OpenBSD: vis_test.c,v 1.3 2011/03/13 22:12:37 deraadt Exp $ */ /* Public domain. 2005, Otto Moerbeek */ #include <limits.h> #include <stdlib.h> +#include <string.h> #include <stdio.h> #include <vis.h> +#define NTESTS 8000 +#define NCH 800 + +char ibuf[NCH]; +char obuf[NCH * 4]; +char rbuf[NCH * 4]; + +int flags[] = { + VIS_ALL, + VIS_GLOB, + VIS_TAB, + VIS_NL, + VIS_WHITE, + VIS_SAFE +}; + +char *flagname[] = { + "VIS_ALL", + "VIS_GLOB", + "VIS_TAB", + "VIS_NL", + "VIS_WHITE", + "VIS_SAFE" +}; + +int title; + +void +dotitle(int i, int j) +{ + if (title == 0) + printf("%d %s:", i, flagname[j]); + title = 1; +} + int -main() +main(int argc, char *argv[]) { + char inp[UCHAR_MAX + 1]; char out[4 * UCHAR_MAX + 1]; - int i; + int i, j, fail = 0; + ssize_t owant, o, r; for (i = 0; i <= UCHAR_MAX; i++) { inp[i] = i; } strvisx(out, inp, UCHAR_MAX + 1, 0); printf("%s\n", out); - exit(0); + + for (i = 0; i < NTESTS; i++) { + arc4random_buf(ibuf, sizeof(ibuf) - 1); + ibuf[sizeof(ibuf) - 1] = '\0'; + title = 0; + + for (j = 0; j < sizeof(flags)/sizeof(flags[0]); j++) { + owant = sizeof(ibuf); + o = strnvis(obuf, ibuf, owant, flags[j]); + if (o >= owant) { + owant = o + 1; + o = strnvis(obuf, ibuf, owant, flags[j]); + if (o > owant) { + dotitle(i, j); + printf("HUGE overflow\n"); + } + if (o < owant - 1) { + dotitle(i, j); + printf("over-estimate of overflow\n"); + } + } else if (o > strlen(ibuf) * 4) { + dotitle(i, j); + printf("wants too much %d %d\n", o, strlen(ibuf) * 4); + continue; + } + + r = strnunvis(rbuf, obuf, sizeof rbuf); + + if (r == -1) { + dotitle(i, j); + printf("cannot decode\n"); + printf("%s\n", obuf); + fail = 1; + } else if (r != strlen(ibuf)) { + dotitle(i, j); + printf("rlen %d != inlen %d\n", r, strlen(ibuf)); + printf("%s\n", obuf); + printf("%s\n", rbuf); + fail = 1; + } else if (bcmp(ibuf, rbuf, r)) { + dotitle(i, j); + printf("strings are different\n"); + printf("%s\n", ibuf); + printf("%s\n", rbuf); + fail = 1; + } + } + } + exit(fail); } |