quote_ut.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "quote.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TCGIEscapeTest) {
  4. Y_UNIT_TEST(ReturnsEndOfTo) {
  5. char r[10];
  6. const char* returned = CGIEscape(r, "123");
  7. UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned);
  8. UNIT_ASSERT_VALUES_EQUAL('\0', *returned);
  9. }
  10. Y_UNIT_TEST(NotZeroTerminated) {
  11. char r[] = {'1', '2', '3', '4'};
  12. char buf[sizeof(r) * 3 + 2];
  13. TString ret(buf, CGIEscape(buf, r, sizeof(r)));
  14. UNIT_ASSERT_EQUAL(ret, "1234");
  15. }
  16. Y_UNIT_TEST(StringBuf) {
  17. char tmp[100];
  18. UNIT_ASSERT_VALUES_EQUAL(CgiEscape(tmp, "!@#$%^&*(){}[]\" "), TStringBuf("!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+"));
  19. }
  20. Y_UNIT_TEST(StrokaRet) {
  21. UNIT_ASSERT_VALUES_EQUAL(CGIEscapeRet("!@#$%^&*(){}[]\" "), TString("!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+"));
  22. }
  23. Y_UNIT_TEST(StrokaAppendRet) {
  24. TString param;
  25. AppendCgiEscaped("!@#$%^&*(){}[]\" ", param);
  26. UNIT_ASSERT_VALUES_EQUAL(param, TString("!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+"));
  27. TString param2 = "&param=";
  28. AppendCgiEscaped("!@#$%^&*(){}[]\" ", param2);
  29. UNIT_ASSERT_VALUES_EQUAL(param2,
  30. TString("&param=!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+"));
  31. param2.append("&param_param=");
  32. AppendCgiEscaped("!@#$%^&*(){}[]\" ", param2);
  33. UNIT_ASSERT_VALUES_EQUAL(param2,
  34. TString("&param=!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+&param_param=!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+"));
  35. }
  36. }
  37. Y_UNIT_TEST_SUITE(TCGIUnescapeTest) {
  38. Y_UNIT_TEST(StringBuf) {
  39. char tmp[100];
  40. UNIT_ASSERT_VALUES_EQUAL(CgiUnescape(tmp, "!@%23$%25^%26*%28%29"), TStringBuf("!@#$%^&*()"));
  41. }
  42. Y_UNIT_TEST(TestValidZeroTerm) {
  43. char r[10];
  44. CGIUnescape(r, "1234");
  45. UNIT_ASSERT_VALUES_EQUAL(r, "1234");
  46. CGIUnescape(r, "%3d");
  47. UNIT_ASSERT_VALUES_EQUAL(r, "=");
  48. CGIUnescape(r, "12%3D34");
  49. UNIT_ASSERT_VALUES_EQUAL(r, "12=34");
  50. }
  51. Y_UNIT_TEST(TestInvalidZeroTerm) {
  52. char r[10];
  53. CGIUnescape(r, "%");
  54. UNIT_ASSERT_VALUES_EQUAL(r, "%");
  55. CGIUnescape(r, "%3");
  56. UNIT_ASSERT_VALUES_EQUAL(r, "%3");
  57. CGIUnescape(r, "%3g");
  58. UNIT_ASSERT_VALUES_EQUAL(r, "%3g");
  59. CGIUnescape(r, "12%3g34");
  60. UNIT_ASSERT_VALUES_EQUAL(r, "12%3g34");
  61. CGIUnescape(r, "%3u123");
  62. UNIT_ASSERT_VALUES_EQUAL(r, "%3u123");
  63. }
  64. Y_UNIT_TEST(TestValidNotZeroTerm) {
  65. char r[10];
  66. CGIUnescape(r, "123456789", 4);
  67. UNIT_ASSERT_VALUES_EQUAL(r, "1234");
  68. CGIUnescape(r, "%3d1234", 3);
  69. UNIT_ASSERT_VALUES_EQUAL(r, "=");
  70. CGIUnescape(r, "12%3D345678", 7);
  71. UNIT_ASSERT_VALUES_EQUAL(r, "12=34");
  72. }
  73. Y_UNIT_TEST(TestInvalidNotZeroTerm) {
  74. char r[10];
  75. CGIUnescape(r, "%3d", 1);
  76. UNIT_ASSERT_VALUES_EQUAL(r, "%");
  77. CGIUnescape(r, "%3d", 2);
  78. UNIT_ASSERT_VALUES_EQUAL(r, "%3");
  79. CGIUnescape(r, "%3g1234", 3);
  80. UNIT_ASSERT_VALUES_EQUAL(r, "%3g");
  81. CGIUnescape(r, "12%3g345678", 7);
  82. UNIT_ASSERT_VALUES_EQUAL(r, "12%3g34");
  83. CGIUnescape(r, "%3u1234", 2);
  84. UNIT_ASSERT_VALUES_EQUAL(r, "%3");
  85. CGIUnescape(r, "%3u1234", 3);
  86. UNIT_ASSERT_VALUES_EQUAL(r, "%3u");
  87. CGIUnescape(r, "%3u1234", 4);
  88. UNIT_ASSERT_VALUES_EQUAL(r, "%3u1");
  89. }
  90. Y_UNIT_TEST(StrokaOutParameterInplace) {
  91. TString s;
  92. s = "hello%3dworld";
  93. CGIUnescape(s);
  94. UNIT_ASSERT_VALUES_EQUAL(s, "hello=world");
  95. s = "+%23+";
  96. CGIUnescape(s);
  97. UNIT_ASSERT_VALUES_EQUAL(s, " # ");
  98. s = "hello%3u";
  99. CGIUnescape(s);
  100. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3u");
  101. s = "0123456789012345";
  102. CGIUnescape(s);
  103. UNIT_ASSERT_VALUES_EQUAL(s, "0123456789012345");
  104. s = "";
  105. CGIUnescape(s);
  106. UNIT_ASSERT_VALUES_EQUAL(s, "");
  107. }
  108. Y_UNIT_TEST(StrokaOutParameterNotInplace) {
  109. TString s, sCopy;
  110. s = "hello%3dworld";
  111. sCopy = s;
  112. CGIUnescape(s);
  113. UNIT_ASSERT_VALUES_EQUAL(s, "hello=world");
  114. s = "+%23+";
  115. sCopy = s;
  116. CGIUnescape(s);
  117. UNIT_ASSERT_VALUES_EQUAL(s, " # ");
  118. s = "hello%3u";
  119. sCopy = s;
  120. CGIUnescape(s);
  121. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3u");
  122. s = "0123456789012345";
  123. sCopy = s;
  124. CGIUnescape(s);
  125. UNIT_ASSERT_VALUES_EQUAL(s, "0123456789012345");
  126. s = "";
  127. sCopy = s;
  128. CGIUnescape(s);
  129. UNIT_ASSERT_VALUES_EQUAL(s, "");
  130. }
  131. }
  132. Y_UNIT_TEST_SUITE(TUrlEscapeTest) {
  133. Y_UNIT_TEST(EscapeEscaped) {
  134. TString s;
  135. s = "hello%3dworld";
  136. UNIT_ASSERT_VALUES_EQUAL(UrlEscapeRet(s), "hello%3dworld");
  137. UrlEscape(s);
  138. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3dworld");
  139. }
  140. Y_UNIT_TEST(EscapeUnescape) {
  141. TString s;
  142. s = "hello%3dworld";
  143. UrlEscape(s);
  144. UrlUnescape(s);
  145. UNIT_ASSERT_VALUES_EQUAL(s, "hello=world");
  146. }
  147. Y_UNIT_TEST(EscapeUnescapeRet) {
  148. TString s;
  149. s = "hello%3dworld";
  150. UNIT_ASSERT_VALUES_EQUAL(UrlUnescapeRet(UrlEscapeRet(s)), "hello=world");
  151. }
  152. Y_UNIT_TEST(EscapeEscapedForce) {
  153. TString s;
  154. s = "hello%3dworld";
  155. UNIT_ASSERT_VALUES_EQUAL(UrlEscapeRet(s, true), "hello%253dworld");
  156. UrlEscape(s, true);
  157. UNIT_ASSERT_VALUES_EQUAL(s, "hello%253dworld");
  158. }
  159. Y_UNIT_TEST(EscapeUnescapeForce) {
  160. TString s;
  161. s = "hello%3dworld";
  162. UrlEscape(s, true);
  163. UrlUnescape(s);
  164. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3dworld");
  165. }
  166. Y_UNIT_TEST(EscapeUnescapeForceRet) {
  167. TString s;
  168. s = "hello%3dworld";
  169. UNIT_ASSERT_VALUES_EQUAL(UrlUnescapeRet(UrlEscapeRet(s, true)), "hello%3dworld");
  170. }
  171. }
  172. Y_UNIT_TEST_SUITE(TUrlUnescapeTest) {
  173. Y_UNIT_TEST(StrokaOutParameterInplace) {
  174. TString s;
  175. s = "hello%3dworld";
  176. UrlUnescape(s);
  177. UNIT_ASSERT_VALUES_EQUAL(s, "hello=world");
  178. s = "+%23+";
  179. UrlUnescape(s);
  180. UNIT_ASSERT_VALUES_EQUAL(s, "+#+");
  181. s = "hello%3u";
  182. UrlUnescape(s);
  183. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3u");
  184. s = "0123456789012345";
  185. UrlUnescape(s);
  186. UNIT_ASSERT_VALUES_EQUAL(s, "0123456789012345");
  187. s = "";
  188. UrlUnescape(s);
  189. UNIT_ASSERT_VALUES_EQUAL(s, "");
  190. }
  191. Y_UNIT_TEST(StrokaOutParameterNotInplace) {
  192. TString s, sCopy;
  193. s = "hello%3dworld";
  194. sCopy = s;
  195. UrlUnescape(s);
  196. UNIT_ASSERT_VALUES_EQUAL(s, "hello=world");
  197. s = "+%23+";
  198. sCopy = s;
  199. UrlUnescape(s);
  200. UNIT_ASSERT_VALUES_EQUAL(s, "+#+");
  201. s = "hello%3u";
  202. sCopy = s;
  203. UrlUnescape(s);
  204. UNIT_ASSERT_VALUES_EQUAL(s, "hello%3u");
  205. s = "0123456789012345";
  206. sCopy = s;
  207. UrlUnescape(s);
  208. UNIT_ASSERT_VALUES_EQUAL(s, "0123456789012345");
  209. s = "";
  210. sCopy = s;
  211. UrlUnescape(s);
  212. UNIT_ASSERT_VALUES_EQUAL(s, "");
  213. }
  214. }
  215. Y_UNIT_TEST_SUITE(TQuoteTest) {
  216. Y_UNIT_TEST(ReturnsEndOfTo) {
  217. char r[10];
  218. const char* returned = Quote(r, "123");
  219. UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned);
  220. UNIT_ASSERT_VALUES_EQUAL('\0', *returned);
  221. }
  222. Y_UNIT_TEST(SlashIsSafeByDefault) {
  223. char r[100];
  224. Quote(r, "/path;tail/path,tail/");
  225. UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", r);
  226. TString s("/path;tail/path,tail/");
  227. Quote(s);
  228. UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", s.c_str());
  229. }
  230. Y_UNIT_TEST(SafeColons) {
  231. char r[100];
  232. Quote(r, "/path;tail/path,tail/", ";,");
  233. UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", r);
  234. TString s("/path;tail/path,tail/");
  235. Quote(s, ";,");
  236. UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", s.c_str());
  237. }
  238. Y_UNIT_TEST(StringBuf) {
  239. char r[100];
  240. char* end = Quote(r, "abc\0/path", "");
  241. UNIT_ASSERT_VALUES_EQUAL("abc\0%2Fpath", TStringBuf(r, end));
  242. }
  243. }