|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- // $Id$
- //
- // \file TestPostfixMilterConf.cpp
- //
- // Copyright (C) 2012, ARM Research Labs, LLC. See
- // www.armresearch.com for the copyright terms.
- //
- // This is the unit test for the class PostfixMilterConf.
- //
-
- #include <iostream>
- #include <sstream>
- #include <stdexcept>
- #include <vector>
-
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
-
- #include "PostfixMilterConf.hpp"
-
- /// Output error.
- #define Error(msg) \
- { \
- std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
- std::cerr << msg; \
- }
-
- /// Exit with error.
- #define ErrorExit(msg) \
- { \
- Error(msg) \
- std::exit(-1); \
- }
-
- bool
- TestIsIntegrated() {
-
- // Object under test.
- PostfixMilterConf TestObj("unix:/var/snf-milter/socket");
-
- // Container of test lines.
- std::vector<std::string> TestInput;
-
- // Test lines containing integration.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters= otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters =otherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters=test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters\t= otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters =\totherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters\t=\ttest otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters \t= otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters =\t otherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters\t = \ttest otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket,secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket,secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket,secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec \tunix:/var/snf-milter/socket\tsecondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket ");
- TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec ");
- TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec\t ");
-
- TestInput.push_back(" smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket ");
- TestInput.push_back("\tsmtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec ");
- TestInput.push_back(" \tsmtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec\t ");
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- if (!TestObj.IsIntegrated()) {
-
- std::string Temp;
-
- Temp = "***Error--IsIntegrated() returned false with line--\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- // Test lines containing no integration.
- TestInput.clear();
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socketb");
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/ socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec uunix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec,udnix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/sockets secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix/var/snf-milter/socket,secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec, unix/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec , unix:var/snf-milter/socket .secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/vr/snf-milter/socket, secondSpec");
-
- TestInput.push_back("#smtpd_milters = otherMilterSpec, unix/var/snf-milter/socket");
- TestInput.push_back("manpage_directory = /usr/share/man");
-
- TestInput.push_back("");
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- if (TestObj.IsIntegrated()) {
-
- std::string Temp;
-
- Temp = "***Error--IsIntegrated() returned true with line--\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- return true;
-
- }
-
- bool
- TestIsMilterLine() {
-
- // Object under test.
- PostfixMilterConf TestObj("unix:/var/snf-milter/socket");
-
- // Container of test lines.
- std::vector<std::string> TestInput;
-
- // Test lines that are milter lines.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket,secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec unix:/var/snf-milter/socket,secondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec,unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket secondSpec");
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter/socket,secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec \tunix:/var/snf-milter/socket\tsecondSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec, unix:/var/snf-milter/socket");
- TestInput.push_back("smtpd_milters = otherMilterSpec , unix:/var/snf-milter/socket .secondSpec");
- TestInput.push_back("smtpd_milters = test otherMilterSpec ,unix:/var/snf-milter/socket, secondSpec");
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- if (!TestObj.IsMilterLine()) {
-
- std::string Temp;
-
- Temp = "***Error--IsMilterLine() returned false with line--\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- // Test lines that are not milter lines.
- TestInput.clear();
-
- TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
- TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
-
- TestInput.push_back("");
- TestInput.push_back("manpage_directory = /usr/share/man");
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- if (TestObj.IsMilterLine()) {
-
- std::string Temp;
-
- Temp = "***Error--IsMilterLine() returned true with line--\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- return true;
-
- }
-
- bool
- TestAddIntegration() {
-
- // Object under test.
- PostfixMilterConf TestObj("unix:/var/snf-milter/socket");
-
- // Container of test lines.
- std::vector<std::string> TestInput;
-
- // Container of expected output.
- std::vector<std::string> ExpectedOutput;
-
- // Test lines that are milter lines with SNFMilter already integrated.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket\t ");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
-
- // Test lines that are milter lines with SNFMilter not integrated.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sockett unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sock ");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sock unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter\t\t");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter\t\t");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter unix:/var/snf-milter/socket");
-
- std::string ActualOutput;
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- TestObj.AddIntegration();
-
- ActualOutput = TestObj.ConfLine();
-
- if (ActualOutput != ExpectedOutput[i]) {
-
- std::string Temp;
-
- Temp = "***Error--AddItegration() returned\n\t'";
- Temp += ActualOutput;
- Temp += "'\nwith input\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- // Test lines that are not milter lines.
- TestInput.clear();
-
- TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
- TestInput.push_back("smtpd_milters unix:/var/snf-milter/socketb");
- TestInput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
- TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
- TestInput.push_back("manpage_directory = /usr/share/man");
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- try {
-
- TestObj.ConfLine(TestInput[i]);
-
- TestObj.AddIntegration();
-
- std::string Temp;
-
- Temp = "***Error--AddItegration() did not throw expected exception with input\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- }
-
- return true;
-
- }
-
- bool
- TestRemoveIntegration() {
-
- // Object under test.
- PostfixMilterConf TestObj("unix:/var/snf-milter/socket");
-
- // Container of test lines.
- std::vector<std::string> TestInput;
-
- // Container of expected output.
- std::vector<std::string> ExpectedOutput;
-
- // Test lines that are milter lines with SNFMilter already integrated.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket ");
- ExpectedOutput.push_back("");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/socket\t ");
- ExpectedOutput.push_back("");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec\txxx qqq,unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec xxx qqq");
-
- // Test lines that are milter lines with SNFMilter not integrated.
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sockett");
-
- TestInput.push_back("smtpd_milters = unix:/var/snf-milter/sock ");
- ExpectedOutput.push_back("smtpd_milters = unix:/var/snf-milter/sock");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter\t\t");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec unix:/var/snf-milter");
-
- TestInput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter\t\t");
- ExpectedOutput.push_back("smtpd_milters = otherMilterSpec\tunix:/var/snf-milter");
-
- // Test lines that are not milter lines.
- TestInput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
- ExpectedOutput.push_back("smtpd_milterss = unix:/var/snf-milter/socketb");
-
- TestInput.push_back("smtpd_milters unix:/var/snf-milter/socket");
- ExpectedOutput.push_back("smtpd_milters unix:/var/snf-milter/socket");
-
- TestInput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
- ExpectedOutput.push_back("smtpd_milters\tunix:/var/snf-milter/socketb");
-
- TestInput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
- ExpectedOutput.push_back("#smtpd_milterss = unix:/var/snf-milter/socketb");
-
- TestInput.push_back("manpage_directory = /usr/share/man");
- ExpectedOutput.push_back("manpage_directory = /usr/share/man");
-
- std::string ActualOutput;
-
- for (unsigned int i = 0; i < TestInput.size(); i++) {
-
- TestObj.ConfLine(TestInput[i]);
-
- TestObj.RemoveIntegration();
-
- ActualOutput = TestObj.ConfLine();
-
- if (ActualOutput != ExpectedOutput[i]) {
-
- std::string Temp;
-
- Temp = "***Error--RemoveItegration() returned\n\t'";
- Temp += ActualOutput;
- Temp += "'\nwith input\n\t'";
- Temp += TestInput[i];
- Temp += "'\n";
-
- Error(Temp);
- return false;
-
- }
-
- }
-
- return true;
-
- }
-
- /// Unit tests for PostfixMilterConf.
- //
- int main(int argc, char* argv[]) {
-
- try { // Catch anything that breaks loose.
-
- if (!TestIsIntegrated()) {
- ErrorExit("TestIsIntegrated() failure.\n");
- }
-
- if (!TestIsMilterLine()) {
- ErrorExit("TestIsMilterLine() failure.\n");
- }
-
- if (!TestAddIntegration()) {
- ErrorExit("TestAddIntegration() failure.\n");
- }
-
- if (!TestRemoveIntegration()) {
- ErrorExit("TestRemoveIntegration() failure.\n");
- }
-
- } // That's all folks.
-
- catch(std::exception& e) { // Report any normal exceptions.
-
- std::cerr << "PostfixMilterConf exception: " << e.what() << std::endl;
- return -1;
-
- }
- catch(...) { // Report any unexpected exceptions.
-
- std::cerr << "Panic! Unknown Exception!" << std::endl;
- return -1;
-
- }
-
- return 0; // Normally we return zero.
- }
|