Reformatted torture.c and torture.h

Signed-off-by: salonidabgar <salonidabgar@gmail.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
salonidabgar
2025-04-17 22:48:54 +05:30
committed by Jakub Jelen
parent 3a4ba8b763
commit efc5bc633f
2 changed files with 147 additions and 134 deletions

View File

@@ -23,12 +23,12 @@
#include "config.h" #include "config.h"
#include "tests_config.h" #include "tests_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef _WIN32 #ifndef _WIN32
#include <dirent.h> #include <dirent.h>
@@ -43,10 +43,11 @@
#define chdir _chdir #define chdir _chdir
#endif #endif
#include "torture.h" #include "libssh/libssh.h"
#include "torture_key.h"
#include "libssh/misc.h" #include "libssh/misc.h"
#include "libssh/token.h" #include "libssh/token.h"
#include "torture.h"
#include "torture_key.h"
#ifdef HAVE_VALGRIND_VALGRIND_H #ifdef HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h> #include <valgrind/valgrind.h>
@@ -83,8 +84,8 @@ static const char *pattern = NULL;
#ifndef _WIN32 #ifndef _WIN32
/* TODO missing code coverage */ /* TODO missing code coverage */
static int _torture_auth_kbdint(ssh_session session, static int _torture_auth_kbdint(ssh_session session, const char *password)
const char *password) { {
const char *prompt; const char *prompt;
char echo; char echo;
int err; int err;
@@ -121,7 +122,8 @@ static int _torture_auth_kbdint(ssh_session session,
return err; return err;
} }
int torture_rmdirs(const char *path) { int torture_rmdirs(const char *path)
{
DIR *d; DIR *d;
struct dirent *dp; struct dirent *dp;
struct stat sb; struct stat sb;
@@ -188,7 +190,8 @@ int torture_rmdirs(const char *path) {
return 0; return 0;
} }
int torture_isdir(const char *path) { int torture_isdir(const char *path)
{
struct stat sb; struct stat sb;
if (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { if (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
@@ -198,8 +201,7 @@ int torture_isdir(const char *path) {
return 0; return 0;
} }
static pid_t static pid_t torture_read_pidfile(const char *pidfile)
torture_read_pidfile(const char *pidfile)
{ {
char buf[8] = {0}; char buf[8] = {0};
long int tmp; long int tmp;
@@ -256,7 +258,8 @@ int torture_terminate_process(const char *pidfile)
usleep(25 * 1000); usleep(25 * 1000);
#ifdef HAVE_VALGRIND_VALGRIND_H #ifdef HAVE_VALGRIND_VALGRIND_H
if (RUNNING_ON_VALGRIND) { if (RUNNING_ON_VALGRIND) {
SSH_LOG(SSH_LOG_INFO, "Running within Valgrind, wait one more " SSH_LOG(SSH_LOG_INFO,
"Running within Valgrind, wait one more "
"second for the server to clean up."); "second for the server to clean up.");
usleep(1000 * 1000); usleep(1000 * 1000);
} }
@@ -275,7 +278,8 @@ int torture_terminate_process(const char *pidfile)
if (is_running) { if (is_running) {
fprintf(stderr, fprintf(stderr,
"WARNING: The process with pid %u is still running!\n", pid); "WARNING: The process with pid %u is still running!\n",
pid);
} }
return rc; return rc;
@@ -285,7 +289,8 @@ ssh_session torture_ssh_session(struct torture_state *s,
const char *host, const char *host,
const unsigned int *port, const unsigned int *port,
const char *user, const char *user,
const char *password) { const char *password)
{
ssh_session session; ssh_session session;
int method; int method;
int rc; int rc;
@@ -327,8 +332,8 @@ ssh_session torture_ssh_session(struct torture_state *s,
} }
} }
if (ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, if (ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config) <
&process_config) < 0) { 0) {
goto failed; goto failed;
} }
@@ -379,7 +384,8 @@ failed:
ssh_bind torture_ssh_bind(const char *addr, ssh_bind torture_ssh_bind(const char *addr,
const unsigned int port, const unsigned int port,
enum ssh_keytypes_e key_type, enum ssh_keytypes_e key_type,
const char *private_key_file) { const char *private_key_file)
{
int rc; int rc;
ssh_bind sshbind = NULL; ssh_bind sshbind = NULL;
enum ssh_bind_options_e opts = -1; enum ssh_bind_options_e opts = -1;
@@ -433,7 +439,8 @@ ssh_bind torture_ssh_bind(const char *addr,
#ifdef WITH_SFTP #ifdef WITH_SFTP
struct torture_sftp *torture_sftp_session_channel(ssh_session session, ssh_channel channel) struct torture_sftp *torture_sftp_session_channel(ssh_session session,
ssh_channel channel)
{ {
struct torture_sftp *t; struct torture_sftp *t;
char template[] = "/tmp/ssh_torture_XXXXXX"; char template[] = "/tmp/ssh_torture_XXXXXX";
@@ -505,7 +512,8 @@ struct torture_sftp *torture_sftp_session(ssh_session session)
return torture_sftp_session_channel(session, NULL); return torture_sftp_session_channel(session, NULL);
} }
void torture_sftp_close(struct torture_sftp *t) { void torture_sftp_close(struct torture_sftp *t)
{
if (t == NULL) { if (t == NULL) {
return; return;
} }
@@ -716,10 +724,7 @@ void torture_setup_create_libssh_config(void **state)
assert_non_null(s->socket_dir); assert_non_null(s->socket_dir);
snprintf(sshd_path, snprintf(sshd_path, sizeof(sshd_path), "%s/sshd", s->socket_dir);
sizeof(sshd_path),
"%s/sshd",
s->socket_dir);
rc = lstat(sshd_path, &sb); rc = lstat(sshd_path, &sb);
if (rc == 0) { /* The directory is already in place */ if (rc == 0) { /* The directory is already in place */
@@ -755,8 +760,8 @@ void torture_setup_create_libssh_config(void **state)
torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0)); torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0));
} }
additional_config = (s->srv_additional_config != NULL ? additional_config =
s->srv_additional_config : ""); (s->srv_additional_config != NULL ? s->srv_additional_config : "");
if (ssh_fips_mode()) { if (ssh_fips_mode()) {
snprintf(sshd_config, snprintf(sshd_config,
@@ -824,7 +829,8 @@ torture_setup_create_sshd_config(void **state, bool pam, bool second_sshd)
#if OPENSSH_VERSION_MAJOR == 8 && OPENSSH_VERSION_MINOR >= 2 #if OPENSSH_VERSION_MAJOR == 8 && OPENSSH_VERSION_MINOR >= 2
"CASignatureAlgorithms " OPENSSH_KEYS "\n" "CASignatureAlgorithms " OPENSSH_KEYS "\n"
#endif #endif
#if (OPENSSH_VERSION_MAJOR == 9 && OPENSSH_VERSION_MINOR >= 8) || OPENSSH_VERSION_MAJOR > 9 #if (OPENSSH_VERSION_MAJOR == 9 && OPENSSH_VERSION_MINOR >= 8) || \
OPENSSH_VERSION_MAJOR > 9
"PerSourcePenaltyExemptList 127.0.0.21\n" "PerSourcePenaltyExemptList 127.0.0.21\n"
#endif #endif
"Ciphers " OPENSSH_CIPHERS "\n" "Ciphers " OPENSSH_CIPHERS "\n"
@@ -858,7 +864,8 @@ torture_setup_create_sshd_config(void **state, bool pam, bool second_sshd)
"%s\n" /* Here comes UsePam */ "%s\n" /* Here comes UsePam */
"%s" /* The space for test-specific options */ "%s" /* The space for test-specific options */
"\n" "\n"
#if (OPENSSH_VERSION_MAJOR == 9 && OPENSSH_VERSION_MINOR >= 8) || OPENSSH_VERSION_MAJOR > 9 #if (OPENSSH_VERSION_MAJOR == 9 && OPENSSH_VERSION_MINOR >= 8) || \
OPENSSH_VERSION_MAJOR > 9
"PerSourcePenaltyExemptList 127.0.0.21\n" "PerSourcePenaltyExemptList 127.0.0.21\n"
#endif #endif
"Ciphers " "Ciphers "
@@ -889,12 +896,10 @@ torture_setup_create_sshd_config(void **state, bool pam, bool second_sshd)
"AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT\n" "AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT\n"
"AcceptEnv LC_IDENTIFICATION LC_ALL LC_LIBSSH\n" "AcceptEnv LC_IDENTIFICATION LC_ALL LC_LIBSSH\n"
"\n" "\n"
"PidFile %s\n"; /* PID file */ "PidFile %s\n";
const char usepam_yes[] = const char usepam_yes[] = "UsePAM yes\n"
"UsePAM yes\n"
"KbdInteractiveAuthentication yes\n"; "KbdInteractiveAuthentication yes\n";
const char usepam_no[] = const char usepam_no[] = "UsePAM no\n"
"UsePAM no\n"
"KbdInteractiveAuthentication no\n"; "KbdInteractiveAuthentication no\n";
size_t sftp_sl_size = ARRAY_SIZE(sftp_server_locations); size_t sftp_sl_size = ARRAY_SIZE(sftp_server_locations);
const char *sftp_server, *usepam; const char *sftp_server, *usepam;
@@ -973,8 +978,8 @@ torture_setup_create_sshd_config(void **state, bool pam, bool second_sshd)
} }
assert_non_null(sftp_server); assert_non_null(sftp_server);
additional_config = (s->srv_additional_config != NULL ? additional_config =
s->srv_additional_config : ""); (s->srv_additional_config != NULL ? s->srv_additional_config : "");
if (ssh_fips_mode()) { if (ssh_fips_mode()) {
snprintf(sshd_config, snprintf(sshd_config,
@@ -1030,8 +1035,7 @@ int torture_wait_for_daemon(unsigned int seconds)
return 1; return 1;
} }
void void torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
{ {
int rc; int rc;
rc = snprintf(env, rc = snprintf(env,
@@ -1050,8 +1054,7 @@ torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
} }
} }
void void torture_set_env_from_str(const char *env)
torture_set_env_from_str(const char *env)
{ {
struct ssh_tokens_st *vars = NULL, *var = NULL; struct ssh_tokens_st *vars = NULL, *var = NULL;
@@ -1121,7 +1124,9 @@ void torture_setup_libssh_server(void **state, const char *server_path)
ld_preload = getenv("LD_PRELOAD"); ld_preload = getenv("LD_PRELOAD");
if (s->srv_additional_config != NULL) { if (s->srv_additional_config != NULL) {
printed = snprintf(extra_options, sizeof(extra_options), " %s ", printed = snprintf(extra_options,
sizeof(extra_options),
" %s ",
s->srv_additional_config); s->srv_additional_config);
if (printed < 0 || printed >= (ssize_t)sizeof(extra_options)) { if (printed < 0 || printed >= (ssize_t)sizeof(extra_options)) {
fail_msg("Failed to print additional config!"); fail_msg("Failed to print additional config!");
@@ -1167,21 +1172,29 @@ void torture_setup_libssh_server(void **state, const char *server_path)
} }
#ifdef WITH_TIMEOUT #ifdef WITH_TIMEOUT
snprintf(timeout_cmd, sizeof(timeout_cmd), snprintf(timeout_cmd,
"%s %s ", TIMEOUT_EXECUTABLE, "5m"); sizeof(timeout_cmd),
"%s %s ",
TIMEOUT_EXECUTABLE,
"5m");
#else #else
timeout_cmd[0] = '\0'; timeout_cmd[0] = '\0';
#endif #endif
/* Write the start command */ /* Write the start command */
printed = snprintf(start_cmd, sizeof(start_cmd), printed = snprintf(start_cmd,
sizeof(start_cmd),
"%s" "%s"
"%s -f%s -v4 -p22 -i%s -C%s%s%s%s%s", "%s -f%s -v4 -p22 -i%s -C%s%s%s%s%s",
timeout_cmd, timeout_cmd,
server_path, s->pcap_file, s->srv_pidfile, server_path,
s->pcap_file,
s->srv_pidfile,
s->srv_config, s->srv_config,
s->log_file ? " -l " : "", s->log_file ? s->log_file : "", s->log_file ? " -l " : "",
extra_options, TORTURE_SSH_SERVER); s->log_file ? s->log_file : "",
extra_options,
TORTURE_SSH_SERVER);
if (printed < 0 || printed >= (ssize_t)sizeof(start_cmd)) { if (printed < 0 || printed >= (ssize_t)sizeof(start_cmd)) {
fail_msg("Failed to print start command!"); fail_msg("Failed to print start command!");
/* Unreachable */ /* Unreachable */
@@ -1206,7 +1219,8 @@ void torture_setup_libssh_server(void **state, const char *server_path)
__builtin_unreachable(); __builtin_unreachable();
} }
execve(arg_tokens->tokens[0], (char **)arg_tokens->tokens, execve(arg_tokens->tokens[0],
(char **)arg_tokens->tokens,
(char **)env_tokens->tokens); (char **)env_tokens->tokens);
/* execve returns only in case of error */ /* execve returns only in case of error */
@@ -1313,8 +1327,7 @@ void torture_setup_sshd_servers(void **state, bool pam)
* @param[in] kinit_script kinit commands to get the TGT * @param[in] kinit_script kinit commands to get the TGT
* *
*/ */
void void torture_setup_kdc_server(void **state,
torture_setup_kdc_server(void **state,
const char *kadmin_script, const char *kadmin_script,
const char *kinit_script) const char *kinit_script)
{ {
@@ -1371,8 +1384,7 @@ torture_setup_kdc_server(void **state,
* @param[in] state A pointer to a pointer to an initialized torture_state * @param[in] state A pointer to a pointer to an initialized torture_state
* structure * structure
*/ */
void void torture_teardown_kdc_server(void **state)
torture_teardown_kdc_server(void **state)
{ {
struct torture_state *s = *state; struct torture_state *s = *state;
int rc; int rc;
@@ -1411,7 +1423,9 @@ void torture_teardown_socket_dir(void **state)
int rc; int rc;
if (env != NULL && env[0] == '1') { if (env != NULL && env[0] == '1') {
fprintf(stderr, "[ TORTURE ] >>> Skipping cleanup of %s\n", s->socket_dir); fprintf(stderr,
"[ TORTURE ] >>> Skipping cleanup of %s\n",
s->socket_dir);
} else { } else {
rc = torture_rmdirs(s->socket_dir); rc = torture_rmdirs(s->socket_dir);
if (rc < 0) { if (rc < 0) {
@@ -1430,8 +1444,7 @@ void torture_teardown_socket_dir(void **state)
torture_free_state(s); torture_free_state(s);
} }
static int static int torture_reload_sshd_server(void **state)
torture_reload_sshd_server(void **state)
{ {
struct torture_state *s = *state; struct torture_state *s = *state;
int rc; int rc;
@@ -1447,8 +1460,7 @@ torture_reload_sshd_server(void **state)
* Note, that this still uses the default configuration options specified * Note, that this still uses the default configuration options specified
* in this file and overwrites options previously specified by this function. * in this file and overwrites options previously specified by this function.
*/ */
int int torture_update_sshd_config(void **state, const char *config)
torture_update_sshd_config(void **state, const char *config)
{ {
struct torture_state *s = *state; struct torture_state *s = *state;
int rc; int rc;
@@ -1468,7 +1480,6 @@ torture_update_sshd_config(void **state, const char *config)
return SSH_OK; return SSH_OK;
} }
void torture_teardown_sshd_server(void **state) void torture_teardown_sshd_server(void **state)
{ {
struct torture_state *s = *state; struct torture_state *s = *state;
@@ -1712,14 +1723,12 @@ static int recursive_rm_dir_content(const char *path)
/* Empty directory */ /* Empty directory */
if (last_error == ERROR_FILE_NOT_FOUND) { if (last_error == ERROR_FILE_NOT_FOUND) {
rc = 0; rc = 0;
} } else {
else {
/*TODO print error message?*/ /*TODO print error message?*/
rc = last_error; rc = last_error;
} }
goto end; goto end;
} } else {
else {
do { do {
rc = strcmp(file_data.cFileName, "."); rc = strcmp(file_data.cFileName, ".");
if (rc == 0) { if (rc == 0) {
@@ -1753,8 +1762,7 @@ static int recursive_rm_dir_content(const char *path)
rc = last_error; rc = last_error;
goto end; goto end;
} }
} } else {
else {
rc = remove(file_path); rc = remove(file_path);
if (rc) { if (rc) {
goto end; goto end;
@@ -1888,7 +1896,8 @@ end:
return rc; return rc;
} }
int torture_libssh_verbosity(void){ int torture_libssh_verbosity(void)
{
return verbosity; return verbosity;
} }
@@ -1900,7 +1909,8 @@ void _torture_filter_tests(struct CMUnitTest *tests, size_t ntests)
return; return;
} }
void torture_write_file(const char *filename, const char *data){ void torture_write_file(const char *filename, const char *data)
{
int fd; int fd;
int rc; int rc;
@@ -1961,7 +1971,8 @@ __attribute__((weak)) int torture_run_tests(void)
} }
#endif /* defined(HAVE_WEAK_ATTRIBUTE) && defined(TORTURE_SHARED) */ #endif /* defined(HAVE_WEAK_ATTRIBUTE) && defined(TORTURE_SHARED) */
int main(int argc, char **argv) { int main(int argc, char **argv)
{
struct argument_s arguments; struct argument_s arguments;
char *env = getenv("LIBSSH_VERBOSITY"); char *env = getenv("LIBSSH_VERBOSITY");

View File

@@ -24,11 +24,11 @@
#ifndef _TORTURE_H #ifndef _TORTURE_H
#define _TORTURE_H #define _TORTURE_H
#include <stdio.h> #include <setjmp.h>
#include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <setjmp.h> #include <stdio.h>
#include <stdlib.h>
#include "libssh/priv.h" #include "libssh/priv.h"
#include "libssh/server.h" #include "libssh/server.h"
@@ -36,13 +36,12 @@
#include <cmocka.h> #include <cmocka.h>
#include "torture_cmocka.h"
#include "tests_config.h" #include "tests_config.h"
#include "torture_cmocka.h"
#ifndef assert_return_code #ifndef assert_return_code
/* hack for older versions of cmocka */ /* hack for older versions of cmocka */
#define assert_return_code(code, errno) \ #define assert_return_code(code, errno) assert_true(code >= 0)
assert_true(code >= 0)
#endif /* assert_return_code */ #endif /* assert_return_code */
#define TORTURE_SSH_SERVER "127.0.0.10" #define TORTURE_SSH_SERVER "127.0.0.10"
@@ -114,12 +113,14 @@ ssh_bind torture_ssh_bind(const char *addr,
const char *private_key_file); const char *private_key_file);
struct torture_sftp *torture_sftp_session(ssh_session session); struct torture_sftp *torture_sftp_session(ssh_session session);
struct torture_sftp *torture_sftp_session_channel(ssh_session session, ssh_channel channel); struct torture_sftp *torture_sftp_session_channel(ssh_session session,
ssh_channel channel);
void torture_sftp_close(struct torture_sftp *t); void torture_sftp_close(struct torture_sftp *t);
void torture_write_file(const char *filename, const char *data); void torture_write_file(const char *filename, const char *data);
#define torture_filter_tests(tests) _torture_filter_tests(tests, sizeof(tests) / sizeof(tests)[0]) #define torture_filter_tests(tests) \
_torture_filter_tests(tests, sizeof(tests) / sizeof(tests)[0])
void _torture_filter_tests(struct CMUnitTest *tests, size_t ntests); void _torture_filter_tests(struct CMUnitTest *tests, size_t ntests);
const char *torture_server_address(int domain); const char *torture_server_address(int domain);
@@ -182,6 +183,7 @@ int torture_change_dir(char *path);
void torture_setenv(char const *variable, char const *value); void torture_setenv(char const *variable, char const *value);
void torture_unsetenv(char const *variable); void torture_unsetenv(char const *variable);
int torture_setup_ssh_agent(struct torture_state *s, const char *add_key); int torture_setup_ssh_agent(struct torture_state *s, const char *add_key);
int torture_cleanup_ssh_agent(void); int torture_cleanup_ssh_agent(void);