Просмотр исходного кода

Unit tested PostfixMilterConf::IsIntegrated, IsMilterLine, and

AddIntegration.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@50 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 12 лет назад
Родитель
Сommit
9f1dd8f368

+ 58
- 4
SNFMilterConfig/PostfixMilterConf.cpp Просмотреть файл

@@ -26,6 +26,8 @@ const std::string SnfMilterSocketSpec("unix:/var/snf-milter/socket");
/// Milter keyword in the postfix main.cf file.
const std::string SmtpdMilterKeyword("smtpd_milters");
const std::string Whitespace(", \t");
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// End of configuration. /////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -79,13 +81,13 @@ PostfixMilterConf::AddIntegration() {
ExistingMilters = Utility::Trim(ConfigurationLine.substr(NextIndex + 1)); // Should contain existing milters.
while (ExistingMilters != "") {
while (std::string::npos != NextIndex) {
NewConfLine += " ";
std::string NextMilter;
NextIndex = ExistingMilters.find_first_of(" ,");
NextIndex = ExistingMilters.find_first_of(Whitespace);
NextMilter = Utility::Trim(ExistingMilters.substr(0, NextIndex));
ExistingMilters = Utility::Trim(ExistingMilters.substr(NextIndex + 1));
@@ -234,13 +236,65 @@ PostfixMilterConf::ConfLine() {
bool
PostfixMilterConf::IsMilterLine() {
return (ConfigurationLine.find(SmtpdMilterKeyword) == 0);
bool StartsWithKeyword;
bool KeywordEndsCorrectly;
char NextChar;
StartsWithKeyword = (ConfigurationLine.find(SmtpdMilterKeyword) == 0);
NextChar = ConfigurationLine[SmtpdMilterKeyword.length()];
KeywordEndsCorrectly =
(' ' == NextChar) ||
('\t' == NextChar) ||
('=' == NextChar);
return (StartsWithKeyword && KeywordEndsCorrectly);
}
bool
PostfixMilterConf::ContainsSnfMilterSocketSpec() {
return (ConfigurationLine.find(SnfMilterSocketSpec) != std::string::npos);
std::string::size_type SpecIndex;
SpecIndex = ConfigurationLine.find("=");
if (std::string::npos == SpecIndex) {
return false;
}
SpecIndex++;
while (SpecIndex < ConfigurationLine.length()) {
SpecIndex = ConfigurationLine.find_first_not_of(Whitespace, SpecIndex); // Find next specification.
if (ConfigurationLine.substr(SpecIndex, SnfMilterSocketSpec.length()) == SnfMilterSocketSpec) {
SpecIndex += SnfMilterSocketSpec.length(); // Skip to after the specification.
if (SpecIndex >= ConfigurationLine.length()) {
return true; // No characters after the specification.
}
if ( (ConfigurationLine[SpecIndex] == ' ') ||
(ConfigurationLine[SpecIndex] == '\t') ||
(ConfigurationLine[SpecIndex] == ',') ) {
return true; // Found specification.
}
}
SpecIndex = ConfigurationLine.find_first_of(Whitespace, SpecIndex); // Find next specification.
}
return false;
}

+ 28
- 0
SNFMilterConfigTests/Makefile.am Просмотреть файл

@@ -0,0 +1,28 @@
## Process this file with automake to produce Makefile.in
##
## $Id: Makefile.am,v 1.2 2007/05/29 19:06:09 adeniz Exp $
##
## automake input for the MicroNeil SNFUtility/Common tests.
##
## Author: Alban Deniz
##
##
## Copyright (C) 2012 ARM Research Labs, LLC.
## See www.armresearch.com for the copyright terms.
##

CXXFLAGS = $(SNF_CXXFLAGS) -I@top_srcdir@/SNFUtility/Common -I@top_srcdir@/SNFUtility/SNFMilterConfig

TESTS = \
TestPostfixMilterConf

check_PROGRAMS = \
TestPostfixMilterConf

TestPostfixMilterConf_SOURCES = \
TestPostfixMilterConf.cpp \
@top_srcdir@/SNFUtility/SNFMilterConfig/PostfixMilterConf.cpp \
@top_srcdir@/SNFUtility/Common/Utility.cpp

clean-local:
rm -f *.gcno *.gcov *.gcda *~

+ 378
- 0
SNFMilterConfigTests/TestPostfixMilterConf.cpp Просмотреть файл

@@ -0,0 +1,378 @@
// $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;
// 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;
// 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;
// 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;
}
/// 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.
}

Загрузка…
Отмена
Сохранить