// $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 #include #include #include #include #include #include #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; // Container of test lines. std::vector 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; // Container of test lines. std::vector 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; // Container of test lines. std::vector TestInput; // Container of expected output. std::vector 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; } /// Unit tests for PostfixMilterConf. // int main(int argc, char* argv[]) { try { // Catch anything that breaks loose. // Test IsIntegrated. if (!TestIsIntegrated()) { ErrorExit("TestIsIntegrated() failure.\n"); } // Test IsMilterLine. if (!TestIsMilterLine()) { ErrorExit("TestIsMilterLine() failure.\n"); } // Test AddItegration. if (!TestAddIntegration()) { ErrorExit("TestAddIntegration() 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. }