0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

tmp: minor changes + fixed tests

This commit is contained in:
Vojtech Vilimek 2023-03-24 15:02:23 +01:00
parent 3ec802e7f0
commit ed676cd1c5
2 changed files with 114 additions and 52 deletions

View File

@ -8,6 +8,7 @@
*/ */
#include "test/birdtest.h" #include "test/birdtest.h"
#include "test/bt-utils.h"
#include "bgp_mib.h" #include "bgp_mib.h"
#include "subagent.h" #include "subagent.h"
@ -60,11 +61,14 @@ test_oid(struct oid *oid, uint base_size)
bt_debug("testing BGP4-MIB::bgpPeerEntry\n"); bt_debug("testing BGP4-MIB::bgpPeerEntry\n");
oid->n_subid = base_size + 4; oid->n_subid = base_size + 4;
bt_debug("arith\n");
oid->ids[2] = 3; oid->ids[2] = 3;
oid->ids[3] = 1; // BGP4-MIB::bgpPeerEntry oid->ids[3] = 1; // BGP4-MIB::bgpPeerEntry
snmp_oid_dump(oid); bt_debug("dumpping\n");
SNMP_EXPECTED(snmp_bgp_state(oid), BGP_INTERNAL_PEER_ENTRY); bt_debug("after dump, assertion\n");
// SNMP_EXPECTED(snmp_bgp_state(oid), BGP_INTERNAL_PEER_ENTRY);
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_PEER_ENTRY); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_PEER_ENTRY);
bt_debug("finish\n");
oid->n_subid = base_size + 5; oid->n_subid = base_size + 5;
oid->ids[2] = 3; oid->ids[2] = 3;
@ -78,6 +82,7 @@ test_oid(struct oid *oid, uint base_size)
oid->ids[4] = 3; // BGP4-MIB::bgpPeerAdminStatus oid->ids[4] = 3; // BGP4-MIB::bgpPeerAdminStatus
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_ADMIN_STATUS); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_ADMIN_STATUS);
bt_debug(" 1/4\n");
oid->ids[4] = 4; // BGP4-MIB::bgpPeerNegotiatedVersion oid->ids[4] = 4; // BGP4-MIB::bgpPeerNegotiatedVersion
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_NEGOTIATED_VERSION); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_NEGOTIATED_VERSION);
@ -99,6 +104,7 @@ test_oid(struct oid *oid, uint base_size)
oid->ids[4] = 10; // BGP4-MIB::bgpPeerInUpdates oid->ids[4] = 10; // BGP4-MIB::bgpPeerInUpdates
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RX_UPDATES); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RX_UPDATES);
bt_debug(" 1/2 \n");
oid->ids[4] = 11; // BGP4-MIB::bgpPeerOutUpdates oid->ids[4] = 11; // BGP4-MIB::bgpPeerOutUpdates
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_TX_UPDATES); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_TX_UPDATES);
@ -119,6 +125,7 @@ test_oid(struct oid *oid, uint base_size)
oid->ids[4] = 17; // BGP4-MIB::bgpPeerConnectionRetryInterval oid->ids[4] = 17; // BGP4-MIB::bgpPeerConnectionRetryInterval
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RETRY_INTERVAL); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_RETRY_INTERVAL);
bt_debug( " 3/4\n");
oid->ids[4] = 18; // BGP4-MIB::bgpPeerHoldTime oid->ids[4] = 18; // BGP4-MIB::bgpPeerHoldTime
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_HOLD_TIME); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_HOLD_TIME);
@ -140,6 +147,8 @@ test_oid(struct oid *oid, uint base_size)
oid->ids[4] = 24; // BGP4-MIB::bgpPeerInUpdateElapsedTime oid->ids[4] = 24; // BGP4-MIB::bgpPeerInUpdateElapsedTime
bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_IN_UPDATE_ELAPSED_TIME); bt_assert(snmp_bgp_state(oid) == BGP_INTERNAL_IN_UPDATE_ELAPSED_TIME);
bt_debug("testing BGP4-MIB::bgpPeerEntry end\n");
} }
static int static int
@ -232,21 +241,30 @@ t_s_prefixize(void)
prefixed->include = 1; prefixed->include = 1;
u32 prefixed_arr[] = { ~((u32) 0), 0, 256 }; u32 prefixed_arr[] = { ~((u32) 0), 0, 256 };
memcpy(&prefixed->ids, prefixed_arr, sizeof(prefixed_arr) / memcpy(&prefixed->ids, prefixed_arr, sizeof(prefixed_arr));
sizeof(prefixed_arr[0]));
bt_assert(memcmp(snmp_prefixize(&snmp_proto, prefixed, BYTE_ORD), prefixed, snmp_oid_size(prefixed)) == 0); struct oid *copy = mb_alloc(&root_pool, snmp_oid_size(prefixed));
memcpy(copy, prefixed, snmp_oid_size(prefixed));
struct oid *result = snmp_prefixize(&snmp_proto, copy, BYTE_ORD);
bt_assert(memcmp(result, prefixed, snmp_oid_size(prefixed)) == 0);
//bt_assert(memcmp(snmp_prefixize(&snmp_proto, copy, BYTE_ORD), prefixed, snmp_oid_size(prefixed)) == 0);
mb_free(copy);
struct oid *to_prefix = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32)); struct oid *to_prefix = mb_alloc(&root_pool, sizeof(struct oid) + 8 * sizeof(u32));
to_prefix->n_subid = 8; to_prefix->n_subid = 8;
to_prefix->prefix = 0; to_prefix->prefix = 0;
to_prefix->include = 1; 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(memcmp(snmp_prefixize(&snmp_proto, to_prefix, BYTE_ORD), prefixed, snmp_oid_size(prefixed)) == 0); u32 to_prefix_arr[] = {1, 3, 6, 1, 100, ~((u32) 0), 0, 256 };
memcpy(to_prefix->ids, to_prefix_arr, sizeof(to_prefix_arr));
struct oid *temp = snmp_prefixize(&snmp_proto, to_prefix, BYTE_ORD);
//bt_assert(memcmp(snmp_prefixize(&snmp_proto, to_prefix, BYTE_ORD), prefixed, snmp_oid_size(prefixed)) == 0);
bt_assert(memcmp(temp, prefixed, snmp_oid_size(prefixed)) == 0);
struct oid *unprefixable = mb_alloc(&root_pool, sizeof(struct oid) + 2 * sizeof(u32)); struct oid *unprefixable = mb_alloc(&root_pool, sizeof(struct oid) + 2 * sizeof(u32));
unprefixable->n_subid = 2; unprefixable->n_subid = 2;
@ -304,10 +322,21 @@ t_oid_compare(void)
bt_assert(snmp_oid_compare(l1, l1) == 0); bt_assert(snmp_oid_compare(l1, l1) == 0);
bt_assert(snmp_oid_compare(r1, r1) == 0); bt_assert(snmp_oid_compare(r1, r1) == 0);
/* 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);
mb_free(l1); mb_free(l1);
mb_free(r1); mb_free(r1);
/* different length, no prefix */ /* different length, no prefix */
l1 = OID_ALLOCATE(4); l1 = OID_ALLOCATE(4);
{ {
@ -318,7 +347,7 @@ t_oid_compare(void)
r1 = OID_ALLOCATE(5); r1 = OID_ALLOCATE(5);
{ {
u32 arr[] = { 1, 2, 3, 4, 1 }; u32 arr[] = { 1, 2, 3, 4, 1 };
OID_INIT(l1, 5, 0, 1, arr); OID_INIT(r1, 5, 0, 1, arr);
} }
bt_assert(snmp_oid_compare(l1, r1) == -1); bt_assert(snmp_oid_compare(l1, r1) == -1);
@ -337,10 +366,21 @@ t_oid_compare(void)
bt_assert(snmp_oid_compare(l1, l1) == 0); bt_assert(snmp_oid_compare(l1, l1) == 0);
bt_assert(snmp_oid_compare(r1, r1) == 0); bt_assert(snmp_oid_compare(r1, r1) == 0);
/* 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);
mb_free(l1); mb_free(l1);
mb_free(r1); mb_free(r1);
/* inverse order different length, no prefix */ /* inverse order different length, no prefix */
l1 = OID_ALLOCATE(4); l1 = OID_ALLOCATE(4);
{ {
@ -370,10 +410,18 @@ t_oid_compare(void)
bt_assert(snmp_oid_compare(l1, l1) == 0); bt_assert(snmp_oid_compare(l1, l1) == 0);
bt_assert(snmp_oid_compare(r1, r1) == 0); bt_assert(snmp_oid_compare(r1, r1) == 0);
/* 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);
mb_free(l1); mb_free(l1);
mb_free(r1); mb_free(r1);
/* ==== MIXED PREFIXED / NON PREFIXED OID compare ==== */ /* ==== MIXED PREFIXED / NON PREFIXED OID compare ==== */
/* same length, mixed */ /* same length, mixed */
l1 = OID_ALLOCATE(6); /* OID .1.2.17.3.21.4 */ l1 = OID_ALLOCATE(6); /* OID .1.2.17.3.21.4 */
@ -385,7 +433,7 @@ t_oid_compare(void)
r1 = OID_ALLOCATE(1); /* OID .1.3.6.1.5.3 */ r1 = OID_ALLOCATE(1); /* OID .1.3.6.1.5.3 */
{ {
u32 arr[] = { 3 }; u32 arr[] = { 3 };
OID_INIT(l1, 1, 5, 1, arr); OID_INIT(r1, 1, 5, 1, arr);
} }
bt_assert(snmp_oid_compare(l1, r1) == -1); bt_assert(snmp_oid_compare(l1, r1) == -1);
@ -447,6 +495,8 @@ int main(int argc, char **argv)
{ {
bt_init(argc, argv); bt_init(argc, argv);
bt_bird_init();
bt_test_suite(t_s_bgp_state, "Function snmp_bgp_state()"); bt_test_suite(t_s_bgp_state, "Function snmp_bgp_state()");
bt_test_suite(t_s_is_oid_empty, "Function snmp_is_oid_empty()"); bt_test_suite(t_s_is_oid_empty, "Function snmp_is_oid_empty()");

View File

@ -276,8 +276,8 @@ refresh_ids(struct snmp_proto *p, struct agentx_header *h)
* @size: number of packet bytes in buffer * @size: number of packet bytes in buffer
* retval number of byte parsed * retval number of byte parsed
* *
* Function parse_ptk() parses Response-PDU and calls do_response(). * function parse_ptk() parses response-pdu and calls do_response().
* Returns number of bytes parsed by function excluding size of header. * returns number of bytes parsed by function excluding size of header.
*/ */
static uint static uint
parse_pkt(struct snmp_proto *p, byte *pkt, uint size) parse_pkt(struct snmp_proto *p, byte *pkt, uint size)
@ -357,14 +357,15 @@ parse_pkt(struct snmp_proto *p, byte *pkt, uint size)
// logical error: need to return number of actually parsed bytes, not what was // logical error: need to return number of actually parsed bytes, not what was
// announced in the packet // announced in the packet
return parsed_len; return parsed_len;
#if 0
#if 0
/* whole buffer was parsed while generating response */ /* whole buffer was parsed while generating response */
if (len == size) if (len == size)
return pkt_size; return pkt_size;
return 1; /* meaning buffer is empty */ return 1; /* meaning buffer is empty */
else else
return 0; /* meaning buffer stil contain some data to be parsed, parsing is not finished */ return 0; /* meaning buffer stil contain some data to be parsed, parsing is not finished */
#endif #endif
} }
static uint static uint
@ -390,9 +391,9 @@ parse_response(struct snmp_proto *p, byte *res, uint size)
} }
snmp_log(" endianity: %s, session %u, transaction: %u", (h->flags & AGENTX_NETWORK_BYTE_ORDER) ? "big end": snmp_log(" endianity: %s, session %u, transaction: %u", (h->flags & AGENTX_NETWORK_BYTE_ORDER) ? "big end":
"little end", h->session_id, h->transaction_id); "little end", h->session_id, h->transaction_id);
snmp_log(" sid: %3u\ttid: %3u\tpid: %3u", p->session_id, p->transaction_id, snmp_log(" sid: %3u\ttid: %3u\tpid: %3u", p->session_id, p->transaction_id,
p->packet_id); p->packet_id);
snmp_log(" pkt size %u", h->payload); snmp_log(" pkt size %u", h->payload);
// snmp_log("uptime: %u s", r->uptime); // snmp_log("uptime: %u s", r->uptime);
@ -401,8 +402,7 @@ p->packet_id);
do_response(p, res, size); do_response(p, res, size);
else else
/* erronous packet should be dropped quietly */ /* erronous packet should be dropped quietly */
snmp_log("an error occured '%s'", snmp_errs[get_u16(&r->err) - snmp_log("an error occured '%s'", snmp_errs[get_u16(&r->err) - SNMP_ERR_SHIFT]);
SNMP_ERR_SHIFT]);
return pkt_size + sizeof(struct agentx_header); return pkt_size + sizeof(struct agentx_header);
} }
@ -696,7 +696,7 @@ static uint parse_gets_pdu(struct snmp_proto *p, byte *req, uint size)
{ {
snmp_log("too big o_start or o_end"); snmp_log("too big o_start or o_end");
snmp_log("o_start_b packet: %u o_end_b packet: %u packet size: %u", snmp_log("o_start_b packet: %u o_end_b packet: %u packet size: %u",
snmp_oid_size(o_start_b), snmp_oid_size(o_end_b), size); snmp_oid_size(o_start_b), snmp_oid_size(o_end_b), size);
err = -1; /* parse error too big n_subid (greater than message) */ err = -1; /* parse error too big n_subid (greater than message) */
continue; continue;
} }
@ -1003,11 +1003,16 @@ has_inet_prefix(struct oid *o)
static int static int
upper_bound_check(struct oid *found, struct oid *bound) upper_bound_check(struct oid *found, struct oid *bound)
{ {
snmp_log("upper_bound_check(*f, *b) %p %p is_empty() %d", found, bound,
snmp_is_oid_empty(bound));
// TODO manage NULL in found & bound
if (snmp_is_oid_empty(bound)) if (snmp_is_oid_empty(bound))
return 1; return 1;
if (snmp_oid_compare(found, bound) < 0) if (snmp_oid_compare(found, bound) < 0)
return 0; return 1;
return 0; return 0;
} }
@ -1051,7 +1056,7 @@ search_mib_unchecked(struct snmp_proto *p, struct oid *o_start, struct oid *o_en
*/ */
default: default:
return NULL; return NULL;
} }
} }
@ -1095,36 +1100,43 @@ find_ospf_record(struct snmp_proto *p, struct oid *o, byte *buf, uint size)
struct oid * struct oid *
snmp_prefixize(struct snmp_proto *proto, struct oid *oid, int byte_ord) snmp_prefixize(struct snmp_proto *proto, struct oid *oid, int byte_ord)
{ {
snmp_log("snmp_prefixize()");
const u32 prefix[] = {1, 3, 6, 1}; const u32 prefix[] = {1, 3, 6, 1};
if (oid == NULL) if (oid == NULL)
return NULL; { snmp_log("NULL"); return NULL; }
if (snmp_is_oid_empty(oid)) if (snmp_is_oid_empty(oid))
{ {
/* allocate new zeroed oid */ /* allocate new zeroed oid */
snmp_log("blank");
return snmp_oid_blank(proto); return snmp_oid_blank(proto);
} }
else if (LOAD(oid->n_subid, byte_ord) != 0)
{ /* already in prefixed form */
struct oid *new = mb_allocz(proto->p.pool, snmp_oid_size(oid)); else if (oid->prefix != 0) {
struct oid *new = mb_alloc(proto->p.pool, snmp_oid_size(oid));
memcpy(new, oid, snmp_oid_size(oid)); memcpy(new, oid, snmp_oid_size(oid));
snmp_log("already prefixed");
return new; return new;
} }
if (oid->n_subid < 5) if (oid->n_subid < 5)
return NULL; { snmp_log("too small"); return NULL; }
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
if (LOAD(oid->ids[i], byte_ord) != prefix[i]) if (LOAD(oid->ids[i], byte_ord) != prefix[i])
return NULL; { snmp_log("different prefix"); return NULL; }
/* validity chech here */ /* validity check here */
if (oid->ids[4] >= 256) if (oid->ids[4] >= 256)
return NULL; { snmp_log("outside byte first id"); return NULL; }
struct oid *new = mb_alloc(proto->p.pool, struct oid *new = mb_alloc(proto->p.pool,
sizeof(struct oid) + MAX((oid->n_subid - 5) * sizeof(u32), 0)); sizeof(struct oid) + MAX((oid->n_subid - 5) * sizeof(u32), 0));
snmp_log(" new %p new->ids %p &new->ids %p oid %p oid->ids %p oid->ids[5] %p"
"&oid->ids[5] %p &(oid->ids[5]) %p", new, new->ids, &new->ids, oid, oid->ids,
oid->ids[5], &oid->ids[5], &(oid->ids[5]));
memcpy(new, oid, sizeof(struct oid)); memcpy(new, oid, sizeof(struct oid));
new->n_subid = oid->n_subid - 5; new->n_subid = oid->n_subid - 5;