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

Generated by: LCOV version 1.14