]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4282] Added back/front to add
authorFrancis Dupont <fdupont@isc.org>
Fri, 16 Jan 2026 15:02:57 +0000 (16:02 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 20 Jan 2026 09:31:10 +0000 (10:31 +0100)
src/hooks/dhcp/radius/client_attribute.cc
src/hooks/dhcp/radius/client_attribute.h
src/hooks/dhcp/radius/client_exchange.cc
src/hooks/dhcp/radius/tests/attribute_unittests.cc
src/hooks/dhcp/radius/tests/status_unittests.cc

index 4e33ee680afc9e8f13988d385383b76985b52957..6ed67534b47a24d036ee4cd378ce8373e03e5802 100644 (file)
@@ -752,11 +752,15 @@ AttrVsa::toElement() const {
 }
 
 void
-Attributes::add(const ConstAttributePtr& attr) {
+Attributes::add(const ConstAttributePtr& attr, bool back) {
     if (!attr) {
         return;
     }
-    static_cast<void>(container_.push_back(attr));
+    if (back) {
+        static_cast<void>(container_.push_back(attr));
+    } else {
+        static_cast<void>(container_.push_front(attr));
+    }
 }
 
 bool
index b20e71b6a226b1f72a184f530535799ba76bc185..33006d3071f9e78a1e9c41225fcbe6515bb8ebcc 100644 (file)
@@ -862,7 +862,8 @@ public:
     /// @brief Adds instance of the attribute to the collection.
     ///
     /// @param attr Pointer to the attribute being added (can be null).
-    void add(const ConstAttributePtr& attr);
+    /// @param back Insert bsk (vs front).
+    void add(const ConstAttributePtr& attr, bool back = true);
 
     /// @brief Deletes an attribute from the collection.
     ///
index 90257c53f8a01ed52a6f7a701e081ef0df7513a4..53ce6802a045c894104f1de56d74da8a9aca4765 100644 (file)
@@ -261,7 +261,9 @@ Exchange::buildRequest() {
     if ((sent_->getCode() == PW_STATUS_SERVER) &&
         (attrs->count(PW_MESSAGE_AUTHENTICATOR) == 0)) {
         const vector<uint8_t> zero(AUTH_VECTOR_LEN);
-        attrs->add(Attribute::fromBinary(PW_MESSAGE_AUTHENTICATOR, zero));
+        // The FreeRADIUS server prefers to get it first.
+        attrs->add(Attribute::fromBinary(PW_MESSAGE_AUTHENTICATOR, zero),
+                   false);
     }
 
     // Encode the request.
index 419592390fc41cc209f52a75f69adcc956a09e60..1e8675ee292be270ebdd0a7f1d5e6a4a5dc4d75f 100644 (file)
@@ -730,6 +730,34 @@ TEST_F(AttributeTest, attributesAddDel) {
     EXPECT_EQ(0, attrs.size());
 }
 
+// Verifies add front.
+TEST_F(AttributeTest, attributesAddFront) {
+    Attributes attrs;
+    EXPECT_TRUE(attrs.empty());
+    EXPECT_EQ(0, attrs.size());
+
+    // Add 2 User-Name and a Service-Type at the front.
+    ASSERT_NO_THROW(attrs.add(Attribute::fromString(PW_USER_NAME, "foobar")));
+    ASSERT_NO_THROW(attrs.add(Attribute::fromString(PW_USER_NAME, "foo")));
+    ASSERT_NO_THROW(attrs.add(Attribute::fromInt(PW_SERVICE_TYPE, 20), false));
+
+    // Get front.
+    ASSERT_EQ(3, attrs.size());
+    ConstAttributePtr attr = *(attrs.begin());
+    ASSERT_TRUE(attr);
+    EXPECT_EQ(PW_SERVICE_TYPE, attr->getType());
+
+    // Check that the default is to insert at the back.
+    attrs.clear();
+    ASSERT_NO_THROW(attrs.add(Attribute::fromString(PW_USER_NAME, "foobar")));
+    ASSERT_NO_THROW(attrs.add(Attribute::fromString(PW_USER_NAME, "foo")));
+    ASSERT_NO_THROW(attrs.add(Attribute::fromInt(PW_SERVICE_TYPE, 20)));
+    ASSERT_EQ(3, attrs.size());
+    attr = *(attrs.begin());
+    ASSERT_TRUE(attr);
+    EXPECT_EQ(PW_USER_NAME, attr->getType());
+}
+
 // Verifies append.
 TEST_F(AttributeTest, attributesAppend) {
     Attributes attrs;
index d2351459e2b999a4f4e28d335115e746ccfa647c..a6a1f3a8b99d89573ed6307abc37f854b7de8884 100644 (file)
@@ -1396,14 +1396,14 @@ TEST_F(StatusTest, accessIdleTimerCallback) {
 
     // Check attributes.
     EXPECT_EQ(44, size);
+    EXPECT_EQ(PW_MESSAGE_AUTHENTICATOR, receive_buffer_[AUTH_HDR_LEN]);
+    EXPECT_EQ(AUTH_VECTOR_LEN + 2, receive_buffer_[AUTH_HDR_LEN + 1]);
     uint8_t expected[] = {
         0x04,                   // NAS-IP-Address
         0x06,                   // length
-        0x7f, 0x00, 0x00, 0x01, // 127.0.0.1
-        0x50,                   // Message-Authenticator
-        0x12                    // length
+        0x7f, 0x00, 0x00, 0x01  // 127.0.0.1
     };
-    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN], 8));
+    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN + 18], 6));
 }
 
 /// Verify that accounting IdleTimerCallback works as expected.
@@ -1448,14 +1448,14 @@ TEST_F(StatusTest, accountingIdleTimerCallback) {
 
     // Check attributes.
     EXPECT_EQ(44, size);
+    EXPECT_EQ(PW_MESSAGE_AUTHENTICATOR, receive_buffer_[AUTH_HDR_LEN]);
+    EXPECT_EQ(AUTH_VECTOR_LEN + 2, receive_buffer_[AUTH_HDR_LEN + 1]);
     uint8_t expected[] = {
         0x04,                   // NAS-IP-Address
         0x06,                   // length
-        0x7f, 0x00, 0x00, 0x01, // 127.0.0.1
-        0x50,                   // Message-Authenticator
-        0x12                    // length
+        0x7f, 0x00, 0x00, 0x01  // 127.0.0.1
     };
-    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN], 8));
+    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN + 18], 6));
 }
 
 /// Verify that access idle timer works as expected.
@@ -1502,14 +1502,14 @@ TEST_F(StatusTest, accessIdleTimer) {
 
     // Check attributes.
     EXPECT_EQ(44, size);
+    EXPECT_EQ(PW_MESSAGE_AUTHENTICATOR, receive_buffer_[AUTH_HDR_LEN]);
+    EXPECT_EQ(AUTH_VECTOR_LEN + 2, receive_buffer_[AUTH_HDR_LEN + 1]);
     uint8_t expected[] = {
         0x04,                   // NAS-IP-Address
         0x06,                   // length
-        0x7f, 0x00, 0x00, 0x01, // 127.0.0.1
-        0x50,                   // Message-Authenticator
-        0x12                    // length
+        0x7f, 0x00, 0x00, 0x01  // 127.0.0.1
     };
-    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN], 8));
+    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN + 18], 6));
 }
 
 /// Verify that accounting idle timer works as expected.
@@ -1556,14 +1556,14 @@ TEST_F(StatusTest, accountingIdleTimer) {
 
     // Check attributes.
     EXPECT_EQ(44, size);
+    EXPECT_EQ(PW_MESSAGE_AUTHENTICATOR, receive_buffer_[AUTH_HDR_LEN]);
+    EXPECT_EQ(AUTH_VECTOR_LEN + 2, receive_buffer_[AUTH_HDR_LEN + 1]);
     uint8_t expected[] = {
         0x04,                   // NAS-IP-Address
         0x06,                   // length
-        0x7f, 0x00, 0x00, 0x01, // 127.0.0.1
-        0x50,                   // Message-Authenticator
-        0x12                    // length
+        0x7f, 0x00, 0x00, 0x01  // 127.0.0.1
     };
-    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN], 8));
+    EXPECT_EQ(0, memcmp(expected, &receive_buffer_[AUTH_HDR_LEN + 18], 6));
 }
 
 } // end of anonymous namespace