Commit Graph

378 Commits

Author SHA1 Message Date
Julian Brost 39ae2e8ca4 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-08-23 12:48:50 +02:00
Julian Brost d5b3ffaa6d 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-08-23 12:48:50 +02:00
Julian Brost 0285028689 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-08-23 12:42:54 +02:00
Julian Brost 704acdc698 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-08-23 12:42:30 +02:00
Julian Brost 090dcfd70f Add tests for Utility::FormatDateTime() 2024-08-22 12:23:22 +02:00
Julian Brost 4c83d793a6
Merge pull request #9983 from Icinga/broken-timeperiod
Fix broken `TimePeriod/ScheduledDowntime`s
2024-08-20 10:05:59 +02:00
Julian Brost c45829b59f 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-08-07 12:06:05 +02:00
Yonas Habteab 4f94891b52 Add advanced timeperiod range,include/exclude test cases 2024-08-02 11:12:51 +02:00
Yonas Habteab ddf7143777 tests: Add some basic tests cases for `LegacyTimePeriod::IsInTimeRange()` 2024-08-02 11:12:51 +02:00
Maciej Dems 2bb5cc62e2 Fix missing values in PerfData normalization 2024-04-25 17:41:12 +02:00
Maciej Dems 91789b2888 Correct tests checks for icinga_perfdata 2024-04-25 17:41:12 +02:00
Alexander Aleksandrovič Klimov 48eb563ca0
Merge pull request #9736 from Icinga/stream-read-allow_partial
Stream#Read(): remove de facto unused param allow_partial
2024-02-13 13:04:15 +01:00
Alexander Aleksandrovič Klimov 949d983a76
Merge pull request #9895 from Icinga/targeted-api-filter
FilterUtility::GetFilterTargets(): don't run filter for specific object(s) for all objects
2023-12-19 15:18:41 +01:00
Alexander Aleksandrovič Klimov 8b2e28a869
Merge pull request #9891 from Icinga/renew-the-ca-9890
ApiListener#Start(): auto-renew CA on its owner
2023-12-19 14:57:47 +01:00
Alexander A. Klimov 175153ce6a 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-19 12:21:03 +01:00
Alexander A. Klimov 4424d57e7a 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-18 16:01:31 +01:00
Alexander A. Klimov dc338a406a Test IsCertUptodate() and IsCaUptodate() 2023-12-18 15:28:11 +01:00
Alexander A. Klimov 191bf93f2e Test ApplyRule::GetTarget*s() 2023-12-18 13:07:31 +01:00
Alexander A. Klimov 97cd05db7a Notification#BeginExecuteNotification(): on recovery clear last_notified_state_per_user 2023-12-13 13:21:22 +01:00
Alexander A. Klimov 44e9c6f40d Notification#BeginExecuteNotification(): discard likely duplicate problem notifications 2023-12-13 13:21:19 +01:00
Alexander A. Klimov 1308ad62af Stream#Read(): remove de facto unused param allow_partial
The only caller passes true, so no one forbids partial reads (even implicitly).
All usages in the implementation just assert it being true (allowed).
2023-07-13 16:55:48 +02: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
Alexander A. Klimov 9531de3835 JsonEncode(): serialize integers w/o trailing .0
... so Icinga DB can parse them as integers.
2021-03-23 17:52:37 +01:00
Julian Brost 49cfda833e Add tests for SHA1 function 2021-03-19 10:31:01 +01:00
Alexander Aleksandrovič Klimov ef8619f76b
Merge pull request #8601 from Icinga/feature/replace-std-boost-bind-with-lambdas-7006
Feature: Replace std/boost::bind() with lambdas
2021-03-18 17:56:13 +01:00
Yonas Habteab 43ba2da39c Replace std/boost::bind() function with lambda expression 2021-03-10 16:29:40 +01:00