Commit 24c0d32faa for frr

commit 24c0d32faa046e51d60071ab734fefe965dcd3c8
Author: Mark Stapp <mjs@cisco.com>
Date:   Thu Sep 17 11:20:05 2026 -0400

    pceplib: update pceplib unit-tests for Path Setup Cap TLV

    Update the unit tests that use the Path Setup Capability TLV
    to account for changes and stricter validation. Also add
    a couple of negative test cases for invalid TLV and sub-TLV
    lengths.

    Signed-off-by: Mark Stapp <mjs@cisco.com>

diff --git a/pceplib/test/pcep_msg_tlvs_test.c b/pceplib/test/pcep_msg_tlvs_test.c
index 0731cee46e..6d8067d387 100644
--- a/pceplib/test/pcep_msg_tlvs_test.c
+++ b/pceplib/test/pcep_msg_tlvs_test.c
@@ -28,6 +28,7 @@
 #include "pcep_msg_tlvs.h"
 #include "pcep_msg_tools.h"
 #include "pcep_utils_memory.h"
+#include "pcep_utils_logging.h"
 #include "pcep_msg_tlvs_test.h"

 /*
@@ -166,9 +167,12 @@ void test_pcep_tlv_create_path_setup_type(void)

 void test_pcep_tlv_create_path_setup_type_capability(void)
 {
+	uint16_t len;
+
 	/* The sub_tlv list is optional */

 	/* Should return NULL if pst_list is NULL */
+	struct pcep_object_tlv_path_setup_type_capability *tlv2;
 	struct pcep_object_tlv_path_setup_type_capability *tlv =
 		pcep_tlv_create_path_setup_type_capability(NULL, NULL);
 	CU_ASSERT_PTR_NULL(tlv);
@@ -232,9 +236,17 @@ void test_pcep_tlv_create_path_setup_type_capability(void)
 	pst_list = dll_initialize();
 	sub_tlv_list = dll_initialize();
 	pst1 = pceplib_malloc(PCEPLIB_MESSAGES, 1);
+	pst2 = pceplib_malloc(PCEPLIB_MESSAGES, 1);
 	*pst1 = 1;
+	*pst2 = 2;
 	dll_append(pst_list, pst1);
+	dll_append(pst_list, pst2);
+	dll_append(sub_tlv_list, sub_tlv);
+
+	/* Add a second sub-tlv */
+	sub_tlv = (void *)pcep_tlv_create_sr_pce_capability(true, false, 0);
 	dll_append(sub_tlv_list, sub_tlv);
+
 	tlv = pcep_tlv_create_path_setup_type_capability(pst_list,
 							 sub_tlv_list);
 	CU_ASSERT_PTR_NOT_NULL(tlv);
@@ -243,9 +255,11 @@ void test_pcep_tlv_create_path_setup_type_capability(void)
 	pcep_encode_tlv(&tlv->header, versioning, tlv_buf);
 	CU_ASSERT_EQUAL(tlv->header.type,
 			PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY);
-	CU_ASSERT_EQUAL(tlv->header.encoded_tlv_length,
-			sizeof(uint32_t) * 2 + TLV_HEADER_LENGTH
-				+ sub_tlv->encoded_tlv_length);
+
+	/* Encoded length is the TLV body, plus two sub-tlvs */
+	len = sizeof(uint32_t) * 2 + (2 * TLV_HEADER_LENGTH) +
+		(2 * sub_tlv->encoded_tlv_length);
+	CU_ASSERT_EQUAL(tlv->header.encoded_tlv_length, len);
 	CU_ASSERT_PTR_NOT_NULL(tlv->pst_list);
 	CU_ASSERT_PTR_NOT_NULL(tlv->sub_tlv_list);
 	uint32_ptr = (uint32_t *)tlv->header.encoded_tlv;
@@ -253,8 +267,8 @@ void test_pcep_tlv_create_path_setup_type_capability(void)
 	CU_ASSERT_EQUAL(uint16_ptr[0],
 			htons(PCEP_OBJ_TLV_TYPE_PATH_SETUP_TYPE_CAPABILITY));
 	CU_ASSERT_EQUAL(uint16_ptr[1], htons(tlv->header.encoded_tlv_length));
-	CU_ASSERT_EQUAL(uint32_ptr[1], htonl(0x00000001));
-	CU_ASSERT_EQUAL(uint32_ptr[2], htonl(0x01000000));
+	CU_ASSERT_EQUAL(uint32_ptr[1], htonl(0x00000002));
+	CU_ASSERT_EQUAL(uint32_ptr[2], htonl(0x01020000));
 	/* Verify the Sub-TLV */
 	uint16_ptr = (uint16_t *)(tlv->header.encoded_tlv + 12);
 	CU_ASSERT_EQUAL(uint16_ptr[0],
@@ -264,6 +278,55 @@ void test_pcep_tlv_create_path_setup_type_capability(void)
 	CU_ASSERT_EQUAL(uint16_ptr[3], htons(0x0300));

 	pcep_obj_free_tlv(&tlv->header);
+
+	pcep_log(LOG_INFO, "%s: validation testing", __func__);
+
+	/* Make the sub-tlv invalid with a huge length */
+	uint16_ptr[1] = 99;
+	/* Ensure the invalid buffer doesn't parse */
+	tlv2 = (void *)pcep_decode_tlv(tlv_buf);
+	CU_ASSERT_PTR_NULL(tlv2);
+	if (tlv2)
+		pcep_obj_free_tlv(&tlv2->header);
+
+	/* Restore sub-tlv length */
+	uint16_ptr[1] = htons(4);
+
+	/* Reduce tlv header length */
+	uint16_ptr = (uint16_t *)tlv_buf;
+	uint16_ptr[1] = htons(len - 4);
+
+	tlv2 = (void *)pcep_decode_tlv(tlv_buf);
+	CU_ASSERT_PTR_NULL(tlv2);
+	if (tlv2)
+		pcep_obj_free_tlv(&tlv2->header);
+
+	/* Test TLV encoder length calculation */
+	pst_list = dll_initialize();
+
+	pst1 = pceplib_malloc(PCEPLIB_MESSAGES, 1);
+	pst2 = pceplib_malloc(PCEPLIB_MESSAGES, 1);
+	*pst1 = 1;
+	*pst2 = 2;
+	dll_append(pst_list, pst1);
+	dll_append(pst_list, pst2);
+	tlv = pcep_tlv_create_path_setup_type_capability(pst_list, NULL);
+	CU_ASSERT_PTR_NOT_NULL(tlv);
+	assert(tlv != NULL);
+
+	reset_tlv_buffer();
+	pcep_encode_tlv(&tlv->header, versioning, tlv_buf);
+
+	/* Encoded length must not include padding */
+	uint16_ptr = (uint16_t *)tlv->header.encoded_tlv;
+	CU_ASSERT_EQUAL(6, htons(uint16_ptr[1]));
+	pcep_obj_free_tlv(&tlv->header);
+
+	/* Decode should succeed */
+	tlv2 = (void *)pcep_decode_tlv(tlv_buf);
+	CU_ASSERT_PTR_NOT_NULL(tlv2);
+	if (tlv2)
+		pcep_obj_free_tlv(&tlv2->header);
 }

 void test_pcep_tlv_create_sr_pce_capability(void)
diff --git a/pceplib/test/pcep_session_logic_test.c b/pceplib/test/pcep_session_logic_test.c
index c5b2bb3e26..08a25e1bb1 100644
--- a/pceplib/test/pcep_session_logic_test.c
+++ b/pceplib/test/pcep_session_logic_test.c
@@ -268,11 +268,11 @@ void test_create_pcep_session_open_tlvs()
 	pceplib_free(PCEPLIB_MESSAGES, encoded_msg);


-	/* Verify the created Open message only has 4 TLVs:
+	/* Verify the created Open message only has 3 TLVs:
 	 *   pcep_tlv_create_stateful_pce_capability()
 	 *   pcep_tlv_create_lsp_db_version()
-	 *   pcep_tlv_create_sr_pce_capability()
-	 *   pcep_tlv_create_path_setup_type_capability() */
+	 *   pcep_tlv_create_path_setup_type_capability()
+	 */
 	reset_mock_socket_comm_info();
 	mock_info->send_message_save_message = true;
 	config.support_sr_te_pst = true;