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