Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestPostfixMilterConf.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // $Id$
  2. //
  3. // \file TestPostfixMilterConf.cpp
  4. //
  5. // Copyright (C) 2012, ARM Research Labs, LLC. See
  6. // www.armresearch.com for the copyright terms.
  7. //
  8. // This is the unit test for the class PostfixMilterConf.
  9. //
  10. #include <iostream>
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <vector>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <cerrno>
  17. #include "PostfixMilterConf.hpp"
  18. /// Output error.
  19. #define Error(msg) \
  20. { \
  21. std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
  22. std::cerr << msg; \
  23. }
  24. /// Exit with error.
  25. #define ErrorExit(msg) \
  26. { \
  27. Error(msg) \
  28. std::exit(-1); \
  29. }
  30. bool
  31. TestIsIntegrated() {
  32. // Object under test.
  33. PostfixMilterConf TestObj;
  34. // Container of test lines.
  35. std::vector<std::string> TestInput;
  36. // Test lines containing integration.
  37. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  38. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
  39. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  40. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  41. TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  42. TestInput.push_back("smtpd_milters= otherMilterSpec unix:/var/snf-milter/socket");
  43. TestInput.push_back("smtpd_milters =otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  44. TestInput.push_back("smtpd_milters=test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  45. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  46. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  47. TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  48. TestInput.push_back("smtpd_milters\t= otherMilterSpec unix:/var/snf-milter/socket");
  49. TestInput.push_back("smtpd_milters =\totherMilterSpec unix:/var/snf-milter/socket secondSpec");
  50. TestInput.push_back("smtpd_milters\t=\ttest otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  51. TestInput.push_back("smtpd_milters \t= otherMilterSpec unix:/var/snf-milter/socket");
  52. TestInput.push_back("smtpd_milters =\t otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  53. TestInput.push_back("smtpd_milters\t = \ttest otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  54. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
  55. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket secondSpec");
  56. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket,secondSpec");
  57. TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket,secondSpec");
  58. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
  59. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket secondSpec");
  60. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket,secondSpec");
  61. TestInput.push_back("smtpd_milters = test otherMilterSpec \tunix:/var/snf-milter/socket\tsecondSpec");
  62. TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket");
  63. TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec");
  64. TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec");
  65. TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket ");
  66. TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec ");
  67. TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec\t ");
  68. TestInput.push_back(" smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket ");
  69. TestInput.push_back("\tsmtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec ");
  70. TestInput.push_back(" \tsmtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec\t ");
  71. for (unsigned int i = 0; i < TestInput.size(); i++) {
  72. TestObj.ConfLine(TestInput[i]);
  73. if (!TestObj.IsIntegrated()) {
  74. std::string Temp;
  75. Temp = "***Error--IsIntegrated() returned false with line--\n\t'";
  76. Temp += TestInput[i];
  77. Temp += "'\n";
  78. Error(Temp);
  79. return false;
  80. }
  81. }
  82. // Test lines containing no integration.
  83. TestInput.clear();
  84. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socketb");
  85. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
  86. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/ socket");
  87. TestInput.push_back("smtpd_milters = otherMilterSpec uunix:/var/snf-milter/socket secondSpec");
  88. TestInput.push_back("smtpd_milters = test otherMilterSpec unix/var/snf-milter/socket secondSpec");
  89. TestInput.push_back("smtpd_milters = otherMilterSpec,udnix:/var/snf-milter/socket");
  90. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/sockets secondSpec");
  91. TestInput.push_back("smtpd_milters = test otherMilterSpec unix/var/snf-milter/socket,secondSpec");
  92. TestInput.push_back("smtpd_milters = otherMilterSpec, unix/var/snf-milter/socket");
  93. TestInput.push_back("smtpd_milters = otherMilterSpec , unix:var/snf-milter/socket .secondSpec");
  94. TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/vr/snf-milter/socket, secondSpec");
  95. TestInput.push_back("#smtpd_milters = otherMilterSpec, unix/var/snf-milter/socket");
  96. TestInput.push_back("manpage_directory = /usr/share/man");
  97. TestInput.push_back("");
  98. for (unsigned int i = 0; i < TestInput.size(); i++) {
  99. TestObj.ConfLine(TestInput[i]);
  100. if (TestObj.IsIntegrated()) {
  101. std::string Temp;
  102. Temp = "***Error--IsIntegrated() returned true with line--\n\t'";
  103. Temp += TestInput[i];
  104. Temp += "'\n";
  105. Error(Temp);
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. bool
  112. TestIsMilterLine() {
  113. // Object under test.
  114. PostfixMilterConf TestObj;
  115. // Container of test lines.
  116. std::vector<std::string> TestInput;
  117. // Test lines that are milter lines.
  118. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  119. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
  120. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  121. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  122. TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
  123. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
  124. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket secondSpec");
  125. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket,secondSpec");
  126. TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket,secondSpec");
  127. TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
  128. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket secondSpec");
  129. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket,secondSpec");
  130. TestInput.push_back("smtpd_milters = test otherMilterSpec \tunix:/var/snf-milter/socket\tsecondSpec");
  131. TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket");
  132. TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec");
  133. TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec");
  134. for (unsigned int i = 0; i < TestInput.size(); i++) {
  135. TestObj.ConfLine(TestInput[i]);
  136. if (!TestObj.IsMilterLine()) {
  137. std::string Temp;
  138. Temp = "***Error--IsMilterLine() returned false with line--\n\t'";
  139. Temp += TestInput[i];
  140. Temp += "'\n";
  141. Error(Temp);
  142. return false;
  143. }
  144. }
  145. // Test lines that are not milter lines.
  146. TestInput.clear();
  147. TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
  148. TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
  149. TestInput.push_back("");
  150. TestInput.push_back("manpage_directory = /usr/share/man");
  151. for (unsigned int i = 0; i < TestInput.size(); i++) {
  152. TestObj.ConfLine(TestInput[i]);
  153. if (TestObj.IsMilterLine()) {
  154. std::string Temp;
  155. Temp = "***Error--IsMilterLine() returned true with line--\n\t'";
  156. Temp += TestInput[i];
  157. Temp += "'\n";
  158. Error(Temp);
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. bool
  165. TestAddIntegration() {
  166. // Object under test.
  167. PostfixMilterConf TestObj;
  168. // Container of test lines.
  169. std::vector<std::string> TestInput;
  170. // Container of expected output.
  171. std::vector<std::string> ExpectedOutput;
  172. // Test lines that are milter lines with SNFMilter already integrated.
  173. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  174. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  175. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
  176. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  177. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket\t ");
  178. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  179. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  180. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  181. TestInput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
  182. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
  183. // Test lines that are milter lines with SNFMilter not integrated.
  184. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
  185. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sockett unix:/var/snf-milter/socket");
  186. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sock ");
  187. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sock unix:/var/snf-milter/socket");
  188. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter\t\t");
  189. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter unix:/var/snf-milter/socket");
  190. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter\t\t");
  191. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter unix:/var/snf-milter/socket");
  192. std::string ActualOutput;
  193. for (unsigned int i = 0; i < TestInput.size(); i++) {
  194. TestObj.ConfLine(TestInput[i]);
  195. TestObj.AddIntegration();
  196. ActualOutput = TestObj.ConfLine();
  197. if (ActualOutput != ExpectedOutput[i]) {
  198. std::string Temp;
  199. Temp = "***Error--AddItegration() returned\n\t'";
  200. Temp += ActualOutput;
  201. Temp += "'\nwith input\n\t'";
  202. Temp += TestInput[i];
  203. Temp += "'\n";
  204. Error(Temp);
  205. return false;
  206. }
  207. }
  208. // Test lines that are not milter lines.
  209. TestInput.clear();
  210. TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
  211. TestInput.push_back("smtpd_milters unix:/var/snf-milter/socketb");
  212. TestInput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
  213. TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
  214. TestInput.push_back("manpage_directory = /usr/share/man");
  215. for (unsigned int i = 0; i < TestInput.size(); i++) {
  216. try {
  217. TestObj.ConfLine(TestInput[i]);
  218. TestObj.AddIntegration();
  219. std::string Temp;
  220. Temp = "***Error--AddItegration() did not throw expected exception with input\n\t'";
  221. Temp += TestInput[i];
  222. Temp += "'\n";
  223. Error(Temp);
  224. return false;
  225. } catch (std::exception &e) {
  226. }
  227. }
  228. return true;
  229. }
  230. /// Unit tests for PostfixMilterConf.
  231. //
  232. int main(int argc, char* argv[]) {
  233. try { // Catch anything that breaks loose.
  234. // Test IsIntegrated.
  235. if (!TestIsIntegrated()) {
  236. ErrorExit("TestIsIntegrated() failure.\n");
  237. }
  238. // Test IsMilterLine.
  239. if (!TestIsMilterLine()) {
  240. ErrorExit("TestIsMilterLine() failure.\n");
  241. }
  242. // Test AddItegration.
  243. if (!TestAddIntegration()) {
  244. ErrorExit("TestAddIntegration() failure.\n");
  245. }
  246. } // That's all folks.
  247. catch(std::exception& e) { // Report any normal exceptions.
  248. std::cerr << "PostfixMilterConf exception: " << e.what() << std::endl;
  249. return -1;
  250. }
  251. catch(...) { // Report any unexpected exceptions.
  252. std::cerr << "Panic! Unknown Exception!" << std::endl;
  253. return -1;
  254. }
  255. return 0; // Normally we return zero.
  256. }