Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TestUtility.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // $Id$
  2. //
  3. // \file TestUtility.cpp
  4. //
  5. // Copyright (C) 2012, ARM Research Labs, LLC.
  6. // See www.armresearch.com for the copyright terms.
  7. //
  8. // This is the unit test for the class Utility.
  9. //
  10. #include <iostream>
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <cstdlib>
  14. #include <cstring>
  15. #include <cerrno>
  16. #include "Utility.hpp"
  17. /// Output error.
  18. #define Error(msg) \
  19. { \
  20. std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
  21. std::cerr << msg; \
  22. }
  23. /// Exit with error.
  24. #define ErrorExit(msg) \
  25. { \
  26. Error(msg) \
  27. std::exit(-1); \
  28. }
  29. bool
  30. TestReplaceXmlAttribute() {
  31. std::string Content;
  32. std::string ExpectedContent;
  33. std::string ElementName = "TestElement";
  34. std::string AttributeName = "TestAttribute";
  35. std::string AttributeNewValue = "NewValue";
  36. // Test with valid input.
  37. std::string OriginalContent;
  38. Content = "<!-- License file created by SNFIdentity-->\n"
  39. "<snf>\n"
  40. "<TestElement TestAttribute='testmode' authentication='setuptestingonly'/>\n"
  41. "<TestXXX XXXAttribute='testmode' authentication='setuptestingonly'/>\n"
  42. "</snf>\n";
  43. OriginalContent = Content;
  44. ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
  45. "<snf>\n"
  46. "<TestElement TestAttribute='NewValue' authentication='setuptestingonly'/>\n"
  47. "<TestXXX XXXAttribute='testmode' authentication='setuptestingonly'/>\n"
  48. "</snf>\n";
  49. try {
  50. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  51. } catch (std::exception &e) {
  52. std::string Temp;
  53. Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
  54. Temp += e.what();
  55. Temp += "\n\nOriginal Content:\n\n";
  56. Temp += OriginalContent + "\n";
  57. Error(Temp);
  58. return false;
  59. }
  60. if (Content != ExpectedContent) {
  61. std::string Temp;
  62. Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
  63. Temp += Content;
  64. Temp += "\n\nExpected:\n\n";
  65. Temp += ExpectedContent + "\n";
  66. Error(Temp);
  67. return false;
  68. }
  69. Content = "<!-- License file created by SNFIdentity-->\n"
  70. "<snf>\n"
  71. "<TestElement TestAttribute\n\t= \n\t\t 'testmode' authentication='setuptestingonly'>\n"
  72. "</TestElement>"
  73. "</snf>\n";
  74. OriginalContent = Content;
  75. ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
  76. "<snf>\n"
  77. "<TestElement TestAttribute\n\t= \n\t\t 'NewValue' authentication='setuptestingonly'>\n"
  78. "</TestElement>"
  79. "</snf>\n";
  80. try {
  81. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  82. } catch (std::exception &e) {
  83. std::string Temp;
  84. Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
  85. Temp += e.what();
  86. Temp += "\n\nOriginal Content:\n\n";
  87. Temp += OriginalContent + "\n";
  88. Error(Temp);
  89. return false;
  90. }
  91. if (Content != ExpectedContent) {
  92. std::string Temp;
  93. Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
  94. Temp += Content;
  95. Temp += "\n\nExpected:\n\n";
  96. Temp += ExpectedContent + "\n";
  97. Error(Temp);
  98. return false;
  99. }
  100. Content = "<!-- License file created by SNFIdentity-->\n"
  101. "<snf>\n"
  102. "<TestElement TestAttribute=\"testmode\" TestXXAttribute='setuptestingonly'/>\n"
  103. "</snf>\n";
  104. OriginalContent = Content;
  105. ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
  106. "<snf>\n"
  107. "<TestElement TestAttribute=\"NewValue\" TestXXAttribute='setuptestingonly'/>\n"
  108. "</snf>\n";
  109. try {
  110. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  111. } catch (std::exception &e) {
  112. std::string Temp;
  113. Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
  114. Temp += e.what();
  115. Temp += "\n\nOriginal Content:\n\n";
  116. Temp += OriginalContent + "\n";
  117. Error(Temp);
  118. return false;
  119. }
  120. if (Content != ExpectedContent) {
  121. std::string Temp;
  122. Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
  123. Temp += Content;
  124. Temp += "\n\nExpected:\n\n";
  125. Temp += ExpectedContent + "\n";
  126. Error(Temp);
  127. return false;
  128. }
  129. Content = "<!-- License file created by SNFIdentity-->\n"
  130. "<snf>\n"
  131. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
  132. "</snf>\n";
  133. OriginalContent = Content;
  134. ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
  135. "<snf>\n"
  136. "<TestElement TestAttribute\t\n= \"NewValue\" authentication='setuptestingonly'/>\n"
  137. "</snf>\n";
  138. try {
  139. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  140. } catch (std::exception &e) {
  141. std::string Temp;
  142. Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
  143. Temp += e.what();
  144. Temp += "\n\nOriginal Content:\n\n";
  145. Temp += OriginalContent + "\n";
  146. Error(Temp);
  147. return false;
  148. }
  149. if (Content != ExpectedContent) {
  150. std::string Temp;
  151. Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
  152. Temp += Content;
  153. Temp += "\n\nExpected:\n\n";
  154. Temp += ExpectedContent + "\n";
  155. Error(Temp);
  156. return false;
  157. }
  158. // Test with invalid input.
  159. // Duplicate element.
  160. Content = "<!-- License file created by SNFIdentity-->\n"
  161. "<snf>\n"
  162. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
  163. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
  164. "</snf>\n";
  165. OriginalContent = Content;
  166. try {
  167. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  168. std::string Temp;
  169. Temp = "TestReplaceXmlAttribute: Exception not thrown with two <TestElement> elements. Original content:\n\n";
  170. Temp += OriginalContent + "\n";
  171. Error(Temp);
  172. return false;
  173. } catch (std::exception &e) {
  174. std::cout << "Duplicate TestElement: " << e.what() << "\n\n";
  175. }
  176. // Missing element.
  177. Content = "<!-- License file created by SNFIdentity-->\n"
  178. "<snf>\n"
  179. "</snf>\n";
  180. OriginalContent = Content;
  181. try {
  182. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  183. std::string Temp;
  184. Temp = "TestReplaceXmlAttribute: Exception not thrown with missing <TestElement> element. Original content:\n\n";
  185. Temp += OriginalContent + "\n";
  186. Error(Temp);
  187. return false;
  188. } catch (std::exception &e) {
  189. std::cout << "Missing TestElement: " << e.what() << "\n\n";
  190. }
  191. // Malformed element.
  192. Content = "<!-- License file created by SNFIdentity-->\n"
  193. "<snf>\n"
  194. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'\n"
  195. "</snf>\n";
  196. OriginalContent = Content;
  197. try {
  198. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  199. std::string Temp;
  200. Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed <TestElement> element. Original content:\n\n";
  201. Temp += OriginalContent + "\n";
  202. Error(Temp);
  203. return false;
  204. } catch (std::exception &e) {
  205. std::cout << "No closing to TestElement: " << e.what() << "\n\n";
  206. }
  207. // Duplicate attribute.
  208. Content = "<!-- License file created by SNFIdentity-->\n"
  209. "<snf>\n"
  210. "<TestElement TestAttribute\t\n= \"testmode\" TestAttribute='setuptestingonly'/>\n"
  211. "</snf>\n";
  212. OriginalContent = Content;
  213. try {
  214. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  215. std::string Temp;
  216. Temp = "TestReplaceXmlAttribute: Exception not thrown with two TestAttribute attributes. Original content:\n\n";
  217. Temp += OriginalContent + "\n";
  218. Error(Temp);
  219. return false;
  220. } catch (std::exception &e) {
  221. std::cout << "Duplicate attribute: " << e.what() << "\n\n";
  222. }
  223. // Attribute in another element
  224. Content = "<!-- License file created by SNFIdentity-->\n"
  225. "<snf>\n"
  226. "<TestElement XXXTestAttribute='setuptestingonly'/>\n"
  227. "<ATestElement TestAttribute\t\n= \"testmode\"/>\n"
  228. "</snf>\n";
  229. OriginalContent = Content;
  230. try {
  231. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  232. std::string Temp;
  233. Temp = "TestReplaceXmlAttribute: Exception not thrown with TestAttribute attribute in another element. "
  234. "Original content:\n\n";
  235. Temp += OriginalContent + "\n";
  236. Error(Temp);
  237. return false;
  238. } catch (std::exception &e) {
  239. std::cout << "TestAttribute in another element: " << e.what() << "\n\n";
  240. }
  241. // Missing attribute.
  242. Content = "<!-- License file created by SNFIdentity-->\n"
  243. "<snf>\n"
  244. "<TestElement XXXTestAttribute\t\n= \"testmode\" YYYTestAttribute='setuptestingonly'/>\n"
  245. "</snf>\n";
  246. OriginalContent = Content;
  247. try {
  248. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  249. std::string Temp;
  250. Temp = "TestReplaceXmlAttribute: Exception not thrown with missing TestAttribute attribute. Original content:\n\n";
  251. Temp += OriginalContent + "\n";
  252. Error(Temp);
  253. return false;
  254. } catch (std::exception &e) {
  255. std::cout << "Missing attribute: " << e.what() << "\n\n";
  256. }
  257. // Malformed attribute value.
  258. Content = "<!-- License file created by SNFIdentity-->\n"
  259. "<snf>\n"
  260. "<TestElement TestAttribute\t\n= \"testmode' />\n"
  261. "</snf>\n";
  262. OriginalContent = Content;
  263. try {
  264. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  265. std::string Temp;
  266. Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed TestAttribute value. "
  267. "Original content:\n\n";
  268. Temp += OriginalContent + "\n";
  269. Error(Temp);
  270. return false;
  271. } catch (std::exception &e) {
  272. std::cout << "No closing to attribute: " << e.what() << "\n\n";
  273. }
  274. return true;
  275. }
  276. /// Unit tests for Utility.
  277. //
  278. int main(int argc, char* argv[]) {
  279. try { // Catch anything that breaks loose.
  280. // Test ReplaceXmlAttribute().
  281. if (!TestReplaceXmlAttribute()) {
  282. ErrorExit("TestReplaceXmlAttribute() failure.\n");
  283. }
  284. } // That's all folks.
  285. catch(std::exception& e) { // Report any normal exceptions.
  286. std::cerr << "Utility exception: " << e.what() << std::endl;
  287. return -1;
  288. }
  289. catch(...) { // Report any unexpected exceptions.
  290. std::cerr << "Panic! Unknown Exception!" << std::endl;
  291. return -1;
  292. }
  293. return 0; // Normally we return zero.
  294. }