Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestUtility.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. // Element in comment.
  159. Content = "<!-- License file created by SNFIdentity -->\n"
  160. "<!-- This is a comment\n"
  161. "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
  162. "-->\n"
  163. "<snf>\n"
  164. "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
  165. "</snf>\n";
  166. OriginalContent = Content;
  167. ExpectedContent = "<!-- License file created by SNFIdentity -->\n"
  168. "<!-- This is a comment\n"
  169. "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
  170. "-->\n"
  171. "<snf>\n"
  172. "<TestElement TestAttribute = \"NewValue\" authentication='setuptestingonly'/>\n"
  173. "</snf>\n";
  174. try {
  175. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  176. } catch (std::exception &e) {
  177. std::string Temp;
  178. Temp = "TestReplaceXmlAttribute: Unexpected exception thrown with valid input: ";
  179. Temp += e.what();
  180. Temp += "\n\nOriginal Content:\n\n";
  181. Temp += OriginalContent + "\n";
  182. Error(Temp);
  183. return false;
  184. }
  185. if (Content != ExpectedContent) {
  186. std::string Temp;
  187. Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
  188. Temp += Content;
  189. Temp += "\n\nExpected:\n\n";
  190. Temp += ExpectedContent + "\n";
  191. Error(Temp);
  192. return false;
  193. }
  194. // Test with invalid input.
  195. // Duplicate element.
  196. Content = "<!-- License file created by SNFIdentity-->\n"
  197. "<snf>\n"
  198. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
  199. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
  200. "</snf>\n";
  201. OriginalContent = Content;
  202. try {
  203. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  204. std::string Temp;
  205. Temp = "TestReplaceXmlAttribute: Exception not thrown with two <TestElement> elements. Original content:\n\n";
  206. Temp += OriginalContent + "\n";
  207. Error(Temp);
  208. return false;
  209. } catch (std::exception &e) {
  210. }
  211. // Missing element.
  212. Content = "<!-- License file created by SNFIdentity-->\n"
  213. "<snf>\n"
  214. "</snf>\n";
  215. OriginalContent = Content;
  216. try {
  217. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  218. std::string Temp;
  219. Temp = "TestReplaceXmlAttribute: Exception not thrown with missing <TestElement> element. Original content:\n\n";
  220. Temp += OriginalContent + "\n";
  221. Error(Temp);
  222. return false;
  223. } catch (std::exception &e) {
  224. }
  225. // Malformed element.
  226. Content = "<!-- License file created by SNFIdentity-->\n"
  227. "<snf>\n"
  228. "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'\n"
  229. "</snf>\n";
  230. OriginalContent = Content;
  231. try {
  232. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  233. std::string Temp;
  234. Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed <TestElement> element. Original content:\n\n";
  235. Temp += OriginalContent + "\n";
  236. Error(Temp);
  237. return false;
  238. } catch (std::exception &e) {
  239. }
  240. // Duplicate attribute.
  241. Content = "<!-- License file created by SNFIdentity-->\n"
  242. "<snf>\n"
  243. "<TestElement TestAttribute\t\n= \"testmode\" TestAttribute='setuptestingonly'/>\n"
  244. "</snf>\n";
  245. OriginalContent = Content;
  246. try {
  247. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  248. std::string Temp;
  249. Temp = "TestReplaceXmlAttribute: Exception not thrown with two TestAttribute attributes. Original content:\n\n";
  250. Temp += OriginalContent + "\n";
  251. Error(Temp);
  252. return false;
  253. } catch (std::exception &e) {
  254. }
  255. // Attribute in another element
  256. Content = "<!-- License file created by SNFIdentity-->\n"
  257. "<snf>\n"
  258. "<TestElement XXXTestAttribute='setuptestingonly'/>\n"
  259. "<ATestElement TestAttribute\t\n= \"testmode\"/>\n"
  260. "</snf>\n";
  261. OriginalContent = Content;
  262. try {
  263. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  264. std::string Temp;
  265. Temp = "TestReplaceXmlAttribute: Exception not thrown with TestAttribute attribute in another element. "
  266. "Original content:\n\n";
  267. Temp += OriginalContent + "\n";
  268. Error(Temp);
  269. return false;
  270. } catch (std::exception &e) {
  271. }
  272. // Missing attribute.
  273. Content = "<!-- License file created by SNFIdentity-->\n"
  274. "<snf>\n"
  275. "<TestElement XXXTestAttribute\t\n= \"testmode\" YYYTestAttribute='setuptestingonly'/>\n"
  276. "</snf>\n";
  277. OriginalContent = Content;
  278. try {
  279. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  280. std::string Temp;
  281. Temp = "TestReplaceXmlAttribute: Exception not thrown with missing TestAttribute attribute. Original content:\n\n";
  282. Temp += OriginalContent + "\n";
  283. Error(Temp);
  284. return false;
  285. } catch (std::exception &e) {
  286. }
  287. // Malformed attribute value.
  288. Content = "<!-- License file created by SNFIdentity-->\n"
  289. "<snf>\n"
  290. "<TestElement TestAttribute\t\n= \"testmode' />\n"
  291. "</snf>\n";
  292. OriginalContent = Content;
  293. try {
  294. Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
  295. std::string Temp;
  296. Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed TestAttribute value. "
  297. "Original content:\n\n";
  298. Temp += OriginalContent + "\n";
  299. Error(Temp);
  300. return false;
  301. } catch (std::exception &e) {
  302. }
  303. return true;
  304. }
  305. /// Unit tests for Utility.
  306. //
  307. int main(int argc, char* argv[]) {
  308. try { // Catch anything that breaks loose.
  309. // Test ReplaceXmlAttribute().
  310. if (!TestReplaceXmlAttribute()) {
  311. ErrorExit("TestReplaceXmlAttribute() failure.\n");
  312. }
  313. } // That's all folks.
  314. catch(std::exception& e) { // Report any normal exceptions.
  315. std::cerr << "Utility exception: " << e.what() << std::endl;
  316. return -1;
  317. }
  318. catch(...) { // Report any unexpected exceptions.
  319. std::cerr << "Panic! Unknown Exception!" << std::endl;
  320. return -1;
  321. }
  322. return 0; // Normally we return zero.
  323. }