bool simple_bib_session(void) { struct bib_entry *bib; struct session_entry *session; bib = create_bib_entry(0, 0); if (!bib) { log_warning("Could not allocate a BIB entry."); return false; } session = create_session_entry(1, 0, 1, 0, bib, IPPROTO_TCP, 12345); if (!session) { log_warning("Could not allocate a Session entry."); return false; } // Insert the BIB entry. if (!bib_add(bib, IPPROTO_TCP)) { log_warning("Test 'BIB insertion' failed: Call returned false."); return false; } if (!assert_bib("BIB insertion", bib, false, true, false)) return false; // Insert the session entry. if (!session_add(session)) { log_warning("Test 'Session insertion' failed: Call returned false."); return false; } if (!assert_session("Session insertion", session, false, true, false)) return false; // The BIB entry has a session entry, so it shouldn't be removable. if (bib_remove(bib, IPPROTO_TCP)) { log_warning("Test 'Bib removal' failed: Removal shouldn't have succeeded."); return false; } if (!assert_bib("Bib removal (bib table)", bib, false, true, false)) return false; if (!assert_session("BIB removal (session table)", session, false, true, false)) return false; // Remove the session entry. // Because the BIB entry no longer has sessions, it should be automatically removed as well. if (!session_remove(session)) { log_warning("Test 'Session removal' failed: Call returned false."); return false; } if (!assert_bib("Session removal (bib table)", bib, false, false, false)) return false; if (!assert_session("Session removal (session table)", session, false, false, false)) return false; // Quit. return true; }
static bool simple_session(void) { struct session_entry *session; bool success = true; session = create_session_entry(1, 0, 1, 0, L4PROTO_TCP); if (!assert_not_null(session, "Allocation of test session entry")) return false; success &= assert_equals_int(0, sessiondb_add(session, SESSIONTIMER_EST), "Session insertion call"); success &= assert_session("Session insertion state", session, false, true, false); if (!success) return false; return true; }
bool test_address_filtering(void) { struct bib_entry *bib; struct session_entry *session; // Init. bib = create_bib_entry(0, 0); if (!bib) { log_warning("Could not allocate a BIB entry."); return false; } session = create_session_entry(0, 0, 0, 0, bib, IPPROTO_UDP, 12345); if (!session) { log_warning("Could not allocate a Session entry."); return false; } if (!bib_add(bib, IPPROTO_UDP)) { log_warning("Could not add the BIB entry."); return false; } if (!session_add(session)) { log_warning("Could not add the session entry."); return false; } // Test the packet is allowed when the tuple and session match perfectly. if (!test_address_filtering_aux(0, 0, 0, 0, true)) return false; // Test a tuple that completely mismatches the session. if (!test_address_filtering_aux(1, 1, 1, 1, false)) return false; // Now test tuples that nearly match the session. if (!test_address_filtering_aux(0, 0, 0, 1, false)) return false; if (!test_address_filtering_aux(0, 0, 1, 0, false)) return false; if (!test_address_filtering_aux(0, 1, 0, 0, true)) return false; // The remote port is the only one that doesn't matter. if (!test_address_filtering_aux(1, 0, 0, 0, false)) return false; return true; }
static struct session_entry *create_and_insert_session(int remote4_id, int local4_id, int local6_id, int remote6_id, struct bib_entry* bib, u_int8_t l4protocol, unsigned int dying_time) { struct session_entry *result; int error; result = create_session_entry(remote4_id, local4_id, local6_id, remote6_id, bib, l4protocol, dying_time); if (!result) { log_warning("Could not allocate a session entry."); return NULL; } error = session_add(result); if (error) { log_warning("Could not insert the session entry to the table; call returned %d.", error); return NULL; } return result; }
static struct session_entry *create_and_insert_session(int remote4_id, int local4_id, int local6_id, int remote6_id) { struct session_entry *result; int error; result = create_session_entry(remote4_id, local4_id, local6_id, remote6_id, L4PROTO_UDP); if (!result) { log_err("Could not allocate a session entry."); return NULL; } error = sessiondb_add(result, SESSIONTIMER_UDP); if (error) { log_err("Could not insert the session entry to the table; call returned %d.", error); return NULL; } return result; }
bool simple_session(void) { struct session_entry *session; bool success = true; session = create_session_entry(1, 0, 1, 0, NULL, IPPROTO_TCP, 12345); if (!assert_not_null(session, "Allocation of test session entry")) return false; success &= assert_equals_int(0, session_add(session), "Session insertion call"); success &= assert_session("Session insertion state", session, false, true, false); if (!success) return false; /* See simple_bib(). */ success &= assert_true(session_remove(session), "Session removal call"); success &= assert_session("Session removal state", session, false, false, false); if (!success) return false; kfree(session); return true; }
bool test_clean_old_sessions(void) { struct bib_entry *bibs[BIB_COUNT]; struct session_entry *sessions[BIB_COUNT][SESSIONS_PER_BIB]; int cbib, cses; // "BIB counter, session counter". Sorry; I use them too much. const unsigned int time_before = jiffies_to_msecs(jiffies) - 1000; const unsigned int time_after = jiffies_to_msecs(jiffies) + 1000; // Allocate. for (cbib = 0; cbib < BIB_COUNT; cbib++) { bibs[cbib] = create_bib_entry(cbib, cbib); if (!bibs[cbib]) { log_warning("Could not allocate a BIB entry %d.", cbib); return false; } } for (cbib = 0; cbib < BIB_COUNT; cbib++) { for (cses = 0; cses < SESSIONS_PER_BIB; cses++) { sessions[cbib][cses] = create_session_entry(cbib, cses + 5, cbib, cses + 5, bibs[cbib], IPPROTO_UDP, time_after); if (!sessions[cbib][cses]) { log_warning("Could not allocate a Session entry %d-%d.", cbib, cses); return false; } } } sessions[3][1]->is_static = true; // Insert to the tables. for (cbib = 0; cbib < BIB_COUNT; cbib++) { if (!bib_add(bibs[cbib], IPPROTO_UDP)) { log_warning("Could not add BIB entry %d.", cbib); return false; } } for (cbib = 0; cbib < BIB_COUNT; cbib++) { for (cses = 0; cses < SESSIONS_PER_BIB; cses++) { if (!session_add(sessions[cbib][cses])) { log_warning("Could not add session entry %d-%d.", cbib, cses); return false; } } } // 1. Nothing has expired: // Test nothing gets deleted. clean_expired_sessions(); ASSERT_SINGLE_BIB("Clean deletes nothing", 0, true, true, true, true); ASSERT_SINGLE_BIB("Clean deletes nothing", 1, true, true, true, true); ASSERT_SINGLE_BIB("Clean deletes nothing", 2, true, true, true, true); ASSERT_SINGLE_BIB("Clean deletes nothing", 3, true, true, true, true); // 2. All of a single BIB's sessions expire: // Test both BIBs and Sessions die. sessions[1][0]->dying_time = time_before; sessions[1][1]->dying_time = time_before; sessions[1][2]->dying_time = time_before; clean_expired_sessions(); ASSERT_SINGLE_BIB("Whole BIB dies", 0, true, true, true, true); ASSERT_SINGLE_BIB("Whole BIB dies", 1, false, false, false, false); ASSERT_SINGLE_BIB("Whole BIB dies", 2, true, true, true, true); ASSERT_SINGLE_BIB("Whole BIB dies", 3, true, true, true, true); // 3. Some sessions of a BIB expire: // Test only they get deleted. sessions[2][0]->dying_time = time_before; sessions[2][1]->dying_time = time_before; clean_expired_sessions(); ASSERT_SINGLE_BIB("Some sessions die", 0, true, true, true, true); ASSERT_SINGLE_BIB("Some sessions die", 1, false, false, false, false); ASSERT_SINGLE_BIB("Some sessions die", 2, true, false, false, true); ASSERT_SINGLE_BIB("Some sessions die", 3, true, true, true, true); // 4. The rest of them expire: // Test the BIB keeps keeps behaving as expected. Perhaps unnecesary. sessions[2][2]->dying_time = time_before; clean_expired_sessions(); ASSERT_SINGLE_BIB("Last session dies", 0, true, true, true, true); ASSERT_SINGLE_BIB("Last session dies", 1, false, false, false, false); ASSERT_SINGLE_BIB("Last session dies", 2, false, false, false, false); ASSERT_SINGLE_BIB("Last session dies", 3, true, true, true, true); // 5. The sessions of a static BIB expire, but one of them is static. // Test only the dynamic ones die. sessions[3][0]->dying_time = time_before; sessions[3][1]->dying_time = time_before; sessions[3][2]->dying_time = time_before; clean_expired_sessions(); ASSERT_SINGLE_BIB("Static session doesn't die", 0, true, true, true, true); ASSERT_SINGLE_BIB("Static session doesn't die", 1, false, false, false, false); ASSERT_SINGLE_BIB("Static session doesn't die", 2, false, false, false, false); ASSERT_SINGLE_BIB("Static session doesn't die", 3, true, false, true, false); // Quit. return true; }