From f9928a1a08c57fe853888119a996c3acc98ee09d Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Fri, 14 Jan 2022 23:51:05 +0100 Subject: Initial version of nodemcuv2 dhcp server Able to offer IP address + DNS/Gateway ... Worked with devices at my hand. --- test/native/lease_db.cc | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/native/lease_db.cc (limited to 'test/native/lease_db.cc') diff --git a/test/native/lease_db.cc b/test/native/lease_db.cc new file mode 100644 index 0000000..8f87912 --- /dev/null +++ b/test/native/lease_db.cc @@ -0,0 +1,85 @@ +// Copyright (c) 2022 Johannes Stoelp + +#include + +#include + +TEST(lease_db, null_client_hash) { + lease_db<2> db; + + ASSERT_EQ(std::nullopt, db.new_lease(0, 0)); + ASSERT_EQ(std::nullopt, db.get_lease(0)); + ASSERT_EQ(0, db.active_leases()); +} + +TEST(lease_db, get_new_lease) { + lease_db<2> db; + + ASSERT_EQ(std::optional(0), db.new_lease(10, 0)); + ASSERT_EQ(std::optional(1), db.new_lease(20, 0)); + ASSERT_EQ(std::nullopt, db.new_lease(30, 0)); // exhausted + + ASSERT_EQ(std::optional(0), db.get_lease(10)); + ASSERT_EQ(std::optional(1), db.get_lease(20)); + ASSERT_EQ(std::nullopt, db.get_lease(30)); // exhausted + + ASSERT_EQ(2, db.active_leases()); +} + +TEST(lease_db, get_new_lease_twice) { + lease_db<2> db; + + ASSERT_EQ(std::optional(0), db.new_lease(10, 0)); + ASSERT_EQ(std::nullopt, db.new_lease(10, 0)); + + ASSERT_EQ(1, db.active_leases()); +} + +TEST(lease_db, flush_expired) { + lease_db<2> db; + + ASSERT_EQ(std::optional(0), db.new_lease(10, 100 /* lease end */)); + ASSERT_EQ(std::optional(1), db.new_lease(20, 200 /* lease end */)); + + db.flush_expired(50 /* current time */); + ASSERT_EQ(2, db.active_leases()); + ASSERT_EQ(std::optional(0), db.get_lease(10)); + ASSERT_EQ(std::optional(1), db.get_lease(20)); + + db.flush_expired(150 /* current time */); + ASSERT_EQ(1, db.active_leases()); + ASSERT_EQ(std::nullopt, db.get_lease(10)); + ASSERT_EQ(std::optional(1), db.get_lease(20)); + + db.flush_expired(250 /* current time */); + ASSERT_EQ(0, db.active_leases()); + ASSERT_EQ(std::nullopt, db.get_lease(10)); + ASSERT_EQ(std::nullopt, db.get_lease(20)); +} + +TEST(lease_db, update_lease) { + lease_db<2> db; + + ASSERT_EQ(std::optional(0), db.new_lease(10, 100 /* lease end */)); + ASSERT_EQ(std::optional(1), db.new_lease(20, 200 /* lease end */)); + + ASSERT_EQ(2, db.active_leases()); + + db.flush_expired(150 /* current time */); + ASSERT_EQ(1, db.active_leases()); + ASSERT_EQ(std::nullopt, db.get_lease(10)); + ASSERT_EQ(std::optional(1), db.get_lease(20)); + + ASSERT_EQ(false, db.update_lease(10, 300 /* lease end */)); + ASSERT_EQ(true, db.update_lease(20, 300 /* lease end */)); + + db.flush_expired(250 /* current time */); + ASSERT_EQ(1, db.active_leases()); + ASSERT_EQ(std::nullopt, db.get_lease(10)); + ASSERT_EQ(std::optional(1), db.get_lease(20)); + + db.flush_expired(350 /* current time */); + ASSERT_EQ(0, db.active_leases()); + ASSERT_EQ(std::nullopt, db.get_lease(10)); + ASSERT_EQ(std::nullopt, db.get_lease(20)); +} -- cgit v1.2.3