curl/tests/unit
Viktor Szakats 193cb00ce9
build: stop overriding standard memory allocation functions
Before this patch curl used the C preprocessor to override standard
memory allocation symbols: malloc, calloc, strdup, realloc, free.
The goal of these is to replace them with curl's debug wrappers in
`CURLDEBUG` builds, another was to replace them with the wrappers
calling user-defined allocators in libcurl. This solution needed a bunch
of workarounds to avoid breaking external headers: it relied on include
order to do the overriding last. For "unity" builds it needed to reset
overrides before external includes. Also in test apps, which are always
built as single source files. It also needed the `(symbol)` trick
to avoid overrides in some places. This would still not fix cases where
the standard symbols were macros. It was also fragile and difficult
to figure out which was the actual function behind an alloc or free call
in a specific piece of code. This in turn caused bugs where the wrong
allocator was accidentally called.

To avoid these problems, this patch replaces this solution with
`curlx_`-prefixed allocator macros, and mapping them _once_ to either
the libcurl wrappers, the debug wrappers or the standard ones, matching
the rest of the code in libtests.

This concludes the long journey to avoid redefining standard functions
in the curl codebase.

Note: I did not update `packages/OS400/*.c` sources. They did not
`#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning
the overrides were never applied to them. This may or may not have been
correct. For now I suppressed the direct use of standard allocators
via a local `.checksrc`. Probably they (except for `curlcl.c`) should be
updated to include `curl_setup.h` and use the `curlx_` macros.

This patch changes mappings in two places:
- `lib/curl_threads.c` in libtests: Before this patch it mapped to
  libcurl allocators. After, it maps to standard allocators, like
  the rest of libtests code.
- `units`: before this patch it mapped to standard allocators. After, it
  maps to libcurl allocators.

Also:
- drop all position-dependent `curl_memory.h` and `memdebug.h` includes,
  and delete the now unnecessary headers.
- rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other
  allocators.
- map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`).
  To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`.
- multibyte: map `curlx_convert_*()` to `_strdup()` on Windows
  (was: `strdup()`).
- src: do not reuse the `strdup` name for the local replacement.
- lib509: call `_strdup()` on Windows (was: `strdup()`).
- test1132: delete test obsoleted by this patch.
- CHECKSRC.md: update text for `SNPRINTF`.
- checksrc: ban standard allocator symbols.

Follow-up to b12da22db1 #18866
Follow-up to db98daab05 #18844
Follow-up to 4deea9396b #18814
Follow-up to 9678ff5b1b #18776
Follow-up to 10bac43b87 #18774
Follow-up to 20142f5d06 #18634
Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #19626
2025-11-28 10:44:26 +01:00
..
.gitignore tests: always make bundles, adapt build and tests 2025-06-14 21:08:23 +02:00
CMakeLists.txt build: tidy up compiler definition for tests 2025-07-28 12:43:01 +02:00
Makefile.am lib: stop overriding system printf symbols 2025-10-06 20:57:59 +02:00
Makefile.inc ratelimit: redesign 2025-11-24 23:34:05 +01:00
README.md badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1300.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1302.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1303.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1304.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1305.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1307.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1309.c tests: stop overriding system printf symbols 2025-10-04 00:51:06 +02:00
unit1323.c lib: rename curlx_timediff to curlx_timeleft_ms 2025-11-13 13:12:58 +01:00
unit1330.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1395.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1396.c tests: stop overriding system printf symbols 2025-10-04 00:51:06 +02:00
unit1397.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1398.c build: address some -Weverything warnings, update picky warnings 2025-09-20 10:16:15 +02:00
unit1399.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1600.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1601.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1602.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1603.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1605.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1606.c ratelimit: redesign 2025-11-24 23:34:05 +01:00
unit1607.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1608.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1609.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1610.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1611.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1612.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1614.c noproxy: replace atoi with curlx_str_number 2025-11-12 10:30:59 +01:00
unit1615.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1616.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1620.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1650.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1651.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit1652.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1653.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1654.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1655.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1656.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit1657.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit1658.c badwords: fix issues found in tests 2025-11-17 13:30:35 +01:00
unit1660.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit1661.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1663.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1664.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit1979.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit1980.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit2600.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit2601.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit2602.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
unit2603.c http: eliminate ssize_t 2025-11-19 16:20:18 +01:00
unit2604.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit2605.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit3200.c build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
unit3205.c tests: constify command-line arguments 2025-07-29 13:44:50 +02:00
unit3211.c lib: change uint sets to operate on uint32_t 2025-11-25 17:22:13 +01:00
unit3212.c lib: change uint sets to operate on uint32_t 2025-11-25 17:22:13 +01:00
unit3213.c lib: change uint sets to operate on uint32_t 2025-11-25 17:22:13 +01:00
unit3214.c multi: add notifications API 2025-10-07 10:55:31 +02:00
unit3216.c ratelimit: redesign 2025-11-24 23:34:05 +01:00

Unit tests

The goal is to add tests for all functions in libcurl. If functions are too big and complicated, we should split them into smaller and testable ones.

Build Unit Tests

./configure --enable-debug is required for the unit tests to build. To enable unit tests, there is a separate static libcurl built that is used exclusively for linking unit test programs. Just build everything as normal, and then you can run the unit test cases as well.

Run Unit Tests

Unit tests are run as part of the regular test suite. If you have built everything to run unit tests, to can do 'make test' at the root level. Or you can cd tests and make and then invoke individual unit tests with ./runtests.pl NNNN where NNNN is the specific test number.

Debug Unit Tests

If a specific test fails you get told. The test case then has output left in the %LOGDIR subdirectory, but most importantly you can re-run the test again using gdb by doing ./runtests.pl -g NNNN. That is, add a -g to make it start up gdb and run the same case using that.

Write Unit Tests

We put tests that focus on an area or a specific function into a single C source file. The source file should be named unitNNNN.c where NNNN is a previously unused number.

Add your test to tests/unit/Makefile.inc (if it is a unit test). Add your test data filename to tests/data/Makefile.am

You also need a separate file called tests/data/testNNNN (using the same number) that describes your test case. See the test1300 file for inspiration and the tests/FILEFORMAT.md documentation.

For the actual C file, here's a simple example:

    #include "unitcheck.h"

    #include "a libcurl header.h" /* from the lib directory */

    static CURLcode test_unit9998(const char *arg)
    {
      UNITTEST_BEGIN_SIMPLE

      /* here you start doing things and checking that the results are good */

      fail_unless( size == 0 , "initial size should be zero" );
      fail_if( head == NULL , "head should not be initiated to NULL" );

      /* you end the test code like this: */

      UNITTEST_END_SIMPLE
    }

Here's an example using optional initialization and cleanup:

    #include "unitcheck.h"

    #include "a libcurl header.h" /* from the lib directory */

    static CURLcode t9999_setup(void)
    {
      /* whatever you want done first */
      return CURLE_OK;
    }

    static void t9999_stop(void)
    {
      /* done before shutting down and exiting */
    }

    static CURLcode test_unit9999(const char *arg)
    {
      UNITTEST_BEGIN(t9999_setup())

      /* here you start doing things and checking that the results are good */

      fail_unless( size == 0 , "initial size should be zero" );
      fail_if( head == NULL , "head should not be initiated to NULL" );

      /* you end the test code like this: */

      UNITTEST_END(t9999_stop())
    }