diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-06-20 15:10:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-20 15:10:56 -0400 |
commit | 931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c (patch) | |
tree | 5dbcccffd509fa71a99c351ed4628ed0841e1e46 /src/system.c | |
parent | 11aa1d44d95da221073e512fbec3bbccc0f1a46b (diff) | |
download | stasis-931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c.tar.gz |
Unit tests (#6)
* Initial commit of unit tests [WIP]
* Address shortcomings and bugs flushed out by unit tests
* Enable unit testing in CI workflow
* Enable verbose ctests
* Handle lack of __FILE_NAME__ define
* Only podman support `run --arch` argument
* Skip docker build testing if CI system cannot pull an image
* Remove errant call to puts()
* Identify local repo user
* Fix missing xmllint
* NULL terminate arrays
* Fix filename assignment in is_url mode
* Break loop when expected lines are exhausted
* strcmp_array expects NULL terminated array. Iterating by size in this case passes NULL to strcmp leading to an invalid read
* Remove debug printf statements
* Disable a few warnings for tests
* Workaround for ctest junit xml truncation
* Update checkout@v4
* Prevent false-positive result
* Return zero on error
* Fix strlist_remove function
* Value argument can be constant
* Fix test to match changes to startswith and endswith
* Add test_ini.c
* Fix redaction code to accept NULL pointers in array
* And let the caller specify the length of the array of strings to redact.
* Redactions now occur directly on authentication strings rather than their command line arguments
* Fix BUILD_TESTING_DEBUG
* Adds missing -D argument
Diffstat (limited to 'src/system.c')
-rw-r--r-- | src/system.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/system.c b/src/system.c index ca2da97..d5e77ce 100644 --- a/src/system.c +++ b/src/system.c @@ -1,11 +1,8 @@ -// -// Created by jhunk on 10/4/23. -// - #include "system.h" #include "omc.h" int shell(struct Process *proc, char *args) { + struct Process selfproc; FILE *fp_out = NULL; FILE *fp_err = NULL; pid_t pid; @@ -13,6 +10,18 @@ int shell(struct Process *proc, char *args) { status = 0; errno = 0; + if (!proc) { + // provide our own proc structure + // albeit not accessible to the user + memset(&selfproc, 0, sizeof(selfproc)); + proc = &selfproc; + } + + if (!args) { + proc->returncode = -1; + return -1; + } + FILE *tp = NULL; char *t_name; t_name = xmkstemp(&tp, "w"); @@ -31,22 +40,20 @@ int shell(struct Process *proc, char *args) { exit(1); } else if (pid == 0) { int retval; - if (proc != NULL) { - if (strlen(proc->f_stdout)) { - fp_out = freopen(proc->f_stdout, "w+", stdout); - } + if (strlen(proc->f_stdout)) { + fp_out = freopen(proc->f_stdout, "w+", stdout); + } - if (strlen(proc->f_stderr)) { - fp_err = freopen(proc->f_stderr, "w+", stderr); - } + if (strlen(proc->f_stderr)) { + fp_err = freopen(proc->f_stderr, "w+", stderr); + } - if (proc->redirect_stderr) { - if (fp_err) { - fclose(fp_err); - fclose(stderr); - } - dup2(fileno(stdout), fileno(stderr)); + if (proc->redirect_stderr) { + if (fp_err) { + fclose(fp_err); + fclose(stderr); } + dup2(fileno(stdout), fileno(stderr)); } retval = execl("/bin/bash", "bash", "-c", t_name, (char *) NULL); @@ -54,7 +61,7 @@ int shell(struct Process *proc, char *args) { remove(t_name); } - if (proc != NULL && strlen(proc->f_stdout)) { + if (strlen(proc->f_stdout)) { if (fp_out != NULL) { fflush(fp_out); fclose(fp_out); @@ -62,7 +69,7 @@ int shell(struct Process *proc, char *args) { fflush(stdout); fclose(stdout); } - if (proc != NULL && strlen(proc->f_stderr)) { + if (strlen(proc->f_stderr)) { if (fp_err) { fflush(fp_err); fclose(fp_err); @@ -89,10 +96,7 @@ int shell(struct Process *proc, char *args) { remove(t_name); } - if (proc != NULL) { - proc->returncode = status; - } - + proc->returncode = status; guard_free(t_name); return WEXITSTATUS(status); } @@ -102,7 +106,7 @@ int shell_safe(struct Process *proc, char *args) { char buf[1024] = {0}; int result; - char *invalid_ch = strpbrk(args, ";&|()"); + char *invalid_ch = strpbrk(args, OMC_SHELL_SAFE_RESTRICT); if (invalid_ch) { args = NULL; } @@ -168,6 +172,6 @@ char *shell_output(const char *command, int *status) { strcat(result, line); memset(line, 0, sizeof(line)); } - pclose(pp); + *status = pclose(pp); return result; }
\ No newline at end of file |