Line data Source code
1 : // Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snaplogger
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // this test checks components
20 : //
21 : #include <snaplogger/component.h>
22 :
23 :
24 : // self
25 : //
26 : #include "catch_main.h"
27 :
28 :
29 : // snaplogger
30 : //
31 : #include <snaplogger/buffer_appender.h>
32 : #include <snaplogger/exception.h>
33 : #include <snaplogger/format.h>
34 : #include <snaplogger/logger.h>
35 : #include <snaplogger/message.h>
36 : #include <snaplogger/options.h>
37 :
38 :
39 :
40 :
41 :
42 :
43 4 : CATCH_TEST_CASE("component", "[component]")
44 : {
45 4 : CATCH_START_SECTION("component: Write component to stream")
46 : {
47 : {
48 1 : std::stringstream ss;
49 1 : ss << snaplogger::section(snaplogger::g_cppthread_component);
50 1 : CATCH_REQUIRE(ss.str() == "(section:cppthread)");
51 1 : }
52 :
53 : {
54 1 : std::stringstream ss;
55 1 : ss << snaplogger::section(snaplogger::g_debug_component);
56 1 : CATCH_REQUIRE(ss.str() == "(section:debug)");
57 1 : }
58 :
59 : {
60 1 : std::stringstream ss;
61 1 : ss << snaplogger::section(snaplogger::g_normal_component);
62 1 : CATCH_REQUIRE(ss.str() == "(section:normal)");
63 1 : }
64 :
65 : {
66 1 : std::stringstream ss;
67 1 : ss << snaplogger::section(snaplogger::g_secure_component);
68 1 : CATCH_REQUIRE(ss.str() == "(section:secure)");
69 1 : }
70 :
71 : {
72 1 : std::stringstream ss;
73 1 : ss << snaplogger::section(snaplogger::g_self_component);
74 1 : CATCH_REQUIRE(ss.str() == "(section:self)");
75 1 : }
76 :
77 : {
78 1 : std::stringstream ss;
79 1 : ss << snaplogger::section(snaplogger::g_banner_component);
80 1 : CATCH_REQUIRE(ss.str() == "(section:banner)");
81 1 : }
82 :
83 : {
84 1 : std::stringstream ss;
85 1 : ss << snaplogger::section(snaplogger::g_not_implemented_component);
86 1 : CATCH_REQUIRE(ss.str() == "(section:not_implemented)");
87 1 : }
88 : }
89 4 : CATCH_END_SECTION()
90 :
91 4 : CATCH_START_SECTION("component: Make sure creating component generates unique entries")
92 : {
93 : // this worked from the start since the private logger instance
94 : // uses a map to store the components
95 : //
96 : struct name_ptr
97 : {
98 : typedef std::vector<name_ptr> vector_t;
99 :
100 : std::string f_name = std::string();
101 : snaplogger::component::pointer_t
102 : f_component = snaplogger::component::pointer_t();
103 : };
104 1 : name_ptr::vector_t names =
105 : {
106 : { "component1", },
107 : { "component2", },
108 : { "component3", },
109 : { "component4", },
110 : { "component5", },
111 : { "component6", },
112 : { "component7", },
113 : { "component8", },
114 : { "component9", },
115 : { "component10", },
116 14 : };
117 :
118 : // create the components
119 : //
120 11 : for(auto & p : names)
121 : {
122 10 : p.f_component = snaplogger::get_component(p.f_name);
123 : }
124 :
125 : // verify the component pointers
126 : //
127 11 : for(auto & p : names)
128 : {
129 10 : CATCH_REQUIRE(p.f_component == snaplogger::get_component(p.f_name));
130 : }
131 1 : }
132 4 : CATCH_END_SECTION()
133 :
134 4 : CATCH_START_SECTION("component: Make sure creating component generates unique entries")
135 : {
136 : // this worked from the start since the private logger instance
137 : // uses a map to store the components
138 : //
139 : struct name_ptr
140 : {
141 : typedef std::vector<name_ptr> vector_t;
142 :
143 : std::string f_name = std::string();
144 : snaplogger::component::pointer_t
145 : f_component = snaplogger::component::pointer_t();
146 : };
147 1 : name_ptr::vector_t names =
148 : {
149 : { "component1", },
150 : { "component2", },
151 : { "component3", },
152 : { "component4", },
153 : { "component5", },
154 : { "Component6", },
155 : { "component7", },
156 : { "component8", },
157 : { "component9", },
158 : { "component10", },
159 14 : };
160 :
161 : // create the components
162 : //
163 11 : for(auto & p : names)
164 : {
165 10 : p.f_component = snaplogger::get_component(p.f_name);
166 : }
167 :
168 : // verify the component pointers
169 : //
170 11 : for(auto & p : names)
171 : {
172 10 : CATCH_REQUIRE(p.f_component == snaplogger::get_component(p.f_name));
173 : }
174 1 : }
175 4 : CATCH_END_SECTION()
176 :
177 4 : CATCH_START_SECTION("component: Send a component via the macros and << operator")
178 : {
179 1 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
180 :
181 1 : snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
182 1 : buffer->add_component(snaplogger::g_debug_component);
183 1 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>(
184 1 : "${message} (${severity:format=number}) -- ${components}"));
185 1 : buffer->set_format(f);
186 1 : l->add_appender(buffer);
187 :
188 1 : char const * cargv[] =
189 : {
190 : "/usr/bin/daemon",
191 : "--log-severity",
192 : "noisy",
193 : nullptr
194 : };
195 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
196 1 : char ** argv = const_cast<char **>(cargv);
197 :
198 1 : advgetopt::options_environment environment_options;
199 1 : environment_options.f_project_name = "test-logger";
200 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
201 1 : environment_options.f_version = "3.4";
202 1 : advgetopt::getopt opts(environment_options);
203 1 : opts.parse_program_name(argv);
204 1 : snaplogger::add_logger_options(opts);
205 1 : opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
206 :
207 3 : CATCH_REQUIRE(snaplogger::process_logger_options(opts, "/etc/my-app/logger"));
208 :
209 1 : buffer->set_config(opts);
210 :
211 4 : SNAP_LOG_NOISY
212 : << "We got a component!"
213 2 : << snaplogger::section(snaplogger::g_debug_component)
214 : << SNAP_LOG_SEND;
215 :
216 1 : CATCH_REQUIRE_FALSE(buffer->empty());
217 :
218 1 : std::string const severity1(std::to_string(static_cast<int>(snaplogger::severity_t::SEVERITY_INFORMATION)));
219 1 : std::string const severity2(std::to_string(static_cast<int>(snaplogger::severity_t::SEVERITY_NOISY)));
220 4 : std::string const expected("-------------------------------------------------- (" + severity1 + ") -- [normal,self,banner]\n"
221 4 : "test-logger v3.4 started. (" + severity1 + ") -- [normal,self,banner]\n"
222 4 : "We got a component! (" + severity2 + ") -- [debug]\n");
223 1 : CATCH_REQUIRE(buffer->str() == expected);
224 :
225 1 : buffer->clear();
226 :
227 1 : l->reset();
228 1 : }
229 4 : CATCH_END_SECTION()
230 10 : }
231 :
232 :
233 2 : CATCH_TEST_CASE("component_errors", "[component][error]")
234 : {
235 2 : CATCH_START_SECTION("component: component name cannot start with a digit")
236 : {
237 11 : for(int d(0); d < 10; ++d)
238 : {
239 10 : std::string bad_name(std::to_string(d) + "name");
240 20 : CATCH_REQUIRE_THROWS_MATCHES(
241 : snaplogger::get_component(bad_name)
242 : , snaplogger::invalid_parameter
243 : , Catch::Matchers::ExceptionMessage(
244 : "logger_error: a component name cannot start with a digits ("
245 : + bad_name
246 : + ")."));
247 10 : }
248 : }
249 2 : CATCH_END_SECTION()
250 :
251 2 : CATCH_START_SECTION("component: component name cannot include certain characters")
252 : {
253 11 : for(int count(0); count < 10; ++count)
254 : {
255 10 : std::string bad_name;
256 10 : int const max(rand() & 31 + 3);
257 10 : bool bad_added(false);
258 10 : char bad_char('\0');
259 155 : for(int idx(0); idx < max || !bad_added; ++idx)
260 : {
261 145 : char c(rand() % 95 + ' ');
262 145 : while(idx == 0 && c >= '0' && c <= '9')
263 : {
264 0 : c = rand() % 95 + ' ';
265 : }
266 145 : bad_name += c;
267 145 : if((c < '0' || c > '9')
268 123 : && (c < 'A' || c > 'Z')
269 88 : && (c < 'a' || c > 'z')
270 55 : && c != '-'
271 53 : && c != '_'
272 50 : && !bad_added)
273 : {
274 10 : bad_added = true;
275 10 : bad_char = c;
276 : }
277 : }
278 40 : CATCH_REQUIRE_THROWS_MATCHES(
279 : snaplogger::get_component(bad_name)
280 : , snaplogger::invalid_parameter
281 : , Catch::Matchers::ExceptionMessage(
282 : std::string("logger_error: a component name cannot include a '")
283 : + bad_char
284 : + "' character ("
285 : + bad_name
286 : + ")."));
287 10 : }
288 : }
289 2 : CATCH_END_SECTION()
290 2 : }
291 :
292 :
293 :
294 : // vim: ts=4 sw=4 et
|