summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>2017-10-31 10:49:51 +1100
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>2017-11-14 16:33:41 +1100
commit8d1e4f053574d69aae89af19983c96500b4156a4 (patch)
tree190c566638fac6bbcc7964566cbe75e6d05d3fff /test
parent669083ee9eda63af65d7cfc43968947f09162996 (diff)
downloadtalos-petitboot-8d1e4f053574d69aae89af19983c96500b4156a4.zip
talos-petitboot-8d1e4f053574d69aae89af19983c96500b4156a4.tar.gz
ui/ncurses: Safely handle lost terminal control commandsv1.6.3
Normally terminal control commands are caught and handled before ncurses or Petitboot could see them. However several combinations of broken terminal emulators or console connections can cause these command sequences to be seen by Petitboot, usually resulting in Petitboot exiting due to the ESC character and then the rest printed to the console. Aside from confusing the user this can also cancel autoboot, so it's important we don't let these sequences go unnoticed if possible. In ui/ncurses/console-codes we add a state machine that recognises the syntax of these control/escape sequences and handles the lost characters. We don't try to emulate the functionality of these commands, instead just logging them for reference. Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'test')
-rw-r--r--test/ui/Makefile.am35
-rw-r--r--test/ui/console-sequence.c112
2 files changed, 147 insertions, 0 deletions
diff --git a/test/ui/Makefile.am b/test/ui/Makefile.am
new file mode 100644
index 0000000..19c3637
--- /dev/null
+++ b/test/ui/Makefile.am
@@ -0,0 +1,35 @@
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ui_TESTS = \
+ test/ui/console-sequence
+
+TESTS += $(ui_TESTS)
+check_PROGRAMS += $(ui_TESTS)
+
+test_ui_console_sequence_SOURCES = \
+ test/ui/console-sequence.c
+
+test_ui_console_sequence_CPPFLAGS = \
+ -DPETITBOOT_TEST \
+ -I$(top_srcdir)/lib
+
+test_ui_console_sequence_LDFLAGS = \
+ $(core_lib)
+
+
+
+#$(ui_TESTS): LIBS += \
+# ui/ncurses/libpbnc.la \
+# $(core_lib) \
+# @MENU_LIB@ @FORM_LIB@ @CURSES_LIB@
diff --git a/test/ui/console-sequence.c b/test/ui/console-sequence.c
new file mode 100644
index 0000000..88e0faf
--- /dev/null
+++ b/test/ui/console-sequence.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2017 IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "talloc/talloc.h"
+
+#define ERR (-1)
+#define pb_log(...) printf(__VA_ARGS__)
+#define pb_debug(...) printf(__VA_ARGS__)
+
+/*
+ * Several example terminal commands, see:
+ * https://vt100.net/docs/vt100-ug/chapter3.html
+ */
+static char identify_rsp[] = {033, 0133, 077, 061, 073, 060, 0143, '\0'};
+static char decdwl[] = {033, 043, 066, '\0'};
+static char attrs[] = {033, 0133, 060, 073, 064, 073, 065, 0155, '\0'};
+static char cursor[] = {033, 070, '\0'};
+static char conf_test[] = {033, 0133, 062, 073, 061, 0171, '\0'};
+static char status[] = {033, 0133, 060, 0156, '\0'};
+static char erase_screen[] = {033, 0133, 062, 0112, '\0'};
+static char status_ok_rsp[] = {033, 0133, 064, 0156, '\0'};
+static char garbage[] = {001, 002, 003, 004, '\0'};
+static char esc_garbage[] = {033, 002, 003, 004, '\0'};
+static char *ptr;
+
+static signed char getch(void)
+{
+ if (!ptr || *ptr == '\0')
+ return -ERR;
+ return *ptr++;
+}
+
+#include "ui/ncurses/console-codes.c"
+
+int main(void)
+{
+ void *ctx;
+ char *seq, *confused;
+
+ ctx = talloc_new(NULL);
+
+ ptr = &identify_rsp[1];
+ printf("Identity response\n");
+ seq = handle_control_sequence(ctx, identify_rsp[0]);
+ assert(strncmp(seq, identify_rsp, strlen(identify_rsp)) == 0);
+
+ printf("DECDWL\n");
+ ptr = &decdwl[1];
+ seq = handle_control_sequence(ctx, decdwl[0]);
+ assert(strncmp(seq, decdwl, strlen(decdwl)) == 0);
+
+ printf("Attributes\n");
+ ptr = &attrs[1];
+ seq = handle_control_sequence(ctx, attrs[0]);
+ assert(strncmp(seq, attrs, strlen(attrs)) == 0);
+
+ printf("Reset Cursor\n");
+ ptr = &cursor[1];
+ seq = handle_control_sequence(ctx, cursor[0]);
+ assert(strncmp(seq, cursor, strlen(cursor)) == 0);
+
+ printf("Confidence Test\n");
+ ptr = &conf_test[1];
+ seq = handle_control_sequence(ctx, conf_test[0]);
+ assert(strncmp(seq, conf_test, strlen(conf_test)) == 0);
+
+ printf("Status\n");
+ ptr = &status[1];
+ seq = handle_control_sequence(ctx, status[0]);
+ assert(strncmp(seq, status, strlen(status)) == 0);
+
+ printf("Erase Screen\n");
+ ptr = &erase_screen[1];
+ seq = handle_control_sequence(ctx, erase_screen[0]);
+ assert(strncmp(seq, erase_screen, strlen(erase_screen)) == 0);
+
+ printf("Status Response\n");
+ ptr = &status_ok_rsp[1];
+ seq = handle_control_sequence(ctx, status_ok_rsp[0]);
+ assert(strncmp(seq, status_ok_rsp, strlen(status_ok_rsp)) == 0);
+
+ printf("Garbage\n");
+ ptr = &garbage[1];
+ seq = handle_control_sequence(ctx, garbage[0]);
+ assert(seq == NULL);
+
+ printf("ESC then Garbage\n");
+ ptr = &esc_garbage[1];
+ confused = talloc_asprintf(ctx, "%c%c...", 033, 002);
+ seq = handle_control_sequence(ctx, esc_garbage[0]);
+ assert(strncmp(seq, confused, strlen(confused)) == 0);
+
+ talloc_free(ctx);
+ return EXIT_SUCCESS;
+}
OpenPOWER on IntegriCloud