]> git.proxmox.com Git - mirror_frr.git/blobdiff - pceplib/test/pcep_msg_tools_test.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pceplib / test / pcep_msg_tools_test.c
index 787df2fd7a05dc4cd37aab7ba98a2befeedc7f93..858650ec8f2dc15a48d6c402e025437675d995bc 100644 (file)
@@ -1,21 +1,9 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
 /*
  * This file is part of the PCEPlib, a PCEP protocol library.
  *
  * Copyright (C) 2020 Volta Networks https://voltanet.io/
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
  * Author : Brady Johnson <brady@voltanet.io>
  *
  */
@@ -25,6 +13,7 @@
 #include "config.h"
 #endif
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -34,6 +23,8 @@
 
 #include <CUnit/CUnit.h>
 
+#include <zebra.h>
+
 #include "pcep_msg_encoding.h"
 #include "pcep_msg_messages.h"
 #include "pcep_msg_tools.h"
@@ -142,7 +133,7 @@ const char *pcep_initiate_cisco_pcc_hexbyte_strs[] = {
 struct pcep_message *create_message(uint8_t msg_type, uint8_t obj1_class,
                                    uint8_t obj2_class, uint8_t obj3_class,
                                    uint8_t obj4_class);
-int convert_hexstrs_to_binary(const char *hexbyte_strs[],
+int convert_hexstrs_to_binary(char *filename, const char *hexbyte_strs[],
                              uint16_t hexbyte_strs_length);
 
 int pcep_tools_test_suite_setup(void)
@@ -166,18 +157,23 @@ void pcep_tools_test_teardown(void)
 {
 }
 
+static const char BASE_TMPFILE[] = "/tmp/pceplib_XXXXXX";
+static int BASE_TMPFILE_SIZE = sizeof(BASE_TMPFILE);
+
 /* Reads an array of hexbyte strs, and writes them to a temporary file.
  * The caller should close the returned file. */
-int convert_hexstrs_to_binary(const char *hexbyte_strs[],
+int convert_hexstrs_to_binary(char *filename, const char *hexbyte_strs[],
                              uint16_t hexbyte_strs_length)
 {
        mode_t oldumask;
-       oldumask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
+       oldumask = umask(S_IXUSR | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
        /* Set umask before anything for security */
        umask(0027);
-       char tmpfile[] = "/tmp/pceplib_XXXXXX";
-       int fd = mkstemp(tmpfile);
+
+       strlcpy(filename, BASE_TMPFILE, BASE_TMPFILE_SIZE);
+       int fd = mkstemp(filename);
        umask(oldumask);
+
        if (fd == -1)
                return -1;
 
@@ -205,14 +201,17 @@ static bool pcep_obj_has_tlv(struct pcep_object_header *obj_hdr)
 
 void test_pcep_msg_read_pcep_initiate()
 {
-       int fd = convert_hexstrs_to_binary(pcep_initiate_hexbyte_strs,
+       char filename[BASE_TMPFILE_SIZE];
+
+       int fd = convert_hexstrs_to_binary(filename, pcep_initiate_hexbyte_strs,
                                           pcep_initiate_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -274,6 +273,7 @@ void test_pcep_msg_read_pcep_initiate()
        double_linked_list *ero_subobj_list =
                ((struct pcep_object_ro *)obj_hdr)->sub_objects;
        CU_ASSERT_PTR_NOT_NULL(ero_subobj_list);
+       assert(ero_subobj_list != NULL);
        CU_ASSERT_EQUAL(ero_subobj_list->num_entries, 2);
        double_linked_list_node *subobj_node = ero_subobj_list->head;
        struct pcep_object_ro_subobj *subobj_hdr =
@@ -299,19 +299,24 @@ void test_pcep_msg_read_pcep_initiate()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 
 void test_pcep_msg_read_pcep_initiate2()
 {
-       int fd = convert_hexstrs_to_binary(pcep_initiate2_hexbyte_strs,
-                                          pcep_initiate2_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       char filename[BASE_TMPFILE_SIZE];
+
+       int fd =
+               convert_hexstrs_to_binary(filename, pcep_initiate2_hexbyte_strs,
+                                         pcep_initiate2_hexbyte_strs_length);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -367,6 +372,7 @@ void test_pcep_msg_read_pcep_initiate2()
        double_linked_list *ero_subobj_list =
                ((struct pcep_object_ro *)obj_hdr)->sub_objects;
        CU_ASSERT_PTR_NOT_NULL(ero_subobj_list);
+       assert(ero_subobj_list != NULL);
        CU_ASSERT_EQUAL(ero_subobj_list->num_entries, 0);
        double_linked_list_node *subobj_node = ero_subobj_list->head;
        CU_ASSERT_PTR_NULL(subobj_node);
@@ -387,18 +393,22 @@ void test_pcep_msg_read_pcep_initiate2()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_open()
 {
-       int fd = convert_hexstrs_to_binary(pcep_open_odl_hexbyte_strs,
+       char filename[BASE_TMPFILE_SIZE];
+
+       int fd = convert_hexstrs_to_binary(filename, pcep_open_odl_hexbyte_strs,
                                           pcep_open_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -431,18 +441,22 @@ void test_pcep_msg_read_pcep_open()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_update()
 {
-       int fd = convert_hexstrs_to_binary(pcep_update_hexbyte_strs,
+       char filename[BASE_TMPFILE_SIZE];
+
+       int fd = convert_hexstrs_to_binary(filename, pcep_update_hexbyte_strs,
                                           pcep_update_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -492,6 +506,7 @@ void test_pcep_msg_read_pcep_update()
        double_linked_list *ero_subobj_list =
                ((struct pcep_object_ro *)obj_hdr)->sub_objects;
        CU_ASSERT_PTR_NOT_NULL(ero_subobj_list);
+       assert(ero_subobj_list != NULL);
        CU_ASSERT_EQUAL(ero_subobj_list->num_entries, 0);
        double_linked_list_node *subobj_node = ero_subobj_list->head;
        CU_ASSERT_PTR_NULL(subobj_node);
@@ -512,19 +527,23 @@ void test_pcep_msg_read_pcep_update()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_open_initiate()
 {
+       char filename[BASE_TMPFILE_SIZE];
+
        int fd = convert_hexstrs_to_binary(
-               pcep_open_initiate_odl_hexbyte_strs,
+               filename, pcep_open_initiate_odl_hexbyte_strs,
                pcep_open_initiate_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 2);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -541,19 +560,23 @@ void test_pcep_msg_read_pcep_open_initiate()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_open_cisco_pce()
 {
+       char filename[BASE_TMPFILE_SIZE];
+
        int fd = convert_hexstrs_to_binary(
-               pcep_open_cisco_pce_hexbyte_strs,
+               filename, pcep_open_cisco_pce_hexbyte_strs,
                pcep_open_cisco_pce_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -573,6 +596,7 @@ void test_pcep_msg_read_pcep_open_cisco_pce()
        CU_ASSERT_EQUAL(open->open_sid, 0);
        CU_ASSERT_EQUAL(open->open_version, 1);
        CU_ASSERT_PTR_NOT_NULL(open->header.tlv_list);
+       assert(open->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(open->header.tlv_list->num_entries, 2);
 
        /* Stateful PCE Capability TLV */
@@ -603,19 +627,23 @@ void test_pcep_msg_read_pcep_open_cisco_pce()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_update_cisco_pce()
 {
+       char filename[BASE_TMPFILE_SIZE];
+
        int fd = convert_hexstrs_to_binary(
-               pcep_update_cisco_pce_hexbyte_strs,
+               filename, pcep_update_cisco_pce_hexbyte_strs,
                pcep_update_cisco_pce_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -631,6 +659,7 @@ void test_pcep_msg_read_pcep_update_cisco_pce()
        CU_ASSERT_EQUAL(srp->header.object_type, PCEP_OBJ_TYPE_SRP);
        CU_ASSERT_EQUAL(srp->header.encoded_object_length, 20);
        CU_ASSERT_PTR_NOT_NULL(srp->header.tlv_list);
+       assert(srp->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(srp->header.tlv_list->num_entries, 1);
        CU_ASSERT_EQUAL(srp->srp_id_number, 1);
        CU_ASSERT_FALSE(srp->flag_lsp_remove);
@@ -651,6 +680,7 @@ void test_pcep_msg_read_pcep_update_cisco_pce()
        CU_ASSERT_EQUAL(lsp->header.object_type, PCEP_OBJ_TYPE_LSP);
        CU_ASSERT_EQUAL(lsp->header.encoded_object_length, 24);
        CU_ASSERT_PTR_NOT_NULL(lsp->header.tlv_list);
+       assert(lsp->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(lsp->header.tlv_list->num_entries, 1);
        CU_ASSERT_EQUAL(lsp->plsp_id, 524303);
        CU_ASSERT_EQUAL(lsp->operational_status, PCEP_LSP_OPERATIONAL_DOWN);
@@ -677,6 +707,7 @@ void test_pcep_msg_read_pcep_update_cisco_pce()
        CU_ASSERT_EQUAL(ero->header.encoded_object_length, 40);
        CU_ASSERT_PTR_NULL(ero->header.tlv_list);
        CU_ASSERT_PTR_NOT_NULL(ero->sub_objects);
+       assert(ero->sub_objects != NULL);
        CU_ASSERT_EQUAL(ero->sub_objects->num_entries, 3);
 
        /* ERO Subobjects */
@@ -744,19 +775,23 @@ void test_pcep_msg_read_pcep_update_cisco_pce()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_report_cisco_pcc()
 {
+       char filename[BASE_TMPFILE_SIZE];
+
        int fd = convert_hexstrs_to_binary(
-               pcep_report_cisco_pcc_hexbyte_strs,
+               filename, pcep_report_cisco_pcc_hexbyte_strs,
                pcep_report_cisco_pcc_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -772,6 +807,7 @@ void test_pcep_msg_read_pcep_report_cisco_pcc()
        CU_ASSERT_EQUAL(srp->header.object_type, PCEP_OBJ_TYPE_SRP);
        CU_ASSERT_EQUAL(srp->header.encoded_object_length, 20);
        CU_ASSERT_PTR_NOT_NULL(srp->header.tlv_list);
+       assert(srp->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(srp->header.tlv_list->num_entries, 1);
        CU_ASSERT_EQUAL(srp->srp_id_number, 0);
        CU_ASSERT_FALSE(srp->flag_lsp_remove);
@@ -792,8 +828,8 @@ void test_pcep_msg_read_pcep_report_cisco_pcc()
        CU_ASSERT_EQUAL(lsp->header.object_type, PCEP_OBJ_TYPE_LSP);
        CU_ASSERT_EQUAL(lsp->header.encoded_object_length, 60);
        CU_ASSERT_PTR_NOT_NULL(lsp->header.tlv_list);
-       /* The TLV with ID 65505 is not recognized, and its not in the list */
-       CU_ASSERT_EQUAL(lsp->header.tlv_list->num_entries, 2);
+       /* The TLV with ID 65505 is now recognized, and its in the list */
+       CU_ASSERT_EQUAL(lsp->header.tlv_list->num_entries, 3);
        CU_ASSERT_EQUAL(lsp->plsp_id, 524303);
        CU_ASSERT_EQUAL(lsp->operational_status, PCEP_LSP_OPERATIONAL_DOWN);
        CU_ASSERT_TRUE(lsp->flag_a);
@@ -838,6 +874,7 @@ void test_pcep_msg_read_pcep_report_cisco_pcc()
        CU_ASSERT_EQUAL(ero->header.encoded_object_length, 4);
        CU_ASSERT_PTR_NULL(ero->header.tlv_list);
        CU_ASSERT_PTR_NOT_NULL(ero->sub_objects);
+       assert(ero->sub_objects != NULL);
        CU_ASSERT_EQUAL(ero->sub_objects->num_entries, 0);
 
        /* LSPA object */
@@ -903,19 +940,23 @@ void test_pcep_msg_read_pcep_report_cisco_pcc()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_pcep_msg_read_pcep_initiate_cisco_pcc()
 {
+       char filename[BASE_TMPFILE_SIZE];
+
        int fd = convert_hexstrs_to_binary(
-               pcep_initiate_cisco_pcc_hexbyte_strs,
+               filename, pcep_initiate_cisco_pcc_hexbyte_strs,
                pcep_initiate_cisco_pcc_hexbyte_strs_length);
-       if(fd == -1){
-               CU_ASSERT_TRUE(fd>=0);
+       if (fd == -1) {
+               CU_ASSERT_TRUE(fd >= 0);
                return;
        }
        double_linked_list *msg_list = pcep_msg_read(fd);
        CU_ASSERT_PTR_NOT_NULL(msg_list);
+       assert(msg_list != NULL);
        CU_ASSERT_EQUAL(msg_list->num_entries, 1);
 
        struct pcep_message *msg = (struct pcep_message *)msg_list->head->data;
@@ -931,6 +972,7 @@ void test_pcep_msg_read_pcep_initiate_cisco_pcc()
        CU_ASSERT_EQUAL(srp->header.object_type, PCEP_OBJ_TYPE_SRP);
        CU_ASSERT_EQUAL(srp->header.encoded_object_length, 20);
        CU_ASSERT_PTR_NOT_NULL(srp->header.tlv_list);
+       assert(srp->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(srp->header.tlv_list->num_entries, 1);
        CU_ASSERT_EQUAL(srp->srp_id_number, 1);
        CU_ASSERT_FALSE(srp->flag_lsp_remove);
@@ -942,6 +984,7 @@ void test_pcep_msg_read_pcep_initiate_cisco_pcc()
        CU_ASSERT_EQUAL(lsp->header.object_type, PCEP_OBJ_TYPE_LSP);
        CU_ASSERT_EQUAL(lsp->header.encoded_object_length, 48);
        CU_ASSERT_PTR_NOT_NULL(lsp->header.tlv_list);
+       assert(lsp->header.tlv_list != NULL);
        CU_ASSERT_EQUAL(lsp->header.tlv_list->num_entries, 2);
        CU_ASSERT_EQUAL(lsp->plsp_id, 0);
        CU_ASSERT_EQUAL(lsp->operational_status, PCEP_LSP_OPERATIONAL_DOWN);
@@ -988,7 +1031,9 @@ void test_pcep_msg_read_pcep_initiate_cisco_pcc()
                        PCEP_OBJ_TYPE_SWITCH_LAYER);
        CU_ASSERT_EQUAL(switch_layer->header.encoded_object_length, 8);
        CU_ASSERT_PTR_NULL(switch_layer->header.tlv_list);
+       assert(switch_layer->header.tlv_list == NULL);
        CU_ASSERT_PTR_NOT_NULL(switch_layer->switch_layer_rows);
+       assert(switch_layer->switch_layer_rows != NULL);
        CU_ASSERT_EQUAL(switch_layer->switch_layer_rows->num_entries, 1);
        struct pcep_object_switch_layer_row *switch_layer_row =
                (struct pcep_object_switch_layer_row *)
@@ -1007,6 +1052,7 @@ void test_pcep_msg_read_pcep_initiate_cisco_pcc()
 
        pcep_msg_free_message_list(msg_list);
        close(fd);
+       unlink(filename);
 }
 
 void test_validate_message_header()