diff options
| author | Denys Fedoryshchenko <denys.f@collabora.com> | 2026-09-28 21:50:27 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-28 21:50:27 +0300 |
| commit | 50c54c477624d5a9b668225fde2b9844d362760a (patch) | |
| tree | d1fe53a3c637f8b963027aa483053ae7afb55b5e /tests/unit/vrf_safeguards_test.c | |
| parent | c9d938173838d6955c824d141f24672d7037eb4e (diff) | |
| parent | 06aa0ef737d6993fd18e7db1bc8fc35601079505 (diff) | |
| download | accel-ppp-master.tar.gz accel-ppp-master.zip | |
Additional safeguards
Diffstat (limited to 'tests/unit/vrf_safeguards_test.c')
| -rw-r--r-- | tests/unit/vrf_safeguards_test.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/unit/vrf_safeguards_test.c b/tests/unit/vrf_safeguards_test.c new file mode 100644 index 00000000..1fa5a414 --- /dev/null +++ b/tests/unit/vrf_safeguards_test.c @@ -0,0 +1,72 @@ +/* Exercise the shared interface lookup and length-delimited VRF API. */ +#include <assert.h> +#include <pthread.h> +#include "triton.h" +#undef DEFINE_INIT +#undef __init +#define __init +#define DEFINE_INIT(o, f) +#include "../../accel-pppd/net.c" +#include "../../accel-pppd/ifcfg.c" +#define init coa_init +#include "../../accel-pppd/radius/dm_coa.c" +#undef init + +static int lookups, changes, last_master; +static char last_name[IFNAMSIZ]; +void log_ppp_error(const char *fmt, ...) {} +void log_ppp_info2(const char *fmt, ...) {} + +static int lookup(const char *name) +{ + lookups++; + assert(strlen(name) < IFNAMSIZ); + strcpy(last_name, name); + return 7; +} +static int set_vrf(int index, int master) +{ + changes++; + last_master = master; + return 0; +} + +int main(void) +{ + struct kern_net kn = {0}; + struct ap_net backend = {.get_ifindex = lookup, .set_vrf = set_vrf}; + struct ap_session ses = {.net = &backend}; + struct radius_pd_t rpd = {.ses = &ses}; + char name[IFNAMSIZ + 1]; + + net = &kn.net; + kn.sock = socket(AF_INET, SOCK_DGRAM, 0); + assert(kn.sock >= 0); + assert(def_get_ifindex("lo") > 0); + assert(def_get_ifindex(NULL) == -1); + memset(name, 'x', sizeof(name)); + name[IFNAMSIZ] = 0; + assert(def_get_ifindex(name) == -1); + assert(ap_session_vrf(&ses, name, -1) == -1); + assert(ap_session_vrf(&ses, name, IFNAMSIZ) == -1); + assert(ap_session_vrf(&ses, NULL, 1) == -1); + assert(ap_session_vrf(&ses, name, -2) == -1); + assert(ap_session_vrf(&ses, "a\0b", 3) == -1); + assert(!lookups && !changes); + /* Explicit length need not be followed by a NUL in caller memory. */ + assert(!ap_session_vrf(&ses, name, IFNAMSIZ - 1)); + assert(strlen(last_name) == IFNAMSIZ - 1 && last_master == 7); + assert(!ap_session_vrf(&ses, NULL, 0)); + assert(last_master == 0 && !ses.vrf_name); + assert(!ap_session_vrf(&ses, NULL, -1)); + assert(lookups == 1 && changes == 3); + assert(!rad_update_vrf(&rpd, "blue\0junk", 9)); + assert(!rad_update_vrf(&rpd, name, IFNAMSIZ)); + assert(rad_update_vrf(&rpd, "0", 1)); + assert(last_master == 0); + assert(rad_update_vrf(&rpd, "0blue", 5)); + assert(last_master == 7 && !strcmp(last_name, "0blue")); + close(kn.sock); + puts("VRF safeguards: PASS"); + return 0; +} |
