382 Commits

Author SHA1 Message Date
Julian Brost
af96e17295 tests: fix FormatDateTime with 32-bit time_t
With a 32-bit time_t, two checks in the FormatDateTime test case didn't work
properly so far:

1. Every time_t value can be represented by struct tm, hence the test makes no
   sense on such platforms and is now disabled there similar to how it's
   already done with other checks in the same function.
2. std::nextafter(2147483647, +double_limit::infinity())) results in something
   like 2147483647.000000238 which simply results in the limit when cast back
   to an integer type, so it didn't actually test the overflow. This is fixed
   by an additional std::ceil()/std::floor().
2025-02-05 13:09:47 +01:00
Maciej Dems
56116d2218 Fix missing values in PerfData normalization 2025-02-04 13:53:45 +01:00
Maciej Dems
3e616c15ff Correct tests checks for icinga_perfdata 2025-02-04 13:53:45 +01:00
Alexander A. Klimov
d956920bd7 Move Timeout instances from heap to stack 2025-01-13 10:42:36 +01:00
Alexander A. Klimov
fe1420523a Timeout#~Timeout(), #Cancel(): support boost::asio::io_context running on multiple threads 2025-01-13 10:42:36 +01:00
Alexander A. Klimov
a47508b7b3 Timeout#Timeout(): drop unnecessary template parameters 2025-01-13 10:42:36 +01:00
Alexander A. Klimov
c23bd4c186 Test Timeout 2025-01-13 10:42:36 +01:00
Yonas Habteab
221041487c
Merge pull request #10122 from Icinga/broken-timeperiod-2.14
Fix broken `TimePeriod/ScheduledDowntime`s
2024-09-17 15:56:49 +02:00
Yonas Habteab
5f9868d97a Add advanced timeperiod range,include/exclude test cases 2024-09-17 12:33:27 +02:00
Yonas Habteab
b48dcc9f77 tests: Add some basic tests cases for LegacyTimePeriod::IsInTimeRange() 2024-09-17 12:33:27 +02:00
Julian Brost
3247e83957 Timeperiods: fix off by one when calculating n-th last weekday of the month
A day specification like "monday -1" refers to the last Monday of the month.
However, there was an off by one if the first day of the next month is the same
day of the week, i.e. a Monday in this example.

LegacyTimePeriod::FindNthWeekday() picks a day to start the search for the day
in question. When given a negative n to search for the n-th last day, it
wrongly used the first day of the following month as the start and counted it
as if it was within the current month. This resulted in a 1/7 chance that the
result was one week too late.

This is fixed by using the last day of the current month instead.
2024-09-17 12:32:10 +02:00
Julian Brost
a24a43e4d0 Utility::FormatDateTime(): provide an overload for tm*
This allows the function to be used both with a double timestamp or a pointer
to a tm struct. With this, a similar implementation inside the tests can simply
use our regular function.
2024-09-03 15:26:51 +02:00
Julian Brost
542b02621c Utility::FormatDateTime(): handle invalid format strings on Windows
On Windows, the strftime() function family invokes an invalid parameter handler
when the format string is invalid (see the "Remarks" section in their
documentation). std::put_time() shows the same behavior as it uses
_wcsftime_l() internally. The default invalid parameter handler may terminate
the process, which can be a problem given that the format string can be
specified by the user from the Icinga DSL.

Thus, temporarily set a thread-local no-op handler to disable the default one
allowing the program to continue. This then simply results in the function
returning an error which then results in an exception as we ask the stream to
throw one.

See also:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strftime-wcsftime-strftime-l-wcsftime-l?view=msvc-170
https://learn.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation?view=msvc-170
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler?view=msvc-170
2024-09-03 15:26:51 +02:00
Julian Brost
6887ac6970 Utility::FormatDateTime(): handle errors from strftime()
So far, the return value of strftime() was simply ignored and the output buffer
passed to the icinga::String constructor. However, there are error conditions
where strftime() returns 0 to signal an error, like if the buffer was too small
for the output. In that case, there's no guarantee on the buffer contents and
reading it can result in undefined behavior. Unfortunately, returning 0 can
also indicate success and strftime() doesn't set errno, so there's no reliable
way to distinguish both situations. Thus, the implementation now returns the
empty string in both cases.

I attempted to use std::put_time() at first as that allows for better error
handling, however, there were problems with the implementation on Windows (see
inline comment), so I put that plan on hold at left strftime() there for the
time being.
2024-09-03 15:26:51 +02:00
Julian Brost
1bc0648b3c Utility::FormatDateTime(): use boost::numeric_cast<>()
The previous implementation actually had undefined behavior when called with a
double that can't be represented as time_t. With boost::numeric_cast, there's a
convenient cast available that avoids this and throws an exceptions on
overflow.

It's undefined behavior ([0], where the implicit conversion rule comes into
play because the C-style cast uses static_cast [1] which in turn uses the
imlicit conversion as per rule 5 of [2]):

> A prvalue of floating-point type can be converted to a prvalue of any integer
> type. The fractional part is truncated, that is, the fractional part is
> discarded.
>
> * If the truncated value cannot fit into the destination type, the behavior
>   is undefined (even when the destination type is unsigned, modulo arithmetic
>   does not apply).

Note that on Linux amd64, the undefined behavior typically manifests itself in
the result being the minimal value of time_t which then results in localtime_r
failing with EOVERFLOW.

[0]: https://en.cppreference.com/w/cpp/language/implicit_conversion#Floating.E2.80.93integral_conversions
[1]: https://en.cppreference.com/w/cpp/language/explicit_cast
[2]: https://en.cppreference.com/w/cpp/language/static_cast
2024-09-03 15:26:51 +02:00
Julian Brost
46f1bd5846 Add tests for Utility::FormatDateTime() 2024-09-03 15:26:51 +02:00
Alexander Aleksandrovič Klimov
61d190f892
Merge pull request #9947 from Icinga/2141morebackport
Truncate too big notification command lines, fix GelfWriter deadlock and return 503 in /v1/console/* during reload
2023-12-20 12:44:07 +01:00
Alexander A. Klimov
0eeac3b385 PluginNotificationTask::ScriptFunc(): on Linux truncate output and comment
not to run into an exec(3) error E2BIG due to a too long argument.
This sends a notification with truncated output instead of not sending.
2023-12-20 10:46:51 +01:00
Alexander Aleksandrovič Klimov
bbb45894dd
Merge pull request #9944 from Icinga/targeted-api-filter-214
FilterUtility::GetFilterTargets(): don't run filter for specific object(s) for all objects
2023-12-19 17:40:59 +01:00
Alexander A. Klimov
03aa5adb7a Tests: config_apply/gettargetservices_*: use BOOST_CHECK_EQUAL_COLLECTIONS()
to show the value diff in case of mismatch.

Co-authored-by: Yonas Habteab <yonas.habteab@icinga.com>
2023-12-19 15:19:20 +01:00
Alexander A. Klimov
b99db24100 Test ApplyRule::GetTarget*s() 2023-12-19 15:19:20 +01:00
Alexander Aleksandrovič Klimov
5059d0f8b0
Merge pull request #9933 from Icinga/renew-the-ca-9890-214
ApiListener#Start(): auto-renew CA on its owner
2023-12-19 15:15:00 +01:00
Alexander A. Klimov
17eac30868 Test IsCertUptodate() and IsCaUptodate() 2023-12-18 17:04:59 +01:00
Alexander A. Klimov
d7500ca1bd Notification#BeginExecuteNotification(): on recovery clear last_notified_state_per_user 2023-12-13 16:14:57 +01:00
Alexander A. Klimov
bbadf1f27b Notification#BeginExecuteNotification(): discard likely duplicate problem notifications 2023-12-13 16:14:57 +01:00
Julian Brost
0e25644151
Merge pull request #8969 from Icinga/bugfix/perfdata-dont-get-parsed-correctly-8912
PluginUtility: Fix PerfData parsing for values separated with multiple spaces
2023-05-22 17:16:31 +02:00
Julian Brost
af9d67b262
Merge pull request #9726 from Icinga/43624b
Remove -and notify- expired downtimes immediately, not every 60s II
2023-05-02 11:25:03 +02:00
Yonas Habteab
e7c5554d87 Test: Add multiline PerfData test case 2023-04-05 15:44:13 +02:00
Alexander A. Klimov
21b68455ce Use Timer::Create() instead of new Timer()
git ls-files -z |xargs -0 perl -pi -e 's/\bnew Timer\b/Timer::Create/g'

ex. in Timer::Create() itself.
2023-04-04 10:35:20 +02:00
Alexander A. Klimov
396a71c6a0 Repair unit tests 2023-02-21 17:49:26 +01:00
Julian Brost
90848f602b Checkable: Add test for state notifications after a suppression ends 2022-03-03 14:25:23 +01:00
Julian Brost
7b2667400b Add tests for LegacyTimePeriod::ScriptFunc when used by TimePeriod::IsInside 2021-11-17 13:09:10 +01:00
Noah Hilverling
10bde2075a Dictionary: Make sure underlaying map is ordered 2021-11-12 13:34:57 +01:00
Alexander Aleksandrovič Klimov
02db80d6f3
Merge pull request #8815 from Icinga/feature/make-base-value-format-test-easier-to-understand
Make base_value/format test easier to understand
2021-08-03 11:37:09 +02:00
Alexander A. Klimov
c666f81361 De-couple package and stage name validation 2021-08-02 15:40:14 +02:00
Julian Brost
d07d48b169 Add tests for DST handling in TimePeriods and ScheduledDowntimes 2021-07-26 16:09:20 +02:00
Alexander A. Klimov
ea5411a6e0 PluginUtility::FormatPerfdata(): normalize UoMs if desired 2021-07-05 19:05:32 +02:00
Alexander Aleksandrovič Klimov
6048d0e800
Merge pull request #8373 from Icinga/feature/improve-crashlog
Improve crashlog
2021-06-29 17:52:25 +02:00
Alexander Aleksandrovič Klimov
9f24107a08
Merge pull request #8748 from Icinga/bugfix/icingadb-cv-function
JsonEncode(): encode non-[]/{} objects as strings, not null
2021-06-23 18:01:21 +02:00
Alexander Aleksandrovič Klimov
2cd9c1d902
Merge pull request #8835 from Icinga/bugfix/api-filename-truncation
Fix/restrict truncation of filenames for API-created objects
2021-06-23 12:06:31 +02:00
Julian Brost
02f761798c
Merge pull request #8825 from Icinga/bugfix/validate-config-package-name
ConfigPackageUtility::ValidateName: replace broken regex
2021-06-22 15:45:38 +02:00
Julian Brost
97d6876803 Utility: add a function to truncate strings while avoiding collisions 2021-06-17 16:21:01 +02:00
Julian Brost
005fac0a23
Merge pull request #8742 from Icinga/bugfix/double-to-string
Convert::ToString(double): handle integral values too large for long long correctly
2021-06-15 12:28:15 +02:00
Julian Brost
c40b18ef61 ConfigPackageUtility::ValidateName: replace broken regex
The old validation regex matched if the name consists only of invalid
character, not that it does not contain them, i.e. something like "foo/bar" was
considered valid.

This commit replaces the regex with a check that all characters in the name are
allowed characters.
2021-06-15 12:16:54 +02:00
Julian Brost
06999faf25 Allow using E in perfdata both as exponent and unit prefix 2021-06-08 12:37:13 +02:00
Julian Brost
62cbe72cdb
Merge pull request #8492 from Icinga/bugfix/perfdata-scientific-notation
Fix perfdata parser not recognize scientific notation
2021-06-07 15:29:52 +02:00
Julian Brost
7012774f57 Make base_value/format test easier to understand
The old test looked really strange where it reads 3 into a Value and then
checks that it's not 3. However, what's going on there is that operator>> for
Value actually always reads a String, so instead check for what v should be,
not what it should not be.
2021-05-31 17:28:35 +02:00
Alexander A. Klimov
fa2666fecf JsonEncode(): encode non-[]/{} objects as strings, not null
... to represent function custom vars as such in Icinga DB.
2021-04-30 15:49:52 +02:00
Julian Brost
2d6d87d10f Convert::ToString(double): handle integral values too large for long long correctly
Even if a double represents an integer value, it might not be safe to cast it
to long long as it may overflow the type. Instead just use print the double
value with 0 decimals using std::setprecision.

Before:

    <1> => 18446744073709551616.to_string()
    "-9223372036854775808"

After:

    <1> => 18446744073709551616.to_string()
    "18446744073709551616"
2021-04-27 17:31:50 +02:00
Julian Brost
af05a50740 Avoid dependency on Visual C++ ATL in tests on Windows
Better not have dependencies on Visual C++ if you can do without.
2021-04-09 15:28:40 +02:00