1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-31 14:50:15 +03:00

Merge pull request #20251 from keszybz/test-format-lifetime

Add test for format_lifetime() and fix prefix
This commit is contained in:
Yu Watanabe 2021-07-20 06:13:50 +09:00 committed by GitHub
commit 8bdef77400
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 17 deletions

View File

@ -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--;
}

View File

@ -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],

View File

@ -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) {

View File

@ -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);

View File

@ -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;
}