|
| 1 | +// Copyright (c) 2026 OverTheFlow and Contributors |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | +// If a copy of the MPL was not distributed with this file, You can obtain one at |
| 5 | +// https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +use std::net::{IpAddr, TcpListener}; |
| 8 | +use zond_common::config::ZondConfig; |
| 9 | +use zond_common::models::ip::set::IpSet; |
| 10 | +use zond_core::scanner; |
| 11 | +use pnet::datalink; |
| 12 | + |
| 13 | +/// Verifies that Windows hardware heuristics (physical/wireless detection) |
| 14 | +/// execute correctly and find plausible adapters. |
| 15 | +#[tokio::test] |
| 16 | +async fn windows_hardware_heuristics() { |
| 17 | + let interfaces = datalink::interfaces(); |
| 18 | + assert!(!interfaces.is_empty(), "No network interfaces found on the system"); |
| 19 | + |
| 20 | + let mut physically_found = 0; |
| 21 | + for iface in interfaces { |
| 22 | + // These calls verify that the GetIfTable2 FFI logic works on the real host |
| 23 | + let physical = zond_common::net::interface::os::is_physical(&iface); |
| 24 | + let wireless = zond_common::net::interface::os::is_wireless(&iface); |
| 25 | + |
| 26 | + if physical { |
| 27 | + physically_found += 1; |
| 28 | + } |
| 29 | + |
| 30 | + // Integrity check: Wireless adapters are always physical |
| 31 | + if wireless { |
| 32 | + assert!(physical, "Interface {} identified as wireless but not physical", iface.name); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // On a real machine, at least one interface (Ethernet or Wi-Fi) should be physical |
| 37 | + assert!(physically_found > 0, "No physical adapters detected - verify Administrator privileges"); |
| 38 | +} |
| 39 | + |
| 40 | +/// Performs a full unprivileged discovery scan against a local mock listener. |
| 41 | +/// This verifies the orchestration path: Dispatcher -> Connect -> Result. |
| 42 | +#[tokio::test] |
| 43 | +async fn windows_local_discovery_integration() { |
| 44 | + let cfg = ZondConfig { |
| 45 | + no_banner: true, |
| 46 | + no_dns: true, |
| 47 | + quiet: 0, |
| 48 | + ..Default::default() |
| 49 | + }; |
| 50 | + |
| 51 | + // 1. Find a valid local IPv4 interface |
| 52 | + let interfaces = datalink::interfaces(); |
| 53 | + let target_ip = interfaces.iter() |
| 54 | + .filter(|i| i.is_up() && !i.is_loopback()) |
| 55 | + .flat_map(|i| i.ips.iter()) |
| 56 | + .find(|ip| ip.is_ipv4()) |
| 57 | + .map(|ip| ip.ip()) |
| 58 | + .expect("No active local IPv4 interface found for testing"); |
| 59 | + |
| 60 | + // 2. Spawn a mock listener on a random port |
| 61 | + let listener = TcpListener::bind((target_ip, 0)).expect("Failed to bind mock listener"); |
| 62 | + let local_addr = listener.local_addr().unwrap(); |
| 63 | + let port = local_addr.port(); |
| 64 | + |
| 65 | + // 3. Run discovery targeting that specific IP |
| 66 | + let mut targets = IpSet::new(); |
| 67 | + targets.insert(target_ip); |
| 68 | + |
| 69 | + let handle = tokio::spawn(async move { |
| 70 | + scanner::discover(targets, &cfg).await |
| 71 | + }); |
| 72 | + |
| 73 | + // We give the scanner a moment. Since it's local, RTT is near zero. |
| 74 | + let result = handle.await.unwrap(); |
| 75 | + assert!(result.is_ok(), "Scanner failed on Windows: {:?}", result.err()); |
| 76 | + |
| 77 | + let hosts = result.unwrap(); |
| 78 | + |
| 79 | + // 4. Verify the host was found |
| 80 | + assert!(!hosts.is_empty(), "Scanner did not find the local host on Windows"); |
| 81 | + assert_eq!(hosts[0].primary_ip, target_ip); |
| 82 | + |
| 83 | + // Explicitly keep listener alive until here |
| 84 | + drop(listener); |
| 85 | +} |
| 86 | + |
| 87 | +/// Verifies loopback discovery on Windows. |
| 88 | +#[tokio::test] |
| 89 | +async fn windows_loopback_fidelity() { |
| 90 | + let cfg = ZondConfig { |
| 91 | + no_banner: true, |
| 92 | + no_dns: true, |
| 93 | + ..Default::default() |
| 94 | + }; |
| 95 | + |
| 96 | + let mut targets = IpSet::new(); |
| 97 | + let localhost = IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)); |
| 98 | + targets.insert(localhost); |
| 99 | + |
| 100 | + let result = scanner::discover(targets, &cfg).await; |
| 101 | + assert!(result.is_ok()); |
| 102 | + let hosts = result.unwrap(); |
| 103 | + |
| 104 | + assert!(!hosts.is_empty(), "Loopback discovery failed on Windows"); |
| 105 | + assert_eq!(hosts[0].primary_ip, localhost); |
| 106 | +} |
0 commit comments