node_warnings_tests.cpp raw

   1  // Copyright (c) 2024-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  //
   5  
   6  #include <node/warnings.h>
   7  #include <util/translation.h>
   8  
   9  #include <test/util/setup_common.h>
  10  
  11  #include <boost/test/unit_test.hpp>
  12  
  13  BOOST_FIXTURE_TEST_SUITE(node_warnings_tests, BasicTestingSetup)
  14  
  15  BOOST_AUTO_TEST_CASE(warnings)
  16  {
  17      node::Warnings warnings;
  18      // On pre-release builds, a warning is generated automatically
  19      warnings.Unset(node::Warning::PRE_RELEASE_TEST_BUILD);
  20  
  21      // For these tests, we don't care what the exact warnings are, so
  22      // just refer to them as warning_1 and warning_2
  23      const auto warning_1{node::Warning::CLOCK_OUT_OF_SYNC};
  24      const auto warning_2{node::Warning::FATAL_INTERNAL_ERROR};
  25  
  26      // Ensure we start without any warnings
  27      BOOST_CHECK(warnings.GetMessages().size() == 0);
  28      // Add two warnings
  29      BOOST_CHECK(warnings.Set(warning_1, _("warning 1")));
  30      BOOST_CHECK(warnings.Set(warning_2, _("warning 2")));
  31      // Unset the second one
  32      BOOST_CHECK(warnings.Unset(warning_2));
  33      // Since it's already been unset, this should return false
  34      BOOST_CHECK(!warnings.Unset(warning_2));
  35      // We should now be able to set w2 again
  36      BOOST_CHECK(warnings.Set(warning_2, _("warning 2 - revision 1")));
  37      // Setting w2 again should return false since it's already set
  38      BOOST_CHECK(!warnings.Set(warning_2, _("warning 2 - revision 2")));
  39  
  40      // Verify messages are correct
  41      const auto messages{warnings.GetMessages()};
  42      BOOST_CHECK(messages.size() == 2);
  43      BOOST_CHECK(messages[0].original == "warning 1");
  44      BOOST_CHECK(messages[1].original == "warning 2 - revision 1");
  45  
  46      // Clearing all warnings should also clear all messages
  47      BOOST_CHECK(warnings.Unset(warning_1));
  48      BOOST_CHECK(warnings.Unset(warning_2));
  49      BOOST_CHECK(warnings.GetMessages().size() == 0);
  50  }
  51  
  52  BOOST_AUTO_TEST_SUITE_END()
  53