test: improve spawn_setuid_setgid test
Check that the child process' uid and gid are correctly set. PR-URL: https://github.com/libuv/libuv/pull/1024 Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
6938f6b0bd
commit
4a71e7747a
@ -56,6 +56,7 @@ int main(int argc, char **argv) {
|
||||
case 1: return run_tests(0);
|
||||
case 2: return maybe_run_test(argc, argv);
|
||||
case 3: return run_test_part(argv[1], argv[2]);
|
||||
case 4: return maybe_run_test(argc, argv);
|
||||
default:
|
||||
fprintf(stderr, "Too many arguments.\n");
|
||||
fflush(stderr);
|
||||
@ -177,5 +178,17 @@ static int maybe_run_test(int argc, char **argv) {
|
||||
return spawn_stdin_stdout();
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
if (strcmp(argv[1], "spawn_helper_setuid_setgid") == 0) {
|
||||
uv_uid_t uid = atoi(argv[2]);
|
||||
uv_gid_t gid = atoi(argv[3]);
|
||||
|
||||
ASSERT(uid == getuid());
|
||||
ASSERT(gid == getgid());
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif /* !_WIN32 */
|
||||
|
||||
return run_test(argv[1], 0, 1);
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ static uv_timer_t timer;
|
||||
static uv_process_options_t options;
|
||||
static char exepath[1024];
|
||||
static size_t exepath_size = 1024;
|
||||
static char* args[3];
|
||||
static char* args[5];
|
||||
static int no_term_signal;
|
||||
static int timer_counter;
|
||||
|
||||
@ -147,6 +147,8 @@ static void init_process_options(char* test, uv_exit_cb exit_cb) {
|
||||
args[0] = exepath;
|
||||
args[1] = test;
|
||||
args[2] = NULL;
|
||||
args[3] = NULL;
|
||||
args[4] = NULL;
|
||||
options.file = exepath;
|
||||
options.args = args;
|
||||
options.exit_cb = exit_cb;
|
||||
@ -1226,6 +1228,8 @@ TEST_IMPL(spawn_with_an_odd_path) {
|
||||
TEST_IMPL(spawn_setuid_setgid) {
|
||||
int r;
|
||||
struct passwd* pw;
|
||||
char uidstr[10];
|
||||
char gidstr[10];
|
||||
|
||||
/* if not root, then this will fail. */
|
||||
uv_uid_t uid = getuid();
|
||||
@ -1233,13 +1237,17 @@ TEST_IMPL(spawn_setuid_setgid) {
|
||||
RETURN_SKIP("It should be run as root user");
|
||||
}
|
||||
|
||||
init_process_options("spawn_helper1", exit_cb);
|
||||
init_process_options("spawn_helper_setuid_setgid", exit_cb);
|
||||
|
||||
/* become the "nobody" user. */
|
||||
pw = getpwnam("nobody");
|
||||
ASSERT(pw != NULL);
|
||||
options.uid = pw->pw_uid;
|
||||
options.gid = pw->pw_gid;
|
||||
snprintf(uidstr, sizeof(uidstr), "%d", pw->pw_uid);
|
||||
snprintf(gidstr, sizeof(gidstr), "%d", pw->pw_gid);
|
||||
options.args[2] = uidstr;
|
||||
options.args[3] = gidstr;
|
||||
options.flags = UV_PROCESS_SETUID | UV_PROCESS_SETGID;
|
||||
|
||||
r = uv_spawn(uv_default_loop(), &process, &options);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user