示例#1
0
bool test_clean_old_sessions(void)
{
	int b, s; /* bib counter, session counter. */
	bool success = true;

	const unsigned int before = jiffies_to_msecs(jiffies) - 1000;
	const unsigned int after = jiffies_to_msecs(jiffies) + 1000;

	/* Allocate and insert to the tables. */
	for (b = 0; b < BIB_COUNT; b++) {
		db_bibs[b] = create_and_insert_bib(b, b, IPPROTO_UDP);
		if (!db_bibs[b])
			return false;

		for (s = 0; s < SESSIONS_PER_BIB; s++) {
			db_sessions[b][s] = create_and_insert_session(b, s + 5, b, s + 5, db_bibs[b], IPPROTO_UDP, after);
			if (!db_sessions[b][s])
				return false;

			memcpy(&sessions[b][s], db_sessions[b][s], sizeof(struct session_entry));
		}

		memcpy(&bibs[b], db_bibs[b], sizeof(struct bib_entry));
	}

	db_bibs[3]->is_static = true;

	/* 1. Nothing has expired: Test nothing gets deleted. */
	clean_expired_sessions();

	success &= ASSERT_SINGLE_BIB("Clean deletes nothing", 0, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Clean deletes nothing", 1, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Clean deletes nothing", 2, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Clean deletes nothing", 3, true, true, true, true);

	if (!success)
		return false;

	/* 2. All of a single BIB's sessions expire: Test both BIBs and Sessions die. */
	db_sessions[1][0]->dying_time = before;
	db_sessions[1][1]->dying_time = before;
	db_sessions[1][2]->dying_time = before;

	clean_expired_sessions();

	success &= ASSERT_SINGLE_BIB("Whole BIB dies 0", 0, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Whole BIB dies 1", 1, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Whole BIB dies 2", 2, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Whole BIB dies 3", 3, true, true, true, true);

	if (!success)
		return false;

	/* 3. Some sessions of a BIB expire: Test only those sessions get deleted. */
	db_sessions[2][0]->dying_time = before;
	db_sessions[2][1]->dying_time = before;

	clean_expired_sessions();

	success &= ASSERT_SINGLE_BIB("Some sessions die", 0, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Some sessions die", 1, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Some sessions die", 2, true, false, false, true);
	success &= ASSERT_SINGLE_BIB("Some sessions die", 3, true, true, true, true);

	if (!success)
		return false;

	/* 4. The rest of them expire: Test the BIB keeps keeps behaving as expected. */
	db_sessions[2][2]->dying_time = before;

	clean_expired_sessions();

	success &= ASSERT_SINGLE_BIB("Last session dies", 0, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Last session dies", 1, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Last session dies", 2, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Last session dies", 3, true, true, true, true);

	if (!success)
		return false;

	/* 5. The sessions of a static BIB expire. Test only the sessions ones die. */
	db_sessions[3][0]->dying_time = before;
	db_sessions[3][1]->dying_time = before;
	db_sessions[3][2]->dying_time = before;

	clean_expired_sessions();

	success &= ASSERT_SINGLE_BIB("Static session doesn't die", 0, true, true, true, true);
	success &= ASSERT_SINGLE_BIB("Static session doesn't die", 1, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Static session doesn't die", 2, false, false, false, false);
	success &= ASSERT_SINGLE_BIB("Static session doesn't die", 3, true, false, false, false);

	/* Quit. */
	return success;
}
示例#2
0
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;
}