static bool simple_session(void) { struct ipv4_range range; bool success = true; if (!insert_test_sessions()) return false; /* ---------------------------------------------------------- */ log_debug("Deleting sessions by BIB."); range.prefix.address.s_addr = cpu_to_be32(0xcb007101u); range.prefix.len = 32; range.ports.min = 1; range.ports.max = 1; bib_rm_range(db, PROTO, &range); sessions[1][1][2][2] = NULL; sessions[1][1][2][1] = NULL; sessions[1][1][1][1] = NULL; sessions[1][1][1][2] = NULL; success &= test_db(); /* ---------------------------------------------------------- */ log_debug("Deleting again."); bib_rm_range(db, PROTO, &range); success &= test_db(); /* ---------------------------------------------------------- */ success &= flush(); if (!insert_test_sessions()) return false; /* ---------------------------------------------------------- */ log_debug("Deleting by range (all addresses, lower ports)."); range.prefix.address.s_addr = cpu_to_be32(0xcb007100u); range.prefix.len = 30; range.ports.min = 0; range.ports.max = 1; bib_rm_range(db, PROTO, &range); sessions[2][1][2][1] = NULL; sessions[2][1][1][1] = NULL; sessions[1][1][2][2] = NULL; sessions[2][1][2][2] = NULL; sessions[2][1][1][2] = NULL; sessions[1][1][2][1] = NULL; sessions[1][1][1][1] = NULL; sessions[1][1][1][2] = NULL; success &= test_db(); /* ---------------------------------------------------------- */ success &= flush(); if (!insert_test_sessions()) return false; /* ---------------------------------------------------------- */ log_debug("Deleting by range (lower addresses, all ports)."); range.prefix.address.s_addr = cpu_to_be32(0xcb007100u); range.prefix.len = 31; range.ports.min = 0; range.ports.max = 65535; bib_rm_range(db, PROTO, &range); sessions[1][2][2][2] = NULL; sessions[1][1][2][2] = NULL; sessions[1][2][1][1] = NULL; sessions[1][1][2][1] = NULL; sessions[1][2][2][1] = NULL; sessions[1][2][1][2] = NULL; sessions[1][1][1][1] = NULL; sessions[1][1][1][2] = NULL; success &= test_db(); /* ---------------------------------------------------------- */ success &= flush(); return success; }
static bool test_foreach(void) { struct unit_iteration_args args; struct session_foreach_func func = { .cb = cb, .arg = &args, }; struct session_foreach_offset offset; int error; bool success = true; offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007102u);/* 203.0.113.2 */ offset.offset.src.l4 = 200; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000202u);/* 192.0.2.2 */ offset.offset.dst.l4 = 1200; /* Empty table, no offset. */ args.i = 0; args.offset = 0; error = bib_foreach_session(db, L4PROTO_UDP, &func, NULL); success &= ASSERT_INT(0, error, "call 1 result"); success &= ASSERT_UINT(0, args.i, "call 1 counter"); /* Empty table, offset, include offset, offset not found. */ args.i = 0; args.offset = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 2 result"); success &= ASSERT_UINT(0, args.i, "call 2 counter"); /* Empty table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 3 result"); success &= ASSERT_UINT(0, args.i, "call 3 counter"); /* ----------------------------------- */ if (!insert_test_sessions()) return false; /* Populated table, no offset. */ args.i = 0; args.offset = 0; error = bib_foreach_session(db, L4PROTO_UDP, &func, NULL); success &= ASSERT_INT(0, error, "call 4 result"); success &= ASSERT_UINT(9, args.i, "call 4 counter"); /* Populated table, offset, include offset, offset found. */ args.i = 0; args.offset = 4; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 5 result"); success &= ASSERT_UINT(5, args.i, "call 5 counter"); /* Populated table, offset, include offset, offset not found. */ args.i = 0; args.offset = 5; offset.include_offset = true; offset.offset.dst.l4 = 1250; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 6 result"); success &= ASSERT_UINT(4, args.i, "call 6 counter"); /* Populated table, offset, do not include offset, offset found. */ args.i = 0; args.offset = 5; offset.include_offset = false; offset.offset.dst.l4 = 1200; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 7 result"); success &= ASSERT_UINT(4, args.i, "call 7 counter"); /* Populated table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 5; offset.include_offset = false; offset.offset.dst.l4 = 1250; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 8 result"); success &= ASSERT_UINT(4, args.i, "call 8 counter"); /* ----------------------------------- */ /* Offset is before first, include offset. */ offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007101u);/* 203.0.113.1 */ offset.offset.src.l4 = 300; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000203u);/* 192.0.2.3 */ offset.offset.dst.l4 = 1200; args.i = 0; args.offset = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 9 result"); success &= ASSERT_UINT(9, args.i, "call 9 counter"); /* Offset is before first, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 10 result"); success &= ASSERT_UINT(9, args.i, "call 10 counter"); /* Offset is first, include offset. */ offset.offset.dst.l4 = 1300; args.i = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 11 result"); success &= ASSERT_UINT(9, args.i, "call 11 counter"); /* Offset is first, do not include offset. */ args.i = 0; args.offset = 1; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 12 result"); success &= ASSERT_UINT(8, args.i, "call 12 counter"); /* Offset is last, include offset. */ offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007103u); /* 203.0.113.3 */ offset.offset.src.l4 = 100; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000201u); /* 192.0.2.1 */ offset.offset.dst.l4 = 1100; args.i = 0; args.offset = 8; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 13 result"); success &= ASSERT_UINT(1, args.i, "call 13 counter"); /* Offset is last, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 14 result"); success &= ASSERT_UINT(0, args.i, "call 14 counter"); /* Offset is after last, include offset. */ offset.offset.src.l4 = 1200; args.i = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 15 result"); success &= ASSERT_UINT(0, args.i, "call 15 counter"); /* Offset is after last, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 16 result"); success &= ASSERT_UINT(0, args.i, "call 16 counter"); return success; }
static bool test_foreach(void) { struct ipv4_transport_addr local; struct ipv4_transport_addr remote; struct unit_iteration_args args; int error; bool success = true; local.l3.s_addr = cpu_to_be32(0xcb007102u); /* 203.0.113.2 */ local.l4 = 200; remote.l3.s_addr = cpu_to_be32(0xc0000202u); /* 192.0.2.2 */ remote.l4 = 1200; /* Empty table, no offset. */ args.i = 0; args.offset = 0; error = __foreach(&table, cb, &args, NULL, NULL, 0); success &= ASSERT_INT(0, error, "call 1 result"); success &= ASSERT_UINT(0, args.i, "call 1 counter"); /* Empty table, offset, include offset, offset not found. */ args.i = 0; args.offset = 0; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 2 result"); success &= ASSERT_UINT(0, args.i, "call 2 counter"); /* Empty table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 0; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 3 result"); success &= ASSERT_UINT(0, args.i, "call 3 counter"); /* ----------------------------------- */ if (!insert_test_sessions()) return false; /* Populated table, no offset. */ args.i = 0; args.offset = 0; error = __foreach(&table, cb, &args, NULL, NULL, 0); success &= ASSERT_INT(0, error, "call 4 result"); success &= ASSERT_UINT(9, args.i, "call 4 counter"); /* Populated table, offset, include offset, offset found. */ args.i = 0; args.offset = 4; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 5 result"); success &= ASSERT_UINT(5, args.i, "call 5 counter"); /* Populated table, offset, include offset, offset not found. */ args.i = 0; args.offset = 5; remote.l4 = 1250; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 6 result"); success &= ASSERT_UINT(4, args.i, "call 6 counter"); /* Populated table, offset, do not include offset, offset found. */ args.i = 0; args.offset = 5; remote.l4 = 1200; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 7 result"); success &= ASSERT_UINT(4, args.i, "call 7 counter"); /* Populated table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 5; remote.l4 = 1250; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 8 result"); success &= ASSERT_UINT(4, args.i, "call 8 counter"); /* ----------------------------------- */ /* Offset is before first, include offset. */ local.l3.s_addr = cpu_to_be32(0xcb007101u); /* 203.0.113.1 */ local.l4 = 300; remote.l3.s_addr = cpu_to_be32(0xc0000203u); /* 192.0.2.3 */ remote.l4 = 1200; args.i = 0; args.offset = 0; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 9 result"); success &= ASSERT_UINT(9, args.i, "call 9 counter"); /* Offset is before first, do not include offset. */ args.i = 0; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 10 result"); success &= ASSERT_UINT(9, args.i, "call 10 counter"); /* Offset is first, include offset. */ remote.l4 = 1300; args.i = 0; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 11 result"); success &= ASSERT_UINT(9, args.i, "call 11 counter"); /* Offset is first, do not include offset. */ args.i = 0; args.offset = 1; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 12 result"); success &= ASSERT_UINT(8, args.i, "call 12 counter"); /* Offset is last, include offset. */ local.l3.s_addr = cpu_to_be32(0xcb007103u); /* 203.0.113.3 */ local.l4 = 100; remote.l3.s_addr = cpu_to_be32(0xc0000201u); /* 192.0.2.1 */ remote.l4 = 1100; args.i = 0; args.offset = 8; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 13 result"); success &= ASSERT_UINT(1, args.i, "call 13 counter"); /* Offset is last, do not include offset. */ args.i = 0; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 14 result"); success &= ASSERT_UINT(0, args.i, "call 14 counter"); /* Offset is after last, include offset. */ remote.l4 = 1200; args.i = 0; error = __foreach(&table, cb, &args, &remote, &local, true); success &= ASSERT_INT(0, error, "call 15 result"); success &= ASSERT_UINT(0, args.i, "call 15 counter"); /* Offset is after last, do not include offset. */ args.i = 0; error = __foreach(&table, cb, &args, &remote, &local, false); success &= ASSERT_INT(0, error, "call 16 result"); success &= ASSERT_UINT(0, args.i, "call 16 counter"); return success; }