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 "tests_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef _WIN32
#include <dirent.h>
@@ -43,10 +43,11 @@
#define chdir _chdir
#endif
#include "torture.h"
#include "torture_key.h"
#include "libssh/libssh.h"
#include "libssh/misc.h"
#include "libssh/token.h"
#include "torture.h"
#include "torture_key.h"
#ifdef HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
@@ -83,8 +84,8 @@ static const char *pattern = NULL;
#ifndef _WIN32
/* TODO missing code coverage */
static int _torture_auth_kbdint(ssh_session session,
const char *password) {
static int _torture_auth_kbdint(ssh_session session, const char *password)
{
const char *prompt;
char echo;
int err;
@@ -121,7 +122,8 @@ static int _torture_auth_kbdint(ssh_session session,
return err;
}
int torture_rmdirs(const char *path) {
int torture_rmdirs(const char *path)
{
DIR *d;
struct dirent *dp;
struct stat sb;
@@ -188,7 +190,8 @@ int torture_rmdirs(const char *path) {
return 0;
}
int torture_isdir(const char *path) {
int torture_isdir(const char *path)
{
struct stat sb;
if (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
@@ -198,8 +201,7 @@ int torture_isdir(const char *path) {
return 0;
}
static pid_t
torture_read_pidfile(const char *pidfile)
static pid_t torture_read_pidfile(const char *pidfile)
{
char buf[8] = {0};
long int tmp;
@@ -256,7 +258,8 @@ int torture_terminate_process(const char *pidfile)
usleep(25 * 1000);
#ifdef HAVE_VALGRIND_VALGRIND_H
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.");
usleep(1000 * 1000);
}
@@ -275,7 +278,8 @@ int torture_terminate_process(const char *pidfile)
if (is_running) {
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;
@@ -285,7 +289,8 @@ ssh_session torture_ssh_session(struct torture_state *s,
const char *host,
const unsigned int *port,
const char *user,
const char *password) {
const char *password)
{
ssh_session session;
int method;
int rc;
@@ -327,8 +332,8 @@ ssh_session torture_ssh_session(struct torture_state *s,
}
}
if (ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG,
&process_config) < 0) {
if (ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config) <
0) {
goto failed;
}
@@ -379,7 +384,8 @@ failed:
ssh_bind torture_ssh_bind(const char *addr,
const unsigned int port,
enum ssh_keytypes_e key_type,
const char *private_key_file) {
const char *private_key_file)
{
int rc;
ssh_bind sshbind = NULL;
enum ssh_bind_options_e opts = -1;
@@ -433,7 +439,8 @@ ssh_bind torture_ssh_bind(const char *addr,
#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;
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);
}
void torture_sftp_close(struct torture_sftp *t) {
void torture_sftp_close(struct torture_sftp *t)
{
if (t == NULL) {
return;
}
@@ -716,10 +724,7 @@ void torture_setup_create_libssh_config(void **state)
assert_non_null(s->socket_dir);
snprintf(sshd_path,
sizeof(sshd_path),
"%s/sshd",
s->socket_dir);
snprintf(sshd_path, sizeof(sshd_path), "%s/sshd", s->socket_dir);
rc = lstat(sshd_path, &sb);
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));
}
additional_config = (s->srv_additional_config != NULL ?
s->srv_additional_config : "");
additional_config =
(s->srv_additional_config != NULL ? s->srv_additional_config : "");
if (ssh_fips_mode()) {
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
"CASignatureAlgorithms " OPENSSH_KEYS "\n"
#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"
#endif
"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" /* The space for test-specific options */
"\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"
#endif
"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_IDENTIFICATION LC_ALL LC_LIBSSH\n"
"\n"
"PidFile %s\n"; /* PID file */
const char usepam_yes[] =
"UsePAM yes\n"
"PidFile %s\n";
const char usepam_yes[] = "UsePAM yes\n"
"KbdInteractiveAuthentication yes\n";
const char usepam_no[] =
"UsePAM no\n"
const char usepam_no[] = "UsePAM no\n"
"KbdInteractiveAuthentication no\n";
size_t sftp_sl_size = ARRAY_SIZE(sftp_server_locations);
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);
additional_config = (s->srv_additional_config != NULL ?
s->srv_additional_config : "");
additional_config =
(s->srv_additional_config != NULL ? s->srv_additional_config : "");
if (ssh_fips_mode()) {
snprintf(sshd_config,
@@ -1030,8 +1035,7 @@ int torture_wait_for_daemon(unsigned int seconds)
return 1;
}
void
torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
void torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
{
int rc;
rc = snprintf(env,
@@ -1050,8 +1054,7 @@ torture_set_kdc_env_str(const char *gss_dir, char *env, size_t size)
}
}
void
torture_set_env_from_str(const char *env)
void torture_set_env_from_str(const char *env)
{
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");
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);
if (printed < 0 || printed >= (ssize_t)sizeof(extra_options)) {
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
snprintf(timeout_cmd, sizeof(timeout_cmd),
"%s %s ", TIMEOUT_EXECUTABLE, "5m");
snprintf(timeout_cmd,
sizeof(timeout_cmd),
"%s %s ",
TIMEOUT_EXECUTABLE,
"5m");
#else
timeout_cmd[0] = '\0';
#endif
/* Write the start command */
printed = snprintf(start_cmd, sizeof(start_cmd),
printed = snprintf(start_cmd,
sizeof(start_cmd),
"%s"
"%s -f%s -v4 -p22 -i%s -C%s%s%s%s%s",
timeout_cmd,
server_path, s->pcap_file, s->srv_pidfile,
server_path,
s->pcap_file,
s->srv_pidfile,
s->srv_config,
s->log_file ? " -l " : "", s->log_file ? s->log_file : "",
extra_options, TORTURE_SSH_SERVER);
s->log_file ? " -l " : "",
s->log_file ? s->log_file : "",
extra_options,
TORTURE_SSH_SERVER);
if (printed < 0 || printed >= (ssize_t)sizeof(start_cmd)) {
fail_msg("Failed to print start command!");
/* Unreachable */
@@ -1206,7 +1219,8 @@ void torture_setup_libssh_server(void **state, const char *server_path)
__builtin_unreachable();
}
execve(arg_tokens->tokens[0], (char **)arg_tokens->tokens,
execve(arg_tokens->tokens[0],
(char **)arg_tokens->tokens,
(char **)env_tokens->tokens);
/* 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
*
*/
void
torture_setup_kdc_server(void **state,
void torture_setup_kdc_server(void **state,
const char *kadmin_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
* structure
*/
void
torture_teardown_kdc_server(void **state)
void torture_teardown_kdc_server(void **state)
{
struct torture_state *s = *state;
int rc;
@@ -1411,7 +1423,9 @@ void torture_teardown_socket_dir(void **state)
int rc;
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 {
rc = torture_rmdirs(s->socket_dir);
if (rc < 0) {
@@ -1430,8 +1444,7 @@ void torture_teardown_socket_dir(void **state)
torture_free_state(s);
}
static int
torture_reload_sshd_server(void **state)
static int torture_reload_sshd_server(void **state)
{
struct torture_state *s = *state;
int rc;
@@ -1447,8 +1460,7 @@ torture_reload_sshd_server(void **state)
* Note, that this still uses the default configuration options specified
* in this file and overwrites options previously specified by this function.
*/
int
torture_update_sshd_config(void **state, const char *config)
int torture_update_sshd_config(void **state, const char *config)
{
struct torture_state *s = *state;
int rc;
@@ -1468,7 +1480,6 @@ torture_update_sshd_config(void **state, const char *config)
return SSH_OK;
}
void torture_teardown_sshd_server(void **state)
{
struct torture_state *s = *state;
@@ -1712,14 +1723,12 @@ static int recursive_rm_dir_content(const char *path)
/* Empty directory */
if (last_error == ERROR_FILE_NOT_FOUND) {
rc = 0;
}
else {
} else {
/*TODO print error message?*/
rc = last_error;
}
goto end;
}
else {
} else {
do {
rc = strcmp(file_data.cFileName, ".");
if (rc == 0) {
@@ -1753,8 +1762,7 @@ static int recursive_rm_dir_content(const char *path)
rc = last_error;
goto end;
}
}
else {
} else {
rc = remove(file_path);
if (rc) {
goto end;
@@ -1888,7 +1896,8 @@ end:
return rc;
}
int torture_libssh_verbosity(void){
int torture_libssh_verbosity(void)
{
return verbosity;
}
@@ -1900,7 +1909,8 @@ void _torture_filter_tests(struct CMUnitTest *tests, size_t ntests)
return;
}
void torture_write_file(const char *filename, const char *data){
void torture_write_file(const char *filename, const char *data)
{
int fd;
int rc;
@@ -1961,7 +1971,8 @@ __attribute__((weak)) int torture_run_tests(void)
}
#endif /* defined(HAVE_WEAK_ATTRIBUTE) && defined(TORTURE_SHARED) */
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
struct argument_s arguments;
char *env = getenv("LIBSSH_VERBOSITY");

View File

@@ -24,11 +24,11 @@
#ifndef _TORTURE_H
#define _TORTURE_H
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include "libssh/priv.h"
#include "libssh/server.h"
@@ -36,13 +36,12 @@
#include <cmocka.h>
#include "torture_cmocka.h"
#include "tests_config.h"
#include "torture_cmocka.h"
#ifndef assert_return_code
/* hack for older versions of cmocka */
#define assert_return_code(code, errno) \
assert_true(code >= 0)
#define assert_return_code(code, errno) assert_true(code >= 0)
#endif /* assert_return_code */
#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);
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_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);
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_unsetenv(char const *variable);
int torture_setup_ssh_agent(struct torture_state *s, const char *add_key);
int torture_cleanup_ssh_agent(void);