2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 $Date: 2009/02/24 08:13:03 $
41 typedef std::pair<std::string, bool> TEST_ENTRY;
44 bool actualError; // Parser error status
45 bool stdException; // Parser throws an std execption
46 bool otherException; // Parser throws another exception
47 std::string message; // Parser error message
50 class RULES_PARSER_TESTER : public std::unary_function<std::pair<std::string, bool>, void> {
52 RULES_PARSER_TESTER(RULES_PARSER & p) : parser(p),
58 ~RULES_PARSER_TESTER()
65 void operator()(const std::pair<std::string, bool> & entry)
67 testLog[entry.first].wantedError = entry.second;
68 testLog[entry.first].actualError = false;
69 testLog[entry.first].stdException = false;
70 testLog[entry.first].otherException = false;
71 testLog[entry.first].message = "";
72 testResult[entry.first] = true;
75 parser.SetFile(entry.first);
77 catch (std::exception & ex)
79 testLog[entry.first].stdException = true;
80 testResult[entry.first] &= false;
84 testLog[entry.first].otherException = true;
85 testResult[entry.first] &= false;
87 testLog[entry.first].actualError = parser.IsError();
88 testLog[entry.first].message = parser.ErrorMsg();
89 testResult[entry.first] &= (parser.IsError() == entry.second);
90 result &= testResult[entry.first];
95 std::cout << "RULES_PARSER_TESTER results:\n";
96 std::cout << "-----------------------------------------------------------------\n";
97 std::map<std::string, bool>::const_iterator it;
98 for (it = testResult.begin(); it != testResult.end(); ++it)
100 std::cout << "File: '" << it->first << "'\t"
101 << "Correct: " << testLog[it->first].wantedError << "\t"
102 << "Actual:" << testLog[it->first].actualError << "\t"
103 << "STD exceptions: " << testLog[it->first].stdException << "\t"
104 << "Other exceptions: " << testLog[it->first].otherException << "\t"
105 << "Result: " << it->second << "\n";
106 if (!testLog[it->first].message.empty())
108 std::cout << "Messages: \n" << testLog[it->first].message << "\n";
111 std::cout << "-----------------------------------------------------------------\n";
112 std::cout << "Final result: " << (result ? "passed" : "failed") << std::endl;
115 bool Result() const { return result; };
117 RULES_PARSER & parser;
118 std::map<std::string, TEST_INFO> testLog;
119 std::map<std::string, bool> testResult;
123 int main(int argc, char ** argv)
126 std::map<std::string, bool> tests;
128 tests["./test_rules"] = false;
129 tests["./rules"] = false;
130 tests["./test_rules_bad_address"] = true;
131 tests["./test_rules_bad_port"] = true;
132 tests["./test_rules_bad_mask"] = true;
133 tests["./test_rules_bad_proto"] = true;
134 tests["./test_rules_bad_dir_prefix"] = true;
135 tests["./test_rules_bad_dir_range"] = true;
136 tests["./test_rules_bad_dir"] = true;
138 /*parser.SetFile("./rules");
139 std::cout << parser.ErrorMsg() << std::endl;*/
141 // TODO: find errors and write checks for regression
143 std::for_each(tests.begin(),
145 RULES_PARSER_TESTER(parser));