mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-30 16:47:14 +09:00
* refactor(tests): reorganize unit tests into domain category folders - Consolidate per-advisory GHSA regression tests into Security/ subfolders by vuln class - Group all 186 test files into domain folders: DataCollection/, Ui/, Core/, Database/, Installer/, Scripts/, Security/, Plugin/ - Strip GitHub issue/GHSA numbers from filenames; keep IDs in test descriptions - Fix all __DIR__ relative paths to match new folder nesting depth - Update CI workflow path triggers and phpunit XML configs for new file locations - Update .github/copilot-instructions.md with unit test organization guidelines - Add tests/vendor/ to .gitignore * add gaurds * fix up guard for unit tests * fix redeclaration of cacti_log in tests * fix updates to unit tests * fix test patjhs * fix: update test __DIR__ paths for reorganized test structure * fix: conditionally define read_user_setting to prevent redeclaration in tests * fix: conditionally define functions to prevent redeclaration in tests - Wrap clean_up_lines, raise_message, cacti_debug_backtrace, and get_debug_prefix with function_exists() checks to prevent conflicts when test stubs are loaded before lib/functions.php in the same PHP process * fix: add unit test bootstrap to prevent function redeclaration errors - Create tests/bootstrap-unit.php to load core libraries before tests - Ensures lib/functions.php loads before test stubs define stub functions - Test stubs use function_exists() checks, so they detect pre-loaded functions - Update phpunit.xml to use bootstrap-unit.php for Unit testsuite - Fixes 'Cannot redeclare read_user_setting()' errors when tests run in same process * fix: load test stubs before global.php in unit test bootstrap - Reorder bootstrap to load CactiStubs.php and UnitStubs.php first - Ensures stub functions like __() are available when global.php loads - global.php calls get_cacti_version_text() which needs __() for translations - Fixes 'Call to undefined function __()' error during bootstrap * fix: remove function conflicts from test stubs - Remove stub definitions for functions already in lib/functions.php: cacti_sizeof, cacti_log, cacti_strtolower, read_config_option, read_user_setting, cacti_count - Keep translation function stubs (__(), __esc()) needed by global.php - Update bootstrap to load lib/functions.php after stubs - This prevents function redeclaration errors while maintaining test isolation * fix: prevent constant and variable redeclaration in bootstrap - Remove explicit PHP_TESTING and CACTI_TEST_BOOTSTRAP defines from bootstrap (CactiStubs.php now handles this with guards) - Add guards to CactiStubs.php to prevent redefinition of PHP_TESTING - Only set CACTI_TEST_BOOTSTRAP env vars if not already set - Fixes PHP warnings about 'Constant X already defined' * fix: remove redundant lib/functions.php load from bootstrap include/global.php already loads lib/functions.php at lines 287-288. The bootstrap was trying to load it again after global.php, but global.php needs format_cacti_version() to be available on lines 293-294 to define CACTI_VERSION and CACTI_VERSION_FULL constants. Solution: Let global.php handle loading lib/functions.php itself, and don't load it separately in the bootstrap. * fix: remove more conflicting function stubs from test helpers - Remove stubs for functions also defined in lib/functions.php: raise_message, clean_up_lines, cacti_debug_backtrace, get_debug_prefix - Remove POLLER_ID constant stub (defined unconditionally by global.php) - Keep only translation functions and test-specific helpers - This prevents function/constant redeclaration errors * fix: minimize test stubs to only translation functions Remove ALL helper functions and constants from UnitStubs.php that are: - Defined in lib/functions.php (raise_message, clean_up_lines, etc.) - Defined in lib/html_utility.php (srv, set_request_var) - Defined in global_constants.php (POLLER_VERBOSITY_*, MESSAGE_LEVEL_*) - Defined in global.php (POLLER_ID) Keep ONLY translation functions (__(), __esc()) that are: - Called by global.php during initialization - Not defined elsewhere - Required for proper bootstrap sequence This eliminates all constant and function redeclaration errors. * fix: remove __esc stub - already defined in global_languages.php Keep only __() translation function stub, which is needed by global.php and not defined elsewhere in Cacti core. * fix: handle no-arg case in __() translation stub The __() function may be called with just a string and no format arguments. Previously, vsprintf() would fail with 'arguments array must contain 2 items'. Now, if no args are provided, return the text as-is.
290 lines
9.5 KiB
PHP
290 lines
9.5 KiB
PHP
<?php
|
|
/*
|
|
+-------------------------------------------------------------------------+
|
|
| Copyright (C) 2004-2026 The Cacti Group |
|
|
| |
|
|
| This program is free software; you can redistribute it and/or |
|
|
| modify it under the terms of the GNU General Public License |
|
|
| as published by the Free Software Foundation; either version 2 |
|
|
| of the License, or (at your option) any later version. |
|
|
+-------------------------------------------------------------------------+
|
|
| Cacti: The Complete RRDtool-based Graphing Solution |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
/*
|
|
* Tests for the empty CDEF guard in rrd.php CDEF emission.
|
|
*
|
|
* When aggregate graphs include GPRINT items whose consolidation
|
|
* function does not match any AREA/STACK/LINE item's DEF, the
|
|
* magic-variable expansion produces an empty RPN string. Without a
|
|
* guard, rrd.php emits "CDEF:cdefX=" which rrdtool rejects with
|
|
* "ERROR: can not parse an empty rpn expression".
|
|
*
|
|
* The fix adds: if ($cdef_string === '') continue;
|
|
*
|
|
* These tests verify the guard logic using a stub that mirrors the
|
|
* CDEF emission path without requiring rrdtool or database access.
|
|
*
|
|
* SYNC WARNING: emit_cdef_line() and expand_similar_nodups() below
|
|
* replicate logic from the CDEF emission block in
|
|
* rrdtool_function_graph_variables() and the SIMILAR_DATA_SOURCES_NODUPS
|
|
* expansion in rrdtool_function_graph() in lib/rrd.php. Production
|
|
* functions require rrdtool and DB state. If the production CDEF emission
|
|
* or magic-variable expansion logic changes, these helpers must be
|
|
* updated to match.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../../../../include/global_constants.php';
|
|
|
|
/**
|
|
* Mirrors the CDEF emission logic in rrd.php.
|
|
* Returns the CDEF line string, or null if the cdef_string is empty
|
|
* (matching the continue guard).
|
|
*/
|
|
function emit_cdef_line(string $cdef_string, int $item_index): ?string {
|
|
// guard: skip empty RPN expressions
|
|
if ($cdef_string === '') {
|
|
return null;
|
|
}
|
|
|
|
// bounds guard: chr(ord('a') + N) only produces a-z for 0..25.
|
|
// Production rrd.php uses generate_graph_def_name() (digit-to-letter
|
|
// lookup) which handles any non-negative int; this guard is specific
|
|
// to the test helper's chr()-based approach.
|
|
if ($item_index < 0 || $item_index > 25) {
|
|
return null;
|
|
}
|
|
|
|
$def_name = chr(ord('a') + $item_index);
|
|
|
|
return 'CDEF:cdef' . $def_name . '=' . $cdef_string;
|
|
}
|
|
|
|
/**
|
|
* Simulates the magic-variable expansion for SIMILAR_DATA_SOURCES_NODUPS.
|
|
* When no matching AREA/STACK/LINE items exist for a given CF, the
|
|
* expansion produces an empty string.
|
|
*/
|
|
function expand_similar_nodups(array $graph_items, string $data_source_name, int $cf_id): string {
|
|
$parts = [];
|
|
$rra_epoch = time() - 86400;
|
|
|
|
foreach ($graph_items as $item) {
|
|
/* only AREA/STACK/LINE items are eligible, matching the preg_match
|
|
filter at rrd.php:2120 */
|
|
if (!isset($item['type_name']) || !preg_match('/(AREA|STACK|LINE[123])/', $item['type_name'])) {
|
|
continue;
|
|
}
|
|
|
|
if ($item['data_source_name'] !== $data_source_name) {
|
|
continue;
|
|
}
|
|
|
|
if ($item['cf_id'] !== $cf_id) {
|
|
continue;
|
|
}
|
|
|
|
$def = $item['def_name'];
|
|
$parts[] = "TIME,{$rra_epoch},GT,{$def},{$def},UN,0,{$def},IF,IF";
|
|
}
|
|
|
|
if (count($parts) === 0) {
|
|
return '';
|
|
}
|
|
|
|
$result = implode(',', $parts);
|
|
|
|
if (count($parts) > 1) {
|
|
$result .= str_repeat(',+', count($parts) - 1);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
// --- Empty CDEF guard ---
|
|
|
|
test('empty cdef string returns null (skipped)', function () {
|
|
expect(emit_cdef_line('', 0))->toBeNull();
|
|
});
|
|
|
|
test('non-empty cdef string emits valid CDEF line', function () {
|
|
$result = emit_cdef_line('a,8,*', 0);
|
|
|
|
expect($result)->toBe('CDEF:cdefa=a,8,*');
|
|
});
|
|
|
|
test('guard uses strict empty string check so null would not match', function () {
|
|
// rrd_substitute_host_query_data() has return type `: string`, so null
|
|
// cannot reach the guard in production. This test documents that the
|
|
// strict === '' comparison does not catch null, confirming reliance on
|
|
// the upstream return type guarantee.
|
|
expect('' === null)->toBeFalse();
|
|
});
|
|
|
|
test('whitespace-only cdef string is NOT treated as empty', function () {
|
|
// the guard uses strict === '' comparison, so whitespace is not empty
|
|
$result = emit_cdef_line(' ', 0);
|
|
|
|
expect($result)->not->toBeNull();
|
|
});
|
|
|
|
// --- Magic-variable expansion produces empty string for unmatched CF ---
|
|
|
|
test('expansion returns empty when no AREA/LINE items match the CF', function () {
|
|
// scenario: GPRINT with CF=MAX (3), but only AREA items with CF=AVERAGE (1)
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'f'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 3);
|
|
|
|
expect($result)->toBe('');
|
|
});
|
|
|
|
test('expansion returns RPN when AREA items match the CF', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'f'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 1);
|
|
|
|
expect($result)->not->toBe('')
|
|
->and($result)->toContain('b,b,UN,0,b,IF,IF')
|
|
->and($result)->toContain('f,f,UN,0,f,IF,IF')
|
|
->and($result)->toContain(',+');
|
|
});
|
|
|
|
test('expansion skips GPRINT items even if CF matches', function () {
|
|
$items = [
|
|
['type_name' => 'GPRINT', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 1);
|
|
|
|
expect($result)->toBe('');
|
|
});
|
|
|
|
test('expansion skips COMMENT items', function () {
|
|
$items = [
|
|
['type_name' => 'COMMENT', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 1);
|
|
|
|
expect($result)->toBe('');
|
|
});
|
|
|
|
// --- Integration: empty expansion feeds into CDEF emission guard ---
|
|
|
|
test('unmatched CF expansion produces empty string that the guard skips', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_out', 'cf_id' => 1, 'def_name' => 'd'],
|
|
];
|
|
|
|
// GPRINT with CF=MAX tries to expand SIMILAR for traffic_in with cf=3
|
|
$cdef_string = expand_similar_nodups($items, 'traffic_in', 3);
|
|
$cdef_line = emit_cdef_line($cdef_string, 7);
|
|
|
|
expect($cdef_string)->toBe('')
|
|
->and($cdef_line)->toBeNull();
|
|
});
|
|
|
|
test('matched CF expansion produces valid CDEF that emits correctly', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'f'],
|
|
];
|
|
|
|
$cdef_string = expand_similar_nodups($items, 'traffic_in', 1);
|
|
$cdef_line = emit_cdef_line($cdef_string, 8);
|
|
|
|
expect($cdef_string)->not->toBe('')
|
|
->and($cdef_line)->toStartWith('CDEF:cdefi=')
|
|
->and($cdef_line)->toContain(',+');
|
|
});
|
|
|
|
// --- Single data source: no trailing plus operator ---
|
|
|
|
test('single matching item produces RPN without trailing plus', function () {
|
|
$items = [
|
|
['type_name' => 'LINE1', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 1);
|
|
|
|
expect($result)->not->toBe('')
|
|
->and($result)->not->toContain(',+');
|
|
});
|
|
|
|
// --- Edge cases ---
|
|
|
|
test('emit_cdef_line returns null for negative item_index', function () {
|
|
expect(emit_cdef_line('a,8,*', -1))->toBeNull();
|
|
});
|
|
|
|
test('emit_cdef_line returns null for item_index above 25', function () {
|
|
expect(emit_cdef_line('a,8,*', 26))->toBeNull();
|
|
});
|
|
|
|
test('emit_cdef_line handles item_index at boundary 25', function () {
|
|
$result = emit_cdef_line('a,8,*', 25);
|
|
|
|
expect($result)->toBe('CDEF:cdefz=a,8,*');
|
|
});
|
|
|
|
test('expand_similar_nodups handles empty graph_items array', function () {
|
|
expect(expand_similar_nodups([], 'traffic_in', 1))->toBe('');
|
|
});
|
|
|
|
test('expand_similar_nodups skips items with missing type_name', function () {
|
|
$items = [
|
|
['data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
];
|
|
|
|
expect(expand_similar_nodups($items, 'traffic_in', 1))->toBe('');
|
|
});
|
|
|
|
// --- Additional edge cases for emit_cdef_line ---
|
|
|
|
test('emit_cdef_line handles item_index at boundary 0', function () {
|
|
$result = emit_cdef_line('a,8,*', 0);
|
|
|
|
expect($result)->toBe('CDEF:cdefa=a,8,*');
|
|
});
|
|
|
|
// --- Additional edge cases for expand_similar_nodups ---
|
|
|
|
test('expand_similar_nodups handles single matching item', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'cpu', 'cf_id' => 1, 'def_name' => 'a'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'cpu', 1);
|
|
|
|
expect($result)->not->toBe('')
|
|
->and($result)->not->toContain(',+');
|
|
});
|
|
|
|
test('expand_similar_nodups skips items with missing data_source_name key', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'cf_id' => 1, 'def_name' => 'b', 'data_source_name' => 'other'],
|
|
];
|
|
|
|
expect(expand_similar_nodups($items, 'traffic_in', 1))->toBe('');
|
|
});
|
|
|
|
test('expand_similar_nodups returns empty when data_source_name matches but cf_id differs', function () {
|
|
$items = [
|
|
['type_name' => 'AREA', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'b'],
|
|
['type_name' => 'STACK', 'data_source_name' => 'traffic_in', 'cf_id' => 1, 'def_name' => 'c'],
|
|
];
|
|
|
|
$result = expand_similar_nodups($items, 'traffic_in', 4);
|
|
|
|
expect($result)->toBe('');
|
|
});
|