From a77ef93f8ae42c1c2a603f21fd13c68301baeda6 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 7 Apr 2020 14:03:58 -0600 Subject: [PATCH] Use regular expressions when matching expected and actual text. --- plugins/python/regress/iohelpers.h | 1 + ..._example_group_plugin_is_able_to_debug.log | 2 +- ...check_example_io_plugin_command_log.stored | 4 +- ...ple_io_plugin_command_log_multiple1.stored | 4 +- ...ple_io_plugin_command_log_multiple2.stored | 4 +- ...e_io_plugin_failed_to_start_command.stored | 4 +- ..._plugin_fails_with_python_backtrace.stderr | 2 +- .../check_loading_fails_wrong_path.stderr | 2 +- ...tiple_approval_plugin_and_arguments.stdout | 40 +++++++++---------- ...on_plugins_do_not_affect_each_other.stdout | 8 ++-- plugins/python/regress/testhelpers.h | 25 ++++++++++-- 11 files changed, 57 insertions(+), 39 deletions(-) diff --git a/plugins/python/regress/iohelpers.h b/plugins/python/regress/iohelpers.h index 88879f7eb..96ec4b21a 100644 --- a/plugins/python/regress/iohelpers.h +++ b/plugins/python/regress/iohelpers.h @@ -22,6 +22,7 @@ #include "config.h" #include "sudo_compat.h" +#include #include #include #include diff --git a/plugins/python/regress/testdata/check_example_group_plugin_is_able_to_debug.log b/plugins/python/regress/testdata/check_example_group_plugin_is_able_to_debug.log index 255409f46..f7f0081e0 100644 --- a/plugins/python/regress/testdata/check_example_group_plugin_is_able_to_debug.log +++ b/plugins/python/regress/testdata/check_example_group_plugin_is_able_to_debug.log @@ -1,4 +1,4 @@ -SudoGroupPlugin.__init__ was called with arguments: () {'args': ('ModulePath=SRC_DIR/example_group_plugin.py', 'ClassName=SudoGroupPlugin'), 'version': '1.0'} +.* SudoGroupPlugin.__init__ returned result: SudoGroupPlugin.query was called with arguments: ('user', 'group', ('pw_name', 'pw_passwd', 1001, 101, 'pw_gecos', 'pw_dir', 'pw_shell')) SudoGroupPlugin.query returned result: RC.REJECT diff --git a/plugins/python/regress/testdata/check_example_io_plugin_command_log.stored b/plugins/python/regress/testdata/check_example_io_plugin_command_log.stored index 73fdc5d75..01a09bc45 100644 --- a/plugins/python/regress/testdata/check_example_io_plugin_command_log.stored +++ b/plugins/python/regress/testdata/check_example_io_plugin_command_log.stored @@ -1,9 +1,9 @@ -- Plugin STARTED -- EXEC id --help -EXEC info [ +EXEC info \[ "command=/bin/id", "runas_uid=0" -] +\] STD IN some standard input STD OUT some standard output STD ERR some standard error diff --git a/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple1.stored b/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple1.stored index bc60c38bd..981e3edb4 100644 --- a/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple1.stored +++ b/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple1.stored @@ -1,9 +1,9 @@ -- Plugin STARTED -- EXEC id --help -EXEC info [ +EXEC info \[ "command=/bin/id", "runas_uid=0" -] +\] STD IN stdin for plugin 1 STD OUT stdout for plugin 1 STD ERR stderr for plugin 1 diff --git a/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple2.stored b/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple2.stored index ed3fdc845..c47b237c7 100644 --- a/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple2.stored +++ b/plugins/python/regress/testdata/check_example_io_plugin_command_log_multiple2.stored @@ -1,9 +1,9 @@ -- Plugin STARTED -- EXEC whoami -EXEC info [ +EXEC info \[ "command=/bin/whoami", "runas_uid=1" -] +\] STD IN stdin for plugin 2 STD OUT stdout for plugin 2 STD ERR stderr for plugin 2 diff --git a/plugins/python/regress/testdata/check_example_io_plugin_failed_to_start_command.stored b/plugins/python/regress/testdata/check_example_io_plugin_failed_to_start_command.stored index 1b99398be..86c276d6e 100644 --- a/plugins/python/regress/testdata/check_example_io_plugin_failed_to_start_command.stored +++ b/plugins/python/regress/testdata/check_example_io_plugin_failed_to_start_command.stored @@ -1,8 +1,8 @@ -- Plugin STARTED -- EXEC cmd -EXEC info [ +EXEC info \[ "command=/usr/share/cmd", "runas_uid=0" -] +\] CLOSE Failed to execute, execve returned 1 (EPERM) -- Plugin DESTROYED -- diff --git a/plugins/python/regress/testdata/check_example_io_plugin_fails_with_python_backtrace.stderr b/plugins/python/regress/testdata/check_example_io_plugin_fails_with_python_backtrace.stderr index 97226385c..8f3c4482a 100644 --- a/plugins/python/regress/testdata/check_example_io_plugin_fails_with_python_backtrace.stderr +++ b/plugins/python/regress/testdata/check_example_io_plugin_fails_with_python_backtrace.stderr @@ -1 +1 @@ -Failed to construct plugin instance: (FileNotFoundError) [Errno 2] No such file or directory: '/some/not/writable/directory/sudo.log' +Failed to construct plugin instance: (FileNotFoundError) \[Errno 2\] No such file or directory: '/some/not/writable/directory/sudo.log' diff --git a/plugins/python/regress/testdata/check_loading_fails_wrong_path.stderr b/plugins/python/regress/testdata/check_loading_fails_wrong_path.stderr index 64a3b2897..265f391f8 100644 --- a/plugins/python/regress/testdata/check_loading_fails_wrong_path.stderr +++ b/plugins/python/regress/testdata/check_loading_fails_wrong_path.stderr @@ -1 +1 @@ -Failed during loading plugin class: (ModuleNotFoundError) No module named 'wrong_path' +Failed during loading plugin class: ([^)]*) No module named 'wrong_path' diff --git a/plugins/python/regress/testdata/check_multiple_approval_plugin_and_arguments.stdout b/plugins/python/regress/testdata/check_multiple_approval_plugin_and_arguments.stdout index 1ba0dcafe..64fc6b260 100644 --- a/plugins/python/regress/testdata/check_multiple_approval_plugin_and_arguments.stdout +++ b/plugins/python/regress/testdata/check_multiple_approval_plugin_and_arguments.stdout @@ -1,61 +1,61 @@ (APPROVAL 1) Constructed: { - "plugin_options": [ + "plugin_options": \[ "ModulePath=SRC_DIR/regress/plugin_approval_test.py", "ClassName=ApprovalTestPlugin", "Id=1" - ], + \], "version": "1.15", - "settings": [ + "settings": \[ "SETTING1=VALUE1", "setting2=value2" - ], - "user_env": [ + \], + "user_env": \[ "USER_ENV1=VALUE1", "USER_ENV2=value2" - ], - "user_info": [ + \], + "user_info": \[ "INFO1=VALUE1", "info2=value2" - ], + \], "submit_optind": 3, - "submit_argv": [ + "submit_argv": \[ "sudo", "-u", "user", "whoami", "--help" - ], + \], "_id": "(APPROVAL 1)" } (APPROVAL 2) Constructed: { - "plugin_options": [ + "plugin_options": \[ "ModulePath=SRC_DIR/regress/plugin_approval_test.py", "ClassName=ApprovalTestPlugin", "Id=2" - ], + \], "version": "1.15", - "settings": [ + "settings": \[ "SETTING1=VALUE1", "setting2=value2" - ], - "user_env": [ + \], + "user_env": \[ "USER_ENV1=VALUE1", "USER_ENV2=value2" - ], - "user_info": [ + \], + "user_info": \[ "INFO1=VALUE1", "info2=value2" - ], + \], "submit_optind": 3, - "submit_argv": [ + "submit_argv": \[ "sudo", "-u", "user", "whoami", "--help" - ], + \], "_id": "(APPROVAL 2)" } (APPROVAL 1) Show version was called with arguments: (0,) diff --git a/plugins/python/regress/testdata/check_python_plugins_do_not_affect_each_other.stdout b/plugins/python/regress/testdata/check_python_plugins_do_not_affect_each_other.stdout index cd5bef997..2fd129c5b 100644 --- a/plugins/python/regress/testdata/check_python_plugins_do_not_affect_each_other.stdout +++ b/plugins/python/regress/testdata/check_python_plugins_do_not_affect_each_other.stdout @@ -1,4 +1,4 @@ -PATH before: [] (should be empty) -PATH set: ['path_for_first_plugin'] -PATH before: [] (should be empty) -PATH set: ['path_for_second_plugin'] +PATH before: \[\] (should be empty) +PATH set: \['path_for_first_plugin'\] +PATH before: \[\] (should be empty) +PATH set: \['path_for_second_plugin'\] diff --git a/plugins/python/regress/testhelpers.h b/plugins/python/regress/testhelpers.h index 70cccb625..b354280de 100644 --- a/plugins/python/regress/testhelpers.h +++ b/plugins/python/regress/testhelpers.h @@ -111,10 +111,27 @@ char ** create_str_array(size_t count, ...); #define VERIFY_STR(actual, expected) \ do { \ const char *actual_str = actual; \ - if (!actual_str || strcmp(actual_str, expected) != 0) { \ - VERIFY_PRINT_MSG("%s", #actual, actual_str ? actual_str : "(null)", #expected, expected, "expected to be"); \ - return false; \ - } \ + regex_t regex; \ + int result = 0; \ + if (!actual_str) { \ + result = -1; \ + } else if (*expected == '\0') { \ + result = strcmp(actual_str, expected); \ + } else { \ + if ((result = regcomp(®ex, expected, REG_NOSUB)) != 0) { \ + char errbuf[1024]; \ + regerror(result, ®ex, errbuf, sizeof(errbuf)); \ + fprintf(stderr, "regcomp failed at %s:%d: %s\npattern: %s\n", \ + __FILE__, __LINE__, errbuf, expected); \ + } else { \ + result = regexec(®ex, actual_str, 0, NULL, 0); \ + regfree(®ex); \ + } \ + } \ + if (result != 0) { \ + VERIFY_PRINT_MSG("%s", #actual, actual_str ? actual_str : "(null)", #expected, expected, "expected to be"); \ + return false; \ + } \ } while(false) #define VERIFY_STR_CONTAINS(actual, expected) \