diff --git a/.github/workflows/plugin-ci-workflow.yml b/.github/workflows/plugin-ci-workflow.yml index 8e92f9e9..0d9b6427 100644 --- a/.github/workflows/plugin-ci-workflow.yml +++ b/.github/workflows/plugin-ci-workflow.yml @@ -119,8 +119,8 @@ jobs: run: | set -euo pipefail sudo add-apt-repository -y ppa:ondrej/php - sudo apt-get update - sudo apt-get install -y \ + sudo apt-get -o Acquire::Retries=3 update + sudo apt-get -o Acquire::Retries=3 install -y \ php8.6-cli php8.6-common \ php8.6-intl php8.6-mysql php8.6-gd php8.6-ldap \ php8.6-gmp php8.6-xml php8.6-curl php8.6-mbstring \ @@ -138,7 +138,7 @@ jobs: # setup-php normally provides Composer; it is skipped for this leg, so # make sure a Composer binary is available for the dependency steps. if ! command -v composer >/dev/null 2>&1; then - sudo apt-get install -y composer + sudo apt-get -o Acquire::Retries=3 install -y composer fi php -v composer --version diff --git a/CHANGELOG.md b/CHANGELOG.md index 22d46c30..1fb4812e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ * issue#446: Keep MacTrack enabled when an ARP/IP, site, device, IP range, 802.1x, MAC or interface view query fails or times out; the record functions returned false from a typed array return and the resulting TypeError made Cacti disable the plugin * issue#446: Join interfaces on site_id and device_id in the ARP/IP view so the query uses the primary key instead of scanning the interface table for every row * issue#448: Retain failed scan loads, stop incomplete scan-date reconciliation, and restart aggregate rebuilds without replaying counts; preserve ambiguous legacy rebuild tables for recovery +* issue#453: Open the IP Addresses report from IP Ranges with the Most Recent scan selected; the link passed scan_date=3, which only the MAC Addresses report knows (Aggregated), so the report showed no rows * issue: Add a tooltip to the interfaces 'Show Totals' control explaining what the totals columns show * issue: Align the documented PHP floor with the PHP 8.2 CI/test contract * issue: Call mactrack_format_mac() from the ARP export instead of a function that does not exist @@ -90,6 +91,7 @@ * feature#426: Partition mac_track_ports by scan date, weekly by default, with daily and legacy per-scan or hourly sizes, so retention drops expired partitions instead of deleting rows; new installs are partitioned and existing tables can be converted between daily, weekly and unpartitioned layouts from Utilities or cli/upgrade_partitioning.php; Utilities rebuilds and analyzes only the latest populated partition (also on the legacy daily layout), maintenance keeps two days of partitions created ahead (daily slots that per-scan partitions are carved from) so scans move no rows in normal operation, and named retention settings keep their day count across DST * feature#435: Leave MACs on ports matching a regular expression out of a scan, so uplink and trunk ports stay out of the database: a new Ports to Ignore While Scanning setting for all devices plus a regular expression on each site and device, matched without regard to case against the port name and alias. The filter runs where every scanner stores its results, an invalid expression is logged and skipped, and the device run message reports how many MACs were skipped; the regular expression fields are text areas like the device Ports to Ignore, and each line is its own expression * feature#451: Partition the IP address table (mac_track_ips) by scan date like the port table, and replace mactrack_convert.php with cli/upgrade_partitioning.php. It checks both tables against the Port Partition Size setting (or --partition=daily|weekly|disabled, which it saves as the setting; Disabled is now a setting value), reports when there is no drift, and rebuilds each drifted table in turn: rename it to _rebuild, create it again with the retention and partitioning scheme, copy each scan date, then drop the original. The run is registered in the process registry, pauses scanning and database maintenance (a maintenance window that passes meanwhile is run by the poller as soon as the rebuild ends), shows a maintenance message on the MAC and IP address views, refuses changes to the history and defers the background database upgrade until it has finished, prints progress, and resumes after an interruption; new installs partition the IP table (following the saved Port Partition Size) and its retention drops whole partitions; --keep-old is removed; the README describes setting up and maintaining partitioning +* feature#455: Add the vendor Virtual/KVM Host for the unregistered MAC prefix 52:54:00 (QEMU/KVM virtual network cards) to the OUI database when the plugin is upgraded and after every IEEE OUI import, which removes the vendors the IEEE list does not hold; a prefix the database already holds is kept * feature: Add a Dashboard tab (the first MacTrack tab) presenting admin status cards in a draggable, add/remove * feature: Add a root manifest.json and upgrade-time file pruning (mactrack_prune_files()) that removes tombstoned and dev-only paths * feature: Add a status pill column to the 802.1x view after the username; clicking the pill drills down by setting the Status filter to that session status diff --git a/lib/mactrack_functions.php b/lib/mactrack_functions.php index c8537c2a..1ff37faa 100644 --- a/lib/mactrack_functions.php +++ b/lib/mactrack_functions.php @@ -3854,6 +3854,32 @@ function mactrack_ports_load(string $table = 'mac_track_ports', int $batch = 100 return $read; } +/** + * Well known MAC prefixes that no vendor has registered with the IEEE, so no + * OUI download holds them, keyed by the prefix as the OUI table stores it. + * 52:54:00 is the prefix QEMU/KVM gives its virtual network cards. + */ +const MACTRACK_KNOWN_VENDOR_MACS = [ + '525400' => 'Virtual/KVM Host', +]; + +/** + * Adds the well known unregistered MAC prefixes to the OUI table. A prefix + * the table already holds, such as one the IEEE registers later, is kept. The + * upgrade runs this, and so does every OUI import, because a full import + * deletes the vendors the IEEE list does not hold. + * + * @return void + */ +function mactrack_ensure_known_vendor_macs(): void { + foreach (MACTRACK_KNOWN_VENDOR_MACS as $vendor_mac => $vendor_name) { + db_execute_prepared('INSERT IGNORE INTO mac_track_oui_database + (vendor_mac, vendor_name, vendor_address, present) + VALUES (?, ?, ?, 1)', + [(string) $vendor_mac, $vendor_name, '']); + } +} + /** * Downloads the IEEE OUI (vendor MAC prefix) database text file and * imports its vendor/MAC-prefix/address records into the @@ -3990,6 +4016,9 @@ function import_oui_database($type = 'ui', $oui_file = 'https://standards-oui.ie // set correct format db_execute("UPDATE mac_track_oui_database SET vendor_mac = REPLACE(vendor_mac, ':', '')"); + // the import retires vendors the IEEE list does not hold + mactrack_ensure_known_vendor_macs(); + // report some information if ($type != 'ui') { print '
'; diff --git a/mactrack_view_ips.php b/mactrack_view_ips.php index b58e9198..2794f5b9 100644 --- a/mactrack_view_ips.php +++ b/mactrack_view_ips.php @@ -300,7 +300,7 @@ function mactrack_view_ip_ranges(): void { '&ip_filter_type_id=3' . '&ip_filter=' . $ip_range['ip_range'] . '.' . '&device_id=-1' . - '&scan_date=3' . + '&scan_date=2' . '&site_id=' . $ip_range['site_id']) . '" title="' . __esc('View IP Addresses', 'mactrack') . '">'; form_alternate_row('line' . $i, true); diff --git a/setup.php b/setup.php index d55240ae..c993db8f 100644 --- a/setup.php +++ b/setup.php @@ -496,6 +496,9 @@ function mactrack_perform_upgrade(): bool { [$info['longname'], $info['author'], $info['homepage'], $info['version'], $id]); } + // the IEEE list has no record of these well known prefixes + mactrack_ensure_known_vendor_macs(); + mactrack_retry_default_site(); return true; diff --git a/tests/Unit/DeviceTypeSqlSafetyTest.php b/tests/Unit/DeviceTypeSqlSafetyTest.php index efbc9bbb..ed98c252 100644 --- a/tests/Unit/DeviceTypeSqlSafetyTest.php +++ b/tests/Unit/DeviceTypeSqlSafetyTest.php @@ -1,126 +1,126 @@ -not->toContain("(mtdt.vendor='\" . get_request_var('vendor')"); - expect($source)->toContain("mtdt.vendor = ' . db_qstr(get_request_var('vendor'))"); -}); - -it('normalizes IDs and uses prepared SQL for aggregated MAC deletion', function () { - $source = plugin_test_read_source('mactrack_view_macs.php'); - - expect($source)->toContain('function mactrack_normalize_ids(array $ids): array'); - expect($source)->toContain("db_execute_prepared('DELETE FROM mac_track_aggregated_ports WHERE row_id IN('"); -}); - -it('uses escaped JSON instead of request deserialization for MAC authorization actions', function () { - $source = plugin_test_read_source('mactrack_view_macs.php'); - - expect($source)->not->toContain("unserialize(get_nfilter_request_var('selected_items')"); - expect($source)->toContain("json_decode(get_nfilter_request_var('selected_items'), true)"); - expect($source)->toContain('html_escape(json_encode($mac_address_array))'); -}); - -it('uses escaped JSON and contextual output encoding for MAC action form values', function () { - $source = plugin_test_read_source('mactrack_view_macs.php'); - - expect($source)->not->toContain("sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'))"); - expect($source)->toContain('html_escape(json_encode($row_array))'); - expect($source)->toContain("html_escape((string) get_request_var('drp_action'))"); -}); - -it('escapes the executable and script paths used by rescan commands', function () { - $source = plugin_test_read_source('lib/mactrack_functions.php'); - - expect($source)->toContain("cacti_escapeshellcmd(read_config_option('path_php_binary'))"); - expect($source)->toContain('cacti_escapeshellarg($command_string)'); -}); - -it('parameterizes site IDs and normalizes PIDs during poller process cleanup', function () { - $source = plugin_test_read_source('poller_mactrack.php'); - - expect($source)->toContain('site_id = ?'); - expect($source)->toContain("intval(\$p['process_id'])"); -}); - -it('escapes the executable and device endpoint values in the Cabletron SNMP command', function () { - $source = plugin_test_read_source('lib/mactrack_cabletron.php'); - - expect($source)->toContain("cacti_escapeshellcmd(read_config_option('path_snmpgetnext'))"); - expect($source)->toContain('cacti_escapeshellarg($device[\'hostname\'] . \':\' . intval($device[\'snmp_port\']))'); -}); - -it('uses safe RLIKE quoting and normalized numeric values for interface filters', function () { - $source = plugin_test_read_source('mactrack_view_interfaces.php'); - - expect($source)->toContain('mactrack_get_ignore_ports_predicate($sql_params)'); - expect($source)->toContain('db_fetch_assoc_prepared($sql_query, $sql_params)'); - expect($source)->toContain('db_fetch_cell_prepared($rows_query_string, $sql_params)'); - expect($source)->not->toContain('db_qstr($match)'); - expect($source)->not->toContain('db_qstr_rlike'); - expect($source)->toContain("intval(get_filter_request_var('bwusage'))"); -}); - -it('normalizes numeric values in the device report SQL filters', function () { - $source = plugin_test_read_source('mactrack_view_devices.php'); - - expect($source)->toContain("intval(get_filter_request_var('status'))"); - expect($source)->toContain("intval(get_filter_request_var('site_id'))"); -}); - -it('normalizes numeric values in the administrative device SQL filters', function () { - $source = plugin_test_read_source('mactrack_devices.php'); - - expect($source)->toContain("intval(get_filter_request_var('status'))"); - expect($source)->toContain("intval(get_filter_request_var('site_id'))"); -}); - -it('validates canonical regular-file paths before importing an OUI database', function () { - $source = plugin_test_read_source('mactrack_import_ouidb.php'); - - expect($source)->toContain('function mactrack_validate_oui_file(string $path): string'); - expect($source)->toContain('is_file($resolved)'); - expect($source)->toContain('is_readable($resolved)'); -}); - -it('preserves PHP 8 compatible argument order for the 1.2.31 signatures', function () { - $arpSource = plugin_test_read_source('mactrack_view_arp.php'); - $dot1xSource = plugin_test_read_source('mactrack_view_dot1x.php'); - $ajaxSource = plugin_test_read_source('mactrack_ajax.php'); - - expect($arpSource)->toContain('function mactrack_view_get_ip_records(&$sql_where, $rows, $apply_limits = true)'); - expect($dot1xSource)->toContain('function mactrack_view_get_dot1x_records(&$sql_where, &$sql_params, $rows, $apply_limits = true)'); - - expect($arpSource)->toContain('mactrack_format_mac('); - expect($arpSource)->not->toContain('format_mac_address('); - expect($ajaxSource)->not->toContain('mactrack_save_graph_settings'); -}); - -it('loads Net_DNS2 from the bundled Net/ directory and reports rather than fatals when missing', function () { - $source = plugin_test_read_source('mactrack_resolver.php'); - - expect($source)->toContain("'/plugins/mactrack' . PATH_SEPARATOR . get_include_path()"); - expect($source)->toContain('if (is_file($dns2))'); - expect($source)->toContain("class_exists('Net_DNS2_Resolver')"); -}); - -it('never gates plugin enablement on the optional DNS library', function () { - $source = plugin_test_read_source('setup.php'); - - expect($source)->not->toContain('Net_DNS2'); - expect($source)->not->toContain('Net/DNS2.php'); -}); +not->toContain("(mtdt.vendor='\" . get_request_var('vendor')"); + expect($source)->toContain("mtdt.vendor = ' . db_qstr(get_request_var('vendor'))"); +}); + +it('normalizes IDs and uses prepared SQL for aggregated MAC deletion', function () { + $source = plugin_test_read_source('mactrack_view_macs.php'); + + expect($source)->toContain('function mactrack_normalize_ids(array $ids): array'); + expect($source)->toContain("db_execute_prepared('DELETE FROM mac_track_aggregated_ports WHERE row_id IN('"); +}); + +it('uses escaped JSON instead of request deserialization for MAC authorization actions', function () { + $source = plugin_test_read_source('mactrack_view_macs.php'); + + expect($source)->not->toContain("unserialize(get_nfilter_request_var('selected_items')"); + expect($source)->toContain("json_decode(get_nfilter_request_var('selected_items'), true)"); + expect($source)->toContain('html_escape(json_encode($mac_address_array))'); +}); + +it('uses escaped JSON and contextual output encoding for MAC action form values', function () { + $source = plugin_test_read_source('mactrack_view_macs.php'); + + expect($source)->not->toContain("sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'))"); + expect($source)->toContain('html_escape(json_encode($row_array))'); + expect($source)->toContain("html_escape((string) get_request_var('drp_action'))"); +}); + +it('escapes the executable and script paths used by rescan commands', function () { + $source = plugin_test_read_source('lib/mactrack_functions.php'); + + expect($source)->toContain("cacti_escapeshellcmd(read_config_option('path_php_binary'))"); + expect($source)->toContain('cacti_escapeshellarg($command_string)'); +}); + +it('parameterizes site IDs and normalizes PIDs during poller process cleanup', function () { + $source = plugin_test_read_source('poller_mactrack.php'); + + expect($source)->toContain('site_id = ?'); + expect($source)->toContain("intval(\$p['process_id'])"); +}); + +it('escapes the executable and device endpoint values in the Cabletron SNMP command', function () { + $source = plugin_test_read_source('lib/mactrack_cabletron.php'); + + expect($source)->toContain("cacti_escapeshellcmd(read_config_option('path_snmpgetnext'))"); + expect($source)->toContain('cacti_escapeshellarg($device[\'hostname\'] . \':\' . intval($device[\'snmp_port\']))'); +}); + +it('uses safe RLIKE quoting and normalized numeric values for interface filters', function () { + $source = plugin_test_read_source('mactrack_view_interfaces.php'); + + expect($source)->toContain('mactrack_get_ignore_ports_predicate($sql_params)'); + expect($source)->toContain('db_fetch_assoc_prepared($sql_query, $sql_params)'); + expect($source)->toContain('db_fetch_cell_prepared($rows_query_string, $sql_params)'); + expect($source)->not->toContain('db_qstr($match)'); + expect($source)->not->toContain('db_qstr_rlike'); + expect($source)->toContain("intval(get_filter_request_var('bwusage'))"); +}); + +it('normalizes numeric values in the device report SQL filters', function () { + $source = plugin_test_read_source('mactrack_view_devices.php'); + + expect($source)->toContain("intval(get_filter_request_var('status'))"); + expect($source)->toContain("intval(get_filter_request_var('site_id'))"); +}); + +it('normalizes numeric values in the administrative device SQL filters', function () { + $source = plugin_test_read_source('mactrack_devices.php'); + + expect($source)->toContain("intval(get_filter_request_var('status'))"); + expect($source)->toContain("intval(get_filter_request_var('site_id'))"); +}); + +it('validates canonical regular-file paths before importing an OUI database', function () { + $source = plugin_test_read_source('mactrack_import_ouidb.php'); + + expect($source)->toContain('function mactrack_validate_oui_file(string $path): string'); + expect($source)->toContain('is_file($resolved)'); + expect($source)->toContain('is_readable($resolved)'); +}); + +it('preserves PHP 8 compatible argument order for the 1.2.31 signatures', function () { + $arpSource = plugin_test_read_source('mactrack_view_arp.php'); + $dot1xSource = plugin_test_read_source('mactrack_view_dot1x.php'); + $ajaxSource = plugin_test_read_source('mactrack_ajax.php'); + + expect($arpSource)->toContain('function mactrack_view_get_ip_records(&$sql_where, $rows, $apply_limits = true)'); + expect($dot1xSource)->toContain('function mactrack_view_get_dot1x_records(&$sql_where, &$sql_params, $rows, $apply_limits = true)'); + + expect($arpSource)->toContain('mactrack_format_mac('); + expect($arpSource)->not->toContain('format_mac_address('); + expect($ajaxSource)->not->toContain('mactrack_save_graph_settings'); +}); + +it('loads Net_DNS2 from the bundled Net/ directory and reports rather than fatals when missing', function () { + $source = plugin_test_read_source('mactrack_resolver.php'); + + expect($source)->toContain("'/plugins/mactrack' . PATH_SEPARATOR . get_include_path()"); + expect($source)->toContain('if (is_file($dns2))'); + expect($source)->toContain("class_exists('Net_DNS2_Resolver')"); +}); + +it('never gates plugin enablement on the optional DNS library', function () { + $source = plugin_test_read_source('setup.php'); + + expect($source)->not->toContain('Net_DNS2'); + expect($source)->not->toContain('Net/DNS2.php'); +}); diff --git a/tests/Unit/FilterOptionEscapingTest.php b/tests/Unit/FilterOptionEscapingTest.php index a1036825..64bafc3a 100644 --- a/tests/Unit/FilterOptionEscapingTest.php +++ b/tests/Unit/FilterOptionEscapingTest.php @@ -1,49 +1,49 @@ - labels built from database content (site - * names, device names/hostnames, device types) are actually wired through - * html_escape() in the views that render them, rather than only asserting - * that html_escape() exists somewhere in the plugin. - */ -describe('filter option label escaping in mactrack', function () { - $checks = [ - 'mactrack_view_arp.php' => [ - "html_escape(\$site['site_name'])", - "html_escape(\$filter_device['device_name'] . '(' . \$filter_device['hostname'] . ')')", - ], - 'mactrack_view_devices.php' => [ - "html_escape(\$site['site_name'])", - 'html_escape($display_text)', - ], - 'mactrack_view_dot1x.php' => [ - "html_escape(\$site['site_name'])", - ], - 'mactrack_view_interfaces.php' => [ - "html_escape(\$site['site_name'])", - "html_escape(\$type['device_type'])", - 'html_escape($device_name)', - ], - 'mactrack_view_ips.php' => [ - "html_escape(\$site['site_name'])", - ], - ]; - - foreach ($checks as $file => $patterns) { - foreach ($patterns as $pattern) { - it("renders {$pattern} in {$file}", function () use ($file, $pattern) { - $source = plugin_test_read_source($file); - - expect($source)->toContain($pattern); - }); - } - } -}); + labels built from database content (site + * names, device names/hostnames, device types) are actually wired through + * html_escape() in the views that render them, rather than only asserting + * that html_escape() exists somewhere in the plugin. + */ +describe('filter option label escaping in mactrack', function () { + $checks = [ + 'mactrack_view_arp.php' => [ + "html_escape(\$site['site_name'])", + "html_escape(\$filter_device['device_name'] . '(' . \$filter_device['hostname'] . ')')", + ], + 'mactrack_view_devices.php' => [ + "html_escape(\$site['site_name'])", + 'html_escape($display_text)', + ], + 'mactrack_view_dot1x.php' => [ + "html_escape(\$site['site_name'])", + ], + 'mactrack_view_interfaces.php' => [ + "html_escape(\$site['site_name'])", + "html_escape(\$type['device_type'])", + 'html_escape($device_name)', + ], + 'mactrack_view_ips.php' => [ + "html_escape(\$site['site_name'])", + ], + ]; + + foreach ($checks as $file => $patterns) { + foreach ($patterns as $pattern) { + it("renders {$pattern} in {$file}", function () use ($file, $pattern) { + $source = plugin_test_read_source($file); + + expect($source)->toContain($pattern); + }); + } + } +}); diff --git a/tests/Unit/IpRangeDrilldownTest.php b/tests/Unit/IpRangeDrilldownTest.php new file mode 100644 index 00000000..86cc7a7f --- /dev/null +++ b/tests/Unit/IpRangeDrilldownTest.php @@ -0,0 +1,66 @@ +not->toBeFalse(); + + preg_match("/'&scan_date=([^']*)'/", substr($source, (int) $start, 600), $match); + + return $match[1] ?? ''; +} + +it('opens the IP Addresses report with the Most Recent scan', function () { + $root = dirname(__DIR__, 2); + $arp = file_get_contents($root . '/mactrack_view_arp.php'); + $link = mt_ir_link_scan_date(file_get_contents($root . '/mactrack_view_ips.php'), 'mactrack_view_arp.php'); + + expect($link)->toBe('2'); + expect($arp)->toContain(""); + expect($arp)->not->toContain(""); +}); + +it('defaults the IP Addresses report to the Most Recent scan', function () { + $arp = file_get_contents(dirname(__DIR__, 2) . '/mactrack_view_arp.php'); + + expect($arp)->toContain("'scan_date' => [\n\t\t\t'filter' => FILTER_CALLBACK,\n\t\t\t'default' => '2',"); +}); diff --git a/tests/Unit/MactrackOuiDownloadTest.php b/tests/Unit/MactrackOuiDownloadTest.php index b71039fb..329acd27 100644 --- a/tests/Unit/MactrackOuiDownloadTest.php +++ b/tests/Unit/MactrackOuiDownloadTest.php @@ -409,6 +409,65 @@ public function testFullImportRemovesAbsentVendors(): void { $this->assertContains('DELETE FROM mac_track_oui_database WHERE present=0', $this->executedSql()); } + /** + * The unregistered KVM prefix is not in any IEEE list, so a full import + * that deletes the vendors it did not refresh would remove it: it is added + * again after the delete, in every kind of import. + * + * @param string $kind full, partial or failed. + * + * @return void + */ + #[\PHPUnit\Framework\Attributes\DataProvider('importKinds')] + public function testImportAddsTheKnownVendorsAfterTheDelete(string $kind): void { + if ($kind === 'failed') { + mactrack_test_queue_return_for('db_execute', 'REPLACE INTO mac_track_oui_database', false); + } + + ob_start(); + import_oui_database('ui', $this->ouiFixtureOf(1000), $kind === 'partial' ? $this->ouiFixtureOf(1000) : $this->unreachablePath()); + ob_end_clean(); + + $calls = $GLOBALS['__test_db_calls']; + $vendor = null; + $last_sql = null; + + foreach ($calls as $i => $call) { + if ($call['fn'] === 'db_execute_prepared' && strpos($call['sql'], 'INSERT IGNORE INTO mac_track_oui_database') !== false) { + $vendor = $i; + $this->assertSame(['525400', 'Virtual/KVM Host', ''], $call['params']); + } + + if ($call['fn'] === 'db_execute' && strpos($call['sql'], 'mac_track_oui_database') !== false) { + $last_sql = $i; + } + } + + $this->assertNotNull($vendor, 'the import must add the known vendors'); + $this->assertGreaterThan($last_sql, $vendor, 'the known vendors are added after the import has written and deleted'); + } + + /** + * @return array + */ + public static function importKinds(): array { + return ['full list' => ['full'], 'partial fallback list' => ['partial'], 'failed batch' => ['failed']]; + } + + /** + * @return void + */ + public function testKnownVendorsKeepARecordTheTableAlreadyHolds(): void { + $this->assertSame(['525400' => 'Virtual/KVM Host'], MACTRACK_KNOWN_VENDOR_MACS); + + mactrack_ensure_known_vendor_macs(); + + $calls = array_values(array_filter($GLOBALS['__test_db_calls'], fn ($c) => $c['fn'] === 'db_execute_prepared')); + + $this->assertCount(1, $calls); + $this->assertStringContainsString('INSERT IGNORE INTO mac_track_oui_database', $calls[0]['sql']); + } + /** * @return void */ diff --git a/tests/Unit/MactrackUpgradeTest.php b/tests/Unit/MactrackUpgradeTest.php index 34e4e1b8..f83ce27e 100644 --- a/tests/Unit/MactrackUpgradeTest.php +++ b/tests/Unit/MactrackUpgradeTest.php @@ -60,6 +60,15 @@ })); expect($updates)->not->toBeEmpty(); + + // The unregistered KVM prefix is added to the OUI table, after the version + // update's housekeeping and before the upgrade returns. + $vendors = array_values(array_filter($GLOBALS['__test_db_calls'], function ($call) { + return $call['fn'] === 'db_execute_prepared' && strpos($call['sql'], 'INSERT IGNORE INTO mac_track_oui_database') !== false; + })); + + expect($vendors)->toHaveCount(1); + expect($vendors[0]['params'])->toBe(['525400', 'Virtual/KVM Host', '']); }); it('reinstalls hooks and upgrades the schema when the plugin is not yet recorded', function () { @@ -132,4 +141,9 @@ expect($hooks)->toBeEmpty(); expect($updates)->toBeEmpty(); + + // An upgrade that has not finished adds no vendors; the retry does. + expect(array_filter($GLOBALS['__test_db_calls'], function ($call) { + return strpos((string) $call['sql'], 'mac_track_oui_database') !== false; + }))->toBeEmpty(); });