aboutsummaryrefslogtreecommitdiff
path: root/test/native/lease_db.cc
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
commitf9928a1a08c57fe853888119a996c3acc98ee09d (patch)
treec9770b76ffcc281da141f3aa2c595600372c0fca /test/native/lease_db.cc
downloadpio-nodemcuv2-dhcp-server-main.tar.gz
pio-nodemcuv2-dhcp-server-main.zip
Initial version of nodemcuv2 dhcp serverHEADmain
Able to offer IP address + DNS/Gateway ... Worked with devices at my hand.
Diffstat (limited to 'test/native/lease_db.cc')
-rw-r--r--test/native/lease_db.cc85
1 files changed, 85 insertions, 0 deletions
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 <lease_db.h>
+
+#include <gtest/gtest.h>
+
+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));
+}