]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#939] Adding basic unit tests
authorPiotrek Zadroga <piotrek@isc.org>
Thu, 16 Mar 2023 12:37:44 +0000 (13:37 +0100)
committerPiotrek Zadroga <piotrek@isc.org>
Thu, 23 Mar 2023 13:51:23 +0000 (14:51 +0100)
src/lib/dhcp/tests/option_opaque_data_tuples_unittest.cc

index 6bbd76660f62c95fdb627074de15872259fa122a..217c2d67b4fae80ec4a403af27e0c44c7f4e9739 100644 (file)
@@ -98,6 +98,25 @@ TEST(OptionOpaqueDataTuples, setTuple) {
     EXPECT_THROW(data_tuple.setTuple(2, tuple), isc::OutOfRange);
 }
 
+// Check that the returned length of the DHCPv4 option is correct when
+// LTF is passed explicitly in constructor.
+TEST(OptionOpaqueDataTuples, len4_constructor_with_ltf) {
+    // Prepare data to decode.
+    const uint8_t buf_data[] = {
+        0x00, 0x0B,                         // tuple length is 11
+        0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
+        0x77, 0x6F, 0x72, 0x6C, 0x64,       // world
+        0x00, 0x03,                         // tuple length is 3
+        0x66, 0x6F, 0x6F                    // foo
+    };
+
+    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
+    OptionOpaqueDataTuples data_tuple(
+        Option::V4, 143, buf.begin(), buf.end(), OpaqueDataTuple::LENGTH_2_BYTES);
+    // Expected len = 20 = 2 (v4 headers) + 2 (LFT) + 11 (1st tuple) + 2 (LFT) + 3 (2nd tuple)
+    ASSERT_EQ(20, data_tuple.len());
+}
+
 // Check that the returned length of the DHCPv6 option is correct.
 TEST(OptionOpaqueDataTuples, len6) {
     OptionOpaqueDataTuples data_tuple(Option::V6, 60);
@@ -147,6 +166,34 @@ TEST(OptionOpaqueDataTuples, pack6) {
                         buf.getLength()));
 }
 
+// This function checks that the DHCPv4 option with two opaque data tuples
+// is parsed correctly. Tuple's LTF is passed explicitly in constructor.
+TEST(OptionOpaqueDataTuples, unpack4_constructor_with_ltf) {
+    // Prepare data to decode.
+    const uint8_t buf_data[] = {
+        0x00, 0x0B,                         // tuple length is 11
+        0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
+        0x77, 0x6F, 0x72, 0x6C, 0x64,       // world
+        0x00, 0x03,                         // tuple length is 3
+        0x66, 0x6F, 0x6F                    // foo
+    };
+    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
+
+    OptionOpaqueDataTuplesPtr data_tuple;
+    ASSERT_NO_THROW(
+        data_tuple = OptionOpaqueDataTuplesPtr(
+            new OptionOpaqueDataTuples(Option::V4,
+                                       143,
+                                       buf.begin(),
+                                       buf.end(),
+                                       OpaqueDataTuple::LENGTH_2_BYTES));
+    );
+    EXPECT_EQ(DHO_V4_SZTP_REDIRECT, data_tuple->getType());
+    ASSERT_EQ(2, data_tuple->getTuplesNum());
+    EXPECT_EQ("Hello world", data_tuple->getTuple(0).getText());
+    EXPECT_EQ("foo", data_tuple->getTuple(1).getText());
+}
+
 // This function checks that the DHCPv6 option with two opaque data tuples
 // is parsed correctly.
 TEST(OptionOpaqueDataTuples, unpack6) {