You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mime_string.cxx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*-
  2. * Copyright 2021 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  17. #include "doctest/doctest.h"
  18. #include "mime_string.hxx"
  19. #include "unicode/uchar.h"
  20. TEST_SUITE("mime_string")
  21. {
  22. using namespace rspamd::mime;
  23. TEST_CASE("mime_string unfiltered ctors")
  24. {
  25. SUBCASE("empty")
  26. {
  27. mime_string st;
  28. CHECK(st.size() == 0);
  29. CHECK(st == "");
  30. }
  31. SUBCASE("unfiltered valid")
  32. {
  33. mime_string st{std::string_view("abcd")};
  34. CHECK(st == "abcd");
  35. }
  36. SUBCASE("unfiltered zero character")
  37. {
  38. mime_string st{"abc\0d", 5};
  39. CHECK(st.has_zeroes());
  40. CHECK(st == "abcd");
  41. }
  42. SUBCASE("unfiltered invalid character - middle")
  43. {
  44. mime_string st{std::string("abc\234d")};
  45. CHECK(st.has_invalid());
  46. CHECK(st == "abc\uFFFDd");
  47. }
  48. SUBCASE("unfiltered invalid character - end")
  49. {
  50. mime_string st{std::string("abc\234")};
  51. CHECK(st.has_invalid());
  52. CHECK(st == "abc\uFFFD");
  53. }
  54. SUBCASE("unfiltered invalid character - start")
  55. {
  56. mime_string st{std::string("\234abc")};
  57. CHECK(st.has_invalid());
  58. CHECK(st == "\uFFFDabc");
  59. }
  60. }
  61. TEST_CASE("mime_string filtered ctors")
  62. {
  63. auto print_filter = [](UChar32 inp) -> UChar32 {
  64. if (!u_isprint(inp)) {
  65. return 0;
  66. }
  67. return inp;
  68. };
  69. auto tolower_filter = [](UChar32 inp) -> UChar32 {
  70. return u_tolower(inp);
  71. };
  72. SUBCASE("empty")
  73. {
  74. mime_string st{std::string_view(""), tolower_filter};
  75. CHECK(st.size() == 0);
  76. CHECK(st == "");
  77. }
  78. SUBCASE("filtered valid")
  79. {
  80. mime_string st{std::string("AbCdУ"), tolower_filter};
  81. CHECK(st == "abcdу");
  82. }
  83. SUBCASE("filtered invalid + filtered")
  84. {
  85. mime_string st{std::string("abcd\234\1"), print_filter};
  86. CHECK(st == "abcd\uFFFD");
  87. }
  88. }
  89. TEST_CASE("mime_string assign")
  90. {
  91. SUBCASE("assign from valid")
  92. {
  93. mime_string st;
  94. CHECK(st.assign_if_valid(std::string("test")));
  95. CHECK(st == "test");
  96. }
  97. SUBCASE("assign from invalid")
  98. {
  99. mime_string st;
  100. CHECK(!st.assign_if_valid(std::string("test\234t")));
  101. CHECK(st == "");
  102. }
  103. }
  104. TEST_CASE("mime_string iterators")
  105. {
  106. SUBCASE("unfiltered iterator ascii")
  107. {
  108. auto in = std::string("abcd");
  109. mime_string st{in};
  110. CHECK(st == "abcd");
  111. int i = 0;
  112. for (auto &&c: st) {
  113. CHECK(c == in[i++]);
  114. }
  115. }
  116. SUBCASE("unfiltered iterator utf8")
  117. {
  118. auto in = std::string("тест");
  119. UChar32 ucs[4] = {1090, 1077, 1089, 1090};
  120. mime_string st{in};
  121. CHECK(st == "тест");
  122. int i = 0;
  123. for (auto &&c: st) {
  124. CHECK(c == ucs[i++]);
  125. }
  126. CHECK(i == sizeof(ucs) / sizeof(ucs[0]));
  127. }
  128. SUBCASE("unfiltered raw iterator ascii")
  129. {
  130. auto in = std::string("abcd");
  131. mime_string st{in};
  132. CHECK(st == "abcd");
  133. int i = 0;
  134. for (auto it = st.raw_begin(); it != st.raw_end(); ++it) {
  135. CHECK(*it == in[i++]);
  136. }
  137. }
  138. SUBCASE("unfiltered raw iterator utf8")
  139. {
  140. auto in = std::string("тест");
  141. mime_string st{in};
  142. CHECK(st == "тест");
  143. int i = 0;
  144. for (auto it = st.raw_begin(); it != st.raw_end(); ++it) {
  145. CHECK(*it == in[i++]);
  146. }
  147. CHECK(i == in.size());
  148. }
  149. }
  150. }