lib: move all UNITTEST prototypes to C files

- make extract-unit-protos handle multi-line prototypes - but they need
  to be above the implementation

- Prototypes for static functions we use in unit tests should not be in
  header files. We generate lib/unitprotos.h for this purpose

- Removed some function wrappers written for unit tests and make them
  use UNITTEST function directly.

- Renamed time2str() in the tool to timebuf() since we have the same
  name in lib/ and in unit tests they can both be used non-static in a
  build.

This reverts commit f95fadd116.

Follow-up to #21010

Closes #21014
This commit is contained in:
Daniel Stenberg 2026-03-19 17:04:00 +01:00
parent 7242cea7f6
commit 98d8e82c74
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
42 changed files with 109 additions and 149 deletions

View file

@ -69,6 +69,11 @@ static const char *doh_strerror(DOHcode code)
/* @unittest 1655
*/
UNITTEST DOHcode doh_req_encode(const char *host,
DNStype dnstype,
unsigned char *dnsp, /* buffer */
size_t len, /* buffer size */
size_t *olen); /* output length */
UNITTEST DOHcode doh_req_encode(const char *host,
DNStype dnstype,
unsigned char *dnsp, /* buffer */
@ -699,6 +704,7 @@ static DOHcode doh_rdata(const unsigned char *doh,
return DOH_OK;
}
UNITTEST void de_init(struct dohentry *d);
UNITTEST void de_init(struct dohentry *de)
{
int i;
@ -708,6 +714,10 @@ UNITTEST void de_init(struct dohentry *de)
curlx_dyn_init(&de->cname[i], DYN_DOH_CNAME);
}
UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
size_t dohlen,
DNStype dnstype,
struct dohentry *d);
UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
size_t dohlen,
DNStype dnstype,
@ -1025,6 +1035,7 @@ static const char *doh_type2name(DNStype dnstype)
}
#endif
UNITTEST void de_cleanup(struct dohentry *d);
UNITTEST void de_cleanup(struct dohentry *d)
{
int i = 0;

View file

@ -25,7 +25,8 @@
***************************************************************************/
#include "urldata.h"
#ifndef CURL_DISABLE_DOH
/* enums outside of the #ifdef to make the types work in unitprotos.h even on
builds without DoH support */
typedef enum {
DOH_OK,
@ -53,6 +54,10 @@ typedef enum {
CURL_DNS_TYPE_HTTPS = 65
} DNStype;
struct dohentry; /* forward-declare for non-DoH builds */
#ifndef CURL_DISABLE_DOH
enum doh_slot_num {
/* Explicit values for first two symbols so as to match hard-coded
* constants in existing code
@ -158,21 +163,6 @@ struct dohentry {
void Curl_doh_close(struct Curl_easy *data);
void Curl_doh_cleanup(struct Curl_easy *data);
#ifdef UNITTESTS
UNITTEST DOHcode doh_req_encode(const char *host,
DNStype dnstype,
unsigned char *dnsp, /* buffer */
size_t len, /* buffer size */
size_t *olen); /* output length */
UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
size_t dohlen,
DNStype dnstype,
struct dohentry *d);
UNITTEST void de_init(struct dohentry *d);
UNITTEST void de_cleanup(struct dohentry *d);
#endif
#else /* CURL_DISABLE_DOH */
#define Curl_doh(a, b, c, d, e) NULL
#define Curl_doh_is_resolved(x, y) CURLE_COULDNT_RESOLVE_HOST

View file

@ -679,6 +679,8 @@ static int compare_func(const void *a, const void *b)
return compare;
}
UNITTEST CURLcode canon_path(const char *q, size_t len,
struct dynbuf *new_path, bool normalize);
UNITTEST CURLcode canon_path(const char *q, size_t len,
struct dynbuf *new_path,
bool do_uri_encode)
@ -705,6 +707,7 @@ UNITTEST CURLcode canon_path(const char *q, size_t len,
return result;
}
UNITTEST CURLcode canon_query(const char *query, struct dynbuf *dq);
UNITTEST CURLcode canon_query(const char *query, struct dynbuf *dq)
{
CURLcode result = CURLE_OK;

View file

@ -31,10 +31,4 @@
/* this is for creating aws_sigv4 header output */
CURLcode Curl_output_aws_sigv4(struct Curl_easy *data);
#ifdef UNITTESTS
UNITTEST CURLcode canon_path(const char *q, size_t len,
struct dynbuf *new_path, bool normalize);
UNITTEST CURLcode canon_query(const char *query, struct dynbuf *dq);
#endif
#endif /* HEADER_CURL_HTTP_AWS_SIGV4_H */

View file

@ -41,6 +41,9 @@
* Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
* specified CIDR address range.
*/
UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
const char *network, /* 1.2.3.4 address */
unsigned int bits);
UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
const char *network, /* 1.2.3.4 address */
unsigned int bits)
@ -74,6 +77,9 @@ UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
return address == check;
}
UNITTEST bool Curl_cidr6_match(const char *ipv6,
const char *network,
unsigned int bits);
UNITTEST bool Curl_cidr6_match(const char *ipv6,
const char *network,
unsigned int bits)

View file

@ -26,17 +26,6 @@
#include "curl_setup.h"
#ifndef CURL_DISABLE_PROXY
#ifdef UNITTESTS
UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
const char *network, /* 1.2.3.4 address */
unsigned int bits);
UNITTEST bool Curl_cidr6_match(const char *ipv6,
const char *network,
unsigned int bits);
#endif
bool Curl_check_noproxy(const char *name, const char *no_proxy);
#endif

View file

@ -129,6 +129,8 @@ static void pgrs_speedinit(struct Curl_easy *data)
/*
* @unittest: 1606
*/
UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
const struct curltime *pnow);
UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
const struct curltime *pnow)
{

View file

@ -82,9 +82,4 @@ void Curl_pgrsTimeWas(struct Curl_easy *data, timerid timer,
void Curl_pgrsEarlyData(struct Curl_easy *data, curl_off_t sent);
#ifdef UNITTESTS
UNITTEST CURLcode pgrs_speedcheck(struct Curl_easy *data,
const struct curltime *pnow);
#endif
#endif /* HEADER_CURL_PROGRESS_H */

View file

@ -68,6 +68,7 @@ void Curl_uint32_bset_destroy(struct uint32_bset *bset)
}
#ifdef UNITTESTS
UNITTEST uint32_t Curl_uint32_bset_capacity(struct uint32_bset *bset);
UNITTEST uint32_t Curl_uint32_bset_capacity(struct uint32_bset *bset)
{
return bset->nslots * 64;

View file

@ -56,9 +56,6 @@ CURLcode Curl_uint32_bset_resize(struct uint32_bset *bset, uint32_t nmax);
/* Destroy the bitset, freeing all resources. */
void Curl_uint32_bset_destroy(struct uint32_bset *bset);
/* Get the bitset capacity, e.g. can hold numbers from 0 to capacity - 1. */
uint32_t Curl_uint32_bset_capacity(struct uint32_bset *bset);
/* Get the cardinality of the bitset, e.g. numbers present in the set. */
uint32_t Curl_uint32_bset_count(struct uint32_bset *bset);

View file

@ -181,7 +181,8 @@ void *Curl_uint32_hash_get(struct uint_hash *h, uint32_t id)
return NULL;
}
static void uint_hash_clear(struct uint_hash *h)
UNITTEST void uint_hash_clear(struct uint_hash *h);
UNITTEST void uint_hash_clear(struct uint_hash *h)
{
if(h && h->table) {
struct uint_hash_entry *he, **he_anchor;
@ -198,13 +199,6 @@ static void uint_hash_clear(struct uint_hash *h)
}
}
#ifdef UNITTESTS
UNITTEST void Curl_uint32_hash_clear(struct uint_hash *h)
{
uint_hash_clear(h);
}
#endif
void Curl_uint32_hash_destroy(struct uint_hash *h)
{
DEBUGASSERT(h->init == CURL_UINT32_HASHINIT);

View file

@ -44,8 +44,6 @@ void Curl_uint32_hash_init(struct uint_hash *h,
uint32_t slots,
Curl_uint32_hash_dtor *dtor);
void Curl_uint32_hash_destroy(struct uint_hash *h);
void Curl_uint32_hash_clear(struct uint_hash *h);
bool Curl_uint32_hash_set(struct uint_hash *h, uint32_t id, void *value);
bool Curl_uint32_hash_remove(struct uint_hash *h, uint32_t id);
void *Curl_uint32_hash_get(struct uint_hash *h, uint32_t id);

View file

@ -32,11 +32,6 @@ CURLUcode Curl_url_set_authority(CURLU *u, const char *authority);
CURLUcode Curl_junkscan(const char *url, size_t *urllen, bool allowspace);
#ifdef UNITTESTS
UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
bool has_scheme);
#endif
#define U_CURLU_URLDECODE (unsigned int)CURLU_URLDECODE
#define U_CURLU_PATH_AS_IS (unsigned int)CURLU_PATH_AS_IS

View file

@ -332,6 +332,8 @@ out:
return result;
}
UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
bool has_scheme);
UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
bool has_scheme)
{

View file

@ -160,10 +160,6 @@ static const struct Curl_OID OIDtable[] = {
* Please note there is no pretension here to rewrite a full SSL library.
*/
static const char *getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end)
WARN_UNUSED_RESULT;
#define CURL_ASN1_MAX_RECURSIONS 16
static const char *getASN1Element_(struct Curl_asn1Element *elem,
@ -233,8 +229,13 @@ static const char *getASN1Element_(struct Curl_asn1Element *elem,
return elem->end;
}
static const char *getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end)
/*
* unit test @1657
*/
UNITTEST const char *getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end);
UNITTEST const char *getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end)
{
return getASN1Element_(elem, beg, end, 0);
}
@ -255,17 +256,6 @@ static const struct Curl_OID *searchOID(const char *oid)
return NULL;
}
#ifdef UNITTESTS
/* used by unit1657.c */
CURLcode Curl_x509_getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end)
{
if(getASN1Element(elem, beg, end))
return CURLE_OK;
return CURLE_BAD_FUNCTION_ARGUMENT;
}
#endif
/*
* Convert an ASN.1 Boolean value into its string representation.
*
@ -427,6 +417,7 @@ static CURLcode utf8asn1str(struct dynbuf *to, int type, const char *from,
*
* @unittest 1666
*/
UNITTEST CURLcode encodeOID(struct dynbuf *buf, const char *b, const char *e);
UNITTEST CURLcode encodeOID(struct dynbuf *store,
const char *beg, const char *end)
{
@ -511,8 +502,13 @@ static CURLcode OID2str(struct dynbuf *store,
return result;
}
static CURLcode GTime2str(struct dynbuf *store,
const char *beg, const char *end)
/*
* Unit test @1656
*/
UNITTEST CURLcode GTime2str(struct dynbuf *store,
const char *beg, const char *end);
UNITTEST CURLcode GTime2str(struct dynbuf *store,
const char *beg, const char *end)
{
const char *tzp;
const char *fracp;
@ -589,15 +585,6 @@ static CURLcode GTime2str(struct dynbuf *store,
sep, (int)tzl, tzp);
}
#ifdef UNITTESTS
/* used by unit1656.c */
CURLcode Curl_x509_GTime2str(struct dynbuf *store,
const char *beg, const char *end)
{
return GTime2str(store, beg, end);
}
#endif
/*
* Convert an ASN.1 UTC time to a printable string.
*

View file

@ -26,6 +26,8 @@
***************************************************************************/
#include "curl_setup.h"
struct Curl_asn1Element;
#if defined(USE_GNUTLS) || defined(USE_WOLFSSL) || defined(USE_SCHANNEL) || \
defined(USE_MBEDTLS) || defined(USE_RUSTLS)
@ -75,23 +77,6 @@ CURLcode Curl_extract_certinfo(struct Curl_easy *data, int certnum,
const char *beg, const char *end);
CURLcode Curl_verifyhost(struct Curl_cfilter *cf, struct Curl_easy *data,
const char *beg, const char *end);
#ifdef UNITTESTS
UNITTEST CURLcode encodeOID(struct dynbuf *store,
const char *beg, const char *end);
#if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_MBEDTLS) || \
defined(USE_RUSTLS)
/* used by unit1656.c */
CURLcode Curl_x509_GTime2str(struct dynbuf *store,
const char *beg, const char *end);
/* used by unit1657.c */
CURLcode Curl_x509_getASN1Element(struct Curl_asn1Element *elem,
const char *beg, const char *end);
#endif /* USE_GNUTLS || USE_SCHANNEL || USE_MBEDTLS || RUSTLS */
#endif /* UNITTESTS */
#endif /* USE_GNUTLS || USE_WOLFSSL || USE_SCHANNEL || USE_MBEDTLS ||
USE_RUSTLS */
#endif /* HEADER_CURL_X509ASN1_H */