]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tests/server: round of tidy-ups
authorViktor Szakats <commit@vsz.me>
Sun, 8 Sep 2024 14:18:57 +0000 (16:18 +0200)
committerViktor Szakats <commit@vsz.me>
Tue, 4 Mar 2025 03:17:44 +0000 (04:17 +0100)
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

tests/server/mqttd.c
tests/server/resolve.c
tests/server/rtspd.c
tests/server/sockfilt.c
tests/server/socksd.c
tests/server/sws.c
tests/server/tftpd.c
tests/server/util.c
tests/server/util.h

index 2fc3a5f94a891442e49817777bc36effaddd39e8..dbc844d8231e33bc7c5d71628eb1f94fe0a19b4d 100644 (file)
 /* include memdebug.h last */
 #include "memdebug.h"
 
-#ifdef USE_WINSOCK
-#undef  EINTR
-#define EINTR    4 /* errno.h value */
-#endif
-
 #define DEFAULT_PORT 1883 /* MQTT default port */
 
-#ifndef DEFAULT_LOGFILE
-#define DEFAULT_LOGFILE "log/mqttd.log"
-#endif
-
 #ifndef DEFAULT_CONFIG
 #define DEFAULT_CONFIG "mqttd.config"
 #endif
@@ -102,7 +93,6 @@ struct configurable {
 
 static struct configurable config;
 
-const char *serverlogfile = DEFAULT_LOGFILE;
 static const char *configfile = DEFAULT_CONFIG;
 static const char *logdir = "log";
 static char loglockfile[256];
@@ -933,6 +923,8 @@ int main(int argc, char *argv[])
   int error;
   int arg = 1;
 
+  serverlogfile = "log/mqttd.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       printf("mqttd IPv4%s\n",
index 56964e03a93df48a9a71cb6e32e9a4ca2f5c2614..0a0c884b94bb674c36d9a270d0066299e7bd78c5 100644 (file)
@@ -53,8 +53,6 @@
 static bool use_ipv6 = FALSE;
 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 arg = 1;
index 181568395fbd26ba1243205df6870090ab8ef37f..5e7fbfca3650df7707ad51d5894862be026b393d 100644 (file)
 /* include memdebug.h last */
 #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
 static bool use_ipv6 = FALSE;
 #endif
@@ -124,11 +117,6 @@ static void storerequest(char *reqbuf, size_t totalsize);
 
 #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 char loglockfile[256];
 
@@ -155,16 +143,6 @@ static char loglockfile[256];
 
 #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 */
 static const char *docquit =
@@ -1063,6 +1041,8 @@ int main(int argc, char *argv[])
 
   memset(&req, 0, sizeof(req));
 
+  serverlogfile = "log/rtspd.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       printf("rtspd IPv4%s"
index 18f75a52367c79b019fb215f07d41aeb28019250..04ec469fb7a96b8d2e1a85a74b6a91a14ba21f2f 100644 (file)
 /* include memdebug.h last */
 #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
 
-#ifndef DEFAULT_LOGFILE
-#define DEFAULT_LOGFILE "log/sockfilt.log"
-#endif
-
 /* buffer is this excessively large only to be able to support things like
   test 1003 which tests exceedingly large server response lines */
 #define BUFFER_SIZE 17010
 
-const char *serverlogfile = DEFAULT_LOGFILE;
-
 static bool verbose = FALSE;
 static bool bind_only = FALSE;
 #ifdef USE_IPV6
@@ -1406,6 +1389,8 @@ int main(int argc, char *argv[])
   enum sockmode mode = PASSIVE_LISTEN; /* default */
   const char *addr = NULL;
 
+  serverlogfile = "log/sockfilt.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       printf("sockfilt IPv4%s\n",
index 92b261454e42bca2c99f4a864c5a4fb5be5ffc1a..e98071ea735353157bac73f8546a4d6f11be79cd 100644 (file)
 /* include memdebug.h last */
 #include "memdebug.h"
 
-#ifdef USE_WINSOCK
-#undef  EINTR
-#define EINTR    4 /* errno.h value */
-#endif
-
 #define DEFAULT_PORT 8905
 
-#ifndef DEFAULT_LOGFILE
-#define DEFAULT_LOGFILE "log/socksd.log"
-#endif
-
 #ifndef DEFAULT_REQFILE
 #define DEFAULT_REQFILE "log/socksd-request.log"
 #endif
@@ -134,7 +125,6 @@ struct configurable {
 
 static struct configurable config;
 
-const char *serverlogfile = DEFAULT_LOGFILE;
 static const char *reqlogfile = DEFAULT_REQFILE;
 static const char *configfile = DEFAULT_CONFIG;
 
@@ -979,6 +969,8 @@ int main(int argc, char *argv[])
   bool unlink_socket = false;
 #endif
 
+  serverlogfile = "log/socksd.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       printf("socksd IPv4%s\n",
index fa83bc6a9a6f6a45a2345add55c4a06252bbcb1d..fc40d3eac9a9ddb50fc5c1fa2c1730e7e061c92c 100644 (file)
 /* include memdebug.h last */
 #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 bool use_gopher = FALSE;
 static int serverlogslocked = 0;
@@ -131,11 +122,6 @@ static void storerequest(const char *reqbuf, size_t totalsize);
 
 #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 char loglockfile[256];
 
@@ -151,7 +137,7 @@ static char loglockfile[256];
 
 /* file in which additional instructions may be found */
 #define DEFAULT_CMDFILE "log/server.cmd"
-const char *cmdfile = DEFAULT_CMDFILE;
+static const char *cmdfile = DEFAULT_CMDFILE;
 
 /* very-big-path support */
 #define MAXDOCNAMELEN 140000
@@ -185,13 +171,6 @@ const char *cmdfile = DEFAULT_CMDFILE;
 
 #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;
 
 /* 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 ... */
   size_t socket_idx;
 
+  serverlogfile = "log/sws.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       puts("sws IPv4"
index 93f6537c6d04c28c548dde4c3e83b4a1fb3643c9..1adaec610d7c52060ef3469fe00373649d0b6db7 100644 (file)
@@ -150,10 +150,6 @@ struct bf {
 #undef MIN
 #define MIN(x,y) ((x)<(y)?(x):(y))
 
-#ifndef DEFAULT_LOGFILE
-#define DEFAULT_LOGFILE "log/tftpd.log"
-#endif
-
 #define REQUEST_DUMP  "server.input"
 
 #define DEFAULT_PORT 8999 /* UDP */
@@ -205,7 +201,6 @@ static bool use_ipv6 = FALSE;
 #endif
 static const char *ipv_inuse = "IPv4";
 
-const char *serverlogfile = DEFAULT_LOGFILE;
 static const char *logdir = "log";
 static char loglockfile[256];
 static const char *pidname = ".tftpd.pid";
@@ -564,6 +559,8 @@ int main(int argc, char **argv)
 
   memset(&test, 0, sizeof(test));
 
+  serverlogfile = "log/tftpd.log";
+
   while(argc > arg) {
     if(!strcmp("--version", argv[arg])) {
       printf("tftpd IPv4%s\n",
index b3ffa71a4965e3e668451325b9fe2d6734fff0aa..df76b7810b021d6ef802c8507e77e73a97fdb834 100644 (file)
 #include "getpart.h"
 #include "util.h"
 #include "timeval.h"
+#include "timediff.h"
 
-#ifdef USE_WINSOCK
-#undef  EINTR
-#define EINTR    4 /* errno.h value */
-#undef  EINVAL
-#define EINVAL  22 /* errno.h value */
-#endif
+const char *serverlogfile = NULL;  /* needs init from main() */
 
 static struct timeval tvnow(void);
 
@@ -143,7 +139,7 @@ void logmsg(const char *msg, ...)
 
 #ifdef _WIN32
 /* use instead of perror() on generic Windows */
-void win32_perror(const char *msg)
+static void win32_perror(const char *msg)
 {
   char buf[512];
   int err = SOCKERRNO;
@@ -174,7 +170,7 @@ int win32_init(void)
   err = WSAStartup(wVersionRequested, &wsaData);
 
   if(err) {
-    perror("Winsock init failed");
+    win32_perror("Winsock init failed");
     logmsg("Error initialising Winsock -- aborting");
     return 1;
   }
@@ -182,7 +178,7 @@ int win32_init(void)
   if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
      HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
     WSACleanup();
-    perror("Winsock init failed");
+    win32_perror("Winsock init failed");
     logmsg("No suitable winsock.dll found -- aborting");
     return 1;
   }
@@ -202,23 +198,36 @@ const char *sstrerror(int err)
 /* set by the main code to point to where the test dir is */
 const char *path = ".";
 
-FILE *test2fopen(long testno, const char *logdir)
+FILE *test2fopen(long testno, const char *logdir2)
 {
   FILE *stream;
   char filename[256];
   /* 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");
   if(stream)
     return stream;
 
   /* 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");
 
   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.
  * Waiting indefinitely with this function is not allowed, a
@@ -455,17 +464,6 @@ static struct timeval tvnow(void)
 
 #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 */
 
 typedef void (*SIGHANDLER_T)(int);
@@ -633,7 +631,7 @@ static unsigned int WINAPI main_window_loop(void *lpParameter)
   wc.hInstance = (HINSTANCE)lpParameter;
   wc.lpszClassName = TEXT("MainWClass");
   if(!RegisterClass(&wc)) {
-    perror("RegisterClass failed");
+    win32_perror("RegisterClass failed");
     return (DWORD)-1;
   }
 
@@ -645,14 +643,14 @@ static unsigned int WINAPI main_window_loop(void *lpParameter)
                                       (HWND)NULL, (HMENU)NULL,
                                       wc.hInstance, (LPVOID)NULL);
   if(!hidden_main_window) {
-    perror("CreateWindowEx failed");
+    win32_perror("CreateWindowEx failed");
     return (DWORD)-1;
   }
 
   do {
     ret = GetMessage(&msg, NULL, 0, 0);
     if(ret == -1) {
-      perror("GetMessage failed");
+      win32_perror("GetMessage failed");
       return (DWORD)-1;
     }
     else if(ret) {
index 991c0703ce71e1fbdc4747f12fb2fe4f941ca7e3..0b112d24b4ec8b0e919ae127dc4e9426cb8726b7 100644 (file)
  ***************************************************************************/
 #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);
 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 */
 
 /* global variable, where to find the 'data' dir */
@@ -39,24 +60,12 @@ extern const char *path;
 /* global variable, log file name */
 extern const char *serverlogfile;
 
-extern const char *cmdfile;
-
 #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);
 const char *sstrerror(int err);
-#else   /* _WIN32 */
-
+#else
 #define sstrerror(e) strerror(e)
-#endif  /* _WIN32 */
+#endif
 
 /* fopens the test case file */
 FILE *test2fopen(long testno, const char *logdir);
@@ -83,16 +92,13 @@ void install_signal_handlers(bool keep_sigalrm);
 void restore_signal_handlers(bool keep_sigalrm);
 
 #ifdef USE_UNIX_SOCKETS
-
 #include <curl/curl.h> /* for curl_socket_t */
-
 #ifdef HAVE_SYS_UN_H
 #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,
-        struct sockaddr_un *sau);
-#endif  /* USE_UNIX_SOCKETS */
+                     struct sockaddr_un *sau);
+#endif /* USE_UNIX_SOCKETS */
 
 unsigned short util_ultous(unsigned long ulnum);