選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestUtility.cpp 12KB

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