diff options
-rw-r--r-- | src/selftest/allowedips.h | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/selftest/allowedips.h b/src/selftest/allowedips.h index 0ca33e6..5ad7612 100644 --- a/src/selftest/allowedips.h +++ b/src/selftest/allowedips.h @@ -199,6 +199,8 @@ static __init bool randomized_test(void) struct horrible_allowedips h; u8 ip[16], mutate_mask[16], mutated[16]; + mutex_init(&mutex); + allowedips_init(&t); horrible_allowedips_init(&h); @@ -216,6 +218,8 @@ static __init bool randomized_test(void) kref_init(&peers[i]->refcount); } + mutex_lock(&mutex); + for (i = 0; i < NUM_RAND_ROUTES; ++i) { prandom_bytes(ip, 4); cidr = prandom_u32_max(32) + 1; @@ -288,6 +292,8 @@ static __init bool randomized_test(void) } } + mutex_unlock(&mutex); + #ifdef DEBUG_PRINT_TRIE_GRAPHVIZ print_tree(t.root4, 32); print_tree(t.root6, 128); @@ -311,7 +317,9 @@ static __init bool randomized_test(void) ret = true; free: + mutex_lock(&mutex); allowedips_free(&t, &mutex); + mutex_unlock(&mutex); horrible_allowedips_free(&h); if (peers) { for (i = 0; i < NUM_PEERS; ++i) @@ -343,6 +351,34 @@ static __init inline struct in6_addr *ip6(u32 a, u32 b, u32 c, u32 d) return &ip; } +struct walk_ctx { + int count; + bool found_a, found_b, found_c, found_d, found_e; + bool found_other; +}; + +static __init int walk_callback(void *ctx, const u8 *ip, u8 cidr, int family) +{ + struct walk_ctx *wctx = ctx; + + wctx->count++; + + if (cidr == 27 && !memcmp(ip, ip4(192, 95, 5, 64), sizeof(struct in_addr))) + wctx->found_a = true; + else if (cidr == 128 && !memcmp(ip, ip6(0x26075300, 0x60006b00, 0, 0xc05f0543), sizeof(struct in6_addr))) + wctx->found_b = true; + else if (cidr == 29 && !memcmp(ip, ip4(10, 1, 0, 16), sizeof(struct in_addr))) + wctx->found_c = true; + else if (cidr == 83 && !memcmp(ip, ip6(0x26075300, 0x6d8a6bf8, 0xdab1e000, 0), sizeof(struct in6_addr))) + wctx->found_d = true; + else if (cidr == 21 && !memcmp(ip, ip6(0x26075000, 0, 0, 0), sizeof(struct in6_addr))) + wctx->found_e = true; + else + wctx->found_other = true; + + return 0; +} + #define init_peer(name) do { \ name = kzalloc(sizeof(struct wireguard_peer), GFP_KERNEL); \ if (!name) { \ @@ -372,10 +408,17 @@ static __init inline struct in6_addr *ip6(u32 a, u32 b, u32 c, u32 d) maybe_fail \ } while (0) +#define test_boolean(cond) do { \ + bool _s = (cond); \ + maybe_fail \ +} while (0) + bool __init allowedips_selftest(void) { DEFINE_MUTEX(mutex); struct allowedips t; + struct walk_ctx wctx = { 0 }; + struct allowedips_cursor cursor = { 0 }; struct wireguard_peer *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL, *h = NULL; size_t i = 0; bool success = false; @@ -383,7 +426,6 @@ bool __init allowedips_selftest(void) __be64 part; mutex_init(&mutex); - mutex_lock(&mutex); allowedips_init(&t); @@ -483,6 +525,23 @@ bool __init allowedips_selftest(void) allowedips_insert_v6(&t, &ip, 128, a, &mutex); } + allowedips_free(&t, &mutex); + + allowedips_init(&t); + insert(4, a, 192, 95, 5, 93, 27); + insert(6, a, 0x26075300, 0x60006b00, 0, 0xc05f0543, 128); + insert(4, a, 10, 1, 0, 20, 29); + insert(6, a, 0x26075300, 0x6d8a6bf8, 0xdab1f1df, 0xc05f1523, 83); + insert(6, a, 0x26075300, 0x6d8a6bf8, 0xdab1f1df, 0xc05f1523, 21); + allowedips_walk_by_peer(&t, &cursor, a, walk_callback, &wctx, &mutex); + test_boolean(wctx.count == 5); + test_boolean(wctx.found_a); + test_boolean(wctx.found_b); + test_boolean(wctx.found_c); + test_boolean(wctx.found_d); + test_boolean(wctx.found_e); + test_boolean(!wctx.found_other); + #ifdef DEBUG_RANDOM_TRIE if (success) success = randomized_test(); |