kenzok8-package/UA2F/test/cache_test.cc

49 lines
1.1 KiB
C++
Raw Normal View History

2024-06-19 04:19:29 +08:00
#include <gtest/gtest.h>
extern "C" {
#include <cache.h>
}
class CacheTest : public ::testing::Test {
protected:
2024-11-27 10:57:37 +08:00
addr_port test_addr{};
2024-06-19 04:19:29 +08:00
void SetUp() override {
test_addr.addr.ip4 = 12345;
test_addr.port = 80;
2024-12-15 20:39:06 +08:00
init_not_http_cache(1);
2024-06-19 04:19:29 +08:00
}
void TearDown() override {
pthread_rwlock_wrlock(&cacheLock);
// Clear the cache after each test
2024-12-15 20:39:06 +08:00
cache *cur, *tmp;
2024-06-19 04:19:29 +08:00
HASH_ITER(hh, not_http_dst_cache, cur, tmp) {
HASH_DEL(not_http_dst_cache, cur);
free(cur);
}
pthread_rwlock_unlock(&cacheLock);
}
};
TEST_F(CacheTest, CacheInitiallyEmpty) {
EXPECT_FALSE(cache_contains(test_addr));
}
TEST_F(CacheTest, AddToCache) {
cache_add(test_addr);
EXPECT_TRUE(cache_contains(test_addr));
}
TEST_F(CacheTest, AddAndRemoveFromCache) {
cache_add(test_addr);
EXPECT_TRUE(cache_contains(test_addr));
sleep(5);
2024-12-15 20:39:06 +08:00
EXPECT_FALSE(cache_contains(test_addr));
2024-06-19 04:19:29 +08:00
}
TEST_F(CacheTest, CacheDoesNotContainNonexistentEntry) {
2024-11-27 10:57:37 +08:00
addr_port nonexistent_addr{};
2024-06-19 04:19:29 +08:00
nonexistent_addr.addr.ip4 = 54321;
EXPECT_FALSE(cache_contains(nonexistent_addr));
}