LCOV - code coverage report
Current view: top level - vnsw/agent/test-xml - test_xml_validate.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 83 103 80.6 %
Date: 2026-08-03 02:19:58 Functions: 13 16 81.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : #include "base/os.h"
       5             : #include <iostream>
       6             : #include <fstream>
       7             : #include <pugixml/pugixml.hpp>
       8             : #include <boost/uuid/uuid.hpp>
       9             : 
      10             : #include <test/test_cmn_util.h>
      11             : #include <pkt/test/test_pkt_util.h>
      12             : #include <pkt/flow_mgmt.h>
      13             : #include "test_xml.h"
      14             : #include "test_xml_validate.h"
      15             : 
      16             : using namespace std;
      17             : using namespace pugi;
      18             : using namespace boost::uuids;
      19             : using namespace AgentUtXmlUtils;
      20             : 
      21             : /////////////////////////////////////////////////////////////////////////////
      22             : //  AgentUtXmlValidate routines
      23             : /////////////////////////////////////////////////////////////////////////////
      24          76 : static bool CheckValidateNode(const string &node_name, const xml_node &node,
      25             :                               string *name) {
      26          76 :     if (strcmp(node.name(), node_name.c_str()) != 0) {
      27           0 :         return false;
      28             :     }
      29             : 
      30          76 :     xml_attribute attr = node.attribute("name");
      31          76 :     if (!attr) {
      32             :         cout << "Attribute \"name\" not found for " << node_name
      33           0 :             << ". Skipping..." << endl;
      34           0 :         return false;;
      35             :     }
      36             : 
      37          76 :     *name = attr.as_string();
      38          76 :     if (*name == "") {
      39           0 :         cout << "Invalid \"name\" for " << node_name << " Skipping" << endl;
      40           0 :         return false;
      41             :     }
      42             : 
      43          76 :     return true;
      44             : }
      45             : 
      46          76 : static bool CheckValidateNodeWithUuid(const string &node_name,
      47             :                                       const xml_node &node,
      48             :                                       uuid *id,
      49             :                                       string *name) {
      50          76 :     if (CheckValidateNode(node_name, node, name) == false)
      51           0 :         return false;
      52             : 
      53          76 :     xml_attribute attr = node.attribute("uuid");
      54          76 :     if (!attr) {
      55             :         cout << "Attribute \"uuid\" not found for " << node_name
      56           0 :             << ". Skipping..." << endl;
      57           0 :         return false;;
      58             :     }
      59             : 
      60          76 :     int x = attr.as_uint();
      61          76 :     if (x == 0) {
      62           0 :         cout << "Invalid \"uuid\" (0) for " << node_name << " Skipping" << endl;
      63           0 :         return false;
      64             :     }
      65             : 
      66          76 :     *id = MakeUuid(x);
      67             : 
      68          76 :     return true;
      69             : }
      70             : 
      71          30 : AgentUtXmlValidate::AgentUtXmlValidate(const string &name,
      72             :                                        const xml_node &node,
      73          30 :                                        AgentUtXmlTestCase *test_case) :
      74          30 :     AgentUtXmlNode(name, node, false, test_case) {
      75          30 : }
      76             : 
      77          60 : AgentUtXmlValidate::~AgentUtXmlValidate() {
      78          30 :     for (AgentUtXmlValidationList::iterator i =  node_list_.begin();
      79         106 :          i != node_list_.end(); i++) {
      80          76 :         delete *i;
      81             :     }
      82          60 : }
      83             : 
      84          30 : void AgentUtXmlValidate::ToString(string *str) {
      85          30 :     AgentUtXmlNode::ToString(str);
      86          30 :     *str += "\n";
      87          30 :     return;
      88             : }
      89             : 
      90          30 : string AgentUtXmlValidate::NodeType() {
      91          30 :     return "Validate";
      92             : }
      93             : 
      94          30 : bool AgentUtXmlValidate::ReadXml() {
      95          30 :     AgentUtXmlNode::ReadXml();
      96         106 :     for (xml_node n = node().first_child(); n; n = n.next_sibling()) {
      97             :         uuid id;
      98          76 :         string name;
      99          76 :         AgentUtXmlValidationNode *val = NULL;
     100             : 
     101             :         AgentUtXmlTest::AgentUtXmlTestValidateCreateFn fn =
     102         152 :             test_case()->test()->GetValidateCreateFn(n.name());
     103          76 :         if (CheckValidateNodeWithUuid(n.name(), n, &id, &name) == true) {
     104          76 :             if (fn.empty() == false)
     105          76 :                 val = fn(n.name(), name, id, n);
     106             :         }
     107             : 
     108          76 :         if (val != NULL) {
     109          76 :             val->ReadCmnXml();
     110          76 :             if (val->ReadXml())
     111          76 :                 node_list_.push_back(val);
     112          76 :             cout << "Validate node " << val->ToString() << endl;
     113             :         } else {
     114           0 :             cout << "Unknown node name <" << n.name() << ">. Ignoring" << endl;
     115             :         }
     116          76 :     }
     117             : 
     118          30 :     return true;
     119             : }
     120             : 
     121           0 : bool AgentUtXmlValidate::ToXml(xml_node *parent) {
     122           0 :     assert(0);
     123             :     return true;
     124             : }
     125             : 
     126          30 : bool AgentUtXmlValidate::Run() {
     127          30 :     cout << "Running validation" << " <" << name() << ">" << endl;
     128          30 :     for (AgentUtXmlValidationList::iterator it = node_list_.begin();
     129         106 :          it != node_list_.end(); it++) {
     130          76 :         TestClient::WaitForIdle();
     131          76 :         AgentUtXmlValidationNode *node = *it;
     132          76 :         cout << "Validating " << node->ToString() << endl;
     133          76 :         uint32_t i = 0;
     134          76 :         bool ret = false;
     135          76 :         while (i < node->wait_count()) {
     136          76 :             TestClient::WaitForIdle();
     137          76 :             if (node->Validate() == true) {
     138          76 :                 ret = true;
     139          76 :                 break;
     140             :             }
     141           0 :             usleep(node->sleep_time());
     142           0 :             i++;
     143             :         }
     144          76 :         EXPECT_TRUE(ret);
     145          76 :         if (ret == false) {
     146           0 :             cout << "Failed validation of " << node->ToString() << endl;
     147             :         }
     148             :     }
     149             : 
     150          30 :     return true;
     151             : }
     152             : 
     153             : /////////////////////////////////////////////////////////////////////////////
     154             : //  AgentUtXmlValidationNode routines
     155             : /////////////////////////////////////////////////////////////////////////////
     156          76 : AgentUtXmlValidationNode::AgentUtXmlValidationNode(const string &name,
     157          76 :                                                    const xml_node &node) :
     158          76 :     name_(name), node_(node), present_(true), delete_marked_(false) {
     159          76 : }
     160             : 
     161          76 : AgentUtXmlValidationNode::~AgentUtXmlValidationNode() {
     162          76 : }
     163             : 
     164          76 : bool AgentUtXmlValidationNode::ReadCmnXml() {
     165          76 :     std::string str;
     166          76 :     if (GetStringAttribute(node_, "present", &str)) {
     167          31 :         if (str == "no" || str == "0")
     168           5 :             present_ = false;
     169             :         else
     170          26 :             present_ = true;
     171             :     }
     172             : 
     173          76 :     if (GetStringAttribute(node_, "deleted", &str)) {
     174           0 :        delete_marked_ = true;
     175             :     }
     176             : 
     177          76 :     if (GetStringAttribute(node_, "del", &str)) {
     178           0 :        delete_marked_ = true;
     179             :     }
     180             : 
     181          76 :     if (GetUintAttribute(node_, "id", &id_) == false)
     182          73 :         GetUintAttribute(node_, "uuid", &id_);
     183             : 
     184          76 :     return true;
     185          76 : }
     186             : 
     187           1 : bool AgentUtXmlValidationNode::ReadXml() {
     188           1 :     return true;
     189             : }
     190             : 
     191           0 : bool AgentUtXmlValidationNode::Validate() {
     192           0 :     return true;
     193             : }

Generated by: LCOV version 1.14