]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3977] Added variants of PktX::makeLabel excluding transaction id.
authorMarcin Siodelski <marcin@isc.org>
Wed, 21 Oct 2015 09:20:15 +0000 (11:20 +0200)
committerMarcin Siodelski <marcin@isc.org>
Wed, 21 Oct 2015 09:20:15 +0000 (11:20 +0200)
src/lib/dhcp/pkt4.cc
src/lib/dhcp/pkt4.h
src/lib/dhcp/pkt6.cc
src/lib/dhcp/pkt6.h
src/lib/dhcp/tests/pkt4_unittest.cc
src/lib/dhcp/tests/pkt6_unittest.cc

index 44a96ca53cdfa551ce9ef1f72df6abfbad51bc56..cae19d07543792d3144b3510184308ac0590e29c 100644 (file)
@@ -357,15 +357,26 @@ Pkt4::getLabel() const {
 std::string
 Pkt4::makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id,
                 const uint32_t transid) {
+    // Create label with HW address and client identifier.
+    stringstream label;
+    label << makeLabel(hwaddr, client_id);
+
+    // Append transaction id.
+    label << ", tid=0x" << hex << transid << dec;
+
+    return label.str();
+}
+
+std::string
+Pkt4::makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id) {
     stringstream label;
     label << "[" << (hwaddr ? hwaddr->toText() : "no hwaddr info")
           << "], cid=[" << (client_id ? client_id->toText() : "no info")
-          << "], tid=0x" << hex << transid << dec;
+          << "]";
 
     return label.str();
 }
 
-
 std::string
 Pkt4::toText() const {
     stringstream output;
index 549be78da8b4dd096e9de192036a5379af280006..0c7d009e1615332687b1a682474e0e2a0c72fe4d 100644 (file)
@@ -119,6 +119,15 @@ public:
                                  const ClientIdPtr& client_id,
                                  const uint32_t transid);
 
+    /// @brief Returns text representation of the given packet identifers.
+    ///
+    /// This variant of the method does not include transaction id.
+    ///
+    /// @param hwaddr hardware address to include in the string, it may be
+    /// NULL.
+    /// @param client_id client id to include in the string, it may be NULL.
+    static std::string makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id);
+
     /// @brief Returns text representation of the packet.
     ///
     /// This function is useful mainly for debugging.
index 788167247642d607b42c432791718b35ba2736c9..91d1e18049d1159c12f4a35becb1cee81ff06d97 100644 (file)
@@ -498,22 +498,31 @@ Pkt6::getMACFromDUID() {
 std::string
 Pkt6::makeLabel(const DuidPtr duid, const uint32_t transid,
                 const HWAddrPtr& hwaddr) {
+    // Create label with DUID and HW address.
+    std::stringstream label;
+    label << makeLabel(duid, hwaddr);
+
+    // Append transaction id.
+    label << ", tid=0x" << std::hex << transid << std::dec;
+
+    return (label.str());
+}
+
+std::string
+Pkt6::makeLabel(const DuidPtr duid, const HWAddrPtr& hwaddr) {
     std::stringstream label;
     // DUID should be present at all times, so explicitly inform when
     // it is no present (no info).
     label << "duid=[" << (duid ? duid->toText() : "no info")
-          << "],";
+          << "]";
 
     // HW address is typically not carried in the DHCPv6 mmessages
     // and can be extracted using various, but not fully reliable,
     // techniques. If it is not present, don't print anything.
     if (hwaddr) {
-        label << " [" << hwaddr->toText() << "],";
+        label << ", [" << hwaddr->toText() << "]";
     }
 
-    // Transaction id is always there.
-    label << " tid=0x" << std::hex << transid << std::dec;
-
     return (label.str());
 }
 
index febb92d35f54ea640141feaaee872ba0c93bda3e..32d960a0c60bfd9896dfeeb5a04c6d6af9d42b14 100644 (file)
@@ -173,6 +173,17 @@ public:
     static std::string makeLabel(const DuidPtr duid, const uint32_t transid,
                                  const HWAddrPtr& hwaddr);
 
+    /// @brief Returns text representation of the given packet identifiers.
+    ///
+    /// This variant of the method does not include transaction id.
+    ///
+    /// @param duid Pointer to the client identifier or NULL.
+    /// @param transid Numeric transaction id to include in the string.
+    /// @param hwaddr Hardware address to include in the string or NULL.
+    ///
+    /// @return String with text representation of the packet identifiers.
+    static std::string makeLabel(const DuidPtr duid, const HWAddrPtr& hwaddr);
+
     /// @brief Returns text representation of the primary packet identifiers
     ///
     /// This method is intended to be used to provide a consistent way to
index 908ab6f2ed5f7aa3ca3f087293c20da29db203ec..6f0ce0ed3fa7f8fd7831bdd9ee6d19a1d3275883 100644 (file)
@@ -1024,6 +1024,23 @@ TEST_F(Pkt4Test, getLabel) {
 
 }
 
+// Tests that the variant of makeLabel which doesn't include transaction
+// id produces expected output.
+TEST_F(Pkt4Test, makeLabelWithoutTransactionId) {
+    EXPECT_EQ("[no hwaddr info], cid=[no info]",
+              Pkt4::makeLabel(HWAddrPtr(), ClientIdPtr()));
+
+    // Test non-null hardware address.
+    HWAddrPtr hwaddr(new HWAddr(HWAddr::fromText("01:02:03:04:05:06", 123)));
+    EXPECT_EQ("[hwtype=123 01:02:03:04:05:06], cid=[no info]",
+              Pkt4::makeLabel(hwaddr, ClientIdPtr()));
+
+    // Test non-null client identifier.
+    ClientIdPtr cid = ClientId::fromText("01:02:03:04");
+    EXPECT_EQ("[hwtype=123 01:02:03:04:05:06], cid=[01:02:03:04]",
+              Pkt4::makeLabel(hwaddr, cid));
+}
+
 // Tests that the correct DHCPv4 message name is returned for various
 // message types.
 TEST_F(Pkt4Test, getName) {
index 12e80e10bd2b160d1a85ffdaa7732af3ec84a8a1..b917241cdbac2d9b16bbed1f64fddcad64b22fbe 100644 (file)
@@ -1474,6 +1474,30 @@ TEST_F(Pkt6Test, makeLabel) {
               Pkt6::makeLabel(DuidPtr(), 0x0, HWAddrPtr()));
 }
 
+// Tests that the variant of makeLabel which doesn't include transaction
+// id produces expected output.
+TEST_F(Pkt6Test, makeLabelWithoutTransactionId) {
+   DuidPtr duid(new DUID(DUID::fromText("0102020202030303030303")));
+    HWAddrPtr hwaddr(new HWAddr(HWAddr::fromText("01:02:03:04:05:06",
+                                                 HTYPE_ETHER)));
+
+    // Specify DUID and no HW Address.
+    EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03]",
+              Pkt6::makeLabel(duid, HWAddrPtr()));
+
+    // Specify HW Address and no DUID.
+    EXPECT_EQ("duid=[no info], [hwtype=1 01:02:03:04:05:06]",
+              Pkt6::makeLabel(DuidPtr(), hwaddr));
+
+    // Specify both DUID and HW Address.
+    EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03], "
+              "[hwtype=1 01:02:03:04:05:06]",
+              Pkt6::makeLabel(duid, hwaddr));
+
+    // Specify neither DUID nor HW Address.
+    EXPECT_EQ("duid=[no info]", Pkt6::makeLabel(DuidPtr(), HWAddrPtr()));
+}
+
 // This test verifies that it is possible to obtain the packet
 // identifiers in the textual format from the packet instance.
 TEST_F(Pkt6Test, getLabel) {