72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include "dablooms.h"
|
|
|
|
struct packet_idetify
|
|
{
|
|
unsigned int tcp_seq;
|
|
unsigned int tcp_ack;
|
|
unsigned short sport;
|
|
unsigned short dport;
|
|
unsigned short checksum;
|
|
unsigned short ip_id;
|
|
unsigned int ip_src;
|
|
unsigned int ip_dst;
|
|
} __attribute__((packed, aligned(1)));
|
|
|
|
struct config
|
|
{
|
|
int enable;
|
|
|
|
unsigned int capacity;
|
|
double error_rate;
|
|
int expiry_time;
|
|
} config = {
|
|
.enable = 1,
|
|
.capacity = 1000000,
|
|
.error_rate = 0.00001,
|
|
.expiry_time = 3,
|
|
};
|
|
|
|
struct packet_idetify idetify = {
|
|
.tcp_seq = 2172673142,
|
|
.tcp_ack = 2198097831,
|
|
.sport = 46582,
|
|
.dport = 443,
|
|
.checksum = 0x2c4b,
|
|
.ip_id = 65535,
|
|
.ip_src = 123456,
|
|
.ip_dst = 789000,
|
|
};
|
|
|
|
TEST(DABLOOMS, TEST)
|
|
{
|
|
struct expiry_dablooms_handle *handle = expiry_dablooms_init(config.capacity, config.error_rate, time(NULL), config.expiry_time);
|
|
EXPECT_TRUE(handle != nullptr);
|
|
|
|
EXPECT_TRUE(expiry_dablooms_search(handle, (const char *)&idetify, sizeof(idetify), time(NULL)) != 1); // no exist
|
|
EXPECT_TRUE(expiry_dablooms_add(handle, (const char *)&idetify, sizeof(idetify), time(NULL)) == 0); // add
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
if (i < config.expiry_time)
|
|
{
|
|
EXPECT_TRUE(expiry_dablooms_search(handle, (const char *)&idetify, sizeof(idetify), time(NULL)) == 1); // exist
|
|
}
|
|
else
|
|
{
|
|
EXPECT_TRUE(expiry_dablooms_search(handle, (const char *)&idetify, sizeof(idetify), time(NULL)) != 1); // no exist
|
|
}
|
|
sleep(1);
|
|
printf("sleep[%02d] 1s\n", i);
|
|
}
|
|
|
|
expiry_dablooms_destroy(handle);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|