|
| 1 | +//! Fuzz test tasks for workspaces with honggfuzz fuzz targets. |
| 2 | +
|
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use serde::Deserialize; |
| 6 | +use xshell::Shell; |
| 7 | + |
| 8 | +use crate::environment::{quiet_println, CONFIG_FILE_PATH}; |
| 9 | +use crate::quiet_cmd; |
| 10 | + |
| 11 | +/// Default package name for fuzz targets. |
| 12 | +const FUZZ_PACKAGE: &str = "fuzz"; |
| 13 | + |
| 14 | +/// Fuzz configuration loaded from rbmt.toml. |
| 15 | +#[derive(Debug, Deserialize, Default)] |
| 16 | +#[serde(default)] |
| 17 | +struct Config { |
| 18 | + fuzz: FuzzConfig, |
| 19 | +} |
| 20 | + |
| 21 | +/// Fuzz-specific configuration. |
| 22 | +#[derive(Debug, Deserialize, Default)] |
| 23 | +#[serde(default)] |
| 24 | +struct FuzzConfig { |
| 25 | + /// Package name containing fuzz targets (defaults to [`FUZZ_PACKAGE`]). |
| 26 | + package: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +impl FuzzConfig { |
| 30 | + /// Load fuzz configuration from workspace root. |
| 31 | + fn load(workspace_root: &Path) -> Result<Self, Box<dyn std::error::Error>> { |
| 32 | + let config_path = workspace_root.join(CONFIG_FILE_PATH); |
| 33 | + |
| 34 | + if !config_path.exists() { |
| 35 | + return Ok(Self::default()); |
| 36 | + } |
| 37 | + |
| 38 | + let contents = std::fs::read_to_string(&config_path)?; |
| 39 | + let config: Config = toml::from_str(&contents)?; |
| 40 | + Ok(config.fuzz) |
| 41 | + } |
| 42 | + |
| 43 | + /// Get the package name (defaults to [`FUZZ_PACKAGE`]). |
| 44 | + fn package_name(&self) -> &str { self.package.as_deref().unwrap_or(FUZZ_PACKAGE) } |
| 45 | +} |
| 46 | + |
| 47 | +/// Discover all fuzz targets using cargo metadata. |
| 48 | +/// |
| 49 | +/// Targets are discovered by querying cargo metadata for all binary targets |
| 50 | +/// in the specified fuzz package. |
| 51 | +fn discover_fuzz_targets( |
| 52 | + sh: &Shell, |
| 53 | + package_name: &str, |
| 54 | +) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
| 55 | + let metadata = quiet_cmd!(sh, "cargo metadata --format-version 1 --no-deps").read()?; |
| 56 | + let json: serde_json::Value = serde_json::from_str(&metadata)?; |
| 57 | + |
| 58 | + let mut targets = Vec::new(); |
| 59 | + |
| 60 | + // Find binary targets in the specified fuzz package. |
| 61 | + if let Some(packages) = json["packages"].as_array() { |
| 62 | + for package in packages { |
| 63 | + if package["name"].as_str() == Some(package_name) { |
| 64 | + if let Some(package_targets) = package["targets"].as_array() { |
| 65 | + for target in package_targets { |
| 66 | + // Filter for binary targets only. |
| 67 | + let Some(kinds) = target["kind"].as_array() else { |
| 68 | + continue; |
| 69 | + }; |
| 70 | + let Some(name) = target["name"].as_str() else { |
| 71 | + continue; |
| 72 | + }; |
| 73 | + |
| 74 | + if kinds.iter().any(|k| k.as_str() == Some("bin")) { |
| 75 | + targets.push(name.to_string()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + break; // Found the package, no need to continue. |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Sort for consistent output. |
| 85 | + targets.sort(); |
| 86 | + |
| 87 | + Ok(targets) |
| 88 | +} |
| 89 | + |
| 90 | +/// List discovered fuzz targets. |
| 91 | +pub fn list(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> { |
| 92 | + let workspace_root = sh.current_dir(); |
| 93 | + let config = FuzzConfig::load(&workspace_root)?; |
| 94 | + let package_name = config.package_name(); |
| 95 | + |
| 96 | + let targets = discover_fuzz_targets(sh, package_name)?; |
| 97 | + |
| 98 | + if targets.is_empty() { |
| 99 | + quiet_println("No fuzz targets found"); |
| 100 | + } else { |
| 101 | + for target in targets { |
| 102 | + println!("{}", target); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +/// Run fuzz tests for the workspace. |
| 110 | +pub fn run(_sh: &Shell) { quiet_println("Fuzz execution not yet implemented"); } |
0 commit comments