tests/server: round of tidy-ups

Dedupe, merge macros, globals, make symbols local where possible.
Drop unused macros and headers. Drop `DEFAULT_LOGFILE` macro in favour
of `--logfile` command-line option.

Ref: #15000
Closes #16525
This commit is contained in:
Viktor Szakats 2024-09-08 16:18:57 +02:00
parent 387311012c
commit 9213e4e497
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
9 changed files with 66 additions and 137 deletions

View File

@ -64,17 +64,8 @@
/* include memdebug.h last */ /* include memdebug.h last */
#include "memdebug.h" #include "memdebug.h"
#ifdef USE_WINSOCK
#undef EINTR
#define EINTR 4 /* errno.h value */
#endif
#define DEFAULT_PORT 1883 /* MQTT default port */ #define DEFAULT_PORT 1883 /* MQTT default port */
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/mqttd.log"
#endif
#ifndef DEFAULT_CONFIG #ifndef DEFAULT_CONFIG
#define DEFAULT_CONFIG "mqttd.config" #define DEFAULT_CONFIG "mqttd.config"
#endif #endif
@ -102,7 +93,6 @@ struct configurable {
static struct configurable config; static struct configurable config;
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *configfile = DEFAULT_CONFIG; static const char *configfile = DEFAULT_CONFIG;
static const char *logdir = "log"; static const char *logdir = "log";
static char loglockfile[256]; static char loglockfile[256];
@ -933,6 +923,8 @@ int main(int argc, char *argv[])
int error; int error;
int arg = 1; int arg = 1;
serverlogfile = "log/mqttd.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
printf("mqttd IPv4%s\n", printf("mqttd IPv4%s\n",

View File

@ -53,8 +53,6 @@
static bool use_ipv6 = FALSE; static bool use_ipv6 = FALSE;
static const char *ipv_inuse = "IPv4"; static const char *ipv_inuse = "IPv4";
const char *serverlogfile = ""; /* for a util.c function we don't use */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int arg = 1; int arg = 1;

View File

@ -56,13 +56,6 @@
/* include memdebug.h last */ /* include memdebug.h last */
#include "memdebug.h" #include "memdebug.h"
#ifdef USE_WINSOCK
#undef EINTR
#define EINTR 4 /* errno.h value */
#undef ERANGE
#define ERANGE 34 /* errno.h value */
#endif
#ifdef USE_IPV6 #ifdef USE_IPV6
static bool use_ipv6 = FALSE; static bool use_ipv6 = FALSE;
#endif #endif
@ -124,11 +117,6 @@ static void storerequest(char *reqbuf, size_t totalsize);
#define DEFAULT_PORT 8999 #define DEFAULT_PORT 8999
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/rtspd.log"
#endif
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *logdir = "log"; static const char *logdir = "log";
static char loglockfile[256]; static char loglockfile[256];
@ -155,16 +143,6 @@ static char loglockfile[256];
#define END_OF_HEADERS "\r\n\r\n" #define END_OF_HEADERS "\r\n\r\n"
enum {
DOCNUMBER_NOTHING = -7,
DOCNUMBER_QUIT = -6,
DOCNUMBER_BADCONNECT = -5,
DOCNUMBER_INTERNAL = -4,
DOCNUMBER_CONNECT = -3,
DOCNUMBER_WERULEZ = -2,
DOCNUMBER_404 = -1
};
/* sent as reply to a QUIT */ /* sent as reply to a QUIT */
static const char *docquit = static const char *docquit =
@ -1063,6 +1041,8 @@ int main(int argc, char *argv[])
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
serverlogfile = "log/rtspd.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
printf("rtspd IPv4%s" printf("rtspd IPv4%s"

View File

@ -114,29 +114,12 @@
/* include memdebug.h last */ /* include memdebug.h last */
#include "memdebug.h" #include "memdebug.h"
#ifdef USE_WINSOCK
#undef EINTR
#define EINTR 4 /* errno.h value */
#undef EAGAIN
#define EAGAIN 11 /* errno.h value */
#undef ENOMEM
#define ENOMEM 12 /* errno.h value */
#undef EINVAL
#define EINVAL 22 /* errno.h value */
#endif
#define DEFAULT_PORT 8999 #define DEFAULT_PORT 8999
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/sockfilt.log"
#endif
/* buffer is this excessively large only to be able to support things like /* buffer is this excessively large only to be able to support things like
test 1003 which tests exceedingly large server response lines */ test 1003 which tests exceedingly large server response lines */
#define BUFFER_SIZE 17010 #define BUFFER_SIZE 17010
const char *serverlogfile = DEFAULT_LOGFILE;
static bool verbose = FALSE; static bool verbose = FALSE;
static bool bind_only = FALSE; static bool bind_only = FALSE;
#ifdef USE_IPV6 #ifdef USE_IPV6
@ -1406,6 +1389,8 @@ int main(int argc, char *argv[])
enum sockmode mode = PASSIVE_LISTEN; /* default */ enum sockmode mode = PASSIVE_LISTEN; /* default */
const char *addr = NULL; const char *addr = NULL;
serverlogfile = "log/sockfilt.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
printf("sockfilt IPv4%s\n", printf("sockfilt IPv4%s\n",

View File

@ -85,17 +85,8 @@
/* include memdebug.h last */ /* include memdebug.h last */
#include "memdebug.h" #include "memdebug.h"
#ifdef USE_WINSOCK
#undef EINTR
#define EINTR 4 /* errno.h value */
#endif
#define DEFAULT_PORT 8905 #define DEFAULT_PORT 8905
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/socksd.log"
#endif
#ifndef DEFAULT_REQFILE #ifndef DEFAULT_REQFILE
#define DEFAULT_REQFILE "log/socksd-request.log" #define DEFAULT_REQFILE "log/socksd-request.log"
#endif #endif
@ -134,7 +125,6 @@ struct configurable {
static struct configurable config; static struct configurable config;
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *reqlogfile = DEFAULT_REQFILE; static const char *reqlogfile = DEFAULT_REQFILE;
static const char *configfile = DEFAULT_CONFIG; static const char *configfile = DEFAULT_CONFIG;
@ -979,6 +969,8 @@ int main(int argc, char *argv[])
bool unlink_socket = false; bool unlink_socket = false;
#endif #endif
serverlogfile = "log/socksd.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
printf("socksd IPv4%s\n", printf("socksd IPv4%s\n",

View File

@ -58,15 +58,6 @@
/* include memdebug.h last */ /* include memdebug.h last */
#include "memdebug.h" #include "memdebug.h"
#ifdef USE_WINSOCK
#undef EINTR
#define EINTR 4 /* errno.h value */
#undef EAGAIN
#define EAGAIN 11 /* errno.h value */
#undef ERANGE
#define ERANGE 34 /* errno.h value */
#endif
static int socket_domain = AF_INET; static int socket_domain = AF_INET;
static bool use_gopher = FALSE; static bool use_gopher = FALSE;
static int serverlogslocked = 0; static int serverlogslocked = 0;
@ -131,11 +122,6 @@ static void storerequest(const char *reqbuf, size_t totalsize);
#define DEFAULT_PORT 8999 #define DEFAULT_PORT 8999
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/sws.log"
#endif
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *logdir = "log"; static const char *logdir = "log";
static char loglockfile[256]; static char loglockfile[256];
@ -151,7 +137,7 @@ static char loglockfile[256];
/* file in which additional instructions may be found */ /* file in which additional instructions may be found */
#define DEFAULT_CMDFILE "log/server.cmd" #define DEFAULT_CMDFILE "log/server.cmd"
const char *cmdfile = DEFAULT_CMDFILE; static const char *cmdfile = DEFAULT_CMDFILE;
/* very-big-path support */ /* very-big-path support */
#define MAXDOCNAMELEN 140000 #define MAXDOCNAMELEN 140000
@ -185,13 +171,6 @@ const char *cmdfile = DEFAULT_CMDFILE;
#define END_OF_HEADERS "\r\n\r\n" #define END_OF_HEADERS "\r\n\r\n"
enum {
DOCNUMBER_NOTHING = -4,
DOCNUMBER_QUIT = -3,
DOCNUMBER_WERULEZ = -2,
DOCNUMBER_404 = -1
};
static const char *end_of_headers = END_OF_HEADERS; static const char *end_of_headers = END_OF_HEADERS;
/* sent as reply to a QUIT */ /* sent as reply to a QUIT */
@ -2060,6 +2039,8 @@ int main(int argc, char *argv[])
/* a default CONNECT port is basically pointless but still ... */ /* a default CONNECT port is basically pointless but still ... */
size_t socket_idx; size_t socket_idx;
serverlogfile = "log/sws.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
puts("sws IPv4" puts("sws IPv4"

View File

@ -150,10 +150,6 @@ struct bf {
#undef MIN #undef MIN
#define MIN(x,y) ((x)<(y)?(x):(y)) #define MIN(x,y) ((x)<(y)?(x):(y))
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/tftpd.log"
#endif
#define REQUEST_DUMP "server.input" #define REQUEST_DUMP "server.input"
#define DEFAULT_PORT 8999 /* UDP */ #define DEFAULT_PORT 8999 /* UDP */
@ -205,7 +201,6 @@ static bool use_ipv6 = FALSE;
#endif #endif
static const char *ipv_inuse = "IPv4"; static const char *ipv_inuse = "IPv4";
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *logdir = "log"; static const char *logdir = "log";
static char loglockfile[256]; static char loglockfile[256];
static const char *pidname = ".tftpd.pid"; static const char *pidname = ".tftpd.pid";
@ -564,6 +559,8 @@ int main(int argc, char **argv)
memset(&test, 0, sizeof(test)); memset(&test, 0, sizeof(test));
serverlogfile = "log/tftpd.log";
while(argc > arg) { while(argc > arg) {
if(!strcmp("--version", argv[arg])) { if(!strcmp("--version", argv[arg])) {
printf("tftpd IPv4%s\n", printf("tftpd IPv4%s\n",

View File

@ -54,13 +54,9 @@
#include "getpart.h" #include "getpart.h"
#include "util.h" #include "util.h"
#include "timeval.h" #include "timeval.h"
#include "timediff.h"
#ifdef USE_WINSOCK const char *serverlogfile = NULL; /* needs init from main() */
#undef EINTR
#define EINTR 4 /* errno.h value */
#undef EINVAL
#define EINVAL 22 /* errno.h value */
#endif
static struct timeval tvnow(void); static struct timeval tvnow(void);
@ -143,7 +139,7 @@ void logmsg(const char *msg, ...)
#ifdef _WIN32 #ifdef _WIN32
/* use instead of perror() on generic Windows */ /* use instead of perror() on generic Windows */
void win32_perror(const char *msg) static void win32_perror(const char *msg)
{ {
char buf[512]; char buf[512];
int err = SOCKERRNO; int err = SOCKERRNO;
@ -174,7 +170,7 @@ int win32_init(void)
err = WSAStartup(wVersionRequested, &wsaData); err = WSAStartup(wVersionRequested, &wsaData);
if(err) { if(err) {
perror("Winsock init failed"); win32_perror("Winsock init failed");
logmsg("Error initialising Winsock -- aborting"); logmsg("Error initialising Winsock -- aborting");
return 1; return 1;
} }
@ -182,7 +178,7 @@ int win32_init(void)
if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) || if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) { HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
WSACleanup(); WSACleanup();
perror("Winsock init failed"); win32_perror("Winsock init failed");
logmsg("No suitable winsock.dll found -- aborting"); logmsg("No suitable winsock.dll found -- aborting");
return 1; return 1;
} }
@ -202,23 +198,36 @@ const char *sstrerror(int err)
/* set by the main code to point to where the test dir is */ /* set by the main code to point to where the test dir is */
const char *path = "."; const char *path = ".";
FILE *test2fopen(long testno, const char *logdir) FILE *test2fopen(long testno, const char *logdir2)
{ {
FILE *stream; FILE *stream;
char filename[256]; char filename[256];
/* first try the alternative, preprocessed, file */ /* first try the alternative, preprocessed, file */
msnprintf(filename, sizeof(filename), ALTTEST_DATA_PATH, logdir, testno); msnprintf(filename, sizeof(filename), "%s/test%ld", logdir2, testno);
stream = fopen(filename, "rb"); stream = fopen(filename, "rb");
if(stream) if(stream)
return stream; return stream;
/* then try the source version */ /* then try the source version */
msnprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno); msnprintf(filename, sizeof(filename), "%s/data/test%ld", path, testno);
stream = fopen(filename, "rb"); stream = fopen(filename, "rb");
return stream; return stream;
} }
#if !defined(MSDOS) && !defined(USE_WINSOCK)
static long timediff(struct timeval newer, struct timeval older)
{
timediff_t diff = newer.tv_sec-older.tv_sec;
if(diff >= (LONG_MAX/1000))
return LONG_MAX;
else if(diff <= (LONG_MIN/1000))
return LONG_MIN;
return (long)(newer.tv_sec-older.tv_sec)*1000+
(long)(newer.tv_usec-older.tv_usec)/1000;
}
#endif
/* /*
* Portable function used for waiting a specific amount of ms. * Portable function used for waiting a specific amount of ms.
* Waiting indefinitely with this function is not allowed, a * Waiting indefinitely with this function is not allowed, a
@ -455,17 +464,6 @@ static struct timeval tvnow(void)
#endif #endif
long timediff(struct timeval newer, struct timeval older)
{
timediff_t diff = newer.tv_sec-older.tv_sec;
if(diff >= (LONG_MAX/1000))
return LONG_MAX;
else if(diff <= (LONG_MIN/1000))
return LONG_MIN;
return (long)(newer.tv_sec-older.tv_sec)*1000+
(long)(newer.tv_usec-older.tv_usec)/1000;
}
/* vars used to keep around previous signal handlers */ /* vars used to keep around previous signal handlers */
typedef void (*SIGHANDLER_T)(int); typedef void (*SIGHANDLER_T)(int);
@ -633,7 +631,7 @@ static unsigned int WINAPI main_window_loop(void *lpParameter)
wc.hInstance = (HINSTANCE)lpParameter; wc.hInstance = (HINSTANCE)lpParameter;
wc.lpszClassName = TEXT("MainWClass"); wc.lpszClassName = TEXT("MainWClass");
if(!RegisterClass(&wc)) { if(!RegisterClass(&wc)) {
perror("RegisterClass failed"); win32_perror("RegisterClass failed");
return (DWORD)-1; return (DWORD)-1;
} }
@ -645,14 +643,14 @@ static unsigned int WINAPI main_window_loop(void *lpParameter)
(HWND)NULL, (HMENU)NULL, (HWND)NULL, (HMENU)NULL,
wc.hInstance, (LPVOID)NULL); wc.hInstance, (LPVOID)NULL);
if(!hidden_main_window) { if(!hidden_main_window) {
perror("CreateWindowEx failed"); win32_perror("CreateWindowEx failed");
return (DWORD)-1; return (DWORD)-1;
} }
do { do {
ret = GetMessage(&msg, NULL, 0, 0); ret = GetMessage(&msg, NULL, 0, 0);
if(ret == -1) { if(ret == -1) {
perror("GetMessage failed"); win32_perror("GetMessage failed");
return (DWORD)-1; return (DWORD)-1;
} }
else if(ret) { else if(ret) {

View File

@ -25,12 +25,33 @@
***************************************************************************/ ***************************************************************************/
#include "server_setup.h" #include "server_setup.h"
#ifdef USE_WINSOCK
/* errno.h values */
#undef EINTR
#define EINTR 4
#undef EAGAIN
#define EAGAIN 11
#undef ENOMEM
#define ENOMEM 12
#undef EINVAL
#define EINVAL 22
#undef ERANGE
#define ERANGE 34
#endif
enum {
DOCNUMBER_NOTHING = -7,
DOCNUMBER_QUIT = -6,
DOCNUMBER_BADCONNECT = -5,
DOCNUMBER_INTERNAL = -4,
DOCNUMBER_CONNECT = -3,
DOCNUMBER_WERULEZ = -2,
DOCNUMBER_404 = -1
};
char *data_to_hex(char *data, size_t len); char *data_to_hex(char *data, size_t len);
void logmsg(const char *msg, ...) CURL_PRINTF(1, 2); void logmsg(const char *msg, ...) CURL_PRINTF(1, 2);
long timediff(struct timeval newer, struct timeval older);
#define TEST_DATA_PATH "%s/data/test%ld"
#define ALTTEST_DATA_PATH "%s/test%ld"
#define SERVERLOGS_LOCKDIR "lock" /* within logdir */ #define SERVERLOGS_LOCKDIR "lock" /* within logdir */
/* global variable, where to find the 'data' dir */ /* global variable, where to find the 'data' dir */
@ -39,24 +60,12 @@ extern const char *path;
/* global variable, log file name */ /* global variable, log file name */
extern const char *serverlogfile; extern const char *serverlogfile;
extern const char *cmdfile;
#ifdef _WIN32 #ifdef _WIN32
#include <process.h>
#include <fcntl.h>
#define sleep(sec) Sleep ((sec)*1000)
#undef perror
#define perror(m) win32_perror(m)
void win32_perror(const char *msg);
int win32_init(void); int win32_init(void);
const char *sstrerror(int err); const char *sstrerror(int err);
#else /* _WIN32 */ #else
#define sstrerror(e) strerror(e) #define sstrerror(e) strerror(e)
#endif /* _WIN32 */ #endif
/* fopens the test case file */ /* fopens the test case file */
FILE *test2fopen(long testno, const char *logdir); FILE *test2fopen(long testno, const char *logdir);
@ -83,13 +92,10 @@ void install_signal_handlers(bool keep_sigalrm);
void restore_signal_handlers(bool keep_sigalrm); void restore_signal_handlers(bool keep_sigalrm);
#ifdef USE_UNIX_SOCKETS #ifdef USE_UNIX_SOCKETS
#include <curl/curl.h> /* for curl_socket_t */ #include <curl/curl.h> /* for curl_socket_t */
#ifdef HAVE_SYS_UN_H #ifdef HAVE_SYS_UN_H
#include <sys/un.h> /* for sockaddr_un */ #include <sys/un.h> /* for sockaddr_un */
#endif /* HAVE_SYS_UN_H */ #endif
int bind_unix_socket(curl_socket_t sock, const char *unix_socket, int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
struct sockaddr_un *sau); struct sockaddr_un *sau);
#endif /* USE_UNIX_SOCKETS */ #endif /* USE_UNIX_SOCKETS */