| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 13 авг. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/ipc/Process.h> |
| 24 |
#include <lsp-plug.in/ipc/Thread.h> |
| 25 |
#include <lsp-plug.in/runtime/system.h> |
| 26 |
|
| 27 |
using namespace lsp; |
| 28 |
|
| 29 |
#define ENV_VAR_NAME "LSP_TEST_ENV_VAR" |
| 30 |
|
| 31 |
UTEST_BEGIN("runtime.ipc", process) |
| 32 |
UTEST_TIMELIMIT(10) |
| 33 |
|
| 34 |
void test_envs(ipc::Process &p) |
| 35 |
{ |
| 36 |
size_t envs = p.envs(); |
| 37 |
UTEST_ASSERT(envs > 0); |
| 38 |
LSPString key, value; |
| 39 |
|
| 40 |
for (size_t i=0; i<envs; ++i) |
| 41 |
{ |
| 42 |
UTEST_ASSERT(p.read_env(i, &key, &value) == STATUS_OK); |
| 43 |
printf(" env: %s = %s\n", key.get_native(), value.get_native()); |
| 44 |
} |
| 45 |
|
| 46 |
UTEST_ASSERT(p.get_env(ENV_VAR_NAME, &value) == STATUS_NOT_FOUND); |
| 47 |
UTEST_ASSERT(p.set_env(ENV_VAR_NAME, "123456") == STATUS_OK); |
| 48 |
UTEST_ASSERT(envs == (p.envs() - 1)); |
| 49 |
value.clear(); |
| 50 |
UTEST_ASSERT(p.get_env(ENV_VAR_NAME, &value) == STATUS_OK); |
| 51 |
printf("get_env(%s) = %s\n", ENV_VAR_NAME, value.get_native()); |
| 52 |
UTEST_ASSERT(value.equals_ascii("123456")); |
| 53 |
value.clear(); |
| 54 |
UTEST_ASSERT(p.remove_env(ENV_VAR_NAME, &value) == STATUS_OK); |
| 55 |
UTEST_ASSERT(value.equals_ascii("123456")); |
| 56 |
UTEST_ASSERT(envs == p.envs()); |
| 57 |
UTEST_ASSERT(p.clear_env() == STATUS_OK); |
| 58 |
UTEST_ASSERT(p.envs() == 0); |
| 59 |
UTEST_ASSERT(p.copy_env() == STATUS_OK); |
| 60 |
UTEST_ASSERT(p.envs() == envs); |
| 61 |
} |
| 62 |
|
| 63 |
void test_args(ipc::Process &p) |
| 64 |
{ |
| 65 |
UTEST_ASSERT(p.args() == 0); |
| 66 |
LSPString tmp; |
| 67 |
char *ctmp = NULL; |
| 68 |
|
| 69 |
UTEST_ASSERT(p.add_arg("arg1") == STATUS_OK); |
| 70 |
UTEST_ASSERT(p.add_arg("arg4") == STATUS_OK); |
| 71 |
UTEST_ASSERT(tmp.set_ascii("arg3")); |
| 72 |
UTEST_ASSERT(p.add_arg(&tmp) == STATUS_OK); |
| 73 |
UTEST_ASSERT(p.insert_arg(1, "arg2") == STATUS_OK); |
| 74 |
UTEST_ASSERT(p.args() == 4); |
| 75 |
UTEST_ASSERT(p.remove_arg(2, &tmp) == STATUS_OK); |
| 76 |
UTEST_ASSERT(tmp.equals_ascii("arg4")); |
| 77 |
UTEST_ASSERT(p.args() == 3); |
| 78 |
|
| 79 |
UTEST_ASSERT(p.get_arg(0, &ctmp) == STATUS_OK); |
| 80 |
UTEST_ASSERT(strcmp(ctmp, "arg1") == 0); |
| 81 |
::free(ctmp); |
| 82 |
|
| 83 |
UTEST_ASSERT(p.get_arg(1, &ctmp) == STATUS_OK); |
| 84 |
UTEST_ASSERT(strcmp(ctmp, "arg2") == 0); |
| 85 |
::free(ctmp); |
| 86 |
|
| 87 |
tmp.clear(); |
| 88 |
UTEST_ASSERT(p.get_arg(2, &tmp) == STATUS_OK); |
| 89 |
UTEST_ASSERT(tmp.equals_ascii("arg3")); |
| 90 |
|
| 91 |
UTEST_ASSERT(p.clear_args() == STATUS_OK); |
| 92 |
UTEST_ASSERT(p.args() == 0); |
| 93 |
} |
| 94 |
|
| 95 |
UTEST_MAIN |
| 96 |
{ |
| 97 |
if (argc <= 0) |
| 98 |
{ |
| 99 |
ipc::Process p; |
| 100 |
test_envs(p); |
| 101 |
test_args(p); |
| 102 |
|
| 103 |
LSPString cmd; |
| 104 |
#ifdef PLATFORM_WINDOWS |
| 105 |
UTEST_ASSERT(cmd.set_utf8(executable())); |
| 106 |
#else |
| 107 |
UTEST_ASSERT(cmd.set_native(executable())); |
| 108 |
#endif |
| 109 |
|
| 110 |
// Form the command line |
| 111 |
p.set_command(&cmd); |
| 112 |
p.add_arg("utest"); |
| 113 |
p.add_arg("--debug"); |
| 114 |
p.add_arg("--verbose"); |
| 115 |
p.add_arg("--nofork"); |
| 116 |
p.add_arg(full_name()); |
| 117 |
p.add_arg("--args"); |
| 118 |
p.add_arg("child"); |
| 119 |
p.add_arg("arg1"); |
| 120 |
p.add_arg("arg2"); |
| 121 |
|
| 122 |
// Set environment |
| 123 |
p.set_env(ENV_VAR_NAME, "test_value"); |
| 124 |
|
| 125 |
// Redirect stdout and stderr |
| 126 |
io::IInStream *xstdout = p.get_stdout(); |
| 127 |
UTEST_ASSERT(xstdout != NULL); |
| 128 |
io::IInStream *xstderr = p.get_stderr(); |
| 129 |
UTEST_ASSERT(xstderr != NULL); |
| 130 |
|
| 131 |
// Launch the process |
| 132 |
printf("Starting child process...\n"); |
| 133 |
UTEST_ASSERT(p.process_id() < 0); |
| 134 |
UTEST_ASSERT(p.launch() == STATUS_OK); |
| 135 |
printf("Started child process, pid=%d\n", int(p.process_id())); |
| 136 |
UTEST_ASSERT(p.wait(0) == STATUS_OK); // Test process status |
| 137 |
UTEST_ASSERT(p.wait(200) == STATUS_OK); // Wait for a short while |
| 138 |
UTEST_ASSERT(p.status() == ipc::Process::PSTATUS_RUNNING); // Check status |
| 139 |
printf("Waiting for process...\n"); |
| 140 |
UTEST_ASSERT(p.wait() == STATUS_OK); // Wait until termination |
| 141 |
|
| 142 |
// Close stdout and stderr |
| 143 |
UTEST_ASSERT(xstdout->close() == STATUS_OK); |
| 144 |
UTEST_ASSERT(xstderr->close() == STATUS_OK); |
| 145 |
|
| 146 |
// Analyze exit status |
| 147 |
int code = 0; |
| 148 |
UTEST_ASSERT(p.exit_code(&code) == STATUS_OK); |
| 149 |
printf("Waited process has terminated with code=%d\n", int(code)); |
| 150 |
UTEST_ASSERT(code == 0); |
| 151 |
|
| 152 |
printf("Parent process has exited\n"); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
printf("Child process started\n"); |
| 157 |
|
| 158 |
// Perform argument check |
| 159 |
UTEST_ASSERT(argc == 3); |
| 160 |
UTEST_ASSERT(strcmp(argv[0], "child") == 0); |
| 161 |
UTEST_ASSERT(strcmp(argv[1], "arg1") == 0); |
| 162 |
UTEST_ASSERT(strcmp(argv[2], "arg2") == 0); |
| 163 |
|
| 164 |
LSPString value; |
| 165 |
UTEST_ASSERT(system::get_env_var(ENV_VAR_NAME, &value) == STATUS_OK); |
| 166 |
UTEST_ASSERT(value.equals_ascii("test_value")); |
| 167 |
|
| 168 |
// Perform a sleep for synchronization with caller |
| 169 |
printf("Entering sleep\n"); |
| 170 |
ipc::Thread::sleep(2 * 1000); |
| 171 |
printf("Child process has exited\n"); |
| 172 |
} |
| 173 |
} |
| 174 |
UTEST_END; |
| 175 |
|
| 176 |
|