]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-ether-addr-util: add a simple test that HW_ADDR_TO_STR works with nesting
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 29 Jun 2021 13:27:12 +0000 (15:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Jul 2021 09:03:35 +0000 (11:03 +0200)
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.

src/basic/ether-addr-util.h
src/test/meson.build
src/test/test-ether-addr-util.c [new file with mode: 0644]

index 63447910c09afd9fcb437f903eccb75c79c25e7e..794fc55bb837b5812b750ae5e10a154389503944 100644 (file)
@@ -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){})
index e106059182c7b448dfb4588b7b54129fc2df2471..14725248cefb11698a5cc6833cff3fd6781b2a71 100644 (file)
@@ -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 (file)
index 0000000..8942158
--- /dev/null
@@ -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;
+}