curl/tests/unit/unit1661.c
Viktor Szakats 2c27a67daa
tests: always make bundles, adapt build and tests
Make test bundles the default. Drop non-bundle build mode.
Also do all the optimizations and tidy-ups this allows, simpler builds,
less bundle exceptions, streamlined build mechanics.

Also rework the init/deinit macro magic for unit tests. The new method
allows using unique init/deinit function names, and calling them with
arguments. This is in turn makes it possible to reduce the use of global
variables.

Note this drop existing build options `-DCURL_TEST_BUNDLES=` from cmake
and `--enable-test-bundles` / `--disable-test-bundles` from autotools.

Also:
- rename test entry functions to have unique names: `test_<testname>`
  This removes the last exception that was handled in the generator.
- fix `make dist` to not miss test sources with test bundles enabled.
- sync and merge `tests/mk-bundle.pl` into `scripts/mk-unity.pl`.
- mk-unity.pl: add `--embed` option and use it when `CURL_CLANG_TIDY=ON`
  to ensure that `clang-tidy` does not miss external test C sources.
  (because `clang-tidy` ignores code that's #included.)
- tests/unit: drop no-op setup/stop functions.
- tests: reduce symbol scopes, global macros, other fixes and tidy-ups.
- tool1621: fix to run, also fix it to pass.
- sockfilt: fix Windows compiler warning in certain unity include order,
  by explicitly including `warnless.h`.

Follow-up to 6897aeb105 #17468

Closes #17590
2025-06-14 21:08:23 +02:00

115 lines
3.4 KiB
C

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curlcheck.h"
#include "bufref.h"
#include "memdebug.h"
static int freecount = 0;
static void test_free(void *p)
{
fail_unless(p, "pointer to free may not be NULL");
freecount++;
free(p);
}
static CURLcode t1661_setup(struct bufref *bufref)
{
Curl_bufref_init(bufref);
return CURLE_OK;
}
static void t1661_stop(struct bufref *bufref)
{
Curl_bufref_free(bufref);
}
static CURLcode test_unit1661(char *arg)
{
struct bufref bufref;
UNITTEST_BEGIN(t1661_setup(&bufref))
const char *buffer = NULL;
CURLcode result = CURLE_OK;
/**
* testing Curl_bufref_init.
* @assumptions:
* 1: data size will be 0
* 2: reference will be NULL
* 3: destructor will be NULL
*/
fail_unless(!bufref.ptr, "Initial reference must be NULL");
fail_unless(!bufref.len, "Initial length must be NULL");
fail_unless(!bufref.dtor, "Destructor must be NULL");
/**
* testing Curl_bufref_set
*/
buffer = malloc(13);
abort_unless(buffer, "Out of memory");
Curl_bufref_set(&bufref, buffer, 13, test_free);
fail_unless((const char *)bufref.ptr == buffer, "Referenced data badly set");
fail_unless(bufref.len == 13, "Data size badly set");
fail_unless(bufref.dtor == test_free, "Destructor badly set");
/**
* testing Curl_bufref_ptr
*/
fail_unless((const char *) Curl_bufref_ptr(&bufref) == buffer,
"Wrong pointer value returned");
/**
* testing Curl_bufref_len
*/
fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
/**
* testing Curl_bufref_memdup
*/
result = Curl_bufref_memdup(&bufref, "1661", 3);
abort_unless(result == CURLE_OK, curl_easy_strerror(result));
fail_unless(freecount == 1, "Destructor not called");
fail_unless((const char *)bufref.ptr != buffer, "Returned pointer not set");
buffer = (const char *)Curl_bufref_ptr(&bufref);
fail_unless(buffer, "Allocated pointer is NULL");
fail_unless(bufref.len == 3, "Wrong data size stored");
if(buffer) {
fail_unless(!buffer[3], "Duplicated data should have been truncated");
fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
}
/**
* testing Curl_bufref_free
*/
Curl_bufref_free(&bufref);
fail_unless(freecount == 1, "Wrong destructor called");
fail_unless(!bufref.ptr, "Initial reference must be NULL");
fail_unless(!bufref.len, "Initial length must be NULL");
fail_unless(!bufref.dtor, "Destructor must be NULL");
UNITTEST_END(t1661_stop(&bufref))
}