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/dhcp.cc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/native/dhcp.cc (limited to 'test/native/dhcp.cc') diff --git a/test/native/dhcp.cc b/test/native/dhcp.cc new file mode 100644 index 0000000..983c6c8 --- /dev/null +++ b/test/native/dhcp.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2022 Johannes Stoelp + +#include +#include + +#include + +TEST(option, get_available_opt) { + u8 opts[] = { + into_raw(dhcp_option::PAD), into_raw(dhcp_option::PAD), into_raw(dhcp_option::DHCP_MESSAGE_TYPE), 5 /* len */, 0, 1, 2, 3, 4, + into_raw(dhcp_option::END), + }; + + auto ov = get_option(opts, sizeof(opts), dhcp_option::DHCP_MESSAGE_TYPE); + + ASSERT_EQ(true, ov.has_value()); + ASSERT_EQ(0, ov.value().data[0]); + ASSERT_EQ(1, ov.value().data[1]); + ASSERT_EQ(2, ov.value().data[2]); + ASSERT_EQ(3, ov.value().data[3]); + ASSERT_EQ(4, ov.value().data[4]); + ASSERT_EQ(5, ov.value().len); +} + +TEST(option, maleformed_len) { + u8 opts[] = { + into_raw(dhcp_option::PAD), into_raw(dhcp_option::PAD), into_raw(dhcp_option::DHCP_MESSAGE_TYPE), 5 /* len */, 0, + into_raw(dhcp_option::END), + }; + + { + auto ov = get_option(opts, sizeof(opts), dhcp_option::DHCP_MESSAGE_TYPE); + + ASSERT_EQ(false, ov.has_value()); + } + { + auto ov = get_option(opts, sizeof(opts), dhcp_option::CLASS_ID); + + ASSERT_EQ(false, ov.has_value()); + } +} + +TEST(option, maleformed_missing_end) { + u8 opts[] = { + into_raw(dhcp_option::PAD), + into_raw(dhcp_option::PAD), + }; + + auto ov = get_option(opts, sizeof(opts), dhcp_option::DHCP_MESSAGE_TYPE); + + ASSERT_EQ(false, ov.has_value()); +} + +TEST(option, put_opt_val) { + u8 options[8] = {0}; + u32 val = 0xdeadbeef; + + u8* nextp = put_opt_val(options, val); + + ASSERT_EQ(options + 4, nextp); + ASSERT_EQ(0xde, options[0]); + ASSERT_EQ(0xad, options[1]); + ASSERT_EQ(0xbe, options[2]); + ASSERT_EQ(0xef, options[3]); +} \ No newline at end of file -- cgit v1.2.3