]> git.stg.codes - stg.git/blob - projects/traffcounter/rules_tester.cpp
Добавление исходников
[stg.git] / projects / traffcounter / rules_tester.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.1.1.1 $
23  $Date: 2009/02/24 08:13:03 $
24  $Author: faust $
25  */
26
27 #include <iostream>
28 #include <cstdlib>
29 #include <string>
30 #include <map>
31 #include <algorithm>
32 #include <functional>
33
34 #include "rules.h"
35 #include "logger.h"
36
37 using namespace STG;
38
39 STGLogger log;
40
41 typedef std::pair<std::string, bool> TEST_ENTRY;
42 struct TEST_INFO {
43     bool wantedError;
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
48 };
49
50 class RULES_PARSER_TESTER : public std::unary_function<std::pair<std::string, bool>, void> {
51 public:
52     RULES_PARSER_TESTER(RULES_PARSER & p) : parser(p),
53                                             testLog(),
54                                             testResult(),
55                                             result(true)
56         {
57         };
58     ~RULES_PARSER_TESTER()
59         {
60         PrintLog();
61         if (result)
62             exit(EXIT_SUCCESS);
63         exit(EXIT_FAILURE);
64         }
65     void operator()(const std::pair<std::string, bool> & entry)
66         {
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;
73         try
74             {
75             parser.SetFile(entry.first);
76             }
77         catch (std::exception & ex)
78             {
79             testLog[entry.first].stdException = true;
80             testResult[entry.first] &= false;
81             }
82         catch (...)
83             {
84             testLog[entry.first].otherException = true;
85             testResult[entry.first] &= false;
86             }
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];
91         };
92
93     void PrintLog()
94         {
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)
99             {
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())
107                 {
108                     std::cout << "Messages: \n" << testLog[it->first].message << "\n";
109                 }
110             }
111         std::cout << "-----------------------------------------------------------------\n";
112         std::cout << "Final result: " << (result ? "passed" : "failed") << std::endl;
113         }
114
115     bool Result() const { return result; };
116 private:
117     RULES_PARSER & parser;
118     std::map<std::string, TEST_INFO> testLog;
119     std::map<std::string, bool> testResult;
120     bool result;
121 };
122
123 int main(int argc, char ** argv)
124 {
125 RULES_PARSER parser;
126 std::map<std::string, bool> tests;
127
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;
137
138 /*parser.SetFile("./rules");
139 std::cout << parser.ErrorMsg() << std::endl;*/
140
141 // TODO: find errors and write checks for regression
142
143 std::for_each(tests.begin(),
144              tests.end(),
145              RULES_PARSER_TESTER(parser));
146
147 return EXIT_FAILURE;
148 }