|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Regression test for issue #269 -- branch-logic invariants. |
| 5 | + * |
| 6 | + * These assertions verify the structural properties that make whitespace-only |
| 7 | + * input fall through to the file-upload branch instead of the textbox branch, |
| 8 | + * and that a non-empty payload is assigned to $xml_data without further |
| 9 | + * modification. Pure source inspection: the functions themselves cannot be |
| 10 | + * called in isolation because they depend on the Cacti runtime. |
| 11 | + */ |
| 12 | + |
| 13 | +$root = dirname(__DIR__, 2); |
| 14 | +$targets = array( |
| 15 | + 'alert_import' => $root . '/syslog_alerts.php', |
| 16 | + 'removal_import' => $root . '/syslog_removal.php', |
| 17 | + 'report_import' => $root . '/syslog_reports.php', |
| 18 | +); |
| 19 | + |
| 20 | +foreach ($targets as $func => $target) { |
| 21 | + $content = file_get_contents($target); |
| 22 | + |
| 23 | + if ($content === false) { |
| 24 | + fwrite(STDERR, "Failed to load $target\n"); |
| 25 | + exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + /* |
| 29 | + * 1. The request variable must be captured into a local first. |
| 30 | + * Whitespace-only input falls through only because trim() is applied |
| 31 | + * to the local; if the assignment were missing the condition would |
| 32 | + * be wrong. |
| 33 | + */ |
| 34 | + if (!preg_match('/\$import_text\s*=\s*get_nfilter_request_var\s*\(\s*\'import_text\'\s*\)/', $content)) { |
| 35 | + fwrite(STDERR, "$func: \$import_text assignment via get_nfilter_request_var missing in $target\n"); |
| 36 | + exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + /* |
| 40 | + * 2. The branch condition must trim the local variable, not the raw |
| 41 | + * request call. This is what makes whitespace-only values fall |
| 42 | + * through to the file-upload branch. |
| 43 | + */ |
| 44 | + if (!preg_match('/trim\s*\(\s*\$import_text\s*\)\s*!=\s*\'\'/', $content)) { |
| 45 | + fwrite(STDERR, "$func: trim(\$import_text) != '' condition missing in $target\n"); |
| 46 | + exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * 3. Inside the textbox branch, $xml_data must be assigned the |
| 51 | + * untrimmed local. A non-empty payload is preserved as-is. |
| 52 | + */ |
| 53 | + if (!preg_match('/\$xml_data\s*=\s*\$import_text\s*;/', $content)) { |
| 54 | + fwrite(STDERR, "$func: \$xml_data = \$import_text assignment missing in $target\n"); |
| 55 | + exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * 4. The file-upload branch must still exist (elseif on $_FILES). |
| 60 | + * Ensures the fallback path was not accidentally removed. |
| 61 | + */ |
| 62 | + if (!preg_match('/elseif\s*\(\s*\(\s*\$_FILES\s*\[/', $content)) { |
| 63 | + fwrite(STDERR, "$func: \$_FILES elseif branch missing in $target\n"); |
| 64 | + exit(1); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +echo "issue269_import_text_branch_logic_test passed\n"; |
0 commit comments