2022-11-05 15:29:00 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Simple Network Management Protocol (SNMP) Unit tests
|
|
|
|
*
|
|
|
|
* (c) 2022 Vojtech Vilimek <vojtech.vilimek@nic.cz>
|
|
|
|
* (c) 2022 CZ.NIC z.s.p.o
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test/birdtest.h"
|
2023-03-24 14:02:23 +00:00
|
|
|
#include "test/bt-utils.h"
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
#include "bgp_mib.h"
|
|
|
|
#include "subagent.h"
|
|
|
|
#include "snmp.h"
|
2022-11-15 15:29:03 +00:00
|
|
|
#include "snmp_utils.h"
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
#define SNMP_EXPECTED(actual, expected) \
|
|
|
|
bt_debug("%s expected: %3u actual: %3u\n", \
|
|
|
|
#expected, expected, actual);
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
#define OID_ALLOCATE(size) mb_alloc(&root_pool, sizeof(struct oid) + (size) * sizeof (u32))
|
2022-11-05 15:29:00 +00:00
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
#define OID_INIT(oid, n_subid_, prefix_, include_, arr_) \
|
|
|
|
(oid)->n_subid = (n_subid_); \
|
|
|
|
(oid)->prefix = (prefix_); \
|
|
|
|
(oid)->include = (include_); \
|
|
|
|
memcpy((oid)->ids, (arr_), sizeof(arr_)); \
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
void
|
2022-11-15 15:29:03 +00:00
|
|
|
test_fill(struct snmp_proto *p)
|
2022-11-05 15:29:00 +00:00
|
|
|
{
|
2022-11-15 15:29:03 +00:00
|
|
|
((struct proto *) p)->pool = &root_pool;
|
2022-11-05 15:29:00 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
static void UNUSED
|
2022-11-05 15:29:00 +00:00
|
|
|
test_oid(struct oid *oid, uint base_size)
|
|
|
|
{
|
2023-11-15 11:37:10 +00:00
|
|
|
#if 0
|
2022-11-05 15:29:00 +00:00
|
|
|
/* tests all states one by one */
|
|
|
|
|
|
|
|
oid->n_subid = base_size + 2;
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 0] = 1;
|
|
|
|
oid->ids[base_size + 1] = 15; // BGP4-MIB::bgp
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_BGP);
|
|
|
|
|
|
|
|
oid->n_subid = base_size + 3;
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 2] = 1; // BGP4-MIB::bgpVersion
|
2023-07-26 12:34:55 +00:00
|
|
|
snmp_oid_dump(oid);
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_VERSION);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 2] = 2; // BGP4-MIB::bgpLocalAs
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_LOCAL_AS);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 2] = 3; // BGP4-MIB::bgpPeerTable
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_PEER_TABLE);
|
|
|
|
|
|
|
|
bt_debug("testing BGP4-MIB::bgpPeerEntry\n");
|
|
|
|
oid->n_subid = base_size + 4;
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_debug("arith\n");
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 2] = 3;
|
|
|
|
oid->ids[base_size + 3] = 1; // BGP4-MIB::bgpPeerEntry
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_debug("dumpping\n");
|
|
|
|
bt_debug("after dump, assertion\n");
|
|
|
|
// SNMP_EXPECTED(snmp_bgp_state(oid), BGP_INTERNAL_PEER_ENTRY);
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_PEER_ENTRY);
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_debug("finish\n");
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
oid->n_subid = base_size + 5;
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 2] = 3;
|
|
|
|
oid->ids[base_size + 3] = 1;
|
|
|
|
oid->ids[base_size + 4] = 1; // BGP4-MIB::bgpPeerIdentifier
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_IDENTIFIER);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 2; // BGP4-MIB::bgpPeerState
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_STATE);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 3; // BGP4-MIB::bgpPeerAdminStatus
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_ADMIN_STATUS);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
|
|
|
bt_debug(" 1/4\n");
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 4; // BGP4-MIB::bgpPeerNegotiatedVersion
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_NEGOTIATED_VERSION);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 5; // BGP4-MIB::bgpPeerLocalAddr
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_LOCAL_ADDR);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 6; // BGP4-MIB::bgpPeerLocalPort
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_LOCAL_PORT);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 7; // BGP4-MIB::bgpPeerRemoteAddr
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_REMOTE_ADDR);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 8; // BGP4-MIB::bgpPeerRemotePort
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_REMOTE_PORT);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 9; // BGP4-MIB::bgpPeerRemoteAs
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_REMOTE_AS);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 10; // BGP4-MIB::bgpPeerInUpdates
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RX_UPDATES);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
|
|
|
bt_debug(" 1/2 \n");
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 11; // BGP4-MIB::bgpPeerOutUpdates
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_TX_UPDATES);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 12; // BGP4-MIB::bgpPeerInTotalMessages
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RX_MESSAGES);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 13; // BGP4-MIB::bgpPeerOutTotalMessages
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_TX_MESSAGES);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 14; // BGP4-MIB::bgpPeerLastError
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_LAST_ERROR);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 15; // BGP4-MIB::bgpPeerFsmEstablishedTransitions
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_FSM_TRANSITIONS);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 16; // BGP4-MIB::bgpPeerFsmEstablishedTime
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_FSM_ESTABLISHED_TIME);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 17; // BGP4-MIB::bgpPeerConnectionRetryInterval
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RETRY_INTERVAL);
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_debug( " 3/4\n");
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 18; // BGP4-MIB::bgpPeerHoldTime
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_HOLD_TIME);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 19; // BGP4-MIB::bgpPeerKeepAlive
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_KEEPALIVE);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 20; // BGP4-MIB::bgpPeerHoldTimeConfigured
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_HOLD_TIME_CONFIGURED);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 21; // BGP4-MIB::bgpPeerKeepAliveConfigured
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_KEEPALIVE_CONFIGURED);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 22; // BGP4-MIB::bgpPeerMinASOriginationInterval
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_ORIGINATION_INTERVAL);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 23; // BGP4-MIB::bgpPeerMinRouteAdvertisementInverval
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_MIN_ROUTE_ADVERTISEMENT);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
oid->ids[base_size + 4] = 24; // BGP4-MIB::bgpPeerInUpdateElapsedTime
|
2022-11-05 15:29:00 +00:00
|
|
|
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_IN_UPDATE_ELAPSED_TIME);
|
2023-03-24 14:02:23 +00:00
|
|
|
|
|
|
|
bt_debug("testing BGP4-MIB::bgpPeerEntry end\n");
|
2023-11-15 11:37:10 +00:00
|
|
|
#endif
|
2022-11-05 15:29:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
static int
|
|
|
|
t_s_is_oid_empty(void)
|
|
|
|
{
|
|
|
|
bt_assert(snmp_is_oid_empty(NULL) == 0);
|
|
|
|
|
|
|
|
struct oid *blank = mb_alloc(&root_pool, sizeof(struct oid));
|
|
|
|
blank->n_subid = 0;
|
|
|
|
blank->prefix = 0;
|
|
|
|
blank->include = 0;
|
|
|
|
|
|
|
|
bt_assert(snmp_is_oid_empty(blank) == 1);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(blank); blank = NULL;
|
|
|
|
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *prefixed = mb_alloc(&root_pool, sizeof(struct oid) + 3 * sizeof(u32));
|
|
|
|
prefixed->n_subid = 3;
|
|
|
|
prefixed->prefix = 100;
|
|
|
|
prefixed->include = 1;
|
|
|
|
|
|
|
|
u32 prefixed_arr[] = { ~((u32) 0), 0, 256 };
|
|
|
|
memcpy(&prefixed->ids, prefixed_arr, sizeof(prefixed_arr) /
|
|
|
|
sizeof(prefixed_arr[0]));
|
|
|
|
|
|
|
|
bt_assert(snmp_is_oid_empty(prefixed) == 0);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(prefixed); prefixed = NULL;
|
|
|
|
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *to_prefix = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32));
|
|
|
|
to_prefix->n_subid = 8;
|
|
|
|
to_prefix->prefix = 0;
|
|
|
|
to_prefix->include = 1;
|
|
|
|
|
|
|
|
u32 to_prefix_arr[] = {1, 3, 6, 1, 100, ~((u32) 0), 0, 256 };
|
|
|
|
memcpy(&to_prefix->n_subid, to_prefix_arr, sizeof(to_prefix_arr) /
|
|
|
|
sizeof(to_prefix_arr[0]));
|
|
|
|
|
|
|
|
bt_assert(snmp_is_oid_empty(to_prefix) == 0);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(to_prefix); to_prefix = NULL;
|
|
|
|
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *unprefixable = mb_alloc(&root_pool, sizeof(struct oid) + 2 * sizeof(u32));
|
|
|
|
unprefixable->n_subid = 2;
|
|
|
|
unprefixable->prefix = 0;
|
|
|
|
unprefixable->include = 0;
|
|
|
|
|
|
|
|
u32 unpref[] = { 65535, 4 };
|
|
|
|
memcpy(&unprefixable->ids, unpref, sizeof(unpref) / sizeof(unpref[0]));
|
|
|
|
|
|
|
|
bt_assert(snmp_is_oid_empty(unprefixable) == 0);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(unprefixable); unprefixable = NULL;
|
|
|
|
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *unprefixable2 = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32));
|
|
|
|
unprefixable2->n_subid = 8;
|
|
|
|
unprefixable2->prefix = 0;
|
|
|
|
unprefixable2->include = 1;
|
|
|
|
|
|
|
|
u32 unpref2[] = { 1, 3, 6, 2, 1, 2, 15, 6 };
|
|
|
|
memcpy(&unprefixable2->ids, unpref2, sizeof(unpref2) / sizeof(unpref2[0]));
|
|
|
|
|
|
|
|
bt_assert(snmp_is_oid_empty(unprefixable2) == 0);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(unprefixable2); unprefixable2 = NULL;
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_s_prefixize(void)
|
|
|
|
{
|
2023-07-26 12:34:55 +00:00
|
|
|
//struct oid *nulled = NULL;
|
2022-11-15 15:29:03 +00:00
|
|
|
struct snmp_proto snmp_proto;
|
|
|
|
test_fill(&snmp_proto);
|
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
//struct oid *result = snmp_prefixize(&snmp_proto, nulled);
|
2023-07-26 12:34:55 +00:00
|
|
|
//bt_assert(NULL == result);
|
|
|
|
//result != NULL ? mb_free(result) : NULL;
|
|
|
|
struct oid *result;
|
2023-03-31 07:56:03 +00:00
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *blank = mb_allocz(&root_pool, sizeof(struct oid));
|
|
|
|
/* here the byte order should not matter */
|
2023-11-15 11:37:10 +00:00
|
|
|
result = snmp_prefixize(&snmp_proto, blank);
|
2023-03-31 07:56:03 +00:00
|
|
|
bt_assert(snmp_is_oid_empty(result) == 1);
|
|
|
|
|
|
|
|
mb_free(result); result = NULL;
|
|
|
|
mb_free(blank); blank = NULL;
|
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
|
|
|
|
struct oid *prefixed = mb_alloc(&root_pool, sizeof(struct oid) + 3 * sizeof(u32));
|
|
|
|
prefixed->n_subid = 3;
|
|
|
|
prefixed->prefix = 100;
|
|
|
|
prefixed->include = 1;
|
|
|
|
|
|
|
|
u32 prefixed_arr[] = { ~((u32) 0), 0, 256 };
|
2023-03-24 14:02:23 +00:00
|
|
|
memcpy(&prefixed->ids, prefixed_arr, sizeof(prefixed_arr));
|
2022-11-15 15:29:03 +00:00
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
/* struct oid */result = snmp_prefixize(&snmp_proto, prefixed);
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_assert(memcmp(result, prefixed, snmp_oid_size(prefixed)) == 0);
|
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(result); result = NULL;
|
2023-07-26 12:34:55 +00:00
|
|
|
//mb_free(prefixed); prefixed = NULL;
|
2023-03-31 07:56:03 +00:00
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
|
|
|
|
struct oid *to_prefix = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32));
|
|
|
|
to_prefix->n_subid = 8;
|
|
|
|
to_prefix->prefix = 0;
|
|
|
|
to_prefix->include = 1;
|
|
|
|
|
|
|
|
u32 to_prefix_arr[] = {1, 3, 6, 1, 100, ~((u32) 0), 0, 256 };
|
2023-03-24 14:02:23 +00:00
|
|
|
memcpy(to_prefix->ids, to_prefix_arr, sizeof(to_prefix_arr));
|
2022-11-15 15:29:03 +00:00
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
result = snmp_prefixize(&snmp_proto, to_prefix);
|
2023-03-31 07:56:03 +00:00
|
|
|
|
|
|
|
bt_assert(memcmp(result, prefixed, snmp_oid_size(prefixed)) == 0);
|
|
|
|
|
|
|
|
mb_free(result); result = NULL;
|
|
|
|
mb_free(to_prefix); to_prefix = NULL;
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
|
|
|
|
struct oid *unprefixable = mb_alloc(&root_pool, sizeof(struct oid) + 2 * sizeof(u32));
|
|
|
|
unprefixable->n_subid = 2;
|
|
|
|
unprefixable->prefix = 0;
|
|
|
|
unprefixable->include = 0;
|
|
|
|
|
|
|
|
u32 unpref[] = { 65535, 4 };
|
|
|
|
memcpy(&unprefixable->ids, unpref, sizeof(unpref) / sizeof(unpref[0]));
|
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
result = snmp_prefixize(&snmp_proto, unprefixable);
|
2023-03-31 07:56:03 +00:00
|
|
|
bt_assert(result == NULL);
|
|
|
|
|
2023-07-26 12:34:55 +00:00
|
|
|
result != NULL ? mb_free(result) : NULL;
|
2022-11-15 15:29:03 +00:00
|
|
|
|
|
|
|
struct oid *unprefixable2 = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32));
|
|
|
|
unprefixable2->n_subid = 8;
|
|
|
|
unprefixable2->prefix = 0;
|
|
|
|
unprefixable2->include = 1;
|
|
|
|
|
|
|
|
u32 unpref2[] = { 1, 3, 6, 2, 1, 2, 15, 6 };
|
|
|
|
memcpy(&unprefixable2->ids, unpref2, sizeof(unpref2) / sizeof(unpref2[0]));
|
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
result = snmp_prefixize(&snmp_proto, unprefixable2);
|
2023-03-31 07:56:03 +00:00
|
|
|
bt_assert(result == NULL);
|
|
|
|
|
2023-07-26 12:34:55 +00:00
|
|
|
result != NULL ? mb_free(result) : NULL;
|
2022-11-15 15:29:03 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
static int
|
|
|
|
t_oid_compare(void)
|
|
|
|
{
|
|
|
|
/* same length, no prefix */
|
|
|
|
struct oid *l1 = OID_ALLOCATE(5);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 4, 5 };
|
|
|
|
OID_INIT(l1, 5, 0, 1, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct oid *r1 = OID_ALLOCATE(5);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 4, 6 };
|
|
|
|
OID_INIT(r1, 5, 0, 0, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == -1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == 1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
|
|
|
/* same results for prefixed oids */
|
|
|
|
l1->prefix = 1;
|
|
|
|
r1->prefix = 1;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == -1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == 1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
/* different prefix -- has higher priority */
|
|
|
|
l1->prefix = 8;
|
|
|
|
r1->prefix = 4;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == 1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == -1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
mb_free(l1);
|
|
|
|
mb_free(r1);
|
|
|
|
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
/* different length, no prefix */
|
|
|
|
l1 = OID_ALLOCATE(4);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 4 };
|
|
|
|
OID_INIT(l1, 4, 0, 0, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 = OID_ALLOCATE(5);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 4, 1 };
|
2023-03-24 14:02:23 +00:00
|
|
|
OID_INIT(r1, 5, 0, 1, arr);
|
2022-11-29 15:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == -1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == 1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
|
|
|
/* same results for prefixed oids */
|
|
|
|
l1->prefix = 3;
|
|
|
|
r1->prefix = 3;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == -1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == 1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
/* different prefix -- has higher priority */
|
|
|
|
l1->prefix = 17;
|
|
|
|
r1->prefix = 14;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == 1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == -1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
mb_free(l1);
|
|
|
|
mb_free(r1);
|
|
|
|
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
|
|
|
|
/* inverse order different length, no prefix */
|
2022-11-29 15:30:20 +00:00
|
|
|
l1 = OID_ALLOCATE(4);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 5 };
|
|
|
|
OID_INIT(l1, 4, 0, 0, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 = OID_ALLOCATE(5);
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 3, 4, 1 };
|
|
|
|
OID_INIT(r1, 5, 0, 0, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == 1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == -1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
|
|
|
/* same results for prefixed oids */
|
|
|
|
l1->prefix = 254;
|
|
|
|
r1->prefix = 254;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == 1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == -1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
/* different prefix -- has higher priority */
|
|
|
|
l1->prefix = 127;
|
|
|
|
r1->prefix = 35;
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == 1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == -1);
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
mb_free(l1);
|
|
|
|
mb_free(r1);
|
|
|
|
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
/* ==== MIXED PREFIXED / NON PREFIXED OID compare ==== */
|
|
|
|
/* same length, mixed */
|
|
|
|
l1 = OID_ALLOCATE(6); /* OID .1.2.17.3.21.4 */
|
|
|
|
{
|
|
|
|
u32 arr[] = { 1, 2, 17, 3, 21, 4 };
|
|
|
|
OID_INIT(l1, 6, 0, 1, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 = OID_ALLOCATE(1); /* OID .1.3.6.1.5.3 */
|
|
|
|
{
|
|
|
|
u32 arr[] = { 3 };
|
2023-03-24 14:02:23 +00:00
|
|
|
OID_INIT(r1, 1, 5, 1, arr);
|
2022-11-29 15:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, r1) == -1);
|
|
|
|
bt_assert(snmp_oid_compare(r1, l1) == 1);
|
|
|
|
|
|
|
|
bt_assert(snmp_oid_compare(l1, l1) == 0);
|
|
|
|
bt_assert(snmp_oid_compare(r1, r1) == 0);
|
|
|
|
|
|
|
|
mb_free(l1);
|
|
|
|
mb_free(r1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-05 15:29:00 +00:00
|
|
|
static int
|
|
|
|
t_s_bgp_state(void)
|
|
|
|
{
|
2022-11-15 15:29:03 +00:00
|
|
|
struct oid *oid = mb_alloc(&root_pool, sizeof(struct oid) + 10 * sizeof(u32));
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
/* oid header */
|
|
|
|
oid->n_subid = 0;
|
|
|
|
oid->prefix = 2;
|
|
|
|
oid->include = 0;
|
|
|
|
oid->pad = 0;
|
|
|
|
|
|
|
|
/* test all states with expected oid length */
|
|
|
|
bt_debug("testing precise oids\n");
|
|
|
|
test_oid(oid, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
oid->ids[i] = (u32) bt_random();
|
|
|
|
|
|
|
|
/* if this subid is too high it does not match the test case
|
|
|
|
* in general test_oid() func
|
|
|
|
*/
|
|
|
|
oid->ids[2] = 0;
|
|
|
|
|
|
|
|
/* test all states with garbage ip */
|
|
|
|
bt_debug("testing oids with random ip index\n");
|
2023-07-26 12:34:55 +00:00
|
|
|
test_oid(oid, 0);
|
2022-11-05 15:29:00 +00:00
|
|
|
|
|
|
|
/* test all states with invalid ip */
|
|
|
|
bt_debug("testing oids with invalid ip index\n");
|
|
|
|
/* zero the states that overlap */
|
|
|
|
oid->ids[2] = 0;
|
|
|
|
oid->ids[3] = 0;
|
|
|
|
oid->ids[4] = 0;
|
|
|
|
|
|
|
|
oid->ids[5] = 0;
|
|
|
|
oid->ids[6] = 257;
|
|
|
|
oid->ids[7] = 127;
|
|
|
|
oid->ids[8] = 0xFFFF;
|
2023-07-26 12:34:55 +00:00
|
|
|
test_oid(oid, 0);
|
2022-11-05 15:29:00 +00:00
|
|
|
|
2023-03-31 07:56:03 +00:00
|
|
|
mb_free(oid);
|
|
|
|
|
2022-11-05 15:29:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
bt_init(argc, argv);
|
|
|
|
|
2023-03-24 14:02:23 +00:00
|
|
|
bt_bird_init();
|
|
|
|
|
2023-11-15 11:37:10 +00:00
|
|
|
//bt_test_suite(t_s_bgp_state, "Function snmp_bgp_state()");
|
2022-11-05 15:29:00 +00:00
|
|
|
|
2022-11-15 15:29:03 +00:00
|
|
|
bt_test_suite(t_s_is_oid_empty, "Function snmp_is_oid_empty()");
|
|
|
|
|
|
|
|
bt_test_suite(t_s_prefixize, "Function snmp_prefixize()");
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
bt_test_suite(t_oid_compare, "Function snmp_oid_compare()");
|
|
|
|
|
2022-11-05 15:29:00 +00:00
|
|
|
return bt_exit_value();
|
|
|
|
}
|