summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-07-19 23:13:50 +0200
committerGitHub <noreply@github.com>2021-07-19 23:13:50 +0200
commit8bdef77400d21a344e09eb73773789bddd6b76af (patch)
treee1ec72616822833c7fe238eefbcea8b93e994835 /src
parentlog-generator: count arguments as offset from an iterator (diff)
parentbasic/time-util: inline one more variable declaration (diff)
downloadsystemd-8bdef77400d21a344e09eb73773789bddd6b76af.tar.xz
systemd-8bdef77400d21a344e09eb73773789bddd6b76af.zip
Merge pull request #20251 from keszybz/test-format-lifetime
Add test for format_lifetime() and fix prefix
Diffstat (limited to 'src')
-rw-r--r--src/basic/time-util.c8
-rw-r--r--src/network/meson.build6
-rw-r--r--src/network/networkd-address.c18
-rw-r--r--src/network/networkd-address.h4
-rw-r--r--src/network/test-networkd-address.c28
5 files changed, 47 insertions, 17 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 0a4fcdcd13..c3b175a192 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -553,14 +553,12 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
/* Let's see if we should shows this in dot notation */
if (t < USEC_PER_MINUTE && b > 0) {
- usec_t cc;
- signed char j;
+ signed char j = 0;
- j = 0;
- for (cc = table[i].usec; cc > 1; cc /= 10)
+ for (usec_t cc = table[i].usec; cc > 1; cc /= 10)
j++;
- for (cc = accuracy; cc > 1; cc /= 10) {
+ for (usec_t cc = accuracy; cc > 1; cc /= 10) {
b /= 10;
j--;
}
diff --git a/src/network/meson.build b/src/network/meson.build
index 4e137d7b9e..2ea12c8d03 100644
--- a/src/network/meson.build
+++ b/src/network/meson.build
@@ -268,6 +268,12 @@ fuzzers += [
]
tests += [
+ [['src/network/test-networkd-address.c'],
+ [libnetworkd_core,
+ libsystemd_network],
+ [],
+ network_includes],
+
[['src/network/test-networkd-conf.c'],
[libnetworkd_core,
libsystemd_network],
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 6718e442d3..f4257b3b91 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -621,19 +621,17 @@ int manager_has_address(Manager *manager, int family, const union in_addr_union
return false;
}
-char *format_lifetime(char *buf, size_t l, uint32_t lifetime) {
- char *p = buf;
-
+const char* format_lifetime(char *buf, size_t l, uint32_t lifetime) {
assert(buf);
- assert(l > 0);
+ assert(l > 4);
- if (lifetime == CACHE_INFO_INFINITY_LIFE_TIME) {
- strscpy(buf, l, "forever");
- return buf;
- }
+ if (lifetime == CACHE_INFO_INFINITY_LIFE_TIME)
+ return "forever";
- l -= strpcpy(&p, l, "for ");
- return format_timespan(p, l, lifetime * USEC_PER_SEC, USEC_PER_SEC);
+ sprintf(buf, "for ");
+ /* format_timespan() never fails */
+ assert_se(format_timespan(buf + 4, l - 4, lifetime * USEC_PER_SEC, USEC_PER_SEC));
+ return buf;
}
static void log_address_debug(const Address *address, const char *str, const Link *link) {
diff --git a/src/network/networkd-address.h b/src/network/networkd-address.h
index 1569b588a0..811940c126 100644
--- a/src/network/networkd-address.h
+++ b/src/network/networkd-address.h
@@ -50,7 +50,7 @@ typedef struct Address {
address_ready_callback_t callback;
} Address;
-char *format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_result_;
+const char* format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_result_;
/* 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 */
@@ -58,7 +58,7 @@ char *format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_resul
format_lifetime((char[FORMAT_TIMESPAN_MAX+STRLEN("for ")]){}, FORMAT_TIMESPAN_MAX+STRLEN("for "), lifetime)
int address_new(Address **ret);
-Address *address_free(Address *address);
+Address* address_free(Address *address);
int address_get(Link *link, const Address *in, Address **ret);
int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg);
int address_remove(const Address *address, Link *link);
diff --git a/src/network/test-networkd-address.c b/src/network/test-networkd-address.c
new file mode 100644
index 0000000000..7c1d65a433
--- /dev/null
+++ b/src/network/test-networkd-address.c
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "networkd-address.h"
+#include "tests.h"
+
+static void test_FORMAT_LIFETIME_one(uint32_t lifetime, const char *expected) {
+ const char *t = FORMAT_LIFETIME(lifetime);
+
+ log_debug("%"PRIu32 " → \"%s\" (expected \"%s\")", lifetime, t, expected);
+ assert_se(streq(t, expected));
+}
+
+static void test_FORMAT_LIFETIME(void) {
+ log_info("/* %s */", __func__);
+
+ test_FORMAT_LIFETIME_one(0, "for 0");
+ test_FORMAT_LIFETIME_one(1, "for 1s");
+ test_FORMAT_LIFETIME_one(3 * (USEC_PER_WEEK/USEC_PER_SEC), "for 3w");
+ test_FORMAT_LIFETIME_one(CACHE_INFO_INFINITY_LIFE_TIME, "forever");
+}
+
+int main(int argc, char *argv[]) {
+ test_setup_logging(LOG_INFO);
+
+ test_FORMAT_LIFETIME();
+
+ return 0;
+}