PageRenderTime 2882ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.3-wix3.0/src/CustomActions/StringTools/StringToolsImplUnitTests.cpp

#
C++ | 341 lines | 269 code | 66 blank | 6 comment | 32 complexity | d3ea5c96a4c16be978cdaa1a0b1f8eef MD5 | raw file
Possible License(s): LGPL-2.0, EPL-1.0, LGPL-2.1
  1. #include "StdAfxUnitTests.h"
  2. #include "StringToolsImplUnitTests.h"
  3. #include <String/StringUtils.h>
  4. CPPUNIT_TEST_SUITE_REGISTRATION(AppSecInc::UnitTests::StringTools::StringToolsImplUnitTests);
  5. using namespace AppSecInc::Msi;
  6. using namespace AppSecInc::UnitTests::StringTools;
  7. void StringToolsImplUnitTests::Test_EntryPoints()
  8. {
  9. HMODULE h = ::LoadLibrary(L"StringTools.dll");
  10. CPPUNIT_ASSERT_MESSAGE("Missing StringTools.dll", h != NULL);
  11. CPPUNIT_ASSERT(NULL != GetProcAddress(h, "String_Replace"));
  12. CPPUNIT_ASSERT_MESSAGE("Missing String_ToLower", NULL != GetProcAddress(h, "String_ToLower"));
  13. CPPUNIT_ASSERT_MESSAGE("Missing String_ToUpper", NULL != GetProcAddress(h, "String_ToUpper"));
  14. CPPUNIT_ASSERT_MESSAGE("Missing String_Trim", NULL != GetProcAddress(h, "String_Trim"));
  15. CPPUNIT_ASSERT_MESSAGE("Missing String_LTrim", NULL != GetProcAddress(h, "String_LTrim"));
  16. CPPUNIT_ASSERT_MESSAGE("Missing String_RTrim", NULL != GetProcAddress(h, "String_RTrim"));
  17. ::FreeLibrary(h);
  18. }
  19. void StringToolsImplUnitTests::Test_StringReplace()
  20. {
  21. AppSecInc::Msi::MsiShim hInstall;
  22. MsiInstall msiInstall(hInstall);
  23. typedef struct
  24. {
  25. LPCSTR testIn;
  26. LPCSTR testFrom;
  27. LPCSTR testTo;
  28. int testExpectedResult;
  29. LPCSTR testExpectedOutput;
  30. } TestData;
  31. TestData testData[] =
  32. {
  33. { "", "", "", 0, "" },
  34. { "x", "x", "y", 1, "y" },
  35. { "x", "xx", "y", 0, "x" },
  36. { "foo", "fo", "f", 1, "fo" },
  37. { "{source}\r", "source", "target", 1, "{target}\r" },
  38. { "{source}\r", "source", "target", 1, "{target}\r" },
  39. { "\n{source}\r", "source", "target", 1, "\n{target}\r" },
  40. { "\n{source}{source}\r", "source", "target", 2, "\n{target}{target}\r" },
  41. { "\n{source}XZY{source}\r", "source", "target", 2, "\n{target}XZY{target}\r" }
  42. };
  43. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  44. {
  45. std::stringstream message;
  46. message << "'" << testData[i].testIn << "'.replace('" <<
  47. testData[i].testFrom << "', '" << testData[i].testTo << "') didn't return '" <<
  48. testData[i].testExpectedResult << "'";
  49. msiInstall.SetProperty("STRING_VALUE_FROM", testData[i].testIn);
  50. msiInstall.SetProperty("STRING_REPLACE_FROM", testData[i].testFrom);
  51. msiInstall.SetProperty("STRING_REPLACE_TO", testData[i].testTo);
  52. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_Replace"));
  53. int replacement_count = atoi(msiInstall.GetProperty("STRING_REPLACE_COUNT").c_str());
  54. std::string output = msiInstall.GetProperty("STRING_VALUE_TO");
  55. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(),
  56. testData[i].testExpectedResult == replacement_count);
  57. message << "'" << testData[i].testIn << "'.replace('" <<
  58. testData[i].testFrom << "', '" << testData[i].testTo << "') didn't return '" <<
  59. testData[i].testExpectedOutput << "' (returned '" << output << "')";
  60. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(),
  61. testData[i].testExpectedOutput == output);
  62. }
  63. }
  64. void StringToolsImplUnitTests::Test_StringTrim()
  65. {
  66. AppSecInc::Msi::MsiShim hInstall;
  67. MsiInstall msiInstall(hInstall);
  68. typedef struct
  69. {
  70. LPCSTR testInput;
  71. LPCSTR testWhitespaces;
  72. LPCSTR testExpectedResult;
  73. } TestData;
  74. TestData testData[] =
  75. {
  76. { " this is a string whith whitespaces\t ", "", "this is a string whith whitespaces" },
  77. { " this is a string whith whitespaces ", " ", "this is a string whith whitespaces" },
  78. { "this string doesn't have whitespaces", "", "this string doesn't have whitespaces" },
  79. { "%%test string%%", "%", "test string" },
  80. { " test string ", "", "test string" }
  81. };
  82. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  83. {
  84. msiInstall.SetProperty("STRING_TRIM_INPUT", testData[i].testInput);
  85. msiInstall.SetProperty("STRING_TRIM_WHITESPACES", testData[i].testWhitespaces);
  86. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_Trim"));
  87. std::string stringTrimResult = msiInstall.GetProperty("STRING_TRIM_RESULT");
  88. std::stringstream message;
  89. message << "StringTrim('" << testData[i].testInput <<
  90. "', '" << testData[i].testWhitespaces << "') returned '" << stringTrimResult <<
  91. "'. It was expected: '" << testData[i].testExpectedResult << "'";
  92. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(), testData[i].testExpectedResult == stringTrimResult);
  93. }
  94. }
  95. void StringToolsImplUnitTests::Test_StringLTrim()
  96. {
  97. AppSecInc::Msi::MsiShim hInstall;
  98. MsiInstall msiInstall(hInstall);
  99. typedef struct
  100. {
  101. LPCSTR testInput;
  102. LPCSTR testWhitespaces;
  103. LPCSTR testExpectedResult;
  104. } TestData;
  105. TestData testData[] =
  106. {
  107. { " this is a string whith whitespaces\t ", "", "this is a string whith whitespaces\t " },
  108. { " this is a string whith whitespaces ", " ", "this is a string whith whitespaces " },
  109. { "this string doesn't have whitespaces", "", "this string doesn't have whitespaces" },
  110. { "%%test string%%", "%", "test string%%" },
  111. { " test string ", "", "test string " }
  112. };
  113. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  114. {
  115. msiInstall.SetProperty("STRING_LTRIM_INPUT", testData[i].testInput);
  116. msiInstall.SetProperty("STRING_LTRIM_WHITESPACES", testData[i].testWhitespaces);
  117. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_LTrim"));
  118. std::string stringLTrimResult = msiInstall.GetProperty("STRING_LTRIM_RESULT");
  119. std::stringstream message;
  120. message << "StringLTrim('" << testData[i].testInput <<
  121. "', '" << testData[i].testWhitespaces << "') returned '" << stringLTrimResult <<
  122. "'. It was expected: '" << testData[i].testExpectedResult << "'";
  123. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(), testData[i].testExpectedResult == stringLTrimResult);
  124. }
  125. }
  126. void StringToolsImplUnitTests::Test_StringRTrim()
  127. {
  128. AppSecInc::Msi::MsiShim hInstall;
  129. MsiInstall msiInstall(hInstall);
  130. typedef struct
  131. {
  132. LPCSTR testInput;
  133. LPCSTR testWhitespaces;
  134. LPCSTR testExpectedResult;
  135. } TestData;
  136. TestData testData[] =
  137. {
  138. { " this is a string whith whitespaces\t ", "", " this is a string whith whitespaces" },
  139. { " this is a string whith whitespaces ", " ", " this is a string whith whitespaces" },
  140. { "this string doesn't have whitespaces", "", "this string doesn't have whitespaces" },
  141. { "%%test string%%", "%", "%%test string" },
  142. { " test string ", "", " test string" }
  143. };
  144. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  145. {
  146. msiInstall.SetProperty("STRING_RTRIM_INPUT", testData[i].testInput);
  147. msiInstall.SetProperty("STRING_RTRIM_WHITESPACES", testData[i].testWhitespaces);
  148. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_RTrim"));
  149. std::string stringRTrimResult = msiInstall.GetProperty("STRING_RTRIM_RESULT");
  150. std::stringstream message;
  151. message << "StringRTrim('" << testData[i].testInput <<
  152. "', '" << testData[i].testWhitespaces << "') returned '" << stringRTrimResult <<
  153. "'. It was expected: '" << testData[i].testExpectedResult << "'";
  154. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(), testData[i].testExpectedResult == stringRTrimResult);
  155. }
  156. }
  157. void StringToolsImplUnitTests::Test_RegexMatch()
  158. {
  159. AppSecInc::Msi::MsiShim hInstall;
  160. MsiInstall msiInstall(hInstall);
  161. typedef struct
  162. {
  163. LPCSTR testInputString;
  164. LPCSTR testExpression;
  165. LPCSTR testExpectedResult;
  166. } TestData;
  167. TestData testData[] =
  168. {
  169. { "<TAG>one<TAG>two</TAG>one</TAG>", "<TAG\\b[^>]*>(.*?)</TAG>", "1" },
  170. { "<TAG>one<TAG>two</TAG>one", "<TAG\\b[^>]*>(.*?)</TAG>", "0" },
  171. { "125", "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$", "1" },
  172. { "1005", "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$", "0" },
  173. { "2008-07-31", "(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])", "1" },
  174. { "2008-15-20", "(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])", "0" },
  175. { "myname@host.com", "\\b[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}\\b", "1" },
  176. { "invalid email@host.com", "\\b[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}\\b", "0" }
  177. };
  178. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  179. {
  180. msiInstall.SetProperty("REGEX_MATCH_INPUT_STRING", testData[i].testInputString);
  181. msiInstall.SetProperty("REGEX_MATCH_EXPRESSION", testData[i].testExpression);
  182. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"Regex_Match"));
  183. std::string regexMatchResult = msiInstall.GetProperty("REGEX_MATCH_RESULT");
  184. std::stringstream message;
  185. message << "RegexMatch('" << testData[i].testInputString <<
  186. "', '" << testData[i].testExpression << "') returned '" << regexMatchResult <<
  187. "'. It was expected: '" << testData[i].testExpectedResult << "'";
  188. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(), testData[i].testExpectedResult == regexMatchResult);
  189. }
  190. }
  191. void StringToolsImplUnitTests::Test_RegexReplace()
  192. {
  193. AppSecInc::Msi::MsiShim hInstall;
  194. MsiInstall msiInstall(hInstall);
  195. typedef struct
  196. {
  197. LPCSTR testInputString;
  198. LPCSTR testExpression;
  199. LPCSTR testFormat;
  200. LPCSTR testExpectedResult;
  201. } TestData;
  202. TestData testData[] =
  203. {
  204. { "I have a dog. Also a machine.", "\\b(cat|dog)\\b", "pet", "I have a pet. Also a machine." },
  205. { "I have a spider. Also a machine.", "\\b(cat|dog)\\b", "pet", "I have a spider. Also a machine." },
  206. { "user@domain", "([^@]*)@(.*)", "$2\\\\$1", "domain\\user" }
  207. };
  208. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  209. {
  210. msiInstall.SetProperty("REGEX_REPLACE_INPUT_STRING", testData[i].testInputString);
  211. msiInstall.SetProperty("REGEX_REPLACE_EXPRESSION", testData[i].testExpression);
  212. msiInstall.SetProperty("REGEX_REPLACE_FORMAT", testData[i].testFormat);
  213. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"Regex_Replace"));
  214. std::string regexReplaceResult = msiInstall.GetProperty("REGEX_REPLACE_RESULT");
  215. std::stringstream message;
  216. message << "RegexReplace('" << testData[i].testInputString <<
  217. "', '" << testData[i].testExpression <<
  218. "', '" << testData[i].testFormat <<
  219. "') returned: '" << regexReplaceResult <<
  220. "'. It was expected: " << testData[i].testExpectedResult << "'";
  221. CPPUNIT_ASSERT_MESSAGE(message.str().c_str(), testData[i].testExpectedResult == regexReplaceResult);
  222. }
  223. }
  224. void StringToolsImplUnitTests::Test_StringToLower()
  225. {
  226. AppSecInc::Msi::MsiShim hInstall;
  227. MsiInstall msiInstall(hInstall);
  228. typedef struct
  229. {
  230. LPCTSTR input;
  231. LPCTSTR expected;
  232. } TestData;
  233. TestData testData[] =
  234. {
  235. { L"ABCdeF123", L"abcdef123" },
  236. //{ L"??????×??", L"??????÷??" }, // not supported by AppSecInc::StringUtils::lowercase
  237. { L"", L"" }
  238. };
  239. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  240. {
  241. msiInstall.SetProperty(L"STRING_TOLOWER_INPUT", testData[i].input);
  242. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_ToLower"));
  243. std::wstring result = msiInstall.GetProperty(L"STRING_TOLOWER_RESULT");
  244. std::wstring expected( testData[i].expected );
  245. // std::wstring expected( testData[i].input );
  246. // AppSecInc::StringUtils::lowercase( expected );
  247. CPPUNIT_ASSERT(expected == result);
  248. }
  249. }
  250. void StringToolsImplUnitTests::Test_StringToUpper()
  251. {
  252. AppSecInc::Msi::MsiShim hInstall;
  253. MsiInstall msiInstall(hInstall);
  254. typedef struct
  255. {
  256. LPCTSTR input;
  257. LPCTSTR expected;
  258. } TestData;
  259. TestData testData[] =
  260. {
  261. { L"ABCdeF123", L"ABCDEF123" },
  262. // { L"??????÷??", L"??????×??" }, // not supported by AppSecInc::StringUtils::uppercase
  263. { L"", L"" }
  264. };
  265. for( unsigned int i = 0; i < ARRAYSIZE(testData); i++ )
  266. {
  267. msiInstall.SetProperty(L"STRING_TOUPPER_INPUT", testData[i].input);
  268. CPPUNIT_ASSERT(ERROR_SUCCESS == hInstall.ExecuteCA(L"StringTools.dll", L"String_ToUpper"));
  269. std::wstring result = msiInstall.GetProperty(L"STRING_TOUPPER_RESULT");
  270. std::wstring expected( testData[i].expected );
  271. // std::wstring expected( testData[i].input );
  272. // AppSecInc::StringUtils::uppercase( expected );
  273. CPPUNIT_ASSERT(expected == result);
  274. }
  275. }