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.

TestPostfixMilterConf.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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("unix:/var/snf-milter/socket");
  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("unix:/var/snf-milter/socket");
  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("unix:/var/snf-milter/socket");
  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. bool
  231. TestRemoveIntegration() {
  232. // Object under test.
  233. PostfixMilterConf TestObj("unix:/var/snf-milter/socket");
  234. // Container of test lines.
  235. std::vector<std::string> TestInput;
  236. // Container of expected output.
  237. std::vector<std::string> ExpectedOutput;
  238. // Test lines that are milter lines with SNFMilter already integrated.
  239. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
  240. ExpectedOutput.push_back("");
  241. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
  242. ExpectedOutput.push_back("");
  243. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket\t ");
  244. ExpectedOutput.push_back("");
  245. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
  246. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec");
  247. TestInput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
  248. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec xxx qqq");
  249. // Test lines that are milter lines with SNFMilter not integrated.
  250. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
  251. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
  252. TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sock ");
  253. ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sock");
  254. TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter\t\t");
  255. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter");
  256. TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter\t\t");
  257. ExpectedOutput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter");
  258. // Test lines that are not milter lines.
  259. TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
  260. ExpectedOutput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
  261. TestInput.push_back("smtpd_milters unix:/var/snf-milter/socket");
  262. ExpectedOutput.push_back("smtpd_milters unix:/var/snf-milter/socket");
  263. TestInput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
  264. ExpectedOutput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
  265. TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
  266. ExpectedOutput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
  267. TestInput.push_back("manpage_directory = /usr/share/man");
  268. ExpectedOutput.push_back("manpage_directory = /usr/share/man");
  269. std::string ActualOutput;
  270. for (unsigned int i = 0; i < TestInput.size(); i++) {
  271. TestObj.ConfLine(TestInput[i]);
  272. TestObj.RemoveIntegration();
  273. ActualOutput = TestObj.ConfLine();
  274. if (ActualOutput != ExpectedOutput[i]) {
  275. std::string Temp;
  276. Temp = "***Error--RemoveItegration() returned\n\t'";
  277. Temp += ActualOutput;
  278. Temp += "'\nwith input\n\t'";
  279. Temp += TestInput[i];
  280. Temp += "'\n";
  281. Error(Temp);
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. /// Unit tests for PostfixMilterConf.
  288. //
  289. int main(int argc, char* argv[]) {
  290. try { // Catch anything that breaks loose.
  291. if (!TestIsIntegrated()) {
  292. ErrorExit("TestIsIntegrated() failure.\n");
  293. }
  294. if (!TestIsMilterLine()) {
  295. ErrorExit("TestIsMilterLine() failure.\n");
  296. }
  297. if (!TestAddIntegration()) {
  298. ErrorExit("TestAddIntegration() failure.\n");
  299. }
  300. if (!TestRemoveIntegration()) {
  301. ErrorExit("TestRemoveIntegration() failure.\n");
  302. }
  303. } // That's all folks.
  304. catch(std::exception& e) { // Report any normal exceptions.
  305. std::cerr << "PostfixMilterConf exception: " << e.what() << std::endl;
  306. return -1;
  307. }
  308. catch(...) { // Report any unexpected exceptions.
  309. std::cerr << "Panic! Unknown Exception!" << std::endl;
  310. return -1;
  311. }
  312. return 0; // Normally we return zero.
  313. }