LCOV - code coverage report
Current view: top level - tests - catch_component.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 114 114
Test Date: 2025-08-10 10:49:55 Functions: 100.0 % 2 2
Legend: Lines: hit not hit

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

Generated by: LCOV version 2.0-1

Snap C++ | List of projects | List of versions