OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_xml_validate.cc
Go to the documentation of this file.
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 
22 // AgentUtXmlValidate routines
24 static bool CheckValidateNode(const string &node_name, const xml_node &node,
25  string *name) {
26  if (strcmp(node.name(), node_name.c_str()) != 0) {
27  return false;
28  }
29 
30  xml_attribute attr = node.attribute("name");
31  if (!attr) {
32  cout << "Attribute \"name\" not found for " << node_name
33  << ". Skipping..." << endl;
34  return false;;
35  }
36 
37  *name = attr.as_string();
38  if (*name == "") {
39  cout << "Invalid \"name\" for " << node_name << " Skipping" << endl;
40  return false;
41  }
42 
43  return true;
44 }
45 
46 static bool CheckValidateNodeWithUuid(const string &node_name,
47  const xml_node &node,
48  uuid *id,
49  string *name) {
50  if (CheckValidateNode(node_name, node, name) == false)
51  return false;
52 
53  xml_attribute attr = node.attribute("uuid");
54  if (!attr) {
55  cout << "Attribute \"uuid\" not found for " << node_name
56  << ". Skipping..." << endl;
57  return false;;
58  }
59 
60  int x = attr.as_uint();
61  if (x == 0) {
62  cout << "Invalid \"uuid\" (0) for " << node_name << " Skipping" << endl;
63  return false;
64  }
65 
66  *id = MakeUuid(x);
67 
68  return true;
69 }
70 
72  const xml_node &node,
73  AgentUtXmlTestCase *test_case) :
74  AgentUtXmlNode(name, node, false, test_case) {
75 }
76 
78  for (AgentUtXmlValidationList::iterator i = node_list_.begin();
79  i != node_list_.end(); i++) {
80  delete *i;
81  }
82 }
83 
84 void AgentUtXmlValidate::ToString(string *str) {
86  *str += "\n";
87  return;
88 }
89 
91  return "Validate";
92 }
93 
96  for (xml_node n = node().first_child(); n; n = n.next_sibling()) {
97  uuid id;
98  string name;
99  AgentUtXmlValidationNode *val = NULL;
100 
102  test_case()->test()->GetValidateCreateFn(n.name());
103  if (CheckValidateNodeWithUuid(n.name(), n, &id, &name) == true) {
104  if (fn.empty() == false)
105  val = fn(n.name(), name, id, n);
106  }
107 
108  if (val != NULL) {
109  val->ReadCmnXml();
110  if (val->ReadXml())
111  node_list_.push_back(val);
112  cout << "Validate node " << val->ToString() << endl;
113  } else {
114  cout << "Unknown node name <" << n.name() << ">. Ignoring" << endl;
115  }
116  }
117 
118  return true;
119 }
120 
121 bool AgentUtXmlValidate::ToXml(xml_node *parent) {
122  assert(0);
123  return true;
124 }
125 
127  cout << "Running validation" << " <" << name() << ">" << endl;
128  for (AgentUtXmlValidationList::iterator it = node_list_.begin();
129  it != node_list_.end(); it++) {
132  cout << "Validating " << node->ToString() << endl;
133  uint32_t i = 0;
134  bool ret = false;
135  while (i < node->wait_count()) {
137  if (node->Validate() == true) {
138  ret = true;
139  break;
140  }
141  usleep(node->sleep_time());
142  i++;
143  }
144  EXPECT_TRUE(ret);
145  if (ret == false) {
146  cout << "Failed validation of " << node->ToString() << endl;
147  }
148  }
149 
150  return true;
151 }
152 
154 // AgentUtXmlValidationNode routines
157  const xml_node &node) :
158  name_(name), node_(node), present_(true), delete_marked_(false) {
159 }
160 
162 }
163 
165  std::string str;
166  if (GetStringAttribute(node_, "present", &str)) {
167  if (str == "no" || str == "0")
168  present_ = false;
169  else
170  present_ = true;
171  }
172 
173  if (GetStringAttribute(node_, "deleted", &str)) {
174  delete_marked_ = true;
175  }
176 
177  if (GetStringAttribute(node_, "del", &str)) {
178  delete_marked_ = true;
179  }
180 
181  if (GetUintAttribute(node_, "id", &id_) == false)
182  GetUintAttribute(node_, "uuid", &id_);
183 
184  return true;
185 }
186 
188  return true;
189 }
190 
192  return true;
193 }
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:56
AgentUtXmlValidate(const std::string &name, const pugi::xml_node &node, AgentUtXmlTestCase *test_case)
AgentUtXmlTestValidateCreateFn GetValidateCreateFn(const std::string &name)
Definition: test_xml.cc:170
static bool CheckValidateNode(const string &node_name, const xml_node &node, string *name)
virtual const std::string ToString()=0
virtual bool ReadXml()=0
Definition: test_xml.cc:466
boost::uuids::uuid uuid
static void WaitForIdle()
static bool CheckValidateNodeWithUuid(const string &node_name, const xml_node &node, uuid *id, string *name)
virtual std::string NodeType()
virtual bool ReadXml()
bool GetStringAttribute(const xml_node &node, const string &name, string *value)
Definition: test_xml.cc:25
AgentUtXmlTest * test()
Definition: test_xml.h:94
AgentUtXmlValidationNode(const std::string &name, const pugi::xml_node &node)
virtual void ToString(std::string *str)
bool GetUintAttribute(const xml_node &node, const string &name, uint16_t *value)
Definition: test_xml.cc:37
virtual bool ToXml(pugi::xml_node *parent)
virtual uint32_t sleep_time() const
AgentUtXmlTestCase * test_case()
Definition: test_xml.h:132
AgentUtXmlValidationList node_list_
const pugi::xml_node & node() const
Definition: test_xml.h:131
virtual void ToString(std::string *str)
Definition: test_xml.cc:478
const std::string & name() const
Definition: test_xml.h:130