Browse Source

Fix print functions

pull/29/head
Milan Stute 1 year ago
parent
commit
e9f42b4d20
5 changed files with 60 additions and 36 deletions
  1. +7
    -7
      src/election.c
  2. +1
    -1
      src/election.h
  3. +10
    -10
      src/peers.c
  4. +2
    -2
      src/peers.h
  5. +40
    -16
      tests/test_awdl_peers.cpp

+ 7
- 7
src/election.c View File

@ -119,17 +119,17 @@ void awdl_election_run(struct awdl_election_state *state, const struct awdl_peer
}
}
int awdl_election_tree_print(const struct awdl_election_state *state, char *str, size_t len) {
int awdl_election_tree_print(const struct awdl_election_state *state, char *str, int len) {
char *cur = str, *const end = str + len;
cur += snprintf(cur, end - cur, "%s", ether_ntoa(&state->self_addr));
cur += snprintf(cur, cur < end ? end - cur: 0, "%s", ether_ntoa(&state->self_addr));
if (state->height > 0)
cur += snprintf(cur, end - cur, " -> %s", ether_ntoa(&state->sync_addr));
cur += snprintf(cur, cur < end ? end - cur : 0, " -> %s", ether_ntoa(&state->sync_addr));
if (state->height > 1) {
cur += snprintf(cur, end - cur, " ");
cur += snprintf(cur, cur < end ? end - cur : 0, " ");
for (uint32_t i = 1; i < state->height; i++)
cur += snprintf(cur, end - cur, "-"); /* one dash for every intermediate hop */
cur += snprintf(cur, end - cur, "> %s", ether_ntoa(&state->master_addr));
cur += snprintf(cur, cur < end ? end - cur : 0, "-"); /* one dash for every intermediate hop */
cur += snprintf(cur, cur < end ? end - cur : 0, "> %s", ether_ntoa(&state->master_addr));
}
cur += snprintf(cur, end - cur, " (met %u, ctr %u)", state->master_metric, state->master_counter);
cur += snprintf(cur, cur < end ? end - cur : 0, " (met %u, ctr %u)", state->master_metric, state->master_counter);
return cur - str;
}

+ 1
- 1
src/election.h View File

@ -45,7 +45,7 @@ void awdl_election_state_init(struct awdl_election_state *state, const struct et
void awdl_election_run(struct awdl_election_state *state, const struct awdl_peer_state *peers);
int awdl_election_tree_print(const struct awdl_election_state *state, char *str, size_t len);
int awdl_election_tree_print(const struct awdl_election_state *state, char *str, int len);
/* Util functions */
int compare_ether_addr(const struct ether_addr *a, const struct ether_addr *b);

+ 10
- 10
src/peers.c View File

@ -142,29 +142,29 @@ enum peers_status awdl_peer_get(awdl_peers_t peers, const struct ether_addr *_ad
return PEERS_OK;
}
int awdl_peer_print(const struct awdl_peer *peer, char *str, size_t len) {
int awdl_peer_print(const struct awdl_peer *peer, char *str, int len) {
char *cur = str, *const end = str + len;
if (strlen(peer->name))
cur += snprintf(cur, end - cur, "%s: ", peer->name);
cur += snprintf(cur, cur < end ? end - cur : 0, "%s: ", peer->name);
else
cur += snprintf(cur, end - cur, "<UNNAMED>: ");
cur += awdl_election_tree_print(&peer->election, cur, end - cur);
return end - cur;
cur += snprintf(cur, cur < end ? end - cur : 0, "<UNNAMED>: ");
cur += awdl_election_tree_print(&peer->election, cur, end - cur);
return cur - str;
}
int awdl_peers_print(awdl_peers_t peers, char *str, size_t len) {
int awdl_peers_print(awdl_peers_t peers, char *str, int len) {
char *cur = str, *const end = str + len;
map_t map = (map_t) peers;
map_it_t it = hashmap_it_new(map);
struct awdl_peer *peer;
while (hashmap_it_next(it, NULL, (any_t *) &peer) == MAP_OK) {
cur += awdl_peer_print(peer, cur, end - cur);
cur += snprintf(cur, end - cur, "\n");
}
cur += awdl_peer_print(peer, cur, end - cur);
cur += snprintf(cur, cur < end ? end - cur : 0, "\n");
}
hashmap_it_free(it);
return end - cur;
return cur - str;
}
void awdl_peers_remove(awdl_peers_t peers, uint64_t before, awdl_peer_cb cb, void *arg) {

+ 2
- 2
src/peers.h View File

@ -76,7 +76,7 @@ enum peers_status awdl_peer_remove(awdl_peers_t peers, const struct ether_addr *
enum peers_status awdl_peer_get(awdl_peers_t peers, const struct ether_addr *addr, struct awdl_peer **peer);
int awdl_peer_print(const struct awdl_peer *peer, char *str, size_t len);
int awdl_peer_print(const struct awdl_peer *peer, char *str, int len);
/**
* Apply callback to and then remove all peers matching a filter
@ -87,7 +87,7 @@ int awdl_peer_print(const struct awdl_peer *peer, char *str, size_t len);
*/
void awdl_peers_remove(awdl_peers_t peers, uint64_t before, awdl_peer_cb cb, void *arg);
int awdl_peers_print(awdl_peers_t peers, char *str, size_t len);
int awdl_peers_print(awdl_peers_t peers, char *str, int len);
/* Iterator functions */
typedef void *awdl_peers_it_t;

+ 40
- 16
tests/test_awdl_peers.cpp View File

@ -23,8 +23,10 @@ extern "C" {
#include "gtest/gtest.h"
static const struct ether_addr TEST_ADDR = {{ 0xc0, 0xff, 0xee, 0xc0, 0xff, 0xee }};
static const struct ether_addr TEST_ADDR2 = {{ 0xc0, 0xff, 0xff, 0xc0, 0xff, 0xff }};
#define TEST_ADDR(i) static const struct ether_addr TEST_ADDR##i = {{ i, i, i, i, i, i }}
TEST_ADDR(0);
TEST_ADDR(1);
TEST(awdl_peers, init_free) {
awdl_peers_t p = awdl_peers_init();
@ -36,7 +38,7 @@ TEST(awdl_peers, init_free) {
TEST(awdl_peers, add) {
int s;
awdl_peers_t p = awdl_peers_init();
s = awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
s = awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
EXPECT_EQ(s, PEERS_OK);
EXPECT_EQ(awdl_peers_length(p), 1);
awdl_peers_free(p);
@ -45,10 +47,10 @@ TEST(awdl_peers, add) {
TEST(awdl_peers, add_two) {
int s;
awdl_peers_t p = awdl_peers_init();
s = awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
s = awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
EXPECT_EQ(s, PEERS_OK);
EXPECT_EQ(awdl_peers_length(p), 1);
s = awdl_peer_add(p, &TEST_ADDR2, 0, NULL, NULL);
s = awdl_peer_add(p, &TEST_ADDR1, 0, NULL, NULL);
EXPECT_EQ(s, PEERS_OK);
EXPECT_EQ(awdl_peers_length(p), 2);
awdl_peers_free(p);
@ -57,10 +59,10 @@ TEST(awdl_peers, add_two) {
TEST(awdl_peers, add_same) {
int s;
awdl_peers_t p = awdl_peers_init();
s = awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
s = awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
EXPECT_EQ(s, PEERS_OK);
EXPECT_EQ(awdl_peers_length(p), 1);
s = awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
s = awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
EXPECT_EQ(s, PEERS_UPDATE);
EXPECT_EQ(awdl_peers_length(p), 1);
awdl_peers_free(p);
@ -69,8 +71,8 @@ TEST(awdl_peers, add_same) {
TEST(awdl_peers, remove) {
int s;
awdl_peers_t p = awdl_peers_init();
awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
s = awdl_peer_remove(p, &TEST_ADDR, NULL, NULL);
awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
s = awdl_peer_remove(p, &TEST_ADDR0, NULL, NULL);
EXPECT_EQ(s, PEERS_OK);
EXPECT_EQ(awdl_peers_length(p), 0);
awdl_peers_free(p);
@ -79,7 +81,7 @@ TEST(awdl_peers, remove) {
TEST(awdl_peers, remove_empty) {
int s;
awdl_peers_t p = awdl_peers_init();
s = awdl_peer_remove(p, &TEST_ADDR, NULL, NULL);
s = awdl_peer_remove(p, &TEST_ADDR0, NULL, NULL);
EXPECT_EQ(s, PEERS_MISSING);
EXPECT_EQ(awdl_peers_length(p), 0);
awdl_peers_free(p);
@ -88,9 +90,9 @@ TEST(awdl_peers, remove_empty) {
TEST(awdl_peers, remove_twice) {
int s;
awdl_peers_t p = awdl_peers_init();
awdl_peer_add(p, &TEST_ADDR, 0, NULL, NULL);
awdl_peer_remove(p, &TEST_ADDR, NULL, NULL);
s = awdl_peer_remove(p, &TEST_ADDR, NULL, NULL);
awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
awdl_peer_remove(p, &TEST_ADDR0, NULL, NULL);
s = awdl_peer_remove(p, &TEST_ADDR0, NULL, NULL);
EXPECT_EQ(s, PEERS_MISSING);
EXPECT_EQ(awdl_peers_length(p), 0);
awdl_peers_free(p);
@ -101,15 +103,15 @@ static int count_cb = 0;
static void test_cb(struct awdl_peer *p, void *ignore) {
(void) ignore;
count_cb++;
EXPECT_TRUE(!memcmp(&TEST_ADDR, &p->addr, sizeof(struct ether_addr)));
EXPECT_TRUE(!memcmp(&TEST_ADDR0, &p->addr, sizeof(struct ether_addr)));
}
TEST(awdl_peers, remove_timedout) {
struct awdl_peer *peer = NULL;
uint64_t now = 0;
awdl_peers_t p = awdl_peers_init();
awdl_peer_add(p, &TEST_ADDR, now, NULL, NULL);
awdl_peer_get(p, &TEST_ADDR, &peer);
awdl_peer_add(p, &TEST_ADDR0, now, NULL, NULL);
awdl_peer_get(p, &TEST_ADDR0, &peer);
peer->is_valid = 1;
EXPECT_EQ(awdl_peers_length(p), 1);
awdl_peers_remove(p, now, test_cb, NULL);
@ -121,3 +123,25 @@ TEST(awdl_peers, remove_timedout) {
EXPECT_EQ(count_cb, 1);
awdl_peers_free(p);
}
TEST(awdl_peers, print) {
char buf[1000]; // Buffer is large enough
awdl_peers_t p = awdl_peers_init();
awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
awdl_peer_add(p, &TEST_ADDR1, 0, NULL, NULL);
int len = awdl_peers_print(p, buf, sizeof(buf));
EXPECT_EQ(len, strlen(buf));
EXPECT_STREQ(buf, "<UNNAMED>: 0:0:0:0:0:0 (met 60, ctr 0)\n"
"<UNNAMED>: 1:1:1:1:1:1 (met 60, ctr 0)\n");
}
TEST(awdl_peers, print_overflow) {
char buf[30]; // Buffer is too small
awdl_peers_t p = awdl_peers_init();
awdl_peer_add(p, &TEST_ADDR0, 0, NULL, NULL);
awdl_peer_add(p, &TEST_ADDR1, 0, NULL, NULL);
int len = awdl_peers_print(p, buf, sizeof(buf));
EXPECT_GT(len, strlen(buf)); // would have written more
EXPECT_EQ(strlen(buf), sizeof(buf) - 1); // buffer is full
EXPECT_STREQ(buf, "<UNNAMED>: 0:0:0:0:0:0 (met 6");
}

Loading…
Cancel
Save