diff options
author | 2016-03-01 16:12:11 +0000 | |
---|---|---|
committer | 2016-03-01 16:12:11 +0000 | |
commit | cc51b90f3023da4006c5bd7a53411f43e628e6d3 (patch) | |
tree | 0f08229d5041f48112c2f707ca0308282bb0d56d | |
parent | add LIBCRYPTO to DPADD (diff) | |
download | wireguard-openbsd-cc51b90f3023da4006c5bd7a53411f43e628e6d3.tar.xz wireguard-openbsd-cc51b90f3023da4006c5bd7a53411f43e628e6d3.zip |
More unit tests for libedit.
Those that still fail are commented out in the Makefiles for now;
i have patches for them.
-rw-r--r-- | regress/lib/libedit/Makefile | 6 | ||||
-rw-r--r-- | regress/lib/libedit/chared/Makefile | 22 | ||||
-rw-r--r-- | regress/lib/libedit/chared/exp_gets.ascii | 10 | ||||
-rw-r--r-- | regress/lib/libedit/chared/exp_gets.utf8 | 11 | ||||
-rw-r--r-- | regress/lib/libedit/chared/inp_gets.txt | 6 | ||||
-rw-r--r-- | regress/lib/libedit/chared/test_gets.c | 96 | ||||
-rw-r--r-- | regress/lib/libedit/keymacro/Makefile | 13 | ||||
-rw-r--r-- | regress/lib/libedit/keymacro/test_get.c | 117 | ||||
-rw-r--r-- | regress/lib/libedit/read/Makefile | 14 | ||||
-rw-r--r-- | regress/lib/libedit/read/exp_getcmd.txt | 3 | ||||
-rw-r--r-- | regress/lib/libedit/read/glue.c | 62 | ||||
-rw-r--r-- | regress/lib/libedit/read/test_getcmd.c | 102 | ||||
-rw-r--r-- | regress/lib/libedit/read/test_read_char.c | 38 |
13 files changed, 458 insertions, 42 deletions
diff --git a/regress/lib/libedit/Makefile b/regress/lib/libedit/Makefile new file mode 100644 index 00000000000..a6935423b45 --- /dev/null +++ b/regress/lib/libedit/Makefile @@ -0,0 +1,6 @@ +# $OpenBSD: Makefile,v 1.1 2016/03/01 16:12:11 schwarze Exp $ + +SUBDIR += keymacro read +#SUBDIR += chared + +.include <bsd.subdir.mk> diff --git a/regress/lib/libedit/chared/Makefile b/regress/lib/libedit/chared/Makefile new file mode 100644 index 00000000000..5d1dc0a6b31 --- /dev/null +++ b/regress/lib/libedit/chared/Makefile @@ -0,0 +1,22 @@ +# $OpenBSD: Makefile,v 1.1 2016/03/01 16:12:11 schwarze Exp $ +# +# Author: Ingo Schwarze <schwarze@openbsd.org>, 2016. Public Domain. +# +# Run "cd /usr/src/lib/libedit && make obj && make depend" first. + +REGRESS_TARGETS = gets + +PROG = test_gets +CPPFLAGS += -DWIDECHAR +CPPFLAGS += -I${.CURDIR}/../../../../lib/libedit +CPPFLAGS += -I${.OBJDIR}/../../../../lib/libedit +MAN = +CLEANFILES += out_gets.utf8 out_gets.ascii + +gets: + LC_ALL=en_US.UTF-8 ./test_gets < ${.CURDIR}/inp_gets.txt > out_gets.utf8 + diff -au ${.CURDIR}/exp_gets.utf8 out_gets.utf8 + LC_ALL=C ./test_gets < ${.CURDIR}/inp_gets.txt > out_gets.ascii + diff -u ${.CURDIR}/exp_gets.ascii out_gets.ascii + +.include <bsd.regress.mk> diff --git a/regress/lib/libedit/chared/exp_gets.ascii b/regress/lib/libedit/chared/exp_gets.ascii new file mode 100644 index 00000000000..42ce317f2d5 --- /dev/null +++ b/regress/lib/libedit/chared/exp_gets.ascii @@ -0,0 +1,10 @@ +0:-1::: +0:-1::: +0::: +0::: +2:ac:ac: +6:Ãété:Ãété: +11:discardÃça:discardÃça: +0:-1::: +0::: +eof:0:-1::: diff --git a/regress/lib/libedit/chared/exp_gets.utf8 b/regress/lib/libedit/chared/exp_gets.utf8 new file mode 100644 index 00000000000..97ba7d061f1 --- /dev/null +++ b/regress/lib/libedit/chared/exp_gets.utf8 @@ -0,0 +1,11 @@ +0:-1::: +0:-1::: +0::: +0::: +2:ac:ac: +3:été:été: +error:84:-1::: +2:ça:ça: +0:-1::: +0::: +eof:0:-1::: diff --git a/regress/lib/libedit/chared/inp_gets.txt b/regress/lib/libedit/chared/inp_gets.txt new file mode 100644 index 00000000000..660c0fded0c --- /dev/null +++ b/regress/lib/libedit/chared/inp_gets.txt @@ -0,0 +1,6 @@ +a +a +abc +èdété +discardÃça +a diff --git a/regress/lib/libedit/chared/test_gets.c b/regress/lib/libedit/chared/test_gets.c new file mode 100644 index 00000000000..1f33e1de35f --- /dev/null +++ b/regress/lib/libedit/chared/test_gets.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> +#include <errno.h> +#include <locale.h> +#include <stdio.h> +#include <wchar.h> + +#include "chared.c" + +/* + * Glue for unit tests of libedit/chared.c. + * Rather than linking in all the various libedit modules, + * provide dummies for those functions called in chared.c. + * Most aren't actually called in c_gets(). + * Requires "make obj && make depend" in src/lib/libedit. + */ + +#define EL EditLine *el __attribute__((__unused__)) +#define UU __attribute__((__unused__)) + +int hist_enlargebuf(EL, size_t oldsz UU, size_t newsz UU) { return 1; } +void re_refresh(EL) { } +void re_refresh_cursor(EL) { } +void terminal_beep(EL) { putchar('\a'); } + +el_action_t +ed_end_of_file(EditLine *el, wint_t c UU) { + *el->el_line.lastchar = '\0'; + return CC_EOF; +} + +int +el_wgetc(EL, Char *cp) { + return (*cp = getwchar()) != WEOF ? 1 : feof(stdin) ? 0 : -1; +} + +#undef EL +#undef UU + +/* + * Unit test steering program for editline/chared.c, c_gets(). + */ + +int +main() +{ + EditLine el; + Char buf[EL_BUFSIZ]; + int i, len; + + if (setlocale(LC_CTYPE, "") == NULL) + err(1, "setlocale"); + if (ch_init(&el) == -1) + err(1, "ch_init"); + while (feof(stdin) == 0) { + errno = 0; + if ((len = c_gets(&el, buf, L"$")) == -1) { + if (feof(stdin)) + fputs("eof:", stdout); + if (ferror(stdin)) { + fputs("error:", stdout); + clearerr(stdin); + } + printf("%d:", errno); + } + printf("%d:", len); + if (len > 0) { + for (i = 0; i < len; i++) + putwchar(buf[i]); + putchar(':'); + for (i = 1; i <= len; i++) + putwchar(el.el_line.buffer[i]); + puts(":"); + } else + puts("::"); + assert(el.el_line.buffer[0] == '\0'); + assert(el.el_line.lastchar == el.el_line.buffer); + assert(el.el_line.cursor == el.el_line.buffer); + } + return 0; +} diff --git a/regress/lib/libedit/keymacro/Makefile b/regress/lib/libedit/keymacro/Makefile new file mode 100644 index 00000000000..e0f020bf8be --- /dev/null +++ b/regress/lib/libedit/keymacro/Makefile @@ -0,0 +1,13 @@ +# $OpenBSD: Makefile,v 1.1 2016/03/01 16:12:11 schwarze Exp $ +# +# Author: Ingo Schwarze <schwarze@openbsd.org>, 2016. Public Domain. +# +# Run "cd /usr/src/lib/libedit && make obj && make depend" first. + +PROG = test_get +CPPFLAGS += -DWIDECHAR +CPPFLAGS += -I${.CURDIR}/../../../../lib/libedit +CPPFLAGS += -I${.OBJDIR}/../../../../lib/libedit +MAN = + +.include <bsd.regress.mk> diff --git a/regress/lib/libedit/keymacro/test_get.c b/regress/lib/libedit/keymacro/test_get.c new file mode 100644 index 00000000000..70063549bc3 --- /dev/null +++ b/regress/lib/libedit/keymacro/test_get.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <err.h> + +#include "keymacro.c" + +/* + * Glue for unit tests of libedit/keymacro.c. + * Rather than linking in all the various libedit modules, + * provide dummies for those functions called in keymacro.c. + * Most aren't actually called in keymacro_get(). + * Requires "make obj && make depend" in src/lib/libedit. + */ + +#define EL EditLine *el __attribute__((__unused__)) +#define UU __attribute__((__unused__)) + +ssize_t ct_encode_char(char *dst UU, size_t len UU, Char c UU) { return -1; } +char *ct_encode_string(const Char *s UU, ct_buffer_t *conv UU) { return NULL; } + +ssize_t +ct_visual_char(Char *dst, size_t len UU, Char c) +{ + *dst = c; + return 1; +} + +int +el_wgetc(EL, Char *cp) { + static const Char *const input_buffer = L"adalixi"; + static const Char *input_ptr = input_buffer; + + *cp = *input_ptr; + if (*cp == '\0') { + input_ptr = input_buffer; + return 0; + } else { + input_ptr++; + return 1; + } +} + +#undef EL +#undef UU + +/* + * Unit test steering program for editline/keymacro.c, keymacro_get(). + */ + +int +main() +{ + EditLine el; + keymacro_value_t val; + Char repl[] = L"repl"; + Char ch; + int irc; + + if (keymacro_init(&el) == -1) + err(1, "keymacro_init"); + keymacro_add(&el, L"ad", keymacro_map_cmd(&el, VI_ADD), XK_CMD); + keymacro_add(&el, L"al", keymacro_map_str(&el, repl), XK_STR); + keymacro_add(&el, L"in", keymacro_map_cmd(&el, ED_INSERT), XK_CMD); + + if (el_wgetc(&el, &ch) == 0) + errx(1, "el_wgetc ad"); + irc = keymacro_get(&el, &ch, &val); + if (irc != XK_CMD) + errx(1, "ad %d != XK_CMD", irc); + if (val.cmd != VI_ADD) + errx(1, "ad %u != VI_ADD", val.cmd); + if (ch != L'd') + errx(1, "ad %lc != d", ch); + + if (el_wgetc(&el, &ch) == 0) + errx(1, "el_wgetc al"); + irc = keymacro_get(&el, &ch, &val); + if (irc != XK_STR) + errx(1, "al %d != XK_STR", irc); + if (wcscmp(val.str, L"repl") != 0) + errx(1, "al %ls != repl", val.str); + if (ch != L'\0') + errx(1, "al %lc != 0", ch); + + if (el_wgetc(&el, &ch) == 0) + errx(1, "el_wgetc ix"); + irc = keymacro_get(&el, &ch, &val); + if (irc != XK_STR) + errx(1, "ix %d != XK_STR", irc); + if (val.str != NULL) + errx(1, "ix %ls != NULL", val.str); + if (ch != L'x') + errx(1, "ix %lc != x", ch); + + if (el_wgetc(&el, &ch) == 0) + errx(1, "el_wgetc i"); + irc = keymacro_get(&el, &ch, &val); + if (irc != XK_CMD) + errx(1, "eof %d != XK_CMD", irc); + if (val.cmd != ED_END_OF_FILE) + errx(1, "ad %u != ED_END_OF_FILE", val.cmd); + return 0; +} diff --git a/regress/lib/libedit/read/Makefile b/regress/lib/libedit/read/Makefile index 5f4539e2251..9799eb1fa6b 100644 --- a/regress/lib/libedit/read/Makefile +++ b/regress/lib/libedit/read/Makefile @@ -1,18 +1,22 @@ -# $OpenBSD: Makefile,v 1.1 2016/02/11 15:37:20 schwarze Exp $ +# $OpenBSD: Makefile,v 1.2 2016/03/01 16:12:11 schwarze Exp $ # # Author: Ingo Schwarze <schwarze@openbsd.org>, 2016. Public Domain. # # Run "cd /usr/src/lib/libedit && make obj && make depend" first. -REGRESS_TARGETS = check +REGRESS_TARGETS = getcmd +#REGRESS_TARGETS += read_char -PROG = test_read_char CPPFLAGS += -DWIDECHAR CPPFLAGS += -I${.CURDIR}/../../../../lib/libedit CPPFLAGS += -I${.OBJDIR}/../../../../lib/libedit -MAN = +CLEANFILES += test_read_char test_getcmd out_getcmd.txt -check: +read_char: test_read_char sh ${.CURDIR}/test_read_char.sh +getcmd: test_getcmd + echo -n s | ./test_getcmd > out_getcmd.txt + diff -u ${.CURDIR}/exp_getcmd.txt out_getcmd.txt + .include <bsd.regress.mk> diff --git a/regress/lib/libedit/read/exp_getcmd.txt b/regress/lib/libedit/read/exp_getcmd.txt new file mode 100644 index 00000000000..5dd9e58e6f1 --- /dev/null +++ b/regress/lib/libedit/read/exp_getcmd.txt @@ -0,0 +1,3 @@ +OK insert L'i' macro[0]=ic(1) +OK command L'c' +EOF diff --git a/regress/lib/libedit/read/glue.c b/regress/lib/libedit/read/glue.c new file mode 100644 index 00000000000..b3ad35dd24d --- /dev/null +++ b/regress/lib/libedit/read/glue.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Glue for unit tests of libedit/read.c. + * Rather than linking in all the various libedit modules, + * provide dummies for those functions called in read.c. + */ + +#define EL EditLine *el __attribute__((__unused__)) +#define UU __attribute__((__unused__)) + +int ch_enlargebufs(EL, size_t addlen UU) { return 1; } +void ch_reset(EL, int mclear UU) { } +void el_resize(EL) { } +int el_set(EL, int op UU, ...) { return 0; } +int el_wset(EL, int op UU, ...) { return 0; } +void re_clear_display(EL) { } +void re_clear_lines(EL) { } +void re_refresh(EL) { } +void re_refresh_cursor(EL) { } +void sig_clr(EL) { } +void sig_set(EL) { } +void terminal__flush(EL) { } +void terminal_beep(EL) { } +int tty_cookedmode(EL) { return 0; } +int tty_rawmode(EL) { return 0; } + +int +keymacro_get(EL, Char *ch, keymacro_value_t *val) +{ + static Char value[] = L"ic"; + + switch (*ch) { + case L'c': + val->cmd = ED_COMMAND; + return XK_CMD; + case L's': + val->str = value; + return XK_STR; + default: + val->str = NULL; + *ch = '\0'; + return XK_STR; + } +} + +#undef EL +#undef UU diff --git a/regress/lib/libedit/read/test_getcmd.c b/regress/lib/libedit/read/test_getcmd.c new file mode 100644 index 00000000000..1692099717c --- /dev/null +++ b/regress/lib/libedit/read/test_getcmd.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> +#include <err.h> +#include <errno.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> + +#include "read.c" +#include "glue.c" + +#define N_KEYS 256 + +/* + * Unit test steering program for editline/read.c, read_getcmd(). + */ + +int +main() +{ + EditLine el; + c_macro_t *ma; + int irc; + Char ch; + el_action_t cmdnum; + + if (setlocale(LC_CTYPE, "") == NULL) + err(1, "setlocale"); + + el.el_errno = ENOMSG; + el.el_flags = CHARSET_IS_UTF8; + el.el_infd = STDIN_FILENO; + + el.el_map.alt = NULL; + if ((el.el_map.key = calloc(N_KEYS, sizeof(el_action_t))) == NULL) + err(1, NULL); + el.el_map.key[(unsigned char)'c'] = ED_SEQUENCE_LEAD_IN; + el.el_map.key[(unsigned char)'i'] = ED_INSERT; + el.el_map.key[(unsigned char)'s'] = ED_SEQUENCE_LEAD_IN; + el.el_map.current = el.el_map.key; + if ((el.el_signal = calloc(1, sizeof(*el.el_signal))) == NULL) + err(1, NULL); + + ma = &el.el_chared.c_macro; + ma->level = -1; + ma->offset = 0; + if ((ma->macro = calloc(EL_MAXMACRO, sizeof(*ma->macro))) == NULL) + err(1, NULL); + + if (read_init(&el) != 0) + err(1, "read_init"); + + do { + irc = read_getcmd(&el, &cmdnum, &ch); + switch (irc) { + case OKCMD: + fputs("OK ", stdout); + switch (cmdnum) { + case ED_COMMAND: + fputs("command", stdout); + break; + case ED_INSERT: + fputs("insert", stdout); + break; + default: + printf("cmdnum=%u", cmdnum); + break; + } + printf(" L'%lc'", ch); + break; + case 0: + fputs("EOF", stdout); + break; + default: + printf("ret(%d)", irc); + break; + } + if (el.el_errno != 0) + printf(" el_errno=%d", el.el_errno); + if (ma->level > -1) + printf(" macro[%d]=%ls(%d)", ma->level, + *ma->macro, ma->offset); + putchar('\n'); + } while (irc); + + return 0; +} diff --git a/regress/lib/libedit/read/test_read_char.c b/regress/lib/libedit/read/test_read_char.c index 75184edac40..5aab7dbed2b 100644 --- a/regress/lib/libedit/read/test_read_char.c +++ b/regress/lib/libedit/read/test_read_char.c @@ -20,43 +20,7 @@ #include <stdlib.h> #include "read.c" - -/* - * Glue for unit tests of libedit/read.c. - * Rather than linking in all the various libedit modules, - * provide dummies for those functions called in read.c. - * Most aren't actually called in read_char(). - * Requires "make obj && make depend" in src/lib/libedit. - */ - -#define EL EditLine *el __attribute__((__unused__)) -#define UU __attribute__((__unused__)) - -int ch_enlargebufs(EL, size_t addlen UU) { return 1; } -void ch_reset(EL, int mclear UU) { } -void el_resize(EL) { } -int el_set(EL, int op UU, ...) { return 0; } -void re_clear_display(EL) { } -void re_clear_lines(EL) { } -void re_refresh(EL) { } -void re_refresh_cursor(EL) { } -void sig_clr(EL) { } -void sig_set(EL) { } -void terminal__flush(EL) { } -void terminal_beep(EL) { } -int tty_cookedmode(EL) { return 0; } -int tty_rawmode(EL) { return 0; } - -int -keymacro_get(EL, Char *ch, keymacro_value_t *val) -{ - val->str = NULL; - *ch = '\0'; - return XK_STR; -} - -#undef EL -#undef UU +#include "glue.c" /* * Unit test steering program for editline/read.c, read_char(). |