tests: merge clients into libtests, drop duplicate code

libtests and clients were built the same way after recent overhauls.
libtests are used by runtests, clients by pytests.

Merge clients into libtests, aligning their entry function signature,
dropping common utility functions, and simplifying the build.

Note: After this patch `CURLDEBUG` applies to cli tests, when enabled.

Also:
- lib552: drop local copy-paste debug callback in favor of testtrace.
- lib552: drop local copy-paste dump function in favor of testtrace.
- clients: use `long` for HTTP version, drop casts.
- clients: replace local dump function in favor of testrace clone.
- sync cli test entry function prototype with libtests'.
- h2_serverpush: replace local trace callback with testtrace.
- de-duplicate 3 websocket close, ping, ping, functions. Kept the pong
  iteration from `ws_pingpong`. Note: the pong clone in `lib2304` was
  returning an error when `curl_ws_recv()` returned non-zero and
  the payload matched the expected one anyway. After this patch, this
  case returns success, as it does in `ws_pingpong`.
  `lib2304` keeps passing, but I'm not sure if the previous behavior
  was intentional.
- display full value in websocket close, ping, pong, drop casts.

Closes #18079
This commit is contained in:
Viktor Szakats 2025-07-29 09:53:14 +02:00
parent 952c929bdf
commit 00887aee8c
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
48 changed files with 541 additions and 1041 deletions

View file

@ -25,14 +25,14 @@
#include "memdebug.h"
struct libtest_trace_cfg libtest_debug_config;
struct libtest_trace_cfg debug_config;
static time_t epoch_offset; /* for test time tracing */
static int known_offset; /* for test time tracing */
static void libtest_debug_dump(const char *timebuf, const char *text,
FILE *stream, const unsigned char *ptr,
size_t size, int nohex)
void debug_dump(const char *timebuf, const char *text,
FILE *stream, const unsigned char *ptr,
size_t size, int nohex)
{
size_t i;
size_t c;
@ -61,9 +61,8 @@ static void libtest_debug_dump(const char *timebuf, const char *text,
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex &&
(i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
(ptr[i + c + 1] == 0x0A)) {
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
@ -71,9 +70,8 @@ static void libtest_debug_dump(const char *timebuf, const char *text,
((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80)) ?
ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex &&
(i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
(ptr[i + c + 2] == 0x0A)) {
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
@ -114,7 +112,7 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
switch(type) {
case CURLINFO_TEXT:
curl_mfprintf(stderr, "%s== Info: %s", timestr, (char *)data);
curl_mfprintf(stderr, "%s== Info: %s", timestr, data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
@ -138,7 +136,101 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
return 0;
}
libtest_debug_dump(timebuf, text, stderr, (unsigned char *)data, size,
trace_cfg->nohex);
debug_dump(timebuf, text, stderr, (const unsigned char *)data, size,
trace_cfg->nohex);
return 0;
}
static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
{
/*
* This is the trace look that is similar to what libcurl makes on its
* own.
*/
static const char * const s_infotype[] = {
"* ", "< ", "> ", "{ ", "} ", "{ ", "} "
};
if(idsbuf && *idsbuf)
curl_mfprintf(log, "%s%s", idsbuf, s_infotype[type]);
else
fputs(s_infotype[type], log);
}
/* callback for CURLOPT_DEBUGFUNCTION (used in client tests) */
int cli_debug_cb(CURL *handle, curl_infotype type,
char *data, size_t size, void *userp)
{
FILE *output = stderr;
static int newl = 0;
static int traced_data = 0;
char idsbuf[60];
curl_off_t xfer_id, conn_id;
(void)handle; /* not used */
(void)userp;
if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
conn_id >= 0) {
curl_msnprintf(idsbuf, sizeof(idsbuf),
"[%" CURL_FORMAT_CURL_OFF_T "-"
"%" CURL_FORMAT_CURL_OFF_T "] ", xfer_id, conn_id);
}
else {
curl_msnprintf(idsbuf, sizeof(idsbuf),
"[%" CURL_FORMAT_CURL_OFF_T "-x] ", xfer_id);
}
}
else
idsbuf[0] = 0;
switch(type) {
case CURLINFO_HEADER_OUT:
if(size > 0) {
size_t st = 0;
size_t i;
for(i = 0; i < size - 1; i++) {
if(data[i] == '\n') { /* LF */
if(!newl) {
log_line_start(output, idsbuf, type);
}
(void)fwrite(data + st, i - st + 1, 1, output);
st = i + 1;
newl = 0;
}
}
if(!newl)
log_line_start(output, idsbuf, type);
(void)fwrite(data + st, i - st + 1, 1, output);
}
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
traced_data = 0;
break;
case CURLINFO_TEXT:
case CURLINFO_HEADER_IN:
if(!newl)
log_line_start(output, idsbuf, type);
(void)fwrite(data, size, 1, output);
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
traced_data = 0;
break;
case CURLINFO_DATA_OUT:
case CURLINFO_DATA_IN:
case CURLINFO_SSL_DATA_IN:
case CURLINFO_SSL_DATA_OUT:
if(!traced_data) {
if(!newl)
log_line_start(output, idsbuf, type);
curl_mfprintf(output, "[%ld bytes data]\n", (long)size);
newl = 0;
traced_data = 1;
}
break;
default: /* nada */
newl = 0;
traced_data = 1;
break;
}
return 0;
}