From: Zbigniew Jędrzejewski-Szmek Date: Tue, 29 Jun 2021 13:27:12 +0000 (+0200) Subject: test-ether-addr-util: add a simple test that HW_ADDR_TO_STR works with nesting X-Git-Tag: v250-rc1~952^2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e265fa819812409380712fbd70817bcd96f069c5;p=thirdparty%2Fsystemd.git test-ether-addr-util: add a simple test that HW_ADDR_TO_STR works with nesting It seems to, but I was a bit incredulous… The comment is adjusted to match the standard. The trick with a temporary buffer is neat. I wasn't sure if it is valid, but the standard says so. The test really tests that we are reading the rules right and that the compiler doesn't do anythign strange or emit an unexpected warning. --- diff --git a/src/basic/ether-addr-util.h b/src/basic/ether-addr-util.h index 63447910c09..794fc55bb83 100644 --- a/src/basic/ether-addr-util.h +++ b/src/basic/ether-addr-util.h @@ -23,7 +23,9 @@ struct hw_addr_data { #define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE) char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]); -/* Use only as function argument, never stand-alone! */ +/* Note: the lifetime of the compound literal is the immediately surrounding block, + * see C11 §6.5.2.5, and + * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ #define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){}) #define HW_ADDR_NULL ((const struct hw_addr_data){}) diff --git a/src/test/meson.build b/src/test/meson.build index e106059182c..14725248cef 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -44,6 +44,8 @@ test_dlopen_c = files('test-dlopen.c') tests += [ [['src/test/test-device-nodes.c']], + [['src/test/test-ether-addr-util.c']], + [['src/test/test-engine.c'], [libcore, libshared], diff --git a/src/test/test-ether-addr-util.c b/src/test/test-ether-addr-util.c new file mode 100644 index 00000000000..894215844a9 --- /dev/null +++ b/src/test/test-ether-addr-util.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "ether-addr-util.h" +#include "tests.h" + +#define INFINIBAD_ADDR_1 ((const struct hw_addr_data){ .length = 20, .infiniband = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} }) + +static void test_HW_ADDR_TO_STRING(void) { + log_info("/* %s */", __func__); + + const char *s = HW_ADDR_TO_STR(&(const struct hw_addr_data){6}); + log_info("null: %s", s); + + log_info("null×2: %s, %s", + HW_ADDR_TO_STR(&(const struct hw_addr_data){6}), + HW_ADDR_TO_STR(&(const struct hw_addr_data){6})); + log_info("null×3: %s, %s, %s", + HW_ADDR_TO_STR(&(const struct hw_addr_data){6}), + s, + HW_ADDR_TO_STR(&(const struct hw_addr_data){6})); + + log_info("infiniband: %s", HW_ADDR_TO_STR(&INFINIBAD_ADDR_1)); + + /* Let's nest function calls in a stupid way. */ + _cleanup_free_ char *t = NULL; + log_info("infiniband×3: %s\n%14s%s\n%14s%s", + HW_ADDR_TO_STR(&(const struct hw_addr_data){20}), "", + t = strdup(HW_ADDR_TO_STR(&INFINIBAD_ADDR_1)), "", + HW_ADDR_TO_STR(&(const struct hw_addr_data){20})); + + const char *p; + /* Let's use a separate selection statement */ + if ((p = HW_ADDR_TO_STR(&(const struct hw_addr_data){6}))) + log_info("joint: %s, %s", s, p); +} + +int main(int argc, char *argv[]) { + test_setup_logging(LOG_INFO); + + test_HW_ADDR_TO_STRING(); + return 0; +}