Skip to content

Commit

Permalink
[libnozzle] add internal execute_bin_sh_command test
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 15, 2018
1 parent 4e00890 commit c0aa392
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 97 deletions.
5 changes: 5 additions & 0 deletions libnozzle/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ api_checks = \
api_nozzle_get_fd_test

int_checks = \
int_execute_bin_sh_command_test \
nozzle_test

fun_checks =
Expand Down Expand Up @@ -100,6 +101,10 @@ api_nozzle_get_name_by_handle_test_SOURCES = api_nozzle_get_name_by_handle.c \
api_nozzle_get_fd_test_SOURCES = api_nozzle_get_fd.c \
test-common.c

int_execute_bin_sh_command_test_SOURCES = int_execute_bin_sh_command.c \
test-common.c \
../internals.c

nozzle_test_SOURCES = nozzle_test.c \
test-common.c \
../internals.c
Expand Down
124 changes: 124 additions & 0 deletions libnozzle/tests/int_execute_bin_sh_command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
*
* Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
*
* This software licensed under GPL-2.0+, LGPL-2.0+
*/

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#include <sys/socket.h>

#include "test-common.h"

static int test(void)
{
int err = 0;
char command[4096];
char *error_string = NULL;

memset(command, 0, sizeof(command));

printf("Testing execute_bin_sh_command\n");

printf("command true\n");

err = execute_bin_sh_command("true", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (err) {
printf("Unable to execute true ?!?!\n");
goto out_clean;
}

printf("command false\n");

err = execute_bin_sh_command("false", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute false successfully?!?!\n");
err = -1;
goto out_clean;
}

printf("command that outputs to stdout (enforcing redirect)\n");

err = execute_bin_sh_command("grep -h 2>&1", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute grep -h successfully?!?\n");
err = -1;
goto out_clean;
}

printf("command that outputs to stderr\n");
err = execute_bin_sh_command("grep -h", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute grep -h successfully?!?\n");
err = -1;
goto out_clean;
}

printf("Testing ERROR conditions\n");

printf("empty command\n");
err = execute_bin_sh_command(NULL, &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if ((!err) || (errno != EINVAL)) {
printf("execute_bin_sh_command returned incorrect error or incorrect errno!\n");
err = -1;
goto out_clean;
}

printf("empty error\n");
err = execute_bin_sh_command("true", NULL);
if ((!err) || (errno != EINVAL)) {
printf("execute_bin_sh_command returned incorrect error or incorrect errno!\n");
err = -1;
goto out_clean;
}

err = 0;

out_clean:

return err;
}

int main(void)
{
need_root();

if (test() < 0)
return FAIL;

return PASS;
}
97 changes: 0 additions & 97 deletions libnozzle/tests/nozzle_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,100 +24,6 @@ char testipv4_2[IPBUFSIZE];
char testipv6_1[IPBUFSIZE];
char testipv6_2[IPBUFSIZE];

static int check_nozzle_execute_bin_sh_command(void)
{
int err = 0;
char command[4096];
char *error_string = NULL;

memset(command, 0, sizeof(command));

printf("Testing execute_bin_sh_command\n");

printf("command true\n");

err = execute_bin_sh_command("true", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (err) {
printf("Unable to execute true ?!?!\n");
goto out_clean;
}

printf("Testing ERROR conditions\n");

printf("command false\n");

err = execute_bin_sh_command("false", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute false successfully?!?!\n");
err = -1;
goto out_clean;
}

printf("command that outputs to stdout (enforcing redirect)\n");

err = execute_bin_sh_command("grep -h 2>&1", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute grep -h successfully?!?\n");
err = -1;
goto out_clean;
}

printf("command that outputs to stderr\n");
err = execute_bin_sh_command("grep -h", &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute grep -h successfully?!?\n");
err = -1;
goto out_clean;
}

printf("empty command\n");
err = execute_bin_sh_command(NULL, &error_string);
if (error_string) {
printf("Error string: %s\n", error_string);
free(error_string);
error_string = NULL;
}
if (!err) {
printf("Can we really execute (nil) successfully?!?!\n");
err = -1;
goto out_clean;
}

printf("empty error\n");
err = execute_bin_sh_command("true", NULL);
if (!err) {
printf("Check EINVAL filter for no error_string!\n");
err = -1;
goto out_clean;
}

err = 0;

out_clean:

return err;
}

static int check_knet_up_down(void)
{
char verifycmd[1024];
Expand Down Expand Up @@ -562,9 +468,6 @@ int main(void)

make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2);

if (check_nozzle_execute_bin_sh_command() < 0)
return -1;

if (check_knet_up_down() < 0)
return -1;

Expand Down

0 comments on commit c0aa392

Please sign in to comment.