Skip to main content

mockforge_bench/
command.rs

1//! Bench command implementation
2
3use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11    MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21    SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24    topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30    load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37/// Parse a comma-separated header string into a `HashMap`.
38///
39/// Format: `Key:Value,Key2:Value2`
40///
41/// **Known limitation**: Header values containing commas will be incorrectly
42/// split. Cookie headers with semicolons work fine, but Cookie values with
43/// commas (e.g. `expires=Thu, 01 Jan 2099`) will break.
44pub fn parse_header_string(input: &str) -> Result<HashMap<String, String>> {
45    let mut headers = HashMap::new();
46
47    for pair in input.split(',') {
48        let parts: Vec<&str> = pair.splitn(2, ':').collect();
49        if parts.len() != 2 {
50            return Err(BenchError::Other(format!(
51                "Invalid header format: '{}'. Expected 'Key:Value'",
52                pair
53            )));
54        }
55        headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
56    }
57
58    Ok(headers)
59}
60
61/// Bench command configuration
62pub struct BenchCommand {
63    /// OpenAPI spec file(s) - can specify multiple
64    pub spec: Vec<PathBuf>,
65    /// Directory containing OpenAPI spec files (discovers .json, .yaml, .yml files)
66    pub spec_dir: Option<PathBuf>,
67    /// Conflict resolution strategy when merging multiple specs: "error" (default), "first", "last"
68    pub merge_conflicts: String,
69    /// Spec mode: "merge" (default) combines all specs, "sequential" runs them in order
70    pub spec_mode: String,
71    /// Dependency configuration file for cross-spec value passing (used with sequential mode)
72    pub dependency_config: Option<PathBuf>,
73    pub target: String,
74    /// API base path prefix (e.g., "/api" or "/v2/api")
75    /// If None, extracts from OpenAPI spec's servers URL
76    pub base_path: Option<String>,
77    pub duration: String,
78    pub vus: u32,
79    /// Target requests-per-second. When `Some(n)`, the generated k6 script
80    /// switches to `constant-arrival-rate` executor at `n` RPS with `vus`
81    /// pre-allocated. When `None`, uses the legacy `ramping-vus` executor
82    /// where RPS is implicit (VUs × 1 req/sec from the script's `sleep(1)`).
83    /// Issue #79 — Srikanth's round-3 reply.
84    pub target_rps: Option<u32>,
85    /// When true, every k6 request opens a new TCP/TLS connection
86    /// (`noConnectionReuse: true` and `--no-vu-connection-reuse`). Lets users
87    /// drive a high connections-per-second rate to exercise connection-limit
88    /// chaos and observe TCP-level fault injection. Issue #79.
89    pub no_keep_alive: bool,
90    pub scenario: String,
91    pub operations: Option<String>,
92    /// Exclude operations from testing (comma-separated)
93    ///
94    /// Supports "METHOD /path" or just "METHOD" to exclude all operations of that type.
95    pub exclude_operations: Option<String>,
96    pub auth: Option<String>,
97    pub headers: Option<String>,
98    pub output: PathBuf,
99    pub generate_only: bool,
100    pub script_output: Option<PathBuf>,
101    pub threshold_percentile: String,
102    pub threshold_ms: u64,
103    pub max_error_rate: f64,
104    pub verbose: bool,
105    pub skip_tls_verify: bool,
106    /// When true, set `Transfer-Encoding: chunked` on every k6 request body so
107    /// the server experiences chunked-encoded traffic. See
108    /// `K6ScriptTemplateData::chunked_request_bodies` for caveats — k6's Go
109    /// transport may still send Content-Length in some cases.
110    pub chunked_request_bodies: bool,
111    /// Optional file containing multiple targets
112    pub targets_file: Option<PathBuf>,
113    /// Maximum number of parallel executions (for multi-target mode)
114    pub max_concurrency: Option<u32>,
115    /// Results format: "per-target", "aggregated", or "both"
116    pub results_format: String,
117    /// Optional file containing parameter value overrides (JSON or YAML)
118    ///
119    /// Allows users to provide custom values for path parameters, query parameters,
120    /// headers, and request bodies instead of auto-generated placeholder values.
121    pub params_file: Option<PathBuf>,
122
123    // === CRUD Flow Options ===
124    /// Enable CRUD flow mode
125    pub crud_flow: bool,
126    /// Custom CRUD flow configuration file
127    pub flow_config: Option<PathBuf>,
128    /// Fields to extract from responses
129    pub extract_fields: Option<String>,
130
131    // === Parallel Execution Options ===
132    /// Number of resources to create in parallel
133    pub parallel_create: Option<u32>,
134
135    // === Data-Driven Testing Options ===
136    /// Test data file (CSV or JSON)
137    pub data_file: Option<PathBuf>,
138    /// Data distribution strategy
139    pub data_distribution: String,
140    /// Data column to field mappings
141    pub data_mappings: Option<String>,
142    /// Enable per-URI control mode (each row specifies method, uri, body, etc.)
143    pub per_uri_control: bool,
144
145    // === Invalid Data Testing Options ===
146    /// Percentage of requests with invalid data
147    pub error_rate: Option<f64>,
148    /// Types of invalid data to generate
149    pub error_types: Option<String>,
150
151    // === Security Testing Options ===
152    /// Enable security testing
153    pub security_test: bool,
154    /// Custom security payloads file
155    pub security_payloads: Option<PathBuf>,
156    /// Security test categories
157    pub security_categories: Option<String>,
158    /// Fields to target for security injection
159    pub security_target_fields: Option<String>,
160
161    // === WAFBench Integration ===
162    /// WAFBench test directory or glob pattern for loading CRS attack patterns
163    pub wafbench_dir: Option<String>,
164    /// Cycle through ALL WAFBench payloads instead of random sampling
165    pub wafbench_cycle_all: bool,
166
167    // === OpenAPI 3.0.0 Conformance Testing ===
168    /// Enable conformance testing mode
169    pub conformance: bool,
170    /// API key for conformance security tests
171    pub conformance_api_key: Option<String>,
172    /// Basic auth credentials for conformance security tests (user:pass)
173    pub conformance_basic_auth: Option<String>,
174    /// Conformance report output file
175    pub conformance_report: PathBuf,
176    /// Conformance categories to test (comma-separated, e.g. "parameters,security")
177    pub conformance_categories: Option<String>,
178    /// Conformance report format: "json" or "sarif"
179    pub conformance_report_format: String,
180    /// Custom headers to inject into every conformance request (for authentication).
181    /// Each entry is "Header-Name: value" format.
182    pub conformance_headers: Vec<String>,
183    /// When true, test ALL operations for method/response/body categories
184    /// instead of just one representative per feature check.
185    pub conformance_all_operations: bool,
186    /// Optional YAML file with custom conformance checks
187    pub conformance_custom: Option<PathBuf>,
188    /// Delay in milliseconds between consecutive conformance requests.
189    /// Useful when testing against rate-limited APIs.
190    pub conformance_delay_ms: u64,
191    /// Use k6 for conformance test execution instead of the native Rust executor
192    pub use_k6: bool,
193    /// Regex filter for custom conformance checks — only checks whose name or
194    /// path matches the pattern are included. Example: "wafcrs|ssl" to test
195    /// only checks with "wafcrs" or "ssl" in the name/path.
196    pub conformance_custom_filter: Option<String>,
197    /// When true, export all request/response pairs to
198    /// `conformance-requests.json` in the output directory.
199    pub export_requests: bool,
200    /// When true, validate each request against the OpenAPI spec and report
201    /// violations to `conformance-request-violations.json`.
202    pub validate_requests: bool,
203
204    // === OWASP API Security Top 10 Testing ===
205    /// Enable OWASP API Security Top 10 testing mode
206    pub owasp_api_top10: bool,
207    /// OWASP API categories to test (comma-separated)
208    pub owasp_categories: Option<String>,
209    /// Authorization header name for OWASP auth tests
210    pub owasp_auth_header: String,
211    /// Valid authorization token for OWASP baseline requests
212    pub owasp_auth_token: Option<String>,
213    /// File containing admin/privileged paths to test
214    pub owasp_admin_paths: Option<PathBuf>,
215    /// Fields containing resource IDs for BOLA testing
216    pub owasp_id_fields: Option<String>,
217    /// OWASP report output file
218    pub owasp_report: Option<PathBuf>,
219    /// OWASP report format (json, sarif)
220    pub owasp_report_format: String,
221    /// Number of iterations per VU for OWASP tests (default: 1)
222    pub owasp_iterations: u32,
223}
224
225impl BenchCommand {
226    /// Load and merge specs from --spec files and --spec-dir
227    pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
228        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
229
230        // Load specs from --spec flags
231        if !self.spec.is_empty() {
232            let specs = load_specs_from_files(self.spec.clone())
233                .await
234                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
235            all_specs.extend(specs);
236        }
237
238        // Load specs from --spec-dir if provided
239        if let Some(spec_dir) = &self.spec_dir {
240            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
241                BenchError::Other(format!("Failed to load specs from directory: {}", e))
242            })?;
243            all_specs.extend(dir_specs);
244        }
245
246        if all_specs.is_empty() {
247            return Err(BenchError::Other(
248                "No spec files provided. Use --spec or --spec-dir.".to_string(),
249            ));
250        }
251
252        // If only one spec, return it directly (extract just the OpenApiSpec)
253        if all_specs.len() == 1 {
254            // Safe to unwrap because we just checked len() == 1
255            return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
256        }
257
258        // Merge multiple specs
259        let conflict_strategy = match self.merge_conflicts.as_str() {
260            "first" => ConflictStrategy::First,
261            "last" => ConflictStrategy::Last,
262            _ => ConflictStrategy::Error,
263        };
264
265        merge_specs(all_specs, conflict_strategy)
266            .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
267    }
268
269    /// Get a display name for the spec(s)
270    fn get_spec_display_name(&self) -> String {
271        if self.spec.len() == 1 {
272            self.spec[0].to_string_lossy().to_string()
273        } else if !self.spec.is_empty() {
274            format!("{} spec files", self.spec.len())
275        } else if let Some(dir) = &self.spec_dir {
276            format!("specs from {}", dir.display())
277        } else {
278            "no specs".to_string()
279        }
280    }
281
282    /// Execute the bench command
283    pub async fn execute(&self) -> Result<()> {
284        // Check if we're in multi-target mode
285        if let Some(targets_file) = &self.targets_file {
286            if self.conformance {
287                return self.execute_multi_target_conformance(targets_file).await;
288            }
289            return self.execute_multi_target(targets_file).await;
290        }
291
292        // Check if we're in sequential spec mode (for dependency handling)
293        if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
294            return self.execute_sequential_specs().await;
295        }
296
297        // Single target mode (existing behavior)
298        // Print header
299        TerminalReporter::print_header(
300            &self.get_spec_display_name(),
301            &self.target,
302            0, // Will be updated later
303            &self.scenario,
304            Self::parse_duration(&self.duration)?,
305        );
306
307        // Validate k6 installation
308        if !K6Executor::is_k6_installed() {
309            TerminalReporter::print_error("k6 is not installed");
310            TerminalReporter::print_warning(
311                "Install k6 from: https://k6.io/docs/get-started/installation/",
312            );
313            return Err(BenchError::K6NotFound);
314        }
315
316        // Check for conformance testing mode (before spec loading — conformance doesn't need a user spec)
317        if self.conformance {
318            return self.execute_conformance_test().await;
319        }
320
321        // Load and parse spec(s)
322        TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
323        let merged_spec = self.load_and_merge_specs().await?;
324        let parser = SpecParser::from_spec(merged_spec);
325        if self.spec.len() > 1 || self.spec_dir.is_some() {
326            TerminalReporter::print_success(&format!(
327                "Loaded and merged {} specification(s)",
328                self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
329            ));
330        } else {
331            TerminalReporter::print_success("Specification loaded");
332        }
333
334        // Check for mock server integration
335        let mock_config = self.build_mock_config().await;
336        if mock_config.is_mock_server {
337            TerminalReporter::print_progress("Mock server integration enabled");
338        }
339
340        // Check for CRUD flow mode
341        if self.crud_flow {
342            return self.execute_crud_flow(&parser).await;
343        }
344
345        // Check for OWASP API Top 10 testing mode
346        if self.owasp_api_top10 {
347            return self.execute_owasp_test(&parser).await;
348        }
349
350        // Get operations
351        TerminalReporter::print_progress("Extracting API operations...");
352        let mut operations = if let Some(filter) = &self.operations {
353            parser.filter_operations(filter)?
354        } else {
355            parser.get_operations()
356        };
357
358        // Apply exclusions if provided
359        if let Some(exclude) = &self.exclude_operations {
360            let before_count = operations.len();
361            operations = parser.exclude_operations(operations, exclude)?;
362            let excluded_count = before_count - operations.len();
363            if excluded_count > 0 {
364                TerminalReporter::print_progress(&format!(
365                    "Excluded {} operations matching '{}'",
366                    excluded_count, exclude
367                ));
368            }
369        }
370
371        if operations.is_empty() {
372            return Err(BenchError::Other("No operations found in spec".to_string()));
373        }
374
375        TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
376
377        // Load parameter overrides if provided
378        let param_overrides = if let Some(params_file) = &self.params_file {
379            TerminalReporter::print_progress("Loading parameter overrides...");
380            let overrides = ParameterOverrides::from_file(params_file)?;
381            TerminalReporter::print_success(&format!(
382                "Loaded parameter overrides ({} operation-specific, {} defaults)",
383                overrides.operations.len(),
384                if overrides.defaults.is_empty() { 0 } else { 1 }
385            ));
386            Some(overrides)
387        } else {
388            None
389        };
390
391        // Generate request templates
392        TerminalReporter::print_progress("Generating request templates...");
393        let templates: Vec<_> = operations
394            .iter()
395            .map(|op| {
396                let op_overrides = param_overrides.as_ref().map(|po| {
397                    po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
398                });
399                RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
400            })
401            .collect::<Result<Vec<_>>>()?;
402        TerminalReporter::print_success("Request templates generated");
403
404        // Parse headers
405        let custom_headers = self.parse_headers()?;
406
407        // Resolve base path (CLI option takes priority over spec's servers URL)
408        let base_path = self.resolve_base_path(&parser);
409        if let Some(ref bp) = base_path {
410            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
411        }
412
413        // Generate k6 script
414        TerminalReporter::print_progress("Generating k6 load test script...");
415        let scenario =
416            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
417
418        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
419
420        let k6_config = K6Config {
421            target_url: self.target.clone(),
422            base_path,
423            scenario,
424            duration_secs: Self::parse_duration(&self.duration)?,
425            max_vus: self.vus,
426            threshold_percentile: self.threshold_percentile.clone(),
427            threshold_ms: self.threshold_ms,
428            max_error_rate: self.max_error_rate,
429            auth_header: self.auth.clone(),
430            custom_headers,
431            skip_tls_verify: self.skip_tls_verify,
432            security_testing_enabled,
433            chunked_request_bodies: self.chunked_request_bodies,
434            target_rps: self.target_rps,
435            no_keep_alive: self.no_keep_alive,
436        };
437
438        let generator = K6ScriptGenerator::new(k6_config, templates);
439        let mut script = generator.generate()?;
440        TerminalReporter::print_success("k6 script generated");
441
442        // Check if any advanced features are enabled
443        let has_advanced_features = self.data_file.is_some()
444            || self.error_rate.is_some()
445            || self.security_test
446            || self.parallel_create.is_some()
447            || self.wafbench_dir.is_some();
448
449        // Enhance script with advanced features
450        if has_advanced_features {
451            script = self.generate_enhanced_script(&script)?;
452        }
453
454        // Add mock server integration code
455        if mock_config.is_mock_server {
456            let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
457            let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
458            let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
459
460            // Insert mock server code after imports
461            if let Some(import_end) = script.find("export const options") {
462                script.insert_str(
463                    import_end,
464                    &format!(
465                        "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
466                        helper_code, setup_code, teardown_code
467                    ),
468                );
469            }
470        }
471
472        // Validate the generated script
473        TerminalReporter::print_progress("Validating k6 script...");
474        let validation_errors = K6ScriptGenerator::validate_script(&script);
475        if !validation_errors.is_empty() {
476            TerminalReporter::print_error("Script validation failed");
477            for error in &validation_errors {
478                eprintln!("  {}", error);
479            }
480            return Err(BenchError::Other(format!(
481                "Generated k6 script has {} validation error(s). Please check the output above.",
482                validation_errors.len()
483            )));
484        }
485        TerminalReporter::print_success("Script validation passed");
486
487        // Write script to file
488        let script_path = if let Some(output) = &self.script_output {
489            output.clone()
490        } else {
491            self.output.join("k6-script.js")
492        };
493
494        if let Some(parent) = script_path.parent() {
495            std::fs::create_dir_all(parent)?;
496        }
497        std::fs::write(&script_path, &script)?;
498        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
499
500        // If generate-only mode, exit here
501        if self.generate_only {
502            println!("\nScript generated successfully. Run it with:");
503            println!("  k6 run {}", script_path.display());
504            return Ok(());
505        }
506
507        // Execute k6
508        TerminalReporter::print_progress("Executing load test...");
509        let executor = K6Executor::new()?;
510
511        std::fs::create_dir_all(&self.output)?;
512
513        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
514
515        // Print results
516        let duration_secs = Self::parse_duration(&self.duration)?;
517        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
518
519        println!("\nResults saved to: {}", self.output.display());
520
521        Ok(())
522    }
523
524    /// Execute multi-target bench testing
525    async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
526        TerminalReporter::print_progress("Parsing targets file...");
527        let targets = parse_targets_file(targets_file)?;
528        let num_targets = targets.len();
529        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
530
531        if targets.is_empty() {
532            return Err(BenchError::Other("No targets found in file".to_string()));
533        }
534
535        // Determine max concurrency
536        let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
537        let max_concurrency = max_concurrency.min(num_targets); // Don't exceed number of targets
538
539        // Print header for multi-target mode
540        TerminalReporter::print_header(
541            &self.get_spec_display_name(),
542            &format!("{} targets", num_targets),
543            0,
544            &self.scenario,
545            Self::parse_duration(&self.duration)?,
546        );
547
548        // Create parallel executor
549        let executor = ParallelExecutor::new(
550            BenchCommand {
551                // Clone all fields except targets_file (we don't need it in the executor)
552                spec: self.spec.clone(),
553                spec_dir: self.spec_dir.clone(),
554                merge_conflicts: self.merge_conflicts.clone(),
555                spec_mode: self.spec_mode.clone(),
556                dependency_config: self.dependency_config.clone(),
557                target: self.target.clone(), // Not used in multi-target mode, but kept for compatibility
558                base_path: self.base_path.clone(),
559                duration: self.duration.clone(),
560                vus: self.vus,
561                target_rps: self.target_rps,
562                no_keep_alive: self.no_keep_alive,
563                scenario: self.scenario.clone(),
564                operations: self.operations.clone(),
565                exclude_operations: self.exclude_operations.clone(),
566                auth: self.auth.clone(),
567                headers: self.headers.clone(),
568                output: self.output.clone(),
569                generate_only: self.generate_only,
570                script_output: self.script_output.clone(),
571                threshold_percentile: self.threshold_percentile.clone(),
572                threshold_ms: self.threshold_ms,
573                max_error_rate: self.max_error_rate,
574                verbose: self.verbose,
575                skip_tls_verify: self.skip_tls_verify,
576                chunked_request_bodies: self.chunked_request_bodies,
577                targets_file: None,
578                max_concurrency: None,
579                results_format: self.results_format.clone(),
580                params_file: self.params_file.clone(),
581                crud_flow: self.crud_flow,
582                flow_config: self.flow_config.clone(),
583                extract_fields: self.extract_fields.clone(),
584                parallel_create: self.parallel_create,
585                data_file: self.data_file.clone(),
586                data_distribution: self.data_distribution.clone(),
587                data_mappings: self.data_mappings.clone(),
588                per_uri_control: self.per_uri_control,
589                error_rate: self.error_rate,
590                error_types: self.error_types.clone(),
591                security_test: self.security_test,
592                security_payloads: self.security_payloads.clone(),
593                security_categories: self.security_categories.clone(),
594                security_target_fields: self.security_target_fields.clone(),
595                wafbench_dir: self.wafbench_dir.clone(),
596                wafbench_cycle_all: self.wafbench_cycle_all,
597                owasp_api_top10: self.owasp_api_top10,
598                owasp_categories: self.owasp_categories.clone(),
599                owasp_auth_header: self.owasp_auth_header.clone(),
600                owasp_auth_token: self.owasp_auth_token.clone(),
601                owasp_admin_paths: self.owasp_admin_paths.clone(),
602                owasp_id_fields: self.owasp_id_fields.clone(),
603                owasp_report: self.owasp_report.clone(),
604                owasp_report_format: self.owasp_report_format.clone(),
605                owasp_iterations: self.owasp_iterations,
606                conformance: false,
607                conformance_api_key: None,
608                conformance_basic_auth: None,
609                conformance_report: PathBuf::from("conformance-report.json"),
610                conformance_categories: None,
611                conformance_report_format: "json".to_string(),
612                conformance_headers: vec![],
613                conformance_all_operations: false,
614                conformance_custom: None,
615                conformance_delay_ms: 0,
616                use_k6: false,
617                conformance_custom_filter: None,
618                export_requests: false,
619                validate_requests: false,
620            },
621            targets,
622            max_concurrency,
623        );
624
625        // Execute all targets
626        let start_time = std::time::Instant::now();
627        let aggregated_results = executor.execute_all().await?;
628        let elapsed = start_time.elapsed();
629
630        // Organize and report results
631        self.report_multi_target_results(&aggregated_results, elapsed)?;
632
633        Ok(())
634    }
635
636    /// Report results for multi-target execution
637    fn report_multi_target_results(
638        &self,
639        results: &AggregatedResults,
640        elapsed: std::time::Duration,
641    ) -> Result<()> {
642        // Print summary
643        TerminalReporter::print_multi_target_summary(results);
644
645        // Print elapsed time
646        let total_secs = elapsed.as_secs();
647        let hours = total_secs / 3600;
648        let minutes = (total_secs % 3600) / 60;
649        let seconds = total_secs % 60;
650        if hours > 0 {
651            println!("\n  Total Elapsed Time:   {}h {}m {}s", hours, minutes, seconds);
652        } else if minutes > 0 {
653            println!("\n  Total Elapsed Time:   {}m {}s", minutes, seconds);
654        } else {
655            println!("\n  Total Elapsed Time:   {}s", seconds);
656        }
657
658        // Save aggregated summary if requested
659        if self.results_format == "aggregated" || self.results_format == "both" {
660            let summary_path = self.output.join("aggregated_summary.json");
661            let summary_json = serde_json::json!({
662                "total_elapsed_seconds": elapsed.as_secs(),
663                "total_targets": results.total_targets,
664                "successful_targets": results.successful_targets,
665                "failed_targets": results.failed_targets,
666                "aggregated_metrics": {
667                    "total_requests": results.aggregated_metrics.total_requests,
668                    "total_failed_requests": results.aggregated_metrics.total_failed_requests,
669                    "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
670                    "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
671                    "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
672                    "error_rate": results.aggregated_metrics.error_rate,
673                    "total_rps": results.aggregated_metrics.total_rps,
674                    "avg_rps": results.aggregated_metrics.avg_rps,
675                    "total_vus_max": results.aggregated_metrics.total_vus_max,
676                },
677                "target_results": results.target_results.iter().map(|r| {
678                    serde_json::json!({
679                        "target_url": r.target_url,
680                        "target_index": r.target_index,
681                        "success": r.success,
682                        "error": r.error,
683                        "total_requests": r.results.total_requests,
684                        "failed_requests": r.results.failed_requests,
685                        "avg_duration_ms": r.results.avg_duration_ms,
686                        "min_duration_ms": r.results.min_duration_ms,
687                        "med_duration_ms": r.results.med_duration_ms,
688                        "p90_duration_ms": r.results.p90_duration_ms,
689                        "p95_duration_ms": r.results.p95_duration_ms,
690                        "p99_duration_ms": r.results.p99_duration_ms,
691                        "max_duration_ms": r.results.max_duration_ms,
692                        "rps": r.results.rps,
693                        "vus_max": r.results.vus_max,
694                        "output_dir": r.output_dir.to_string_lossy(),
695                    })
696                }).collect::<Vec<_>>(),
697            });
698
699            std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
700            TerminalReporter::print_success(&format!(
701                "Aggregated summary saved to: {}",
702                summary_path.display()
703            ));
704        }
705
706        // Write CSV with all per-target results for easy parsing
707        let csv_path = self.output.join("all_targets.csv");
708        let mut csv = String::from(
709            "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
710        );
711        for r in &results.target_results {
712            csv.push_str(&format!(
713                "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
714                r.target_url,
715                r.success,
716                r.results.total_requests,
717                r.results.failed_requests,
718                r.results.rps,
719                r.results.vus_max,
720                r.results.min_duration_ms,
721                r.results.avg_duration_ms,
722                r.results.med_duration_ms,
723                r.results.p90_duration_ms,
724                r.results.p95_duration_ms,
725                r.results.p99_duration_ms,
726                r.results.max_duration_ms,
727                r.error.as_deref().unwrap_or(""),
728            ));
729        }
730        let _ = std::fs::write(&csv_path, &csv);
731
732        println!("\nResults saved to: {}", self.output.display());
733        println!("  - Per-target results: {}", self.output.join("target_*").display());
734        println!("  - All targets CSV:    {}", csv_path.display());
735        if self.results_format == "aggregated" || self.results_format == "both" {
736            println!(
737                "  - Aggregated summary: {}",
738                self.output.join("aggregated_summary.json").display()
739            );
740        }
741
742        Ok(())
743    }
744
745    /// Parse duration string (e.g., "30s", "5m", "1h") to seconds
746    pub fn parse_duration(duration: &str) -> Result<u64> {
747        let duration = duration.trim();
748
749        if let Some(secs) = duration.strip_suffix('s') {
750            secs.parse::<u64>()
751                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
752        } else if let Some(mins) = duration.strip_suffix('m') {
753            mins.parse::<u64>()
754                .map(|m| m * 60)
755                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
756        } else if let Some(hours) = duration.strip_suffix('h') {
757            hours
758                .parse::<u64>()
759                .map(|h| h * 3600)
760                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
761        } else {
762            // Try parsing as seconds without suffix
763            duration
764                .parse::<u64>()
765                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
766        }
767    }
768
769    /// Parse headers from command line format (Key:Value,Key2:Value2)
770    pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
771        match &self.headers {
772            Some(s) => parse_header_string(s),
773            None => Ok(HashMap::new()),
774        }
775    }
776
777    fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
778        let extracted_path = output_dir.join("extracted_values.json");
779        if !extracted_path.exists() {
780            return Ok(ExtractedValues::new());
781        }
782
783        let content = std::fs::read_to_string(&extracted_path)
784            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
785        let parsed: serde_json::Value = serde_json::from_str(&content)
786            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
787
788        let mut extracted = ExtractedValues::new();
789        if let Some(values) = parsed.as_object() {
790            for (key, value) in values {
791                extracted.set(key.clone(), value.clone());
792            }
793        }
794
795        Ok(extracted)
796    }
797
798    /// Resolve the effective base path for API endpoints
799    ///
800    /// Priority:
801    /// 1. CLI --base-path option (if provided, even if empty string)
802    /// 2. Base path extracted from OpenAPI spec's servers URL
803    /// 3. None (no base path)
804    ///
805    /// An empty string from CLI explicitly disables base path.
806    fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
807        // CLI option takes priority (including empty string to disable)
808        if let Some(cli_base_path) = &self.base_path {
809            if cli_base_path.is_empty() {
810                // Empty string explicitly means "no base path"
811                return None;
812            }
813            return Some(cli_base_path.clone());
814        }
815
816        // Fall back to spec's base path
817        parser.get_base_path()
818    }
819
820    /// Build mock server integration configuration
821    async fn build_mock_config(&self) -> MockIntegrationConfig {
822        // Check if target looks like a mock server
823        if MockServerDetector::looks_like_mock_server(&self.target) {
824            // Try to detect if it's actually a MockForge server
825            if let Ok(info) = MockServerDetector::detect(&self.target).await {
826                if info.is_mockforge {
827                    TerminalReporter::print_success(&format!(
828                        "Detected MockForge server (version: {})",
829                        info.version.as_deref().unwrap_or("unknown")
830                    ));
831                    return MockIntegrationConfig::mock_server();
832                }
833            }
834        }
835        MockIntegrationConfig::real_api()
836    }
837
838    /// Build CRUD flow configuration
839    fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
840        if !self.crud_flow {
841            return None;
842        }
843
844        // If flow_config file is provided, load it
845        if let Some(config_path) = &self.flow_config {
846            match CrudFlowConfig::from_file(config_path) {
847                Ok(config) => return Some(config),
848                Err(e) => {
849                    TerminalReporter::print_warning(&format!(
850                        "Failed to load flow config: {}. Using auto-detection.",
851                        e
852                    ));
853                }
854            }
855        }
856
857        // Parse extract fields
858        let extract_fields = self
859            .extract_fields
860            .as_ref()
861            .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
862            .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
863
864        Some(CrudFlowConfig {
865            flows: Vec::new(), // Will be auto-detected
866            default_extract_fields: extract_fields,
867        })
868    }
869
870    /// Build data-driven testing configuration
871    fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
872        let data_file = self.data_file.as_ref()?;
873
874        let distribution = DataDistribution::from_str(&self.data_distribution)
875            .unwrap_or(DataDistribution::UniquePerVu);
876
877        let mappings = self
878            .data_mappings
879            .as_ref()
880            .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
881            .unwrap_or_default();
882
883        Some(DataDrivenConfig {
884            file_path: data_file.to_string_lossy().to_string(),
885            distribution,
886            mappings,
887            csv_has_header: true,
888            per_uri_control: self.per_uri_control,
889            per_uri_columns: crate::data_driven::PerUriColumns::default(),
890        })
891    }
892
893    /// Build invalid data testing configuration
894    fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
895        let error_rate = self.error_rate?;
896
897        let error_types = self
898            .error_types
899            .as_ref()
900            .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
901            .unwrap_or_default();
902
903        Some(InvalidDataConfig {
904            error_rate,
905            error_types,
906            target_fields: Vec::new(),
907        })
908    }
909
910    /// Build security testing configuration
911    fn build_security_config(&self) -> Option<SecurityTestConfig> {
912        if !self.security_test {
913            return None;
914        }
915
916        let categories = self
917            .security_categories
918            .as_ref()
919            .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
920            .unwrap_or_else(|| {
921                let mut default = HashSet::new();
922                default.insert(SecurityCategory::SqlInjection);
923                default.insert(SecurityCategory::Xss);
924                default
925            });
926
927        let target_fields = self
928            .security_target_fields
929            .as_ref()
930            .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
931            .unwrap_or_default();
932
933        let custom_payloads_file =
934            self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
935
936        Some(SecurityTestConfig {
937            enabled: true,
938            categories,
939            target_fields,
940            custom_payloads_file,
941            include_high_risk: false,
942        })
943    }
944
945    /// Build parallel execution configuration
946    fn build_parallel_config(&self) -> Option<ParallelConfig> {
947        let count = self.parallel_create?;
948
949        Some(ParallelConfig::new(count))
950    }
951
952    /// Load WAFBench payloads from the specified directory or pattern
953    fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
954        let Some(ref wafbench_dir) = self.wafbench_dir else {
955            return Vec::new();
956        };
957
958        let mut loader = WafBenchLoader::new();
959
960        if let Err(e) = loader.load_from_pattern(wafbench_dir) {
961            TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
962            return Vec::new();
963        }
964
965        let stats = loader.stats();
966
967        if stats.files_processed == 0 {
968            TerminalReporter::print_warning(&format!(
969                "No WAFBench YAML files found matching '{}'",
970                wafbench_dir
971            ));
972            // Also report any parse errors that may explain why no files were processed
973            if !stats.parse_errors.is_empty() {
974                TerminalReporter::print_warning("Some files were found but failed to parse:");
975                for error in &stats.parse_errors {
976                    TerminalReporter::print_warning(&format!("  - {}", error));
977                }
978            }
979            return Vec::new();
980        }
981
982        TerminalReporter::print_progress(&format!(
983            "Loaded {} WAFBench files, {} test cases, {} payloads",
984            stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
985        ));
986
987        // Print category breakdown
988        for (category, count) in &stats.by_category {
989            TerminalReporter::print_progress(&format!("  - {}: {} tests", category, count));
990        }
991
992        // Report any parse errors
993        for error in &stats.parse_errors {
994            TerminalReporter::print_warning(&format!("  Parse error: {}", error));
995        }
996
997        loader.to_security_payloads()
998    }
999
1000    /// Generate enhanced k6 script with advanced features
1001    pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1002        let mut enhanced_script = base_script.to_string();
1003        let mut additional_code = String::new();
1004
1005        // Add data-driven testing code
1006        if let Some(config) = self.build_data_driven_config() {
1007            TerminalReporter::print_progress("Adding data-driven testing support...");
1008            additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1009            additional_code.push('\n');
1010            TerminalReporter::print_success("Data-driven testing enabled");
1011        }
1012
1013        // Add invalid data generation code
1014        if let Some(config) = self.build_invalid_data_config() {
1015            TerminalReporter::print_progress("Adding invalid data testing support...");
1016            additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1017            additional_code.push('\n');
1018            additional_code
1019                .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1020            additional_code.push('\n');
1021            additional_code
1022                .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1023            additional_code.push('\n');
1024            TerminalReporter::print_success(&format!(
1025                "Invalid data testing enabled ({}% error rate)",
1026                (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1027            ));
1028        }
1029
1030        // Add security testing code
1031        let security_config = self.build_security_config();
1032        let wafbench_payloads = self.load_wafbench_payloads();
1033        let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1034
1035        if security_config.is_some() || !wafbench_payloads.is_empty() {
1036            TerminalReporter::print_progress("Adding security testing support...");
1037
1038            // Combine built-in payloads with WAFBench payloads
1039            let mut payload_list: Vec<SecurityPayload> = Vec::new();
1040
1041            if let Some(ref config) = security_config {
1042                payload_list.extend(SecurityPayloads::get_payloads(config));
1043            }
1044
1045            // Add WAFBench payloads
1046            if !wafbench_payloads.is_empty() {
1047                TerminalReporter::print_progress(&format!(
1048                    "Loading {} WAFBench attack patterns...",
1049                    wafbench_payloads.len()
1050                ));
1051                payload_list.extend(wafbench_payloads);
1052            }
1053
1054            let target_fields =
1055                security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1056
1057            additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1058                &payload_list,
1059                self.wafbench_cycle_all,
1060            ));
1061            additional_code.push('\n');
1062            additional_code
1063                .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1064            additional_code.push('\n');
1065            additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1066            additional_code.push('\n');
1067
1068            let mode = if self.wafbench_cycle_all {
1069                "cycle-all"
1070            } else {
1071                "random"
1072            };
1073            TerminalReporter::print_success(&format!(
1074                "Security testing enabled ({} payloads, {} mode)",
1075                payload_list.len(),
1076                mode
1077            ));
1078        } else if security_requested {
1079            // User requested security testing (e.g., --wafbench-dir) but no payloads were loaded.
1080            // The template has security_testing_enabled=true so it renders calling code.
1081            // We must inject stub definitions to avoid undefined function references.
1082            TerminalReporter::print_warning(
1083                "Security testing was requested but no payloads were loaded. \
1084                 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1085            );
1086            additional_code
1087                .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1088            additional_code.push('\n');
1089            additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1090            additional_code.push('\n');
1091        }
1092
1093        // Add parallel execution code
1094        if let Some(config) = self.build_parallel_config() {
1095            TerminalReporter::print_progress("Adding parallel execution support...");
1096            additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1097            additional_code.push('\n');
1098            TerminalReporter::print_success(&format!(
1099                "Parallel execution enabled (count: {})",
1100                config.count
1101            ));
1102        }
1103
1104        // Insert additional code after the imports section
1105        if !additional_code.is_empty() {
1106            // Find the end of the import section
1107            if let Some(import_end) = enhanced_script.find("export const options") {
1108                enhanced_script.insert_str(
1109                    import_end,
1110                    &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1111                );
1112            }
1113        }
1114
1115        Ok(enhanced_script)
1116    }
1117
1118    /// Execute specs sequentially with dependency ordering and value passing
1119    async fn execute_sequential_specs(&self) -> Result<()> {
1120        TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1121
1122        // Load all specs (without merging)
1123        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1124
1125        if !self.spec.is_empty() {
1126            let specs = load_specs_from_files(self.spec.clone())
1127                .await
1128                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1129            all_specs.extend(specs);
1130        }
1131
1132        if let Some(spec_dir) = &self.spec_dir {
1133            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1134                BenchError::Other(format!("Failed to load specs from directory: {}", e))
1135            })?;
1136            all_specs.extend(dir_specs);
1137        }
1138
1139        if all_specs.is_empty() {
1140            return Err(BenchError::Other(
1141                "No spec files found for sequential execution".to_string(),
1142            ));
1143        }
1144
1145        TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1146
1147        // Load dependency config or auto-detect
1148        let execution_order = if let Some(config_path) = &self.dependency_config {
1149            TerminalReporter::print_progress("Loading dependency configuration...");
1150            let config = SpecDependencyConfig::from_file(config_path)?;
1151
1152            if !config.disable_auto_detect && config.execution_order.is_empty() {
1153                // Auto-detect if config doesn't specify order
1154                self.detect_and_sort_specs(&all_specs)?
1155            } else {
1156                // Use configured order
1157                config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1158            }
1159        } else {
1160            // Auto-detect dependencies
1161            self.detect_and_sort_specs(&all_specs)?
1162        };
1163
1164        TerminalReporter::print_success(&format!(
1165            "Execution order: {}",
1166            execution_order
1167                .iter()
1168                .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1169                .collect::<Vec<_>>()
1170                .join(" → ")
1171        ));
1172
1173        // Execute each spec in order
1174        let mut extracted_values = ExtractedValues::new();
1175        let total_specs = execution_order.len();
1176
1177        for (index, spec_path) in execution_order.iter().enumerate() {
1178            let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1179
1180            TerminalReporter::print_progress(&format!(
1181                "[{}/{}] Executing spec: {}",
1182                index + 1,
1183                total_specs,
1184                spec_name
1185            ));
1186
1187            // Find the spec in our loaded specs (match by full path or filename)
1188            let spec = all_specs
1189                .iter()
1190                .find(|(p, _)| {
1191                    p == spec_path
1192                        || p.file_name() == spec_path.file_name()
1193                        || p.file_name() == Some(spec_path.as_os_str())
1194                })
1195                .map(|(_, s)| s.clone())
1196                .ok_or_else(|| {
1197                    BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1198                })?;
1199
1200            // Execute this spec with any extracted values from previous specs
1201            let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1202
1203            // Merge extracted values for the next spec
1204            extracted_values.merge(&new_values);
1205
1206            TerminalReporter::print_success(&format!(
1207                "[{}/{}] Completed: {} (extracted {} values)",
1208                index + 1,
1209                total_specs,
1210                spec_name,
1211                new_values.values.len()
1212            ));
1213        }
1214
1215        TerminalReporter::print_success(&format!(
1216            "Sequential execution complete: {} specs executed",
1217            total_specs
1218        ));
1219
1220        Ok(())
1221    }
1222
1223    /// Detect dependencies and return topologically sorted spec paths
1224    fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1225        TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1226
1227        let mut detector = DependencyDetector::new();
1228        let dependencies = detector.detect_dependencies(specs);
1229
1230        if dependencies.is_empty() {
1231            TerminalReporter::print_progress("No dependencies detected, using file order");
1232            return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1233        }
1234
1235        TerminalReporter::print_progress(&format!(
1236            "Detected {} cross-spec dependencies",
1237            dependencies.len()
1238        ));
1239
1240        for dep in &dependencies {
1241            TerminalReporter::print_progress(&format!(
1242                "  {} → {} (via field '{}')",
1243                dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1244                dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1245                dep.field_name
1246            ));
1247        }
1248
1249        topological_sort(specs, &dependencies)
1250    }
1251
1252    /// Execute a single spec and extract values for dependent specs
1253    async fn execute_single_spec(
1254        &self,
1255        spec: &OpenApiSpec,
1256        spec_name: &str,
1257        _external_values: &ExtractedValues,
1258    ) -> Result<ExtractedValues> {
1259        let parser = SpecParser::from_spec(spec.clone());
1260
1261        // For now, we execute in CRUD flow mode if enabled, otherwise standard mode
1262        if self.crud_flow {
1263            // Execute CRUD flow and extract values
1264            self.execute_crud_flow_with_extraction(&parser, spec_name).await
1265        } else {
1266            // Execute standard benchmark (no value extraction in non-CRUD mode)
1267            self.execute_standard_spec(&parser, spec_name).await?;
1268            Ok(ExtractedValues::new())
1269        }
1270    }
1271
1272    /// Execute CRUD flow with value extraction for sequential mode
1273    async fn execute_crud_flow_with_extraction(
1274        &self,
1275        parser: &SpecParser,
1276        spec_name: &str,
1277    ) -> Result<ExtractedValues> {
1278        let operations = parser.get_operations();
1279        let flows = CrudFlowDetector::detect_flows(&operations);
1280
1281        if flows.is_empty() {
1282            TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1283            return Ok(ExtractedValues::new());
1284        }
1285
1286        TerminalReporter::print_progress(&format!(
1287            "  {} CRUD flow(s) in {}",
1288            flows.len(),
1289            spec_name
1290        ));
1291
1292        // Generate and execute the CRUD flow script
1293        let mut handlebars = handlebars::Handlebars::new();
1294        // Register json helper for serializing arrays/objects in templates
1295        handlebars.register_helper(
1296            "json",
1297            Box::new(
1298                |h: &handlebars::Helper,
1299                 _: &handlebars::Handlebars,
1300                 _: &handlebars::Context,
1301                 _: &mut handlebars::RenderContext,
1302                 out: &mut dyn handlebars::Output|
1303                 -> handlebars::HelperResult {
1304                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1305                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1306                    Ok(())
1307                },
1308            ),
1309        );
1310        let template = include_str!("templates/k6_crud_flow.hbs");
1311        let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1312
1313        let custom_headers = self.parse_headers()?;
1314        let config = self.build_crud_flow_config().unwrap_or_default();
1315
1316        // Load parameter overrides if provided (for body configurations)
1317        let param_overrides = if let Some(params_file) = &self.params_file {
1318            let overrides = ParameterOverrides::from_file(params_file)?;
1319            Some(overrides)
1320        } else {
1321            None
1322        };
1323
1324        // Generate stages from scenario
1325        let duration_secs = Self::parse_duration(&self.duration)?;
1326        let scenario =
1327            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1328        let stages = scenario.generate_stages(duration_secs, self.vus);
1329
1330        // Resolve base path (CLI option takes priority over spec's servers URL)
1331        let api_base_path = self.resolve_base_path(parser);
1332
1333        // Build headers JSON string for the template
1334        let mut all_headers = custom_headers.clone();
1335        if let Some(auth) = &self.auth {
1336            all_headers.insert("Authorization".to_string(), auth.clone());
1337        }
1338        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1339
1340        // Track all dynamic placeholders across all operations
1341        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1342
1343        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1344            // Use the metric-name sanitizer (caps at 112 chars + hash suffix)
1345            // so deeply nested flow names don't blow past k6's 128-char limit
1346            // when concatenated with `_step{i}_latency`. See issue #79.
1347            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1348            serde_json::json!({
1349                "name": sanitized_name.clone(),
1350                "display_name": f.name,
1351                "base_path": f.base_path,
1352                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1353                    // Parse operation to get method and path
1354                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1355                    let method_raw = if !parts.is_empty() {
1356                        parts[0].to_uppercase()
1357                    } else {
1358                        "GET".to_string()
1359                    };
1360                    let method = if !parts.is_empty() {
1361                        let m = parts[0].to_lowercase();
1362                        // k6 uses 'del' for DELETE
1363                        if m == "delete" { "del".to_string() } else { m }
1364                    } else {
1365                        "get".to_string()
1366                    };
1367                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1368                    // Prepend API base path if configured
1369                    let path = if let Some(ref bp) = api_base_path {
1370                        format!("{}{}", bp, raw_path)
1371                    } else {
1372                        raw_path.to_string()
1373                    };
1374                    let is_get_or_head = method == "get" || method == "head";
1375                    // POST, PUT, PATCH typically have bodies
1376                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1377
1378                    // Look up body from params file if available
1379                    let body_value = if has_body {
1380                        param_overrides.as_ref()
1381                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1382                            .and_then(|oo| oo.body)
1383                            .unwrap_or_else(|| serde_json::json!({}))
1384                    } else {
1385                        serde_json::json!({})
1386                    };
1387
1388                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1389                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1390
1391                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1392                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1393                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1394
1395                    serde_json::json!({
1396                        "operation": s.operation,
1397                        "method": method,
1398                        "path": path,
1399                        "extract": s.extract,
1400                        "use_values": s.use_values,
1401                        "use_body": s.use_body,
1402                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1403                        "inject_attacks": s.inject_attacks,
1404                        "attack_types": s.attack_types,
1405                        "description": s.description,
1406                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1407                        "is_get_or_head": is_get_or_head,
1408                        "has_body": has_body,
1409                        "body": processed_body.value,
1410                        "body_is_dynamic": body_is_dynamic,
1411                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1412                    })
1413                }).collect::<Vec<_>>(),
1414            })
1415        }).collect();
1416
1417        // Collect all placeholders from all steps
1418        for flow_data in &flows_data {
1419            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1420                for step in steps {
1421                    if let Some(placeholders_arr) =
1422                        step.get("_placeholders").and_then(|p| p.as_array())
1423                    {
1424                        for p_str in placeholders_arr {
1425                            if let Some(p_name) = p_str.as_str() {
1426                                match p_name {
1427                                    "VU" => {
1428                                        all_placeholders.insert(DynamicPlaceholder::VU);
1429                                    }
1430                                    "Iteration" => {
1431                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1432                                    }
1433                                    "Timestamp" => {
1434                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1435                                    }
1436                                    "UUID" => {
1437                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1438                                    }
1439                                    "Random" => {
1440                                        all_placeholders.insert(DynamicPlaceholder::Random);
1441                                    }
1442                                    "Counter" => {
1443                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1444                                    }
1445                                    "Date" => {
1446                                        all_placeholders.insert(DynamicPlaceholder::Date);
1447                                    }
1448                                    "VuIter" => {
1449                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1450                                    }
1451                                    _ => {}
1452                                }
1453                            }
1454                        }
1455                    }
1456                }
1457            }
1458        }
1459
1460        // Get required imports and globals based on placeholders used
1461        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1462        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1463
1464        // Check if security testing is enabled
1465        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1466
1467        let data = serde_json::json!({
1468            "base_url": self.target,
1469            "flows": flows_data,
1470            "extract_fields": config.default_extract_fields,
1471            "duration_secs": duration_secs,
1472            "max_vus": self.vus,
1473            "auth_header": self.auth,
1474            "custom_headers": custom_headers,
1475            "skip_tls_verify": self.skip_tls_verify,
1476            // Add missing template fields
1477            "stages": stages.iter().map(|s| serde_json::json!({
1478                "duration": s.duration,
1479                "target": s.target,
1480            })).collect::<Vec<_>>(),
1481            "threshold_percentile": self.threshold_percentile,
1482            "threshold_ms": self.threshold_ms,
1483            "max_error_rate": self.max_error_rate,
1484            "headers": headers_json,
1485            "dynamic_imports": required_imports,
1486            "dynamic_globals": required_globals,
1487            "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1488            // Security testing settings
1489            "security_testing_enabled": security_testing_enabled,
1490            "has_custom_headers": !custom_headers.is_empty(),
1491        });
1492
1493        let mut script = handlebars
1494            .render_template(template, &data)
1495            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1496
1497        // Enhance script with security testing support if enabled
1498        if security_testing_enabled {
1499            script = self.generate_enhanced_script(&script)?;
1500        }
1501
1502        // Write and execute script
1503        let script_path =
1504            self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1505
1506        std::fs::create_dir_all(self.output.clone())?;
1507        std::fs::write(&script_path, &script)?;
1508
1509        if !self.generate_only {
1510            let executor = K6Executor::new()?;
1511            std::fs::create_dir_all(&output_dir)?;
1512
1513            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1514
1515            let extracted = Self::parse_extracted_values(&output_dir)?;
1516            TerminalReporter::print_progress(&format!(
1517                "  Extracted {} value(s) from {}",
1518                extracted.values.len(),
1519                spec_name
1520            ));
1521            return Ok(extracted);
1522        }
1523
1524        Ok(ExtractedValues::new())
1525    }
1526
1527    /// Execute standard (non-CRUD) spec benchmark
1528    async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1529        let mut operations = if let Some(filter) = &self.operations {
1530            parser.filter_operations(filter)?
1531        } else {
1532            parser.get_operations()
1533        };
1534
1535        if let Some(exclude) = &self.exclude_operations {
1536            operations = parser.exclude_operations(operations, exclude)?;
1537        }
1538
1539        if operations.is_empty() {
1540            TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1541            return Ok(());
1542        }
1543
1544        TerminalReporter::print_progress(&format!(
1545            "  {} operations in {}",
1546            operations.len(),
1547            spec_name
1548        ));
1549
1550        // Generate request templates
1551        let templates: Vec<_> = operations
1552            .iter()
1553            .map(RequestGenerator::generate_template)
1554            .collect::<Result<Vec<_>>>()?;
1555
1556        // Parse headers
1557        let custom_headers = self.parse_headers()?;
1558
1559        // Resolve base path
1560        let base_path = self.resolve_base_path(parser);
1561
1562        // Generate k6 script
1563        let scenario =
1564            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1565
1566        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1567
1568        let k6_config = K6Config {
1569            target_url: self.target.clone(),
1570            base_path,
1571            scenario,
1572            duration_secs: Self::parse_duration(&self.duration)?,
1573            max_vus: self.vus,
1574            threshold_percentile: self.threshold_percentile.clone(),
1575            threshold_ms: self.threshold_ms,
1576            max_error_rate: self.max_error_rate,
1577            auth_header: self.auth.clone(),
1578            custom_headers,
1579            skip_tls_verify: self.skip_tls_verify,
1580            security_testing_enabled,
1581            chunked_request_bodies: self.chunked_request_bodies,
1582            target_rps: self.target_rps,
1583            no_keep_alive: self.no_keep_alive,
1584        };
1585
1586        let generator = K6ScriptGenerator::new(k6_config, templates);
1587        let mut script = generator.generate()?;
1588
1589        // Enhance script with advanced features (security testing, etc.)
1590        let has_advanced_features = self.data_file.is_some()
1591            || self.error_rate.is_some()
1592            || self.security_test
1593            || self.parallel_create.is_some()
1594            || self.wafbench_dir.is_some();
1595
1596        if has_advanced_features {
1597            script = self.generate_enhanced_script(&script)?;
1598        }
1599
1600        // Write and execute script
1601        let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1602
1603        std::fs::create_dir_all(self.output.clone())?;
1604        std::fs::write(&script_path, &script)?;
1605
1606        if !self.generate_only {
1607            let executor = K6Executor::new()?;
1608            let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1609            std::fs::create_dir_all(&output_dir)?;
1610
1611            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1612        }
1613
1614        Ok(())
1615    }
1616
1617    /// Execute CRUD flow testing mode
1618    async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1619        // Check if a custom flow config is provided
1620        let config = self.build_crud_flow_config().unwrap_or_default();
1621
1622        // Use flows from config if provided, otherwise auto-detect
1623        let flows = if !config.flows.is_empty() {
1624            TerminalReporter::print_progress("Using custom flow configuration...");
1625            config.flows.clone()
1626        } else {
1627            TerminalReporter::print_progress("Detecting CRUD operations...");
1628            let operations = parser.get_operations();
1629            CrudFlowDetector::detect_flows(&operations)
1630        };
1631
1632        if flows.is_empty() {
1633            return Err(BenchError::Other(
1634                "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1635            ));
1636        }
1637
1638        if config.flows.is_empty() {
1639            TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1640        } else {
1641            TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1642        }
1643
1644        for flow in &flows {
1645            TerminalReporter::print_progress(&format!(
1646                "  - {}: {} steps",
1647                flow.name,
1648                flow.steps.len()
1649            ));
1650        }
1651
1652        // Generate CRUD flow script
1653        let mut handlebars = handlebars::Handlebars::new();
1654        // Register json helper for serializing arrays/objects in templates
1655        handlebars.register_helper(
1656            "json",
1657            Box::new(
1658                |h: &handlebars::Helper,
1659                 _: &handlebars::Handlebars,
1660                 _: &handlebars::Context,
1661                 _: &mut handlebars::RenderContext,
1662                 out: &mut dyn handlebars::Output|
1663                 -> handlebars::HelperResult {
1664                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1665                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1666                    Ok(())
1667                },
1668            ),
1669        );
1670        let template = include_str!("templates/k6_crud_flow.hbs");
1671
1672        let custom_headers = self.parse_headers()?;
1673
1674        // Load parameter overrides if provided (for body configurations)
1675        let param_overrides = if let Some(params_file) = &self.params_file {
1676            TerminalReporter::print_progress("Loading parameter overrides...");
1677            let overrides = ParameterOverrides::from_file(params_file)?;
1678            TerminalReporter::print_success(&format!(
1679                "Loaded parameter overrides ({} operation-specific, {} defaults)",
1680                overrides.operations.len(),
1681                if overrides.defaults.is_empty() { 0 } else { 1 }
1682            ));
1683            Some(overrides)
1684        } else {
1685            None
1686        };
1687
1688        // Generate stages from scenario
1689        let duration_secs = Self::parse_duration(&self.duration)?;
1690        let scenario =
1691            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1692        let stages = scenario.generate_stages(duration_secs, self.vus);
1693
1694        // Resolve base path (CLI option takes priority over spec's servers URL)
1695        let api_base_path = self.resolve_base_path(parser);
1696        if let Some(ref bp) = api_base_path {
1697            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1698        }
1699
1700        // Build headers JSON string for the template
1701        let mut all_headers = custom_headers.clone();
1702        if let Some(auth) = &self.auth {
1703            all_headers.insert("Authorization".to_string(), auth.clone());
1704        }
1705        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1706
1707        // Track all dynamic placeholders across all operations
1708        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1709
1710        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1711            // Sanitize flow name for use as JavaScript variable and k6 metric names.
1712            // Use the metric-name sanitizer (caps at 112 chars + hash suffix) so
1713            // deeply nested flow names don't blow past k6's 128-char limit when
1714            // concatenated with `_step{i}_latency`. See issue #79.
1715            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1716            serde_json::json!({
1717                "name": sanitized_name.clone(),  // Use sanitized name for variable names
1718                "display_name": f.name,          // Keep original for comments/display
1719                "base_path": f.base_path,
1720                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1721                    // Parse operation to get method and path
1722                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1723                    let method_raw = if !parts.is_empty() {
1724                        parts[0].to_uppercase()
1725                    } else {
1726                        "GET".to_string()
1727                    };
1728                    let method = if !parts.is_empty() {
1729                        let m = parts[0].to_lowercase();
1730                        // k6 uses 'del' for DELETE
1731                        if m == "delete" { "del".to_string() } else { m }
1732                    } else {
1733                        "get".to_string()
1734                    };
1735                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1736                    // Prepend API base path if configured
1737                    let path = if let Some(ref bp) = api_base_path {
1738                        format!("{}{}", bp, raw_path)
1739                    } else {
1740                        raw_path.to_string()
1741                    };
1742                    let is_get_or_head = method == "get" || method == "head";
1743                    // POST, PUT, PATCH typically have bodies
1744                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1745
1746                    // Look up body from params file if available (use raw_path for matching)
1747                    let body_value = if has_body {
1748                        param_overrides.as_ref()
1749                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1750                            .and_then(|oo| oo.body)
1751                            .unwrap_or_else(|| serde_json::json!({}))
1752                    } else {
1753                        serde_json::json!({})
1754                    };
1755
1756                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1757                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1758                    // Note: all_placeholders is captured by the closure but we can't mutate it directly
1759                    // We'll collect placeholders separately below
1760
1761                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1762                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1763                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1764
1765                    serde_json::json!({
1766                        "operation": s.operation,
1767                        "method": method,
1768                        "path": path,
1769                        "extract": s.extract,
1770                        "use_values": s.use_values,
1771                        "use_body": s.use_body,
1772                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1773                        "inject_attacks": s.inject_attacks,
1774                        "attack_types": s.attack_types,
1775                        "description": s.description,
1776                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1777                        "is_get_or_head": is_get_or_head,
1778                        "has_body": has_body,
1779                        "body": processed_body.value,
1780                        "body_is_dynamic": body_is_dynamic,
1781                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1782                    })
1783                }).collect::<Vec<_>>(),
1784            })
1785        }).collect();
1786
1787        // Collect all placeholders from all steps
1788        for flow_data in &flows_data {
1789            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1790                for step in steps {
1791                    if let Some(placeholders_arr) =
1792                        step.get("_placeholders").and_then(|p| p.as_array())
1793                    {
1794                        for p_str in placeholders_arr {
1795                            if let Some(p_name) = p_str.as_str() {
1796                                // Parse placeholder from debug string
1797                                match p_name {
1798                                    "VU" => {
1799                                        all_placeholders.insert(DynamicPlaceholder::VU);
1800                                    }
1801                                    "Iteration" => {
1802                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1803                                    }
1804                                    "Timestamp" => {
1805                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1806                                    }
1807                                    "UUID" => {
1808                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1809                                    }
1810                                    "Random" => {
1811                                        all_placeholders.insert(DynamicPlaceholder::Random);
1812                                    }
1813                                    "Counter" => {
1814                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1815                                    }
1816                                    "Date" => {
1817                                        all_placeholders.insert(DynamicPlaceholder::Date);
1818                                    }
1819                                    "VuIter" => {
1820                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1821                                    }
1822                                    _ => {}
1823                                }
1824                            }
1825                        }
1826                    }
1827                }
1828            }
1829        }
1830
1831        // Get required imports and globals based on placeholders used
1832        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1833        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1834
1835        // Build invalid data config if error injection is enabled
1836        let invalid_data_config = self.build_invalid_data_config();
1837        let error_injection_enabled = invalid_data_config.is_some();
1838        let error_rate = self.error_rate.unwrap_or(0.0);
1839        let error_types: Vec<String> = invalid_data_config
1840            .as_ref()
1841            .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
1842            .unwrap_or_default();
1843
1844        if error_injection_enabled {
1845            TerminalReporter::print_progress(&format!(
1846                "Error injection enabled ({}% rate)",
1847                (error_rate * 100.0) as u32
1848            ));
1849        }
1850
1851        // Check if security testing is enabled
1852        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1853
1854        let data = serde_json::json!({
1855            "base_url": self.target,
1856            "flows": flows_data,
1857            "extract_fields": config.default_extract_fields,
1858            "duration_secs": duration_secs,
1859            "max_vus": self.vus,
1860            "auth_header": self.auth,
1861            "custom_headers": custom_headers,
1862            "skip_tls_verify": self.skip_tls_verify,
1863            // Add missing template fields
1864            "stages": stages.iter().map(|s| serde_json::json!({
1865                "duration": s.duration,
1866                "target": s.target,
1867            })).collect::<Vec<_>>(),
1868            "threshold_percentile": self.threshold_percentile,
1869            "threshold_ms": self.threshold_ms,
1870            "max_error_rate": self.max_error_rate,
1871            "headers": headers_json,
1872            "dynamic_imports": required_imports,
1873            "dynamic_globals": required_globals,
1874            "extracted_values_output_path": self
1875                .output
1876                .join("crud_flow_extracted_values.json")
1877                .to_string_lossy(),
1878            // Error injection settings
1879            "error_injection_enabled": error_injection_enabled,
1880            "error_rate": error_rate,
1881            "error_types": error_types,
1882            // Security testing settings
1883            "security_testing_enabled": security_testing_enabled,
1884            "has_custom_headers": !custom_headers.is_empty(),
1885        });
1886
1887        let mut script = handlebars
1888            .render_template(template, &data)
1889            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1890
1891        // Enhance script with security testing support if enabled
1892        if security_testing_enabled {
1893            script = self.generate_enhanced_script(&script)?;
1894        }
1895
1896        // Validate the generated CRUD flow script
1897        TerminalReporter::print_progress("Validating CRUD flow script...");
1898        let validation_errors = K6ScriptGenerator::validate_script(&script);
1899        if !validation_errors.is_empty() {
1900            TerminalReporter::print_error("CRUD flow script validation failed");
1901            for error in &validation_errors {
1902                eprintln!("  {}", error);
1903            }
1904            return Err(BenchError::Other(format!(
1905                "CRUD flow script validation failed with {} error(s)",
1906                validation_errors.len()
1907            )));
1908        }
1909
1910        TerminalReporter::print_success("CRUD flow script generated");
1911
1912        // Write and execute script
1913        let script_path = if let Some(output) = &self.script_output {
1914            output.clone()
1915        } else {
1916            self.output.join("k6-crud-flow-script.js")
1917        };
1918
1919        if let Some(parent) = script_path.parent() {
1920            std::fs::create_dir_all(parent)?;
1921        }
1922        std::fs::write(&script_path, &script)?;
1923        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
1924
1925        if self.generate_only {
1926            println!("\nScript generated successfully. Run it with:");
1927            println!("  k6 run {}", script_path.display());
1928            return Ok(());
1929        }
1930
1931        // Execute k6
1932        TerminalReporter::print_progress("Executing CRUD flow test...");
1933        let executor = K6Executor::new()?;
1934        std::fs::create_dir_all(&self.output)?;
1935
1936        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
1937
1938        let duration_secs = Self::parse_duration(&self.duration)?;
1939        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
1940
1941        Ok(())
1942    }
1943
1944    /// Execute OpenAPI 3.0.0 conformance testing mode
1945    async fn execute_conformance_test(&self) -> Result<()> {
1946        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
1947        use crate::conformance::report::ConformanceReport;
1948        use crate::conformance::spec::ConformanceFeature;
1949
1950        TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
1951
1952        // Conformance testing is a functional correctness check (1 VU, 1 iteration).
1953        // --vus and -d flags are always ignored in this mode.
1954        TerminalReporter::print_progress(
1955            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
1956        );
1957
1958        // Parse category filter
1959        let categories = self.conformance_categories.as_ref().map(|cats_str| {
1960            cats_str
1961                .split(',')
1962                .filter_map(|s| {
1963                    let trimmed = s.trim();
1964                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
1965                        Some(canonical.to_string())
1966                    } else {
1967                        TerminalReporter::print_warning(&format!(
1968                            "Unknown conformance category: '{}'. Valid categories: {}",
1969                            trimmed,
1970                            ConformanceFeature::cli_category_names()
1971                                .iter()
1972                                .map(|(cli, _)| *cli)
1973                                .collect::<Vec<_>>()
1974                                .join(", ")
1975                        ));
1976                        None
1977                    }
1978                })
1979                .collect::<Vec<String>>()
1980        });
1981
1982        // Parse custom headers from "Key: Value" format
1983        let custom_headers: Vec<(String, String)> = self
1984            .conformance_headers
1985            .iter()
1986            .filter_map(|h| {
1987                let (name, value) = h.split_once(':')?;
1988                Some((name.trim().to_string(), value.trim().to_string()))
1989            })
1990            .collect();
1991
1992        if !custom_headers.is_empty() {
1993            TerminalReporter::print_progress(&format!(
1994                "Using {} custom header(s) for authentication",
1995                custom_headers.len()
1996            ));
1997        }
1998
1999        if self.conformance_delay_ms > 0 {
2000            TerminalReporter::print_progress(&format!(
2001                "Using {}ms delay between conformance requests",
2002                self.conformance_delay_ms
2003            ));
2004        }
2005
2006        // Ensure output dir exists so canonicalize works for the report path
2007        std::fs::create_dir_all(&self.output)?;
2008
2009        let config = ConformanceConfig {
2010            target_url: self.target.clone(),
2011            api_key: self.conformance_api_key.clone(),
2012            basic_auth: self.conformance_basic_auth.clone(),
2013            skip_tls_verify: self.skip_tls_verify,
2014            categories,
2015            base_path: self.base_path.clone(),
2016            custom_headers,
2017            output_dir: Some(self.output.clone()),
2018            all_operations: self.conformance_all_operations,
2019            custom_checks_file: self.conformance_custom.clone(),
2020            request_delay_ms: self.conformance_delay_ms,
2021            custom_filter: self.conformance_custom_filter.clone(),
2022            export_requests: self.export_requests,
2023            validate_requests: self.validate_requests,
2024        };
2025
2026        // Branch: spec-driven mode vs reference mode
2027        // Annotate operations if spec is provided (used by both native and k6 paths)
2028        let annotated_ops = if !self.spec.is_empty() {
2029            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2030            let parser = SpecParser::from_file(&self.spec[0]).await?;
2031            let operations = parser.get_operations();
2032
2033            let annotated =
2034                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2035                    &operations,
2036                    parser.spec(),
2037                );
2038            TerminalReporter::print_success(&format!(
2039                "Analyzed {} operations, found {} feature annotations",
2040                operations.len(),
2041                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2042            ));
2043            Some(annotated)
2044        } else {
2045            None
2046        };
2047
2048        // Request validation against OpenAPI spec (if --validate-requests is set)
2049        if self.validate_requests && !self.spec.is_empty() {
2050            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2051            let violation_count = crate::conformance::request_validator::run_request_validation(
2052                &self.spec,
2053                self.conformance_custom.as_deref(),
2054                self.base_path.as_deref(),
2055                &self.output,
2056            )
2057            .await?;
2058            if violation_count > 0 {
2059                TerminalReporter::print_warning(&format!(
2060                    "{} request validation violation(s) found — see conformance-request-violations.json",
2061                    violation_count
2062                ));
2063            } else {
2064                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2065            }
2066        }
2067
2068        // If generate-only OR --use-k6, use the k6 script generation path
2069        if self.generate_only || self.use_k6 {
2070            let script = if let Some(annotated) = &annotated_ops {
2071                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2072                    config,
2073                    annotated.clone(),
2074                );
2075                let op_count = gen.operation_count();
2076                let (script, check_count) = gen.generate()?;
2077                TerminalReporter::print_success(&format!(
2078                    "Conformance: {} operations analyzed, {} unique checks generated",
2079                    op_count, check_count
2080                ));
2081                script
2082            } else {
2083                let generator = ConformanceGenerator::new(config);
2084                generator.generate()?
2085            };
2086
2087            let script_path = self.output.join("k6-conformance.js");
2088            std::fs::write(&script_path, &script).map_err(|e| {
2089                BenchError::Other(format!("Failed to write conformance script: {}", e))
2090            })?;
2091            TerminalReporter::print_success(&format!(
2092                "Conformance script generated: {}",
2093                script_path.display()
2094            ));
2095
2096            if self.generate_only {
2097                println!("\nScript generated. Run with:");
2098                println!("  k6 run {}", script_path.display());
2099                return Ok(());
2100            }
2101
2102            // --use-k6: execute via k6
2103            if !K6Executor::is_k6_installed() {
2104                TerminalReporter::print_error("k6 is not installed");
2105                TerminalReporter::print_warning(
2106                    "Install k6 from: https://k6.io/docs/get-started/installation/",
2107                );
2108                return Err(BenchError::K6NotFound);
2109            }
2110
2111            TerminalReporter::print_progress("Running conformance tests via k6...");
2112            let executor = K6Executor::new()?;
2113            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2114
2115            let report_path = self.output.join("conformance-report.json");
2116            if report_path.exists() {
2117                let report = ConformanceReport::from_file(&report_path)?;
2118                report.print_report_with_options(self.conformance_all_operations);
2119                self.save_conformance_report(&report, &report_path)?;
2120            } else {
2121                TerminalReporter::print_warning(
2122                    "Conformance report not generated (k6 handleSummary may not have run)",
2123                );
2124            }
2125
2126            return Ok(());
2127        }
2128
2129        // Default: Native Rust executor (no k6 dependency)
2130        TerminalReporter::print_progress("Running conformance tests (native executor)...");
2131
2132        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2133
2134        executor = if let Some(annotated) = &annotated_ops {
2135            executor.with_spec_driven_checks(annotated)
2136        } else {
2137            executor.with_reference_checks()
2138        };
2139        executor = executor.with_custom_checks()?;
2140
2141        TerminalReporter::print_success(&format!(
2142            "Executing {} conformance checks...",
2143            executor.check_count()
2144        ));
2145
2146        let report = executor.execute().await?;
2147        report.print_report_with_options(self.conformance_all_operations);
2148
2149        // Save failure details to a separate file for easy debugging
2150        let failure_details = report.failure_details();
2151        if !failure_details.is_empty() {
2152            let details_path = self.output.join("conformance-failure-details.json");
2153            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2154                let _ = std::fs::write(&details_path, json);
2155                TerminalReporter::print_success(&format!(
2156                    "Failure details saved to: {}",
2157                    details_path.display()
2158                ));
2159            }
2160        }
2161
2162        // Save report
2163        let report_path = self.output.join("conformance-report.json");
2164        let report_json = serde_json::to_string_pretty(&report.to_json())
2165            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2166        std::fs::write(&report_path, &report_json)
2167            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2168        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2169
2170        self.save_conformance_report(&report, &report_path)?;
2171
2172        Ok(())
2173    }
2174
2175    /// Save conformance report in the requested format (SARIF or JSON copy)
2176    fn save_conformance_report(
2177        &self,
2178        report: &crate::conformance::report::ConformanceReport,
2179        report_path: &Path,
2180    ) -> Result<()> {
2181        if self.conformance_report_format == "sarif" {
2182            use crate::conformance::sarif::ConformanceSarifReport;
2183            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2184            TerminalReporter::print_success(&format!(
2185                "SARIF report saved to: {}",
2186                self.conformance_report.display()
2187            ));
2188        } else if self.conformance_report != *report_path {
2189            std::fs::copy(report_path, &self.conformance_report)?;
2190            TerminalReporter::print_success(&format!(
2191                "Report saved to: {}",
2192                self.conformance_report.display()
2193            ));
2194        }
2195        Ok(())
2196    }
2197
2198    /// Execute conformance tests against multiple targets from a targets file.
2199    ///
2200    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
2201    /// tested sequentially to avoid overwhelming them, and per-target headers from
2202    /// the targets file are merged with the base `--conformance-header` headers.
2203    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2204        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2205        use crate::conformance::report::ConformanceReport;
2206        use crate::conformance::spec::ConformanceFeature;
2207
2208        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2209
2210        // Parse targets file
2211        TerminalReporter::print_progress("Parsing targets file...");
2212        let targets = parse_targets_file(targets_file)?;
2213        let num_targets = targets.len();
2214        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2215
2216        if targets.is_empty() {
2217            return Err(BenchError::Other("No targets found in file".to_string()));
2218        }
2219
2220        TerminalReporter::print_progress(
2221            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2222        );
2223
2224        // Parse category filter (shared across all targets)
2225        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2226            cats_str
2227                .split(',')
2228                .filter_map(|s| {
2229                    let trimmed = s.trim();
2230                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2231                        Some(canonical.to_string())
2232                    } else {
2233                        TerminalReporter::print_warning(&format!(
2234                            "Unknown conformance category: '{}'. Valid categories: {}",
2235                            trimmed,
2236                            ConformanceFeature::cli_category_names()
2237                                .iter()
2238                                .map(|(cli, _)| *cli)
2239                                .collect::<Vec<_>>()
2240                                .join(", ")
2241                        ));
2242                        None
2243                    }
2244                })
2245                .collect::<Vec<String>>()
2246        });
2247
2248        // Parse base custom headers from --conformance-header flags
2249        let base_custom_headers: Vec<(String, String)> = self
2250            .conformance_headers
2251            .iter()
2252            .filter_map(|h| {
2253                let (name, value) = h.split_once(':')?;
2254                Some((name.trim().to_string(), value.trim().to_string()))
2255            })
2256            .collect();
2257
2258        if !base_custom_headers.is_empty() {
2259            TerminalReporter::print_progress(&format!(
2260                "Using {} base custom header(s) for authentication",
2261                base_custom_headers.len()
2262            ));
2263        }
2264
2265        // Load spec once if provided (shared across all targets)
2266        let annotated_ops = if !self.spec.is_empty() {
2267            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2268            let parser = SpecParser::from_file(&self.spec[0]).await?;
2269            let operations = parser.get_operations();
2270            let annotated =
2271                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2272                    &operations,
2273                    parser.spec(),
2274                );
2275            TerminalReporter::print_success(&format!(
2276                "Analyzed {} operations, found {} feature annotations",
2277                operations.len(),
2278                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2279            ));
2280            Some(annotated)
2281        } else {
2282            None
2283        };
2284
2285        // Ensure output dir exists
2286        std::fs::create_dir_all(&self.output)?;
2287
2288        // Collect per-target results for the summary
2289        struct TargetResult {
2290            url: String,
2291            passed: usize,
2292            failed: usize,
2293            elapsed: std::time::Duration,
2294            report_json: serde_json::Value,
2295            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2296        }
2297
2298        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2299        let total_start = std::time::Instant::now();
2300
2301        for (idx, target) in targets.iter().enumerate() {
2302            tracing::info!(
2303                "Running conformance tests against target {}/{}: {}",
2304                idx + 1,
2305                num_targets,
2306                target.url
2307            );
2308            TerminalReporter::print_progress(&format!(
2309                "\n--- Target {}/{}: {} ---",
2310                idx + 1,
2311                num_targets,
2312                target.url
2313            ));
2314
2315            // Merge base headers with per-target headers
2316            let mut merged_headers = base_custom_headers.clone();
2317            if let Some(ref target_headers) = target.headers {
2318                for (name, value) in target_headers {
2319                    // Per-target headers override base headers with the same name
2320                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2321                        existing.1 = value.clone();
2322                    } else {
2323                        merged_headers.push((name.clone(), value.clone()));
2324                    }
2325                }
2326            }
2327            // Add auth header if present on target
2328            if let Some(ref auth) = target.auth {
2329                if let Some(existing) =
2330                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2331                {
2332                    existing.1 = auth.clone();
2333                } else {
2334                    merged_headers.push(("Authorization".to_string(), auth.clone()));
2335                }
2336            }
2337
2338            // Per-target output dir (used by both native and k6 paths).
2339            // Created before the config so we can point the k6 script's
2340            // handleSummary at the per-target directory rather than the shared
2341            // parent output dir (otherwise every target would overwrite the
2342            // same conformance-report.json).
2343            let target_dir = self.output.join(format!("target_{}", idx));
2344            std::fs::create_dir_all(&target_dir)?;
2345
2346            let config = ConformanceConfig {
2347                target_url: target.url.clone(),
2348                api_key: self.conformance_api_key.clone(),
2349                basic_auth: self.conformance_basic_auth.clone(),
2350                skip_tls_verify: self.skip_tls_verify,
2351                categories: categories.clone(),
2352                base_path: self.base_path.clone(),
2353                custom_headers: merged_headers,
2354                output_dir: Some(target_dir.clone()),
2355                all_operations: self.conformance_all_operations,
2356                custom_checks_file: self.conformance_custom.clone(),
2357                request_delay_ms: self.conformance_delay_ms,
2358                custom_filter: self.conformance_custom_filter.clone(),
2359                export_requests: self.export_requests,
2360                validate_requests: self.validate_requests,
2361            };
2362
2363            let target_start = std::time::Instant::now();
2364            let report = if self.use_k6 {
2365                if !K6Executor::is_k6_installed() {
2366                    TerminalReporter::print_error("k6 is not installed");
2367                    TerminalReporter::print_warning(
2368                        "Install k6 from: https://k6.io/docs/get-started/installation/",
2369                    );
2370                    return Err(BenchError::K6NotFound);
2371                }
2372
2373                let script = if let Some(ref annotated) = annotated_ops {
2374                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2375                        config.clone(),
2376                        annotated.clone(),
2377                    );
2378                    let (script, _check_count) = gen.generate()?;
2379                    script
2380                } else {
2381                    let generator = ConformanceGenerator::new(config.clone());
2382                    generator.generate()?
2383                };
2384
2385                let script_path = target_dir.join("k6-conformance.js");
2386                std::fs::write(&script_path, &script).map_err(|e| {
2387                    BenchError::Other(format!("Failed to write conformance script: {}", e))
2388                })?;
2389                TerminalReporter::print_success(&format!(
2390                    "Conformance script generated: {}",
2391                    script_path.display()
2392                ));
2393
2394                TerminalReporter::print_progress(&format!(
2395                    "Running conformance tests via k6 against {}...",
2396                    target.url
2397                ));
2398                let k6 = K6Executor::new()?;
2399                // Unique k6 API port per target to avoid collisions.
2400                let api_port = 6565u16.saturating_add(idx as u16);
2401                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2402                    .await?;
2403
2404                let report_path = target_dir.join("conformance-report.json");
2405                if report_path.exists() {
2406                    ConformanceReport::from_file(&report_path)?
2407                } else {
2408                    TerminalReporter::print_warning(&format!(
2409                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2410                        target.url
2411                    ));
2412                    continue;
2413                }
2414            } else {
2415                let mut executor =
2416                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2417
2418                executor = if let Some(ref annotated) = annotated_ops {
2419                    executor.with_spec_driven_checks(annotated)
2420                } else {
2421                    executor.with_reference_checks()
2422                };
2423                executor = executor.with_custom_checks()?;
2424
2425                TerminalReporter::print_success(&format!(
2426                    "Executing {} conformance checks against {}...",
2427                    executor.check_count(),
2428                    target.url
2429                ));
2430
2431                executor.execute().await?
2432            };
2433            let target_elapsed = target_start.elapsed();
2434
2435            let report_json = report.to_json();
2436
2437            // Extract pass/fail from the summary in the JSON
2438            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2439            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2440            let total_checks = passed + failed;
2441            let rate = if total_checks == 0 {
2442                0.0
2443            } else {
2444                (passed as f64 / total_checks as f64) * 100.0
2445            };
2446
2447            TerminalReporter::print_success(&format!(
2448                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2449                target.url,
2450                passed,
2451                total_checks,
2452                rate,
2453                target_elapsed.as_secs_f64()
2454            ));
2455
2456            // Save per-target report (target_dir created above)
2457            let target_report_path = target_dir.join("conformance-report.json");
2458            let report_str = serde_json::to_string_pretty(&report_json)
2459                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2460            std::fs::write(&target_report_path, &report_str)
2461                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2462
2463            // Save failure details if any
2464            let failure_details = report.failure_details();
2465            if !failure_details.is_empty() {
2466                let details_path = target_dir.join("conformance-failure-details.json");
2467                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2468                    let _ = std::fs::write(&details_path, json);
2469                }
2470            }
2471
2472            // Compute OWASP coverage for this target
2473            let owasp_coverage = report.owasp_coverage_data();
2474
2475            target_results.push(TargetResult {
2476                url: target.url.clone(),
2477                passed,
2478                failed,
2479                elapsed: target_elapsed,
2480                report_json,
2481                owasp_coverage,
2482            });
2483        }
2484
2485        let total_elapsed = total_start.elapsed();
2486
2487        // Print summary table
2488        println!("\n{}", "=".repeat(80));
2489        println!("  Multi-Target Conformance Summary");
2490        println!("{}", "=".repeat(80));
2491        println!(
2492            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
2493            "Target URL", "Passed", "Failed", "Rate", "Time"
2494        );
2495        println!("  {}", "-".repeat(76));
2496
2497        let mut total_passed = 0usize;
2498        let mut total_failed = 0usize;
2499
2500        for result in &target_results {
2501            let total_checks = result.passed + result.failed;
2502            let rate = if total_checks == 0 {
2503                0.0
2504            } else {
2505                (result.passed as f64 / total_checks as f64) * 100.0
2506            };
2507
2508            // Truncate long URLs for display
2509            let display_url = if result.url.len() > 38 {
2510                format!("{}...", &result.url[..35])
2511            } else {
2512                result.url.clone()
2513            };
2514
2515            println!(
2516                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2517                display_url,
2518                result.passed,
2519                result.failed,
2520                rate,
2521                result.elapsed.as_secs_f64()
2522            );
2523
2524            total_passed += result.passed;
2525            total_failed += result.failed;
2526        }
2527
2528        let grand_total = total_passed + total_failed;
2529        let overall_rate = if grand_total == 0 {
2530            0.0
2531        } else {
2532            (total_passed as f64 / grand_total as f64) * 100.0
2533        };
2534
2535        println!("  {}", "-".repeat(76));
2536        println!(
2537            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2538            format!("TOTAL ({} targets)", num_targets),
2539            total_passed,
2540            total_failed,
2541            overall_rate,
2542            total_elapsed.as_secs_f64()
2543        );
2544        println!("{}", "=".repeat(80));
2545
2546        // Print per-target OWASP coverage
2547        for result in &target_results {
2548            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
2549            for entry in &result.owasp_coverage {
2550                let status = if !entry.tested {
2551                    "-"
2552                } else if entry.all_passed {
2553                    "pass"
2554                } else {
2555                    "FAIL"
2556                };
2557                let via = if entry.via_categories.is_empty() {
2558                    String::new()
2559                } else {
2560                    format!(" (via {})", entry.via_categories.join(", "))
2561                };
2562                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
2563            }
2564        }
2565
2566        // Save combined summary
2567        let per_target_summaries: Vec<serde_json::Value> = target_results
2568            .iter()
2569            .enumerate()
2570            .map(|(idx, r)| {
2571                let total_checks = r.passed + r.failed;
2572                let rate = if total_checks == 0 {
2573                    0.0
2574                } else {
2575                    (r.passed as f64 / total_checks as f64) * 100.0
2576                };
2577                let owasp_json: Vec<serde_json::Value> = r
2578                    .owasp_coverage
2579                    .iter()
2580                    .map(|e| {
2581                        serde_json::json!({
2582                            "id": e.id,
2583                            "name": e.name,
2584                            "tested": e.tested,
2585                            "all_passed": e.all_passed,
2586                            "via_categories": e.via_categories,
2587                        })
2588                    })
2589                    .collect();
2590                serde_json::json!({
2591                    "target_url": r.url,
2592                    "target_index": idx,
2593                    "checks_passed": r.passed,
2594                    "checks_failed": r.failed,
2595                    "total_checks": total_checks,
2596                    "pass_rate": rate,
2597                    "elapsed_seconds": r.elapsed.as_secs_f64(),
2598                    "report": r.report_json,
2599                    "owasp_coverage": owasp_json,
2600                })
2601            })
2602            .collect();
2603
2604        let combined_summary = serde_json::json!({
2605            "total_targets": num_targets,
2606            "total_checks_passed": total_passed,
2607            "total_checks_failed": total_failed,
2608            "overall_pass_rate": overall_rate,
2609            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
2610            "targets": per_target_summaries,
2611        });
2612
2613        let summary_path = self.output.join("multi-target-conformance-summary.json");
2614        let summary_str = serde_json::to_string_pretty(&combined_summary)
2615            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
2616        std::fs::write(&summary_path, &summary_str)
2617            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
2618        TerminalReporter::print_success(&format!(
2619            "Combined summary saved to: {}",
2620            summary_path.display()
2621        ));
2622
2623        Ok(())
2624    }
2625
2626    /// Execute OWASP API Security Top 10 testing mode
2627    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
2628        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
2629
2630        // Parse custom headers from CLI
2631        let custom_headers = self.parse_headers()?;
2632
2633        // Build OWASP configuration from CLI options
2634        let mut config = OwaspApiConfig::new()
2635            .with_auth_header(&self.owasp_auth_header)
2636            .with_verbose(self.verbose)
2637            .with_insecure(self.skip_tls_verify)
2638            .with_concurrency(self.vus as usize)
2639            .with_iterations(self.owasp_iterations as usize)
2640            .with_base_path(self.base_path.clone())
2641            .with_custom_headers(custom_headers);
2642
2643        // Set valid auth token if provided
2644        if let Some(ref token) = self.owasp_auth_token {
2645            config = config.with_valid_auth_token(token);
2646        }
2647
2648        // Parse categories if provided
2649        if let Some(ref cats_str) = self.owasp_categories {
2650            let categories: Vec<OwaspCategory> = cats_str
2651                .split(',')
2652                .filter_map(|s| {
2653                    let trimmed = s.trim();
2654                    match trimmed.parse::<OwaspCategory>() {
2655                        Ok(cat) => Some(cat),
2656                        Err(e) => {
2657                            TerminalReporter::print_warning(&e);
2658                            None
2659                        }
2660                    }
2661                })
2662                .collect();
2663
2664            if !categories.is_empty() {
2665                config = config.with_categories(categories);
2666            }
2667        }
2668
2669        // Load admin paths from file if provided
2670        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
2671            config.admin_paths_file = Some(admin_paths_file.clone());
2672            if let Err(e) = config.load_admin_paths() {
2673                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
2674            }
2675        }
2676
2677        // Set ID fields if provided
2678        if let Some(ref id_fields_str) = self.owasp_id_fields {
2679            let id_fields: Vec<String> = id_fields_str
2680                .split(',')
2681                .map(|s| s.trim().to_string())
2682                .filter(|s| !s.is_empty())
2683                .collect();
2684            if !id_fields.is_empty() {
2685                config = config.with_id_fields(id_fields);
2686            }
2687        }
2688
2689        // Set report path and format
2690        if let Some(ref report_path) = self.owasp_report {
2691            config = config.with_report_path(report_path);
2692        }
2693        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
2694            config = config.with_report_format(format);
2695        }
2696
2697        // Print configuration summary
2698        let categories = config.categories_to_test();
2699        TerminalReporter::print_success(&format!(
2700            "Testing {} OWASP categories: {}",
2701            categories.len(),
2702            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
2703        ));
2704
2705        if config.valid_auth_token.is_some() {
2706            TerminalReporter::print_progress("Using provided auth token for baseline requests");
2707        }
2708
2709        // Create the OWASP generator
2710        TerminalReporter::print_progress("Generating OWASP security test script...");
2711        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
2712
2713        // Generate the script
2714        let script = generator.generate()?;
2715        TerminalReporter::print_success("OWASP security test script generated");
2716
2717        // Write script to file
2718        let script_path = if let Some(output) = &self.script_output {
2719            output.clone()
2720        } else {
2721            self.output.join("k6-owasp-security-test.js")
2722        };
2723
2724        if let Some(parent) = script_path.parent() {
2725            std::fs::create_dir_all(parent)?;
2726        }
2727        std::fs::write(&script_path, &script)?;
2728        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2729
2730        // If generate-only mode, exit here
2731        if self.generate_only {
2732            println!("\nOWASP security test script generated. Run it with:");
2733            println!("  k6 run {}", script_path.display());
2734            return Ok(());
2735        }
2736
2737        // Execute k6
2738        TerminalReporter::print_progress("Executing OWASP security tests...");
2739        let executor = K6Executor::new()?;
2740        std::fs::create_dir_all(&self.output)?;
2741
2742        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2743
2744        let duration_secs = Self::parse_duration(&self.duration)?;
2745        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2746
2747        println!("\nOWASP security test results saved to: {}", self.output.display());
2748
2749        Ok(())
2750    }
2751}
2752
2753#[cfg(test)]
2754mod tests {
2755    use super::*;
2756    use tempfile::tempdir;
2757
2758    #[test]
2759    fn test_parse_duration() {
2760        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
2761        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
2762        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
2763        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
2764    }
2765
2766    #[test]
2767    fn test_parse_duration_invalid() {
2768        assert!(BenchCommand::parse_duration("invalid").is_err());
2769        assert!(BenchCommand::parse_duration("30x").is_err());
2770    }
2771
2772    #[test]
2773    fn test_parse_headers() {
2774        let cmd = BenchCommand {
2775            spec: vec![PathBuf::from("test.yaml")],
2776            spec_dir: None,
2777            merge_conflicts: "error".to_string(),
2778            spec_mode: "merge".to_string(),
2779            dependency_config: None,
2780            target: "http://localhost".to_string(),
2781            base_path: None,
2782            duration: "1m".to_string(),
2783            vus: 10,
2784            scenario: "ramp-up".to_string(),
2785            operations: None,
2786            exclude_operations: None,
2787            auth: None,
2788            headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
2789            output: PathBuf::from("output"),
2790            generate_only: false,
2791            script_output: None,
2792            threshold_percentile: "p(95)".to_string(),
2793            threshold_ms: 500,
2794            max_error_rate: 0.05,
2795            verbose: false,
2796            skip_tls_verify: false,
2797            chunked_request_bodies: false,
2798            target_rps: None,
2799            no_keep_alive: false,
2800            targets_file: None,
2801            max_concurrency: None,
2802            results_format: "both".to_string(),
2803            params_file: None,
2804            crud_flow: false,
2805            flow_config: None,
2806            extract_fields: None,
2807            parallel_create: None,
2808            data_file: None,
2809            data_distribution: "unique-per-vu".to_string(),
2810            data_mappings: None,
2811            per_uri_control: false,
2812            error_rate: None,
2813            error_types: None,
2814            security_test: false,
2815            security_payloads: None,
2816            security_categories: None,
2817            security_target_fields: None,
2818            wafbench_dir: None,
2819            wafbench_cycle_all: false,
2820            owasp_api_top10: false,
2821            owasp_categories: None,
2822            owasp_auth_header: "Authorization".to_string(),
2823            owasp_auth_token: None,
2824            owasp_admin_paths: None,
2825            owasp_id_fields: None,
2826            owasp_report: None,
2827            owasp_report_format: "json".to_string(),
2828            owasp_iterations: 1,
2829            conformance: false,
2830            conformance_api_key: None,
2831            conformance_basic_auth: None,
2832            conformance_report: PathBuf::from("conformance-report.json"),
2833            conformance_categories: None,
2834            conformance_report_format: "json".to_string(),
2835            conformance_headers: vec![],
2836            conformance_all_operations: false,
2837            conformance_custom: None,
2838            conformance_delay_ms: 0,
2839            use_k6: false,
2840            conformance_custom_filter: None,
2841            export_requests: false,
2842            validate_requests: false,
2843        };
2844
2845        let headers = cmd.parse_headers().unwrap();
2846        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
2847        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
2848    }
2849
2850    #[test]
2851    fn test_get_spec_display_name() {
2852        let cmd = BenchCommand {
2853            spec: vec![PathBuf::from("test.yaml")],
2854            spec_dir: None,
2855            merge_conflicts: "error".to_string(),
2856            spec_mode: "merge".to_string(),
2857            dependency_config: None,
2858            target: "http://localhost".to_string(),
2859            base_path: None,
2860            duration: "1m".to_string(),
2861            vus: 10,
2862            scenario: "ramp-up".to_string(),
2863            operations: None,
2864            exclude_operations: None,
2865            auth: None,
2866            headers: None,
2867            output: PathBuf::from("output"),
2868            generate_only: false,
2869            script_output: None,
2870            threshold_percentile: "p(95)".to_string(),
2871            threshold_ms: 500,
2872            max_error_rate: 0.05,
2873            verbose: false,
2874            skip_tls_verify: false,
2875            chunked_request_bodies: false,
2876            target_rps: None,
2877            no_keep_alive: false,
2878            targets_file: None,
2879            max_concurrency: None,
2880            results_format: "both".to_string(),
2881            params_file: None,
2882            crud_flow: false,
2883            flow_config: None,
2884            extract_fields: None,
2885            parallel_create: None,
2886            data_file: None,
2887            data_distribution: "unique-per-vu".to_string(),
2888            data_mappings: None,
2889            per_uri_control: false,
2890            error_rate: None,
2891            error_types: None,
2892            security_test: false,
2893            security_payloads: None,
2894            security_categories: None,
2895            security_target_fields: None,
2896            wafbench_dir: None,
2897            wafbench_cycle_all: false,
2898            owasp_api_top10: false,
2899            owasp_categories: None,
2900            owasp_auth_header: "Authorization".to_string(),
2901            owasp_auth_token: None,
2902            owasp_admin_paths: None,
2903            owasp_id_fields: None,
2904            owasp_report: None,
2905            owasp_report_format: "json".to_string(),
2906            owasp_iterations: 1,
2907            conformance: false,
2908            conformance_api_key: None,
2909            conformance_basic_auth: None,
2910            conformance_report: PathBuf::from("conformance-report.json"),
2911            conformance_categories: None,
2912            conformance_report_format: "json".to_string(),
2913            conformance_headers: vec![],
2914            conformance_all_operations: false,
2915            conformance_custom: None,
2916            conformance_delay_ms: 0,
2917            use_k6: false,
2918            conformance_custom_filter: None,
2919            export_requests: false,
2920            validate_requests: false,
2921        };
2922
2923        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
2924
2925        // Test multiple specs
2926        let cmd_multi = BenchCommand {
2927            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
2928            spec_dir: None,
2929            merge_conflicts: "error".to_string(),
2930            spec_mode: "merge".to_string(),
2931            dependency_config: None,
2932            target: "http://localhost".to_string(),
2933            base_path: None,
2934            duration: "1m".to_string(),
2935            vus: 10,
2936            scenario: "ramp-up".to_string(),
2937            operations: None,
2938            exclude_operations: None,
2939            auth: None,
2940            headers: None,
2941            output: PathBuf::from("output"),
2942            generate_only: false,
2943            script_output: None,
2944            threshold_percentile: "p(95)".to_string(),
2945            threshold_ms: 500,
2946            max_error_rate: 0.05,
2947            verbose: false,
2948            skip_tls_verify: false,
2949            chunked_request_bodies: false,
2950            target_rps: None,
2951            no_keep_alive: false,
2952            targets_file: None,
2953            max_concurrency: None,
2954            results_format: "both".to_string(),
2955            params_file: None,
2956            crud_flow: false,
2957            flow_config: None,
2958            extract_fields: None,
2959            parallel_create: None,
2960            data_file: None,
2961            data_distribution: "unique-per-vu".to_string(),
2962            data_mappings: None,
2963            per_uri_control: false,
2964            error_rate: None,
2965            error_types: None,
2966            security_test: false,
2967            security_payloads: None,
2968            security_categories: None,
2969            security_target_fields: None,
2970            wafbench_dir: None,
2971            wafbench_cycle_all: false,
2972            owasp_api_top10: false,
2973            owasp_categories: None,
2974            owasp_auth_header: "Authorization".to_string(),
2975            owasp_auth_token: None,
2976            owasp_admin_paths: None,
2977            owasp_id_fields: None,
2978            owasp_report: None,
2979            owasp_report_format: "json".to_string(),
2980            owasp_iterations: 1,
2981            conformance: false,
2982            conformance_api_key: None,
2983            conformance_basic_auth: None,
2984            conformance_report: PathBuf::from("conformance-report.json"),
2985            conformance_categories: None,
2986            conformance_report_format: "json".to_string(),
2987            conformance_headers: vec![],
2988            conformance_all_operations: false,
2989            conformance_custom: None,
2990            conformance_delay_ms: 0,
2991            use_k6: false,
2992            conformance_custom_filter: None,
2993            export_requests: false,
2994            validate_requests: false,
2995        };
2996
2997        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
2998    }
2999
3000    #[test]
3001    fn test_parse_extracted_values_from_output_dir() {
3002        let dir = tempdir().unwrap();
3003        let path = dir.path().join("extracted_values.json");
3004        std::fs::write(
3005            &path,
3006            r#"{
3007  "pool_id": "abc123",
3008  "count": 0,
3009  "enabled": false,
3010  "metadata": { "owner": "team-a" }
3011}"#,
3012        )
3013        .unwrap();
3014
3015        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3016        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3017        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3018        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3019        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3020    }
3021
3022    #[test]
3023    fn test_parse_extracted_values_missing_file() {
3024        let dir = tempdir().unwrap();
3025        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3026        assert!(extracted.values.is_empty());
3027    }
3028}