Browse Source

Completed unit tests.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@51 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 12 years ago
parent
commit
d6b0d2ee33
2 changed files with 131 additions and 60 deletions
  1. 38
    57
      SNFMilterConfig/PostfixMilterConf.cpp
  2. 93
    3
      SNFMilterConfigTests/TestPostfixMilterConf.cpp

+ 38
- 57
SNFMilterConfig/PostfixMilterConf.cpp View File

} }
ConfigurationLine = Utility::Trim(ConfigurationLine);
} }
void void
} }
if (IsMilterLine()) { // Remove from an existing milter line.
std::string NewConfLine;
NewConfLine = SmtpdMilterKeyword + " =";
// Skip to "=" in configuration line.
std::string::size_type NextIndex;
NextIndex = ConfigurationLine.find("=");
if (std::string::npos == NextIndex) { // Check format.
std::ostringstream Temp;
std::string NewConfLine;
Temp << "Error processing postfix main.cf file smtpd_milters line: '"
<< ConfigurationLine << "'";
NewConfLine = SmtpdMilterKeyword + " =";
throw std::runtime_error(Temp.str());
// Skip to "=" in configuration line.
std::string::size_type NextIndex;
}
std::string ExistingMilters;
bool AddedMilter = false;
NextIndex = ConfigurationLine.find("=");
ExistingMilters = Utility::Trim(ConfigurationLine.substr(NextIndex + 1)); // Should contain existing milters.
while (ExistingMilters != "") {
NewConfLine += " ";
std::string NextMilter;
if (std::string::npos == NextIndex) { // Check format.
NextIndex = ExistingMilters.find_first_of(" ,");
std::ostringstream Temp;
if (std::string::npos == NextIndex) {
Temp << "Error processing postfix main.cf file smtpd_milters line: '"
<< ConfigurationLine << "'";
NextMilter = ExistingMilters;
ExistingMilters = "";
throw std::runtime_error(Temp.str());
} else {
}
NextMilter = Utility::Trim(ExistingMilters.substr(0, NextIndex));
ExistingMilters = Utility::Trim(ExistingMilters.substr(NextIndex + 1));
std::string ExistingMilters;
bool AddedMilter = false;
}
ExistingMilters = Utility::Trim(ConfigurationLine.substr(NextIndex + 1)); // Should contain existing milters.
if (NextMilter == "") {
while (std::string::npos != NextIndex) {
std::ostringstream Temp;
NewConfLine += " ";
Temp << "Error processing postfix main.cf file smtpd_milters line: '"
<< ConfigurationLine << "'";
std::string NextMilter;
throw std::runtime_error(Temp.str());
NextIndex = ExistingMilters.find_first_of(Whitespace);
NextMilter = Utility::Trim(ExistingMilters.substr(0, NextIndex));
ExistingMilters = Utility::Trim(ExistingMilters.substr(NextIndex + 1));
}
if (NextMilter == "") {
if (NextMilter != SnfMilterSocketSpec) { // Copy if not for SNFMilter.
std::ostringstream Temp;
NewConfLine += NextMilter;
AddedMilter = true;
Temp << "Error processing postfix main.cf file smtpd_milters line: '"
<< ConfigurationLine << "'";
}
throw std::runtime_error(Temp.str());
} }
if (AddedMilter) {
ConfigurationLine = NewConfLine;
} else {
if (NextMilter != SnfMilterSocketSpec) { // Copy if not for SNFMilter.
ConfigurationLine = "";
NewConfLine += NextMilter;
AddedMilter = true;
} }
} else { // Unexpected non-empty line.
}
std::ostringstream Temp;
if (AddedMilter) {
Temp << "Internal error: Attempted to modify a line in main.cf that does not begin with '"
<< SmtpdMilterKeyword << "'";
ConfigurationLine = NewConfLine;
throw std::runtime_error(Temp.str());
} else {
ConfigurationLine = "";
} }
ConfigurationLine = Utility::Trim(ConfigurationLine);
} }
std::string std::string

+ 93
- 3
SNFMilterConfigTests/TestPostfixMilterConf.cpp View File

} }
bool
TestRemoveIntegration() {
// 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("");
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. /// Unit tests for PostfixMilterConf.
// //
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try { // Catch anything that breaks loose. try { // Catch anything that breaks loose.
// Test IsIntegrated.
if (!TestIsIntegrated()) { if (!TestIsIntegrated()) {
ErrorExit("TestIsIntegrated() failure.\n"); ErrorExit("TestIsIntegrated() failure.\n");
} }
// Test IsMilterLine.
if (!TestIsMilterLine()) { if (!TestIsMilterLine()) {
ErrorExit("TestIsMilterLine() failure.\n"); ErrorExit("TestIsMilterLine() failure.\n");
} }
// Test AddItegration.
if (!TestAddIntegration()) { if (!TestAddIntegration()) {
ErrorExit("TestAddIntegration() failure.\n"); ErrorExit("TestAddIntegration() failure.\n");
} }
if (!TestRemoveIntegration()) {
ErrorExit("TestRemoveIntegration() failure.\n");
}
} // That's all folks. } // That's all folks.
catch(std::exception& e) { // Report any normal exceptions. catch(std::exception& e) { // Report any normal exceptions.

Loading…
Cancel
Save