From c55bfb0045094c38a158d22666bea78682df1716 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Mon, 15 Dec 2014 23:44:31 +0000 Subject: [PATCH] tests/uid.awk: rewrite in a more maintainable style Since the test is virtually a FSM, rewrite it as a FSM. --- tests/uid.awk | 103 +++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 65 deletions(-) diff --git a/tests/uid.awk b/tests/uid.awk index 7c431aa7..d5e350c2 100644 --- a/tests/uid.awk +++ b/tests/uid.awk @@ -1,51 +1,48 @@ BEGIN { - ok = 0 + r_uint = "(0|[1-9][0-9]*)" + regexp = "^getuid" suffix "\\(\\)[[:space:]]+= " r_uint "$" + expected = "getuid" fail = 0 - r_uid = "(0|[1-9][0-9]*)" - r_getuid = "^getuid" suffix "\\(\\)[[:space:]]+= " r_uid "$" - r_setuid = "^/$" - r_getresuid = "^/$" - r_setreuid = "^/$" - r_setresuid = "^/$" - r_chown = "^/$" - s_last = "/" } -ok == 1 { +regexp == "" { fail = 1 next } -$0 == s_last { - ok = 1 - next -} - { - if (match($0, r_getuid, a)) { - r_uid = a[1] - r_setuid = "^setuid" suffix "\\(" r_uid "\\)[[:space:]]+= 0$" - next - } - if (match($0, r_setuid)) { - r_getresuid = "^getresuid" suffix "\\(\\[" r_uid "\\], \\[" r_uid "\\], \\[" r_uid "\\]\\)[[:space:]]+= 0$" - next - } - if (match($0, r_getresuid)) { - r_setreuid = "^setreuid" suffix "\\(-1, -1\\)[[:space:]]+= 0$" - next - } - if (match($0, r_setreuid)) { - r_setresuid = "^setresuid" suffix "\\(-1, " r_uid ", -1\\)[[:space:]]+= 0$" - next - } - if (match($0, r_setresuid)) { - r_chown = "^chown" suffix "\\(\".\", -1, -1\\)[[:space:]]+= 0$" - next - } - if (match($0, r_chown)) { - s_last = "+++ exited with 0 +++" - next + if (match($0, regexp, a)) { + switch (expected) { + case "getuid": + uid = a[1] + expected = "setuid" + regexp = "^setuid" suffix "\\(" uid "\\)[[:space:]]+= 0$" + next + case "setuid": + expected = "getresuid" + regexp = "^getresuid" suffix "\\(\\[" uid "\\], \\[" uid "\\], \\[" uid "\\]\\)[[:space:]]+= 0$" + next + case "getresuid": + expected = "setreuid" + regexp = "^setreuid" suffix "\\(-1, -1\\)[[:space:]]+= 0$" + next + case "setreuid": + expected = "setresuid" + regexp = "^setresuid" suffix "\\(-1, " uid ", -1\\)[[:space:]]+= 0$" + next + case "setresuid": + expected = "chown" + regexp = "^chown" suffix "\\(\".\", -1, -1\\)[[:space:]]+= 0$" + next + case "chown": + expected = "the last line" + regexp = "^\\+\\+\\+ exited with 0 \\+\\+\\+$" + next + case "the last line": + expected = "nothing" + regexp = "" + next + } } next } @@ -55,32 +52,8 @@ END { print "Unexpected output after exit" exit 1 } - if (ok) + if (regexp == "") exit 0 - if (r_setuid == "^/$") { - print "getuid doesn't match" - exit 1 - } - if (r_getresuid == "^/$") { - print "setuid doesn't match" - exit 1 - } - if (r_setreuid == "^/$") { - print "getresuid doesn't match" - exit 1 - } - if (r_setresuid == "^/$") { - print "setreuid doesn't match" - exit 1 - } - if (r_chown == "^/$") { - print "setresuid doesn't match" - exit 1 - } - if (s_last == "/") { - print "chown doesn't match" - exit 1 - } - print "The last line doesn't match" + print "error: " expected " doesn't match" exit 1 } -- 2.40.0