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    /// Issue #79 round 13 (4) — when true, replace the standard
204    /// conformance run with a positive + per-category negative
205    /// self-test driver. Verifies that the server actually rejects
206    /// the negatives with 4xx (i.e. its validator is wired correctly).
207    /// Useful to confirm the round-13 (3) validator-bypass fix took
208    /// effect against the user's spec.
209    pub conformance_self_test: bool,
210
211    /// Round 18.5 — local source IPs to bind self-test requests to.
212    /// Each entry must be a valid `IpAddr` and already assigned to
213    /// an interface on the host. Operations round-robin through the
214    /// pool. Empty → one default client.
215    pub source_ips: Vec<String>,
216    /// Round 18.5 — fake source IPs to advertise via forwarded-IP
217    /// headers (rotated per operation). Used for GEODB testing
218    /// where the destination reads the IP from a header.
219    pub geo_source_ips: Vec<String>,
220    /// Round 18.5 — which forwarded-IP header(s) to populate when
221    /// `geo_source_ips` is non-empty. Empty → default 3-header set
222    /// (X-Forwarded-For, True-Client-IP, CF-Connecting-IP).
223    pub geo_source_headers: Vec<String>,
224
225    /// Round 21.1 — cap the HTML conformance report's missed-negative
226    /// drill-down at N rows. `Some(0)` means no cap; `None` keeps the
227    /// default of 200. The JSON report always carries the full set
228    /// regardless of this knob — it only controls what the HTML drill
229    /// view shows so a 50 000-violation run doesn't produce a 5 MB
230    /// browser-choking HTML file by default.
231    pub report_missed_cap: Option<u32>,
232
233    // === OWASP API Security Top 10 Testing ===
234    /// Enable OWASP API Security Top 10 testing mode
235    pub owasp_api_top10: bool,
236    /// OWASP API categories to test (comma-separated)
237    pub owasp_categories: Option<String>,
238    /// Authorization header name for OWASP auth tests
239    pub owasp_auth_header: String,
240    /// Valid authorization token for OWASP baseline requests
241    pub owasp_auth_token: Option<String>,
242    /// File containing admin/privileged paths to test
243    pub owasp_admin_paths: Option<PathBuf>,
244    /// Fields containing resource IDs for BOLA testing
245    pub owasp_id_fields: Option<String>,
246    /// OWASP report output file
247    pub owasp_report: Option<PathBuf>,
248    /// OWASP report format (json, sarif)
249    pub owasp_report_format: String,
250    /// Number of iterations per VU for OWASP tests (default: 1)
251    pub owasp_iterations: u32,
252}
253
254/// Round 18.5 / 19 — parse a list of CLI IP strings. Each entry may be:
255/// - a single IPv4/IPv6 (`10.0.0.5` / `2001:db8::1`)
256/// - a comma-separated list (`10.0.0.5,10.0.0.6,2001:db8::1`)
257/// - a CIDR range (`10.0.0.0/29` expands to 8 hosts;
258///   `2001:db8::/126` expands to 4 IPv6 hosts)
259///
260/// CIDR ranges are capped at `MAX_CIDR_EXPANSION` (256) host
261/// addresses to avoid OOM'ing on `/8` typos. The cap is generous
262/// for GEODB testing (you want 20–100 IPs, not 10M) and the warning
263/// names the cap so it's debuggable.
264///
265/// Malformed entries log a warning and are dropped; the bench
266/// continues with whatever resolved cleanly.
267fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
268    use std::net::IpAddr;
269    const MAX_CIDR_EXPANSION: usize = 256;
270    let mut out = Vec::new();
271    for entry in raw {
272        for piece in entry.split(',') {
273            let s = piece.trim();
274            if s.is_empty() {
275                continue;
276            }
277            // CIDR form: `ip/prefix`
278            if let Some((addr_part, prefix_part)) = s.split_once('/') {
279                let prefix: u32 = match prefix_part.parse() {
280                    Ok(p) => p,
281                    Err(e) => {
282                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
283                        continue;
284                    }
285                };
286                let net_addr: IpAddr = match addr_part.parse() {
287                    Ok(a) => a,
288                    Err(e) => {
289                        tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
290                        continue;
291                    }
292                };
293                expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
294                continue;
295            }
296            // Plain IP form
297            match s.parse::<IpAddr>() {
298                Ok(ip) => out.push(ip),
299                Err(e) => {
300                    tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
301                }
302            }
303        }
304    }
305    out
306}
307
308/// Expand a CIDR (IPv4 or IPv6) into individual host IPs, appending
309/// to `out`. Capped at `cap` to prevent runaway expansion on a `/8`
310/// typo. When the cap kicks in we log a warning and skip the rest.
311fn expand_cidr(
312    net: std::net::IpAddr,
313    prefix: u32,
314    cap: usize,
315    flag_name: &str,
316    raw: &str,
317    out: &mut Vec<std::net::IpAddr>,
318) {
319    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
320    match net {
321        IpAddr::V4(ipv4) => {
322            if prefix > 32 {
323                tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
324                return;
325            }
326            let total: u64 = 1u64 << (32 - prefix);
327            let take = total.min(cap as u64) as u32;
328            if total > cap as u64 {
329                tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
330            }
331            let mask: u32 = if prefix == 0 {
332                0
333            } else {
334                !0u32 << (32 - prefix)
335            };
336            let net_u32 = u32::from(ipv4) & mask;
337            for i in 0..take {
338                out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
339            }
340        }
341        IpAddr::V6(ipv6) => {
342            if prefix > 128 {
343                tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
344                return;
345            }
346            // Total addresses = 2^(128-prefix). Cap at u128::MAX
347            // conceptually but since `cap` is small (256) we just
348            // iterate up to cap.
349            let mask: u128 = if prefix == 0 {
350                0
351            } else {
352                !0u128 << (128 - prefix)
353            };
354            let net_u128 = u128::from(ipv6) & mask;
355            let remaining_bits = 128 - prefix;
356            // Compute total carefully — for prefix=0 this is 2^128
357            // which overflows; we just clamp via take.
358            let total_capped = if remaining_bits >= 64 {
359                cap as u128
360            } else {
361                (1u128 << remaining_bits).min(cap as u128)
362            };
363            if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
364                tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
365            }
366            for i in 0..total_capped {
367                out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
368            }
369        }
370    }
371}
372
373impl BenchCommand {
374    /// Load and merge specs from --spec files and --spec-dir
375    pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
376        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
377
378        // Load specs from --spec flags
379        if !self.spec.is_empty() {
380            let specs = load_specs_from_files(self.spec.clone())
381                .await
382                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
383            all_specs.extend(specs);
384        }
385
386        // Load specs from --spec-dir if provided
387        if let Some(spec_dir) = &self.spec_dir {
388            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
389                BenchError::Other(format!("Failed to load specs from directory: {}", e))
390            })?;
391            all_specs.extend(dir_specs);
392        }
393
394        if all_specs.is_empty() {
395            return Err(BenchError::Other(
396                "No spec files provided. Use --spec or --spec-dir.".to_string(),
397            ));
398        }
399
400        // If only one spec, return it directly (extract just the OpenApiSpec)
401        if all_specs.len() == 1 {
402            // Safe to unwrap because we just checked len() == 1
403            return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
404        }
405
406        // Merge multiple specs
407        let conflict_strategy = match self.merge_conflicts.as_str() {
408            "first" => ConflictStrategy::First,
409            "last" => ConflictStrategy::Last,
410            _ => ConflictStrategy::Error,
411        };
412
413        merge_specs(all_specs, conflict_strategy)
414            .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
415    }
416
417    /// Get a display name for the spec(s)
418    fn get_spec_display_name(&self) -> String {
419        if self.spec.len() == 1 {
420            self.spec[0].to_string_lossy().to_string()
421        } else if !self.spec.is_empty() {
422            format!("{} spec files", self.spec.len())
423        } else if let Some(dir) = &self.spec_dir {
424            format!("specs from {}", dir.display())
425        } else {
426            "no specs".to_string()
427        }
428    }
429
430    /// Execute the bench command
431    pub async fn execute(&self) -> Result<()> {
432        // Check if we're in multi-target mode
433        if let Some(targets_file) = &self.targets_file {
434            if self.conformance {
435                return self.execute_multi_target_conformance(targets_file).await;
436            }
437            return self.execute_multi_target(targets_file).await;
438        }
439
440        // Check if we're in sequential spec mode (for dependency handling)
441        if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
442            return self.execute_sequential_specs().await;
443        }
444
445        // Single target mode (existing behavior)
446        // Print header
447        TerminalReporter::print_header(
448            &self.get_spec_display_name(),
449            &self.target,
450            0, // Will be updated later
451            &self.scenario,
452            Self::parse_duration(&self.duration)?,
453        );
454
455        // Validate k6 installation
456        if !K6Executor::is_k6_installed() {
457            TerminalReporter::print_error("k6 is not installed");
458            TerminalReporter::print_warning(
459                "Install k6 from: https://k6.io/docs/get-started/installation/",
460            );
461            return Err(BenchError::K6NotFound);
462        }
463
464        // Check for conformance testing mode (before spec loading — conformance doesn't need a user spec)
465        if self.conformance {
466            return self.execute_conformance_test().await;
467        }
468
469        // Load and parse spec(s)
470        TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
471        let merged_spec = self.load_and_merge_specs().await?;
472        let parser = SpecParser::from_spec(merged_spec);
473        if self.spec.len() > 1 || self.spec_dir.is_some() {
474            TerminalReporter::print_success(&format!(
475                "Loaded and merged {} specification(s)",
476                self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
477            ));
478        } else {
479            TerminalReporter::print_success("Specification loaded");
480        }
481
482        // Check for mock server integration
483        let mock_config = self.build_mock_config().await;
484        if mock_config.is_mock_server {
485            TerminalReporter::print_progress("Mock server integration enabled");
486        }
487
488        // Check for CRUD flow mode
489        if self.crud_flow {
490            return self.execute_crud_flow(&parser).await;
491        }
492
493        // Check for OWASP API Top 10 testing mode
494        if self.owasp_api_top10 {
495            return self.execute_owasp_test(&parser).await;
496        }
497
498        // Get operations
499        TerminalReporter::print_progress("Extracting API operations...");
500        let mut operations = if let Some(filter) = &self.operations {
501            parser.filter_operations(filter)?
502        } else {
503            parser.get_operations()
504        };
505
506        // Apply exclusions if provided
507        if let Some(exclude) = &self.exclude_operations {
508            let before_count = operations.len();
509            operations = parser.exclude_operations(operations, exclude)?;
510            let excluded_count = before_count - operations.len();
511            if excluded_count > 0 {
512                TerminalReporter::print_progress(&format!(
513                    "Excluded {} operations matching '{}'",
514                    excluded_count, exclude
515                ));
516            }
517        }
518
519        if operations.is_empty() {
520            return Err(BenchError::Other("No operations found in spec".to_string()));
521        }
522
523        TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
524
525        // Load parameter overrides if provided
526        let param_overrides = if let Some(params_file) = &self.params_file {
527            TerminalReporter::print_progress("Loading parameter overrides...");
528            let overrides = ParameterOverrides::from_file(params_file)?;
529            TerminalReporter::print_success(&format!(
530                "Loaded parameter overrides ({} operation-specific, {} defaults)",
531                overrides.operations.len(),
532                if overrides.defaults.is_empty() { 0 } else { 1 }
533            ));
534            Some(overrides)
535        } else {
536            None
537        };
538
539        // Generate request templates
540        TerminalReporter::print_progress("Generating request templates...");
541        let templates: Vec<_> = operations
542            .iter()
543            .map(|op| {
544                let op_overrides = param_overrides.as_ref().map(|po| {
545                    po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
546                });
547                RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
548            })
549            .collect::<Result<Vec<_>>>()?;
550        TerminalReporter::print_success("Request templates generated");
551
552        // Parse headers
553        let custom_headers = self.parse_headers()?;
554
555        // Resolve base path (CLI option takes priority over spec's servers URL)
556        let base_path = self.resolve_base_path(&parser);
557        if let Some(ref bp) = base_path {
558            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
559        }
560
561        // Generate k6 script
562        TerminalReporter::print_progress("Generating k6 load test script...");
563        let scenario =
564            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
565
566        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
567
568        // Issue #79 round 6 follow-up — Srikanth reported k6 emitting
569        // "Insufficient VUs, reached 5 active VUs and cannot initialize more"
570        // when running `--rps 100 --vus 5`. With the `constant-arrival-rate`
571        // executor, k6 needs roughly `rps × avg_request_seconds` VUs to keep
572        // up; if `--vus` is too low it can't sustain the rate. Warn pre-flight
573        // so users know to bump `--vus` rather than chase the warning.
574        //
575        // Round 8 (#79): the static 100ms heuristic was wrong for fast targets
576        // (~2ms latency). Probe the actual target first to measure baseline
577        // latency, then derive a more accurate sizing recommendation. Fall
578        // back to the 100ms heuristic only when the probe can't reach the
579        // target (auth-gated endpoints, strict WAFs, etc).
580        //
581        // Round 9 (#79): factor in operation count. k6's constant-arrival-rate
582        // counts ITERATIONS, not requests — and every iteration runs all N
583        // operations sequentially, so required VUs scale with N. Srikanth's
584        // 12-op spec at --rps 100 with 15ms latency needs ~19 VUs, not 3.
585        let num_ops = operations.len() as u32;
586        if let Some(rps) = self.target_rps {
587            let probe =
588                crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
589
590            let (required_vus, basis) = match probe {
591                Some(p) => (
592                    p.required_vus(rps, num_ops),
593                    format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
594                ),
595                None => {
596                    // Static fallback: ~100ms heuristic × num_ops per iteration.
597                    let fallback = (rps as u64)
598                        .saturating_mul(num_ops.max(1) as u64)
599                        .div_ceil(10)
600                        .min(u32::MAX as u64) as u32;
601                    (fallback, "~100ms (default — probe failed)".to_string())
602                }
603            };
604
605            if self.vus < required_vus {
606                // Round 10 (#79): Srikanth's 11422-op spec at --rps 100 produced
607                // a recommendation of ~10,740 VUs, which is absurd in practice.
608                // When the recommendation goes super-linear, the real fix is to
609                // reduce the workload (use --operations filter), not bump VUs.
610                // Cap the suggestion at 1000 and steer the user toward filtering.
611                const VU_RECOMMENDATION_CAP: u32 = 1000;
612                let recommendation = required_vus.max(self.vus + 1);
613                if recommendation > VU_RECOMMENDATION_CAP {
614                    TerminalReporter::print_warning(&format!(
615                        "Workload is very large: --rps {} × {} ops/iteration × {} \
616                         baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
617                         practical to drive. Two ways to fix:\n  1. Reduce \
618                         operations per iteration with `--operations 'pattern,…'` \
619                         (or `--exclude-operations`) to focus the bench on a \
620                         representative subset.\n  2. Drop `--rps` and use \
621                         `--vus {}` alone — closed-model load runs as fast as \
622                         the VU pool allows, bounded by latency, with no per-\
623                         iteration deadline. Expect 1-iteration coverage of ~{} \
624                         operations in {}s.",
625                        rps,
626                        num_ops,
627                        basis,
628                        recommendation,
629                        self.vus.max(5),
630                        num_ops,
631                        Self::parse_duration(&self.duration).unwrap_or(0),
632                    ));
633                } else {
634                    TerminalReporter::print_warning(&format!(
635                        "--vus {} may be insufficient for --rps {} × {} ops/iteration \
636                         (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
637                         and each runs every operation in the spec — required ≈ rps × ops × \
638                         latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
639                         warnings.",
640                        self.vus, rps, num_ops, basis, recommendation,
641                    ));
642                }
643            } else if probe.is_some() {
644                TerminalReporter::print_progress(&format!(
645                    "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
646                     is sufficient for --rps {}",
647                    basis, num_ops, self.vus, rps,
648                ));
649            }
650        }
651
652        let k6_config = K6Config {
653            target_url: self.target.clone(),
654            base_path,
655            scenario,
656            duration_secs: Self::parse_duration(&self.duration)?,
657            max_vus: self.vus,
658            threshold_percentile: self.threshold_percentile.clone(),
659            threshold_ms: self.threshold_ms,
660            max_error_rate: self.max_error_rate,
661            auth_header: self.auth.clone(),
662            custom_headers,
663            skip_tls_verify: self.skip_tls_verify,
664            security_testing_enabled,
665            chunked_request_bodies: self.chunked_request_bodies,
666            target_rps: self.target_rps,
667            no_keep_alive: self.no_keep_alive,
668        };
669
670        let generator = K6ScriptGenerator::new(k6_config, templates);
671        let mut script = generator.generate()?;
672        TerminalReporter::print_success("k6 script generated");
673
674        // Check if any advanced features are enabled
675        let has_advanced_features = self.data_file.is_some()
676            || self.error_rate.is_some()
677            || self.security_test
678            || self.parallel_create.is_some()
679            || self.wafbench_dir.is_some();
680
681        // Enhance script with advanced features
682        if has_advanced_features {
683            script = self.generate_enhanced_script(&script)?;
684        }
685
686        // Add mock server integration code
687        if mock_config.is_mock_server {
688            let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
689            let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
690            let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
691
692            // Insert mock server code after imports
693            if let Some(import_end) = script.find("export const options") {
694                script.insert_str(
695                    import_end,
696                    &format!(
697                        "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
698                        helper_code, setup_code, teardown_code
699                    ),
700                );
701            }
702        }
703
704        // Validate the generated script
705        TerminalReporter::print_progress("Validating k6 script...");
706        let validation_errors = K6ScriptGenerator::validate_script(&script);
707        if !validation_errors.is_empty() {
708            TerminalReporter::print_error("Script validation failed");
709            for error in &validation_errors {
710                eprintln!("  {}", error);
711            }
712            return Err(BenchError::Other(format!(
713                "Generated k6 script has {} validation error(s). Please check the output above.",
714                validation_errors.len()
715            )));
716        }
717        TerminalReporter::print_success("Script validation passed");
718
719        // Write script to file
720        let script_path = if let Some(output) = &self.script_output {
721            output.clone()
722        } else {
723            self.output.join("k6-script.js")
724        };
725
726        if let Some(parent) = script_path.parent() {
727            std::fs::create_dir_all(parent)?;
728        }
729        std::fs::write(&script_path, &script)?;
730        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
731
732        // If generate-only mode, exit here
733        if self.generate_only {
734            println!("\nScript generated successfully. Run it with:");
735            println!("  k6 run {}", script_path.display());
736            return Ok(());
737        }
738
739        // Execute k6
740        TerminalReporter::print_progress("Executing load test...");
741        let executor = K6Executor::new()?;
742
743        std::fs::create_dir_all(&self.output)?;
744
745        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
746
747        // Print results
748        let duration_secs = Self::parse_duration(&self.duration)?;
749        TerminalReporter::print_summary_full(
750            &results,
751            duration_secs,
752            self.no_keep_alive,
753            Some(num_ops),
754        );
755
756        println!("\nResults saved to: {}", self.output.display());
757
758        Ok(())
759    }
760
761    /// Execute multi-target bench testing
762    async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
763        TerminalReporter::print_progress("Parsing targets file...");
764        let targets = parse_targets_file(targets_file)?;
765        let num_targets = targets.len();
766        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
767
768        if targets.is_empty() {
769            return Err(BenchError::Other("No targets found in file".to_string()));
770        }
771
772        // Determine max concurrency
773        let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
774        let max_concurrency = max_concurrency.min(num_targets); // Don't exceed number of targets
775
776        // Print header for multi-target mode
777        TerminalReporter::print_header(
778            &self.get_spec_display_name(),
779            &format!("{} targets", num_targets),
780            0,
781            &self.scenario,
782            Self::parse_duration(&self.duration)?,
783        );
784
785        // Create parallel executor
786        let executor = ParallelExecutor::new(
787            BenchCommand {
788                // Clone all fields except targets_file (we don't need it in the executor)
789                spec: self.spec.clone(),
790                spec_dir: self.spec_dir.clone(),
791                merge_conflicts: self.merge_conflicts.clone(),
792                spec_mode: self.spec_mode.clone(),
793                dependency_config: self.dependency_config.clone(),
794                target: self.target.clone(), // Not used in multi-target mode, but kept for compatibility
795                base_path: self.base_path.clone(),
796                duration: self.duration.clone(),
797                vus: self.vus,
798                target_rps: self.target_rps,
799                no_keep_alive: self.no_keep_alive,
800                scenario: self.scenario.clone(),
801                operations: self.operations.clone(),
802                exclude_operations: self.exclude_operations.clone(),
803                auth: self.auth.clone(),
804                headers: self.headers.clone(),
805                output: self.output.clone(),
806                generate_only: self.generate_only,
807                script_output: self.script_output.clone(),
808                threshold_percentile: self.threshold_percentile.clone(),
809                threshold_ms: self.threshold_ms,
810                max_error_rate: self.max_error_rate,
811                verbose: self.verbose,
812                skip_tls_verify: self.skip_tls_verify,
813                chunked_request_bodies: self.chunked_request_bodies,
814                targets_file: None,
815                max_concurrency: None,
816                results_format: self.results_format.clone(),
817                params_file: self.params_file.clone(),
818                crud_flow: self.crud_flow,
819                flow_config: self.flow_config.clone(),
820                extract_fields: self.extract_fields.clone(),
821                parallel_create: self.parallel_create,
822                data_file: self.data_file.clone(),
823                data_distribution: self.data_distribution.clone(),
824                data_mappings: self.data_mappings.clone(),
825                per_uri_control: self.per_uri_control,
826                error_rate: self.error_rate,
827                error_types: self.error_types.clone(),
828                security_test: self.security_test,
829                security_payloads: self.security_payloads.clone(),
830                security_categories: self.security_categories.clone(),
831                security_target_fields: self.security_target_fields.clone(),
832                wafbench_dir: self.wafbench_dir.clone(),
833                wafbench_cycle_all: self.wafbench_cycle_all,
834                owasp_api_top10: self.owasp_api_top10,
835                owasp_categories: self.owasp_categories.clone(),
836                owasp_auth_header: self.owasp_auth_header.clone(),
837                owasp_auth_token: self.owasp_auth_token.clone(),
838                owasp_admin_paths: self.owasp_admin_paths.clone(),
839                owasp_id_fields: self.owasp_id_fields.clone(),
840                owasp_report: self.owasp_report.clone(),
841                owasp_report_format: self.owasp_report_format.clone(),
842                owasp_iterations: self.owasp_iterations,
843                conformance: false,
844                conformance_api_key: None,
845                conformance_basic_auth: None,
846                conformance_report: PathBuf::from("conformance-report.json"),
847                conformance_categories: None,
848                conformance_report_format: "json".to_string(),
849                conformance_headers: vec![],
850                conformance_all_operations: false,
851                conformance_custom: None,
852                conformance_delay_ms: 0,
853                use_k6: false,
854                conformance_custom_filter: None,
855                export_requests: false,
856                validate_requests: false,
857                conformance_self_test: false,
858                source_ips: Vec::new(),
859                geo_source_ips: Vec::new(),
860                geo_source_headers: Vec::new(),
861                report_missed_cap: None,
862            },
863            targets,
864            max_concurrency,
865        );
866
867        // Execute all targets
868        let start_time = std::time::Instant::now();
869        let aggregated_results = executor.execute_all().await?;
870        let elapsed = start_time.elapsed();
871
872        // Organize and report results
873        self.report_multi_target_results(&aggregated_results, elapsed)?;
874
875        Ok(())
876    }
877
878    /// Report results for multi-target execution
879    fn report_multi_target_results(
880        &self,
881        results: &AggregatedResults,
882        elapsed: std::time::Duration,
883    ) -> Result<()> {
884        // Print summary
885        TerminalReporter::print_multi_target_summary(results);
886
887        // Print elapsed time
888        let total_secs = elapsed.as_secs();
889        let hours = total_secs / 3600;
890        let minutes = (total_secs % 3600) / 60;
891        let seconds = total_secs % 60;
892        if hours > 0 {
893            println!("\n  Total Elapsed Time:   {}h {}m {}s", hours, minutes, seconds);
894        } else if minutes > 0 {
895            println!("\n  Total Elapsed Time:   {}m {}s", minutes, seconds);
896        } else {
897            println!("\n  Total Elapsed Time:   {}s", seconds);
898        }
899
900        // Save aggregated summary if requested
901        if self.results_format == "aggregated" || self.results_format == "both" {
902            let summary_path = self.output.join("aggregated_summary.json");
903            let summary_json = serde_json::json!({
904                "total_elapsed_seconds": elapsed.as_secs(),
905                "total_targets": results.total_targets,
906                "successful_targets": results.successful_targets,
907                "failed_targets": results.failed_targets,
908                "aggregated_metrics": {
909                    "total_requests": results.aggregated_metrics.total_requests,
910                    "total_failed_requests": results.aggregated_metrics.total_failed_requests,
911                    "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
912                    "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
913                    "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
914                    "error_rate": results.aggregated_metrics.error_rate,
915                    "total_rps": results.aggregated_metrics.total_rps,
916                    "avg_rps": results.aggregated_metrics.avg_rps,
917                    "total_vus_max": results.aggregated_metrics.total_vus_max,
918                },
919                "target_results": results.target_results.iter().map(|r| {
920                    serde_json::json!({
921                        "target_url": r.target_url,
922                        "target_index": r.target_index,
923                        "success": r.success,
924                        "error": r.error,
925                        "total_requests": r.results.total_requests,
926                        "failed_requests": r.results.failed_requests,
927                        "avg_duration_ms": r.results.avg_duration_ms,
928                        "min_duration_ms": r.results.min_duration_ms,
929                        "med_duration_ms": r.results.med_duration_ms,
930                        "p90_duration_ms": r.results.p90_duration_ms,
931                        "p95_duration_ms": r.results.p95_duration_ms,
932                        "p99_duration_ms": r.results.p99_duration_ms,
933                        "max_duration_ms": r.results.max_duration_ms,
934                        "rps": r.results.rps,
935                        "vus_max": r.results.vus_max,
936                        "output_dir": r.output_dir.to_string_lossy(),
937                    })
938                }).collect::<Vec<_>>(),
939            });
940
941            std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
942            TerminalReporter::print_success(&format!(
943                "Aggregated summary saved to: {}",
944                summary_path.display()
945            ));
946        }
947
948        // Write CSV with all per-target results for easy parsing
949        let csv_path = self.output.join("all_targets.csv");
950        let mut csv = String::from(
951            "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
952        );
953        for r in &results.target_results {
954            csv.push_str(&format!(
955                "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
956                r.target_url,
957                r.success,
958                r.results.total_requests,
959                r.results.failed_requests,
960                r.results.rps,
961                r.results.vus_max,
962                r.results.min_duration_ms,
963                r.results.avg_duration_ms,
964                r.results.med_duration_ms,
965                r.results.p90_duration_ms,
966                r.results.p95_duration_ms,
967                r.results.p99_duration_ms,
968                r.results.max_duration_ms,
969                r.error.as_deref().unwrap_or(""),
970            ));
971        }
972        let _ = std::fs::write(&csv_path, &csv);
973
974        println!("\nResults saved to: {}", self.output.display());
975        println!("  - Per-target results: {}", self.output.join("target_*").display());
976        println!("  - All targets CSV:    {}", csv_path.display());
977        if self.results_format == "aggregated" || self.results_format == "both" {
978            println!(
979                "  - Aggregated summary: {}",
980                self.output.join("aggregated_summary.json").display()
981            );
982        }
983
984        Ok(())
985    }
986
987    /// Parse duration string (e.g., "30s", "5m", "1h") to seconds
988    pub fn parse_duration(duration: &str) -> Result<u64> {
989        let duration = duration.trim();
990
991        if let Some(secs) = duration.strip_suffix('s') {
992            secs.parse::<u64>()
993                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
994        } else if let Some(mins) = duration.strip_suffix('m') {
995            mins.parse::<u64>()
996                .map(|m| m * 60)
997                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
998        } else if let Some(hours) = duration.strip_suffix('h') {
999            hours
1000                .parse::<u64>()
1001                .map(|h| h * 3600)
1002                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1003        } else {
1004            // Try parsing as seconds without suffix
1005            duration
1006                .parse::<u64>()
1007                .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1008        }
1009    }
1010
1011    /// Parse headers from command line format (Key:Value,Key2:Value2)
1012    pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1013        match &self.headers {
1014            Some(s) => parse_header_string(s),
1015            None => Ok(HashMap::new()),
1016        }
1017    }
1018
1019    fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1020        let extracted_path = output_dir.join("extracted_values.json");
1021        if !extracted_path.exists() {
1022            return Ok(ExtractedValues::new());
1023        }
1024
1025        let content = std::fs::read_to_string(&extracted_path)
1026            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1027        let parsed: serde_json::Value = serde_json::from_str(&content)
1028            .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1029
1030        let mut extracted = ExtractedValues::new();
1031        if let Some(values) = parsed.as_object() {
1032            for (key, value) in values {
1033                extracted.set(key.clone(), value.clone());
1034            }
1035        }
1036
1037        Ok(extracted)
1038    }
1039
1040    /// Resolve the effective base path for API endpoints
1041    ///
1042    /// Priority:
1043    /// 1. CLI --base-path option (if provided, even if empty string)
1044    /// 2. Base path extracted from OpenAPI spec's servers URL
1045    /// 3. None (no base path)
1046    ///
1047    /// An empty string from CLI explicitly disables base path.
1048    fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1049        // CLI option takes priority (including empty string to disable)
1050        if let Some(cli_base_path) = &self.base_path {
1051            if cli_base_path.is_empty() {
1052                // Empty string explicitly means "no base path"
1053                return None;
1054            }
1055            return Some(cli_base_path.clone());
1056        }
1057
1058        // Fall back to spec's base path
1059        parser.get_base_path()
1060    }
1061
1062    /// Build mock server integration configuration
1063    async fn build_mock_config(&self) -> MockIntegrationConfig {
1064        // Check if target looks like a mock server
1065        if MockServerDetector::looks_like_mock_server(&self.target) {
1066            // Try to detect if it's actually a MockForge server
1067            if let Ok(info) = MockServerDetector::detect(&self.target).await {
1068                if info.is_mockforge {
1069                    TerminalReporter::print_success(&format!(
1070                        "Detected MockForge server (version: {})",
1071                        info.version.as_deref().unwrap_or("unknown")
1072                    ));
1073                    return MockIntegrationConfig::mock_server();
1074                }
1075            }
1076        }
1077        MockIntegrationConfig::real_api()
1078    }
1079
1080    /// Build CRUD flow configuration
1081    fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1082        if !self.crud_flow {
1083            return None;
1084        }
1085
1086        // If flow_config file is provided, load it
1087        if let Some(config_path) = &self.flow_config {
1088            match CrudFlowConfig::from_file(config_path) {
1089                Ok(config) => return Some(config),
1090                Err(e) => {
1091                    TerminalReporter::print_warning(&format!(
1092                        "Failed to load flow config: {}. Using auto-detection.",
1093                        e
1094                    ));
1095                }
1096            }
1097        }
1098
1099        // Parse extract fields
1100        let extract_fields = self
1101            .extract_fields
1102            .as_ref()
1103            .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1104            .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1105
1106        Some(CrudFlowConfig {
1107            flows: Vec::new(), // Will be auto-detected
1108            default_extract_fields: extract_fields,
1109        })
1110    }
1111
1112    /// Build data-driven testing configuration
1113    fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1114        let data_file = self.data_file.as_ref()?;
1115
1116        let distribution = DataDistribution::from_str(&self.data_distribution)
1117            .unwrap_or(DataDistribution::UniquePerVu);
1118
1119        let mappings = self
1120            .data_mappings
1121            .as_ref()
1122            .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1123            .unwrap_or_default();
1124
1125        Some(DataDrivenConfig {
1126            file_path: data_file.to_string_lossy().to_string(),
1127            distribution,
1128            mappings,
1129            csv_has_header: true,
1130            per_uri_control: self.per_uri_control,
1131            per_uri_columns: crate::data_driven::PerUriColumns::default(),
1132        })
1133    }
1134
1135    /// Build invalid data testing configuration
1136    fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1137        let error_rate = self.error_rate?;
1138
1139        let error_types = self
1140            .error_types
1141            .as_ref()
1142            .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1143            .unwrap_or_default();
1144
1145        Some(InvalidDataConfig {
1146            error_rate,
1147            error_types,
1148            target_fields: Vec::new(),
1149        })
1150    }
1151
1152    /// Build security testing configuration
1153    fn build_security_config(&self) -> Option<SecurityTestConfig> {
1154        if !self.security_test {
1155            return None;
1156        }
1157
1158        let categories = self
1159            .security_categories
1160            .as_ref()
1161            .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1162            .unwrap_or_else(|| {
1163                let mut default = HashSet::new();
1164                default.insert(SecurityCategory::SqlInjection);
1165                default.insert(SecurityCategory::Xss);
1166                default
1167            });
1168
1169        let target_fields = self
1170            .security_target_fields
1171            .as_ref()
1172            .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1173            .unwrap_or_default();
1174
1175        let custom_payloads_file =
1176            self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1177
1178        Some(SecurityTestConfig {
1179            enabled: true,
1180            categories,
1181            target_fields,
1182            custom_payloads_file,
1183            include_high_risk: false,
1184        })
1185    }
1186
1187    /// Build parallel execution configuration
1188    fn build_parallel_config(&self) -> Option<ParallelConfig> {
1189        let count = self.parallel_create?;
1190
1191        Some(ParallelConfig::new(count))
1192    }
1193
1194    /// Load WAFBench payloads from the specified directory or pattern
1195    fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1196        let Some(ref wafbench_dir) = self.wafbench_dir else {
1197            return Vec::new();
1198        };
1199
1200        let mut loader = WafBenchLoader::new();
1201
1202        if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1203            TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1204            return Vec::new();
1205        }
1206
1207        let stats = loader.stats();
1208
1209        if stats.files_processed == 0 {
1210            TerminalReporter::print_warning(&format!(
1211                "No WAFBench YAML files found matching '{}'",
1212                wafbench_dir
1213            ));
1214            // Also report any parse errors that may explain why no files were processed
1215            if !stats.parse_errors.is_empty() {
1216                TerminalReporter::print_warning("Some files were found but failed to parse:");
1217                for error in &stats.parse_errors {
1218                    TerminalReporter::print_warning(&format!("  - {}", error));
1219                }
1220            }
1221            return Vec::new();
1222        }
1223
1224        TerminalReporter::print_progress(&format!(
1225            "Loaded {} WAFBench files, {} test cases, {} payloads",
1226            stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1227        ));
1228
1229        // Print category breakdown
1230        for (category, count) in &stats.by_category {
1231            TerminalReporter::print_progress(&format!("  - {}: {} tests", category, count));
1232        }
1233
1234        // Report any parse errors
1235        for error in &stats.parse_errors {
1236            TerminalReporter::print_warning(&format!("  Parse error: {}", error));
1237        }
1238
1239        loader.to_security_payloads()
1240    }
1241
1242    /// Generate enhanced k6 script with advanced features
1243    pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1244        let mut enhanced_script = base_script.to_string();
1245        let mut additional_code = String::new();
1246
1247        // Add data-driven testing code
1248        if let Some(config) = self.build_data_driven_config() {
1249            TerminalReporter::print_progress("Adding data-driven testing support...");
1250            additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1251            additional_code.push('\n');
1252            TerminalReporter::print_success("Data-driven testing enabled");
1253        }
1254
1255        // Add invalid data generation code
1256        if let Some(config) = self.build_invalid_data_config() {
1257            TerminalReporter::print_progress("Adding invalid data testing support...");
1258            additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1259            additional_code.push('\n');
1260            additional_code
1261                .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1262            additional_code.push('\n');
1263            additional_code
1264                .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1265            additional_code.push('\n');
1266            TerminalReporter::print_success(&format!(
1267                "Invalid data testing enabled ({}% error rate)",
1268                (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1269            ));
1270        }
1271
1272        // Add security testing code
1273        let security_config = self.build_security_config();
1274        let wafbench_payloads = self.load_wafbench_payloads();
1275        let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1276
1277        if security_config.is_some() || !wafbench_payloads.is_empty() {
1278            TerminalReporter::print_progress("Adding security testing support...");
1279
1280            // Combine built-in payloads with WAFBench payloads
1281            let mut payload_list: Vec<SecurityPayload> = Vec::new();
1282
1283            if let Some(ref config) = security_config {
1284                payload_list.extend(SecurityPayloads::get_payloads(config));
1285            }
1286
1287            // Add WAFBench payloads
1288            if !wafbench_payloads.is_empty() {
1289                TerminalReporter::print_progress(&format!(
1290                    "Loading {} WAFBench attack patterns...",
1291                    wafbench_payloads.len()
1292                ));
1293                payload_list.extend(wafbench_payloads);
1294            }
1295
1296            let target_fields =
1297                security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1298
1299            additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1300                &payload_list,
1301                self.wafbench_cycle_all,
1302            ));
1303            additional_code.push('\n');
1304            additional_code
1305                .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1306            additional_code.push('\n');
1307            additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1308            additional_code.push('\n');
1309
1310            let mode = if self.wafbench_cycle_all {
1311                "cycle-all"
1312            } else {
1313                "random"
1314            };
1315            TerminalReporter::print_success(&format!(
1316                "Security testing enabled ({} payloads, {} mode)",
1317                payload_list.len(),
1318                mode
1319            ));
1320        } else if security_requested {
1321            // User requested security testing (e.g., --wafbench-dir) but no payloads were loaded.
1322            // The template has security_testing_enabled=true so it renders calling code.
1323            // We must inject stub definitions to avoid undefined function references.
1324            TerminalReporter::print_warning(
1325                "Security testing was requested but no payloads were loaded. \
1326                 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1327            );
1328            additional_code
1329                .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1330            additional_code.push('\n');
1331            additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1332            additional_code.push('\n');
1333        }
1334
1335        // Add parallel execution code
1336        if let Some(config) = self.build_parallel_config() {
1337            TerminalReporter::print_progress("Adding parallel execution support...");
1338            additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1339            additional_code.push('\n');
1340            TerminalReporter::print_success(&format!(
1341                "Parallel execution enabled (count: {})",
1342                config.count
1343            ));
1344        }
1345
1346        // Insert additional code after the imports section
1347        if !additional_code.is_empty() {
1348            // Find the end of the import section
1349            if let Some(import_end) = enhanced_script.find("export const options") {
1350                enhanced_script.insert_str(
1351                    import_end,
1352                    &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1353                );
1354            }
1355        }
1356
1357        Ok(enhanced_script)
1358    }
1359
1360    /// Execute specs sequentially with dependency ordering and value passing
1361    async fn execute_sequential_specs(&self) -> Result<()> {
1362        TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1363
1364        // Load all specs (without merging)
1365        let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1366
1367        if !self.spec.is_empty() {
1368            let specs = load_specs_from_files(self.spec.clone())
1369                .await
1370                .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1371            all_specs.extend(specs);
1372        }
1373
1374        if let Some(spec_dir) = &self.spec_dir {
1375            let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1376                BenchError::Other(format!("Failed to load specs from directory: {}", e))
1377            })?;
1378            all_specs.extend(dir_specs);
1379        }
1380
1381        if all_specs.is_empty() {
1382            return Err(BenchError::Other(
1383                "No spec files found for sequential execution".to_string(),
1384            ));
1385        }
1386
1387        TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1388
1389        // Load dependency config or auto-detect
1390        let execution_order = if let Some(config_path) = &self.dependency_config {
1391            TerminalReporter::print_progress("Loading dependency configuration...");
1392            let config = SpecDependencyConfig::from_file(config_path)?;
1393
1394            if !config.disable_auto_detect && config.execution_order.is_empty() {
1395                // Auto-detect if config doesn't specify order
1396                self.detect_and_sort_specs(&all_specs)?
1397            } else {
1398                // Use configured order
1399                config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1400            }
1401        } else {
1402            // Auto-detect dependencies
1403            self.detect_and_sort_specs(&all_specs)?
1404        };
1405
1406        TerminalReporter::print_success(&format!(
1407            "Execution order: {}",
1408            execution_order
1409                .iter()
1410                .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1411                .collect::<Vec<_>>()
1412                .join(" → ")
1413        ));
1414
1415        // Execute each spec in order
1416        let mut extracted_values = ExtractedValues::new();
1417        let total_specs = execution_order.len();
1418
1419        for (index, spec_path) in execution_order.iter().enumerate() {
1420            let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1421
1422            TerminalReporter::print_progress(&format!(
1423                "[{}/{}] Executing spec: {}",
1424                index + 1,
1425                total_specs,
1426                spec_name
1427            ));
1428
1429            // Find the spec in our loaded specs (match by full path or filename)
1430            let spec = all_specs
1431                .iter()
1432                .find(|(p, _)| {
1433                    p == spec_path
1434                        || p.file_name() == spec_path.file_name()
1435                        || p.file_name() == Some(spec_path.as_os_str())
1436                })
1437                .map(|(_, s)| s.clone())
1438                .ok_or_else(|| {
1439                    BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1440                })?;
1441
1442            // Execute this spec with any extracted values from previous specs
1443            let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1444
1445            // Merge extracted values for the next spec
1446            extracted_values.merge(&new_values);
1447
1448            TerminalReporter::print_success(&format!(
1449                "[{}/{}] Completed: {} (extracted {} values)",
1450                index + 1,
1451                total_specs,
1452                spec_name,
1453                new_values.values.len()
1454            ));
1455        }
1456
1457        TerminalReporter::print_success(&format!(
1458            "Sequential execution complete: {} specs executed",
1459            total_specs
1460        ));
1461
1462        Ok(())
1463    }
1464
1465    /// Detect dependencies and return topologically sorted spec paths
1466    fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1467        TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1468
1469        let mut detector = DependencyDetector::new();
1470        let dependencies = detector.detect_dependencies(specs);
1471
1472        if dependencies.is_empty() {
1473            TerminalReporter::print_progress("No dependencies detected, using file order");
1474            return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1475        }
1476
1477        TerminalReporter::print_progress(&format!(
1478            "Detected {} cross-spec dependencies",
1479            dependencies.len()
1480        ));
1481
1482        for dep in &dependencies {
1483            TerminalReporter::print_progress(&format!(
1484                "  {} → {} (via field '{}')",
1485                dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1486                dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1487                dep.field_name
1488            ));
1489        }
1490
1491        topological_sort(specs, &dependencies)
1492    }
1493
1494    /// Execute a single spec and extract values for dependent specs
1495    async fn execute_single_spec(
1496        &self,
1497        spec: &OpenApiSpec,
1498        spec_name: &str,
1499        _external_values: &ExtractedValues,
1500    ) -> Result<ExtractedValues> {
1501        let parser = SpecParser::from_spec(spec.clone());
1502
1503        // For now, we execute in CRUD flow mode if enabled, otherwise standard mode
1504        if self.crud_flow {
1505            // Execute CRUD flow and extract values
1506            self.execute_crud_flow_with_extraction(&parser, spec_name).await
1507        } else {
1508            // Execute standard benchmark (no value extraction in non-CRUD mode)
1509            self.execute_standard_spec(&parser, spec_name).await?;
1510            Ok(ExtractedValues::new())
1511        }
1512    }
1513
1514    /// Execute CRUD flow with value extraction for sequential mode
1515    async fn execute_crud_flow_with_extraction(
1516        &self,
1517        parser: &SpecParser,
1518        spec_name: &str,
1519    ) -> Result<ExtractedValues> {
1520        let operations = parser.get_operations();
1521        let flows = CrudFlowDetector::detect_flows(&operations);
1522
1523        if flows.is_empty() {
1524            TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1525            return Ok(ExtractedValues::new());
1526        }
1527
1528        TerminalReporter::print_progress(&format!(
1529            "  {} CRUD flow(s) in {}",
1530            flows.len(),
1531            spec_name
1532        ));
1533
1534        // Generate and execute the CRUD flow script
1535        let mut handlebars = handlebars::Handlebars::new();
1536        // Register json helper for serializing arrays/objects in templates
1537        handlebars.register_helper(
1538            "json",
1539            Box::new(
1540                |h: &handlebars::Helper,
1541                 _: &handlebars::Handlebars,
1542                 _: &handlebars::Context,
1543                 _: &mut handlebars::RenderContext,
1544                 out: &mut dyn handlebars::Output|
1545                 -> handlebars::HelperResult {
1546                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1547                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1548                    Ok(())
1549                },
1550            ),
1551        );
1552        let template = include_str!("templates/k6_crud_flow.hbs");
1553        let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1554
1555        let custom_headers = self.parse_headers()?;
1556        let config = self.build_crud_flow_config().unwrap_or_default();
1557
1558        // Load parameter overrides if provided (for body configurations)
1559        let param_overrides = if let Some(params_file) = &self.params_file {
1560            let overrides = ParameterOverrides::from_file(params_file)?;
1561            Some(overrides)
1562        } else {
1563            None
1564        };
1565
1566        // Generate stages from scenario
1567        let duration_secs = Self::parse_duration(&self.duration)?;
1568        let scenario =
1569            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1570        let stages = scenario.generate_stages(duration_secs, self.vus);
1571
1572        // Resolve base path (CLI option takes priority over spec's servers URL)
1573        let api_base_path = self.resolve_base_path(parser);
1574
1575        // Build headers JSON string for the template
1576        let mut all_headers = custom_headers.clone();
1577        if let Some(auth) = &self.auth {
1578            all_headers.insert("Authorization".to_string(), auth.clone());
1579        }
1580        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1581
1582        // Track all dynamic placeholders across all operations
1583        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1584
1585        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1586            // Use the metric-name sanitizer (caps at 112 chars + hash suffix)
1587            // so deeply nested flow names don't blow past k6's 128-char limit
1588            // when concatenated with `_step{i}_latency`. See issue #79.
1589            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1590            serde_json::json!({
1591                "name": sanitized_name.clone(),
1592                "display_name": f.name,
1593                "base_path": f.base_path,
1594                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1595                    // Parse operation to get method and path
1596                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1597                    let method_raw = if !parts.is_empty() {
1598                        parts[0].to_uppercase()
1599                    } else {
1600                        "GET".to_string()
1601                    };
1602                    let method = if !parts.is_empty() {
1603                        let m = parts[0].to_lowercase();
1604                        // k6 uses 'del' for DELETE
1605                        if m == "delete" { "del".to_string() } else { m }
1606                    } else {
1607                        "get".to_string()
1608                    };
1609                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1610                    // Prepend API base path if configured
1611                    let path = if let Some(ref bp) = api_base_path {
1612                        format!("{}{}", bp, raw_path)
1613                    } else {
1614                        raw_path.to_string()
1615                    };
1616                    let is_get_or_head = method == "get" || method == "head";
1617                    // POST, PUT, PATCH typically have bodies
1618                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1619
1620                    // Look up body from params file if available
1621                    let body_value = if has_body {
1622                        param_overrides.as_ref()
1623                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1624                            .and_then(|oo| oo.body)
1625                            .unwrap_or_else(|| serde_json::json!({}))
1626                    } else {
1627                        serde_json::json!({})
1628                    };
1629
1630                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1631                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1632
1633                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
1634                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1635                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1636
1637                    serde_json::json!({
1638                        "operation": s.operation,
1639                        "method": method,
1640                        "path": path,
1641                        "extract": s.extract,
1642                        "use_values": s.use_values,
1643                        "use_body": s.use_body,
1644                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1645                        "inject_attacks": s.inject_attacks,
1646                        "attack_types": s.attack_types,
1647                        "description": s.description,
1648                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1649                        "is_get_or_head": is_get_or_head,
1650                        "has_body": has_body,
1651                        "body": processed_body.value,
1652                        "body_is_dynamic": body_is_dynamic,
1653                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1654                    })
1655                }).collect::<Vec<_>>(),
1656            })
1657        }).collect();
1658
1659        // Collect all placeholders from all steps
1660        for flow_data in &flows_data {
1661            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1662                for step in steps {
1663                    if let Some(placeholders_arr) =
1664                        step.get("_placeholders").and_then(|p| p.as_array())
1665                    {
1666                        for p_str in placeholders_arr {
1667                            if let Some(p_name) = p_str.as_str() {
1668                                match p_name {
1669                                    "VU" => {
1670                                        all_placeholders.insert(DynamicPlaceholder::VU);
1671                                    }
1672                                    "Iteration" => {
1673                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
1674                                    }
1675                                    "Timestamp" => {
1676                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
1677                                    }
1678                                    "UUID" => {
1679                                        all_placeholders.insert(DynamicPlaceholder::UUID);
1680                                    }
1681                                    "Random" => {
1682                                        all_placeholders.insert(DynamicPlaceholder::Random);
1683                                    }
1684                                    "Counter" => {
1685                                        all_placeholders.insert(DynamicPlaceholder::Counter);
1686                                    }
1687                                    "Date" => {
1688                                        all_placeholders.insert(DynamicPlaceholder::Date);
1689                                    }
1690                                    "VuIter" => {
1691                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
1692                                    }
1693                                    _ => {}
1694                                }
1695                            }
1696                        }
1697                    }
1698                }
1699            }
1700        }
1701
1702        // Get required imports and globals based on placeholders used
1703        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1704        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1705
1706        // Check if security testing is enabled
1707        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1708
1709        let data = serde_json::json!({
1710            "base_url": self.target,
1711            "flows": flows_data,
1712            "extract_fields": config.default_extract_fields,
1713            "duration_secs": duration_secs,
1714            "max_vus": self.vus,
1715            "auth_header": self.auth,
1716            "custom_headers": custom_headers,
1717            "skip_tls_verify": self.skip_tls_verify,
1718            // Add missing template fields
1719            "stages": stages.iter().map(|s| serde_json::json!({
1720                "duration": s.duration,
1721                "target": s.target,
1722            })).collect::<Vec<_>>(),
1723            "threshold_percentile": self.threshold_percentile,
1724            "threshold_ms": self.threshold_ms,
1725            "max_error_rate": self.max_error_rate,
1726            "headers": headers_json,
1727            "dynamic_imports": required_imports,
1728            "dynamic_globals": required_globals,
1729            "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1730            // Security testing settings
1731            "security_testing_enabled": security_testing_enabled,
1732            "has_custom_headers": !custom_headers.is_empty(),
1733        });
1734
1735        let mut script = handlebars
1736            .render_template(template, &data)
1737            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1738
1739        // Enhance script with security testing support if enabled
1740        if security_testing_enabled {
1741            script = self.generate_enhanced_script(&script)?;
1742        }
1743
1744        // Write and execute script
1745        let script_path =
1746            self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1747
1748        std::fs::create_dir_all(self.output.clone())?;
1749        std::fs::write(&script_path, &script)?;
1750
1751        if !self.generate_only {
1752            let executor = K6Executor::new()?;
1753            std::fs::create_dir_all(&output_dir)?;
1754
1755            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1756
1757            let extracted = Self::parse_extracted_values(&output_dir)?;
1758            TerminalReporter::print_progress(&format!(
1759                "  Extracted {} value(s) from {}",
1760                extracted.values.len(),
1761                spec_name
1762            ));
1763            return Ok(extracted);
1764        }
1765
1766        Ok(ExtractedValues::new())
1767    }
1768
1769    /// Execute standard (non-CRUD) spec benchmark
1770    async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1771        let mut operations = if let Some(filter) = &self.operations {
1772            parser.filter_operations(filter)?
1773        } else {
1774            parser.get_operations()
1775        };
1776
1777        if let Some(exclude) = &self.exclude_operations {
1778            operations = parser.exclude_operations(operations, exclude)?;
1779        }
1780
1781        if operations.is_empty() {
1782            TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1783            return Ok(());
1784        }
1785
1786        TerminalReporter::print_progress(&format!(
1787            "  {} operations in {}",
1788            operations.len(),
1789            spec_name
1790        ));
1791
1792        // Generate request templates
1793        let templates: Vec<_> = operations
1794            .iter()
1795            .map(RequestGenerator::generate_template)
1796            .collect::<Result<Vec<_>>>()?;
1797
1798        // Parse headers
1799        let custom_headers = self.parse_headers()?;
1800
1801        // Resolve base path
1802        let base_path = self.resolve_base_path(parser);
1803
1804        // Generate k6 script
1805        let scenario =
1806            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1807
1808        let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1809
1810        let k6_config = K6Config {
1811            target_url: self.target.clone(),
1812            base_path,
1813            scenario,
1814            duration_secs: Self::parse_duration(&self.duration)?,
1815            max_vus: self.vus,
1816            threshold_percentile: self.threshold_percentile.clone(),
1817            threshold_ms: self.threshold_ms,
1818            max_error_rate: self.max_error_rate,
1819            auth_header: self.auth.clone(),
1820            custom_headers,
1821            skip_tls_verify: self.skip_tls_verify,
1822            security_testing_enabled,
1823            chunked_request_bodies: self.chunked_request_bodies,
1824            target_rps: self.target_rps,
1825            no_keep_alive: self.no_keep_alive,
1826        };
1827
1828        let generator = K6ScriptGenerator::new(k6_config, templates);
1829        let mut script = generator.generate()?;
1830
1831        // Enhance script with advanced features (security testing, etc.)
1832        let has_advanced_features = self.data_file.is_some()
1833            || self.error_rate.is_some()
1834            || self.security_test
1835            || self.parallel_create.is_some()
1836            || self.wafbench_dir.is_some();
1837
1838        if has_advanced_features {
1839            script = self.generate_enhanced_script(&script)?;
1840        }
1841
1842        // Write and execute script
1843        let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1844
1845        std::fs::create_dir_all(self.output.clone())?;
1846        std::fs::write(&script_path, &script)?;
1847
1848        if !self.generate_only {
1849            let executor = K6Executor::new()?;
1850            let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1851            std::fs::create_dir_all(&output_dir)?;
1852
1853            executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1854        }
1855
1856        Ok(())
1857    }
1858
1859    /// Execute CRUD flow testing mode
1860    async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1861        // Check if a custom flow config is provided
1862        let config = self.build_crud_flow_config().unwrap_or_default();
1863
1864        // Use flows from config if provided, otherwise auto-detect
1865        let flows = if !config.flows.is_empty() {
1866            TerminalReporter::print_progress("Using custom flow configuration...");
1867            config.flows.clone()
1868        } else {
1869            TerminalReporter::print_progress("Detecting CRUD operations...");
1870            let operations = parser.get_operations();
1871            CrudFlowDetector::detect_flows(&operations)
1872        };
1873
1874        if flows.is_empty() {
1875            return Err(BenchError::Other(
1876                "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1877            ));
1878        }
1879
1880        if config.flows.is_empty() {
1881            TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1882        } else {
1883            TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1884        }
1885
1886        for flow in &flows {
1887            TerminalReporter::print_progress(&format!(
1888                "  - {}: {} steps",
1889                flow.name,
1890                flow.steps.len()
1891            ));
1892        }
1893
1894        // Generate CRUD flow script
1895        let mut handlebars = handlebars::Handlebars::new();
1896        // Register json helper for serializing arrays/objects in templates
1897        handlebars.register_helper(
1898            "json",
1899            Box::new(
1900                |h: &handlebars::Helper,
1901                 _: &handlebars::Handlebars,
1902                 _: &handlebars::Context,
1903                 _: &mut handlebars::RenderContext,
1904                 out: &mut dyn handlebars::Output|
1905                 -> handlebars::HelperResult {
1906                    let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1907                    out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1908                    Ok(())
1909                },
1910            ),
1911        );
1912        let template = include_str!("templates/k6_crud_flow.hbs");
1913
1914        let custom_headers = self.parse_headers()?;
1915
1916        // Load parameter overrides if provided (for body configurations)
1917        let param_overrides = if let Some(params_file) = &self.params_file {
1918            TerminalReporter::print_progress("Loading parameter overrides...");
1919            let overrides = ParameterOverrides::from_file(params_file)?;
1920            TerminalReporter::print_success(&format!(
1921                "Loaded parameter overrides ({} operation-specific, {} defaults)",
1922                overrides.operations.len(),
1923                if overrides.defaults.is_empty() { 0 } else { 1 }
1924            ));
1925            Some(overrides)
1926        } else {
1927            None
1928        };
1929
1930        // Generate stages from scenario
1931        let duration_secs = Self::parse_duration(&self.duration)?;
1932        let scenario =
1933            LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1934        let stages = scenario.generate_stages(duration_secs, self.vus);
1935
1936        // Resolve base path (CLI option takes priority over spec's servers URL)
1937        let api_base_path = self.resolve_base_path(parser);
1938        if let Some(ref bp) = api_base_path {
1939            TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1940        }
1941
1942        // Build headers JSON string for the template
1943        let mut all_headers = custom_headers.clone();
1944        if let Some(auth) = &self.auth {
1945            all_headers.insert("Authorization".to_string(), auth.clone());
1946        }
1947        let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1948
1949        // Track all dynamic placeholders across all operations
1950        let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1951
1952        let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1953            // Sanitize flow name for use as JavaScript variable and k6 metric names.
1954            // Use the metric-name sanitizer (caps at 112 chars + hash suffix) so
1955            // deeply nested flow names don't blow past k6's 128-char limit when
1956            // concatenated with `_step{i}_latency`. See issue #79.
1957            let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1958            serde_json::json!({
1959                "name": sanitized_name.clone(),  // Use sanitized name for variable names
1960                "display_name": f.name,          // Keep original for comments/display
1961                "base_path": f.base_path,
1962                "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1963                    // Parse operation to get method and path
1964                    let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1965                    let method_raw = if !parts.is_empty() {
1966                        parts[0].to_uppercase()
1967                    } else {
1968                        "GET".to_string()
1969                    };
1970                    let method = if !parts.is_empty() {
1971                        let m = parts[0].to_lowercase();
1972                        // k6 uses 'del' for DELETE
1973                        if m == "delete" { "del".to_string() } else { m }
1974                    } else {
1975                        "get".to_string()
1976                    };
1977                    let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1978                    // Prepend API base path if configured
1979                    let path = if let Some(ref bp) = api_base_path {
1980                        format!("{}{}", bp, raw_path)
1981                    } else {
1982                        raw_path.to_string()
1983                    };
1984                    let is_get_or_head = method == "get" || method == "head";
1985                    // POST, PUT, PATCH typically have bodies
1986                    let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1987
1988                    // Look up body from params file if available (use raw_path for matching)
1989                    let body_value = if has_body {
1990                        param_overrides.as_ref()
1991                            .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1992                            .and_then(|oo| oo.body)
1993                            .unwrap_or_else(|| serde_json::json!({}))
1994                    } else {
1995                        serde_json::json!({})
1996                    };
1997
1998                    // Process body for dynamic placeholders like ${__VU}, ${__ITER}, etc.
1999                    let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2000                    // Note: all_placeholders is captured by the closure but we can't mutate it directly
2001                    // We'll collect placeholders separately below
2002
2003                    // Also check for ${extracted.xxx} placeholders which need runtime substitution
2004                    let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2005                    let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2006
2007                    serde_json::json!({
2008                        "operation": s.operation,
2009                        "method": method,
2010                        "path": path,
2011                        "extract": s.extract,
2012                        "use_values": s.use_values,
2013                        "use_body": s.use_body,
2014                        "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2015                        "inject_attacks": s.inject_attacks,
2016                        "attack_types": s.attack_types,
2017                        "description": s.description,
2018                        "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2019                        "is_get_or_head": is_get_or_head,
2020                        "has_body": has_body,
2021                        "body": processed_body.value,
2022                        "body_is_dynamic": body_is_dynamic,
2023                        "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2024                    })
2025                }).collect::<Vec<_>>(),
2026            })
2027        }).collect();
2028
2029        // Collect all placeholders from all steps
2030        for flow_data in &flows_data {
2031            if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2032                for step in steps {
2033                    if let Some(placeholders_arr) =
2034                        step.get("_placeholders").and_then(|p| p.as_array())
2035                    {
2036                        for p_str in placeholders_arr {
2037                            if let Some(p_name) = p_str.as_str() {
2038                                // Parse placeholder from debug string
2039                                match p_name {
2040                                    "VU" => {
2041                                        all_placeholders.insert(DynamicPlaceholder::VU);
2042                                    }
2043                                    "Iteration" => {
2044                                        all_placeholders.insert(DynamicPlaceholder::Iteration);
2045                                    }
2046                                    "Timestamp" => {
2047                                        all_placeholders.insert(DynamicPlaceholder::Timestamp);
2048                                    }
2049                                    "UUID" => {
2050                                        all_placeholders.insert(DynamicPlaceholder::UUID);
2051                                    }
2052                                    "Random" => {
2053                                        all_placeholders.insert(DynamicPlaceholder::Random);
2054                                    }
2055                                    "Counter" => {
2056                                        all_placeholders.insert(DynamicPlaceholder::Counter);
2057                                    }
2058                                    "Date" => {
2059                                        all_placeholders.insert(DynamicPlaceholder::Date);
2060                                    }
2061                                    "VuIter" => {
2062                                        all_placeholders.insert(DynamicPlaceholder::VuIter);
2063                                    }
2064                                    _ => {}
2065                                }
2066                            }
2067                        }
2068                    }
2069                }
2070            }
2071        }
2072
2073        // Get required imports and globals based on placeholders used
2074        let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2075        let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2076
2077        // Build invalid data config if error injection is enabled
2078        let invalid_data_config = self.build_invalid_data_config();
2079        let error_injection_enabled = invalid_data_config.is_some();
2080        let error_rate = self.error_rate.unwrap_or(0.0);
2081        let error_types: Vec<String> = invalid_data_config
2082            .as_ref()
2083            .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2084            .unwrap_or_default();
2085
2086        if error_injection_enabled {
2087            TerminalReporter::print_progress(&format!(
2088                "Error injection enabled ({}% rate)",
2089                (error_rate * 100.0) as u32
2090            ));
2091        }
2092
2093        // Check if security testing is enabled
2094        let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2095
2096        let data = serde_json::json!({
2097            "base_url": self.target,
2098            "flows": flows_data,
2099            "extract_fields": config.default_extract_fields,
2100            "duration_secs": duration_secs,
2101            "max_vus": self.vus,
2102            "auth_header": self.auth,
2103            "custom_headers": custom_headers,
2104            "skip_tls_verify": self.skip_tls_verify,
2105            // Add missing template fields
2106            "stages": stages.iter().map(|s| serde_json::json!({
2107                "duration": s.duration,
2108                "target": s.target,
2109            })).collect::<Vec<_>>(),
2110            "threshold_percentile": self.threshold_percentile,
2111            "threshold_ms": self.threshold_ms,
2112            "max_error_rate": self.max_error_rate,
2113            "headers": headers_json,
2114            "dynamic_imports": required_imports,
2115            "dynamic_globals": required_globals,
2116            "extracted_values_output_path": self
2117                .output
2118                .join("crud_flow_extracted_values.json")
2119                .to_string_lossy(),
2120            // Error injection settings
2121            "error_injection_enabled": error_injection_enabled,
2122            "error_rate": error_rate,
2123            "error_types": error_types,
2124            // Security testing settings
2125            "security_testing_enabled": security_testing_enabled,
2126            "has_custom_headers": !custom_headers.is_empty(),
2127        });
2128
2129        let mut script = handlebars
2130            .render_template(template, &data)
2131            .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2132
2133        // Enhance script with security testing support if enabled
2134        if security_testing_enabled {
2135            script = self.generate_enhanced_script(&script)?;
2136        }
2137
2138        // Validate the generated CRUD flow script
2139        TerminalReporter::print_progress("Validating CRUD flow script...");
2140        let validation_errors = K6ScriptGenerator::validate_script(&script);
2141        if !validation_errors.is_empty() {
2142            TerminalReporter::print_error("CRUD flow script validation failed");
2143            for error in &validation_errors {
2144                eprintln!("  {}", error);
2145            }
2146            return Err(BenchError::Other(format!(
2147                "CRUD flow script validation failed with {} error(s)",
2148                validation_errors.len()
2149            )));
2150        }
2151
2152        TerminalReporter::print_success("CRUD flow script generated");
2153
2154        // Write and execute script
2155        let script_path = if let Some(output) = &self.script_output {
2156            output.clone()
2157        } else {
2158            self.output.join("k6-crud-flow-script.js")
2159        };
2160
2161        if let Some(parent) = script_path.parent() {
2162            std::fs::create_dir_all(parent)?;
2163        }
2164        std::fs::write(&script_path, &script)?;
2165        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2166
2167        if self.generate_only {
2168            println!("\nScript generated successfully. Run it with:");
2169            println!("  k6 run {}", script_path.display());
2170            return Ok(());
2171        }
2172
2173        // Execute k6
2174        TerminalReporter::print_progress("Executing CRUD flow test...");
2175        let executor = K6Executor::new()?;
2176        std::fs::create_dir_all(&self.output)?;
2177
2178        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2179
2180        let duration_secs = Self::parse_duration(&self.duration)?;
2181        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2182
2183        Ok(())
2184    }
2185
2186    /// Execute OpenAPI 3.0.0 conformance testing mode
2187    async fn execute_conformance_test(&self) -> Result<()> {
2188        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2189        use crate::conformance::report::ConformanceReport;
2190        use crate::conformance::spec::ConformanceFeature;
2191
2192        TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2193
2194        // Conformance testing is a functional correctness check (1 VU, 1 iteration).
2195        // --vus and -d flags are always ignored in this mode.
2196        TerminalReporter::print_progress(
2197            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2198        );
2199
2200        // Parse category filter
2201        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2202            cats_str
2203                .split(',')
2204                .filter_map(|s| {
2205                    let trimmed = s.trim();
2206                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2207                        Some(canonical.to_string())
2208                    } else {
2209                        TerminalReporter::print_warning(&format!(
2210                            "Unknown conformance category: '{}'. Valid categories: {}",
2211                            trimmed,
2212                            ConformanceFeature::cli_category_names()
2213                                .iter()
2214                                .map(|(cli, _)| *cli)
2215                                .collect::<Vec<_>>()
2216                                .join(", ")
2217                        ));
2218                        None
2219                    }
2220                })
2221                .collect::<Vec<String>>()
2222        });
2223
2224        // Parse custom headers from "Key: Value" format
2225        let custom_headers: Vec<(String, String)> = self
2226            .conformance_headers
2227            .iter()
2228            .filter_map(|h| {
2229                let (name, value) = h.split_once(':')?;
2230                Some((name.trim().to_string(), value.trim().to_string()))
2231            })
2232            .collect();
2233
2234        if !custom_headers.is_empty() {
2235            TerminalReporter::print_progress(&format!(
2236                "Using {} custom header(s) for authentication",
2237                custom_headers.len()
2238            ));
2239        }
2240
2241        if self.conformance_delay_ms > 0 {
2242            TerminalReporter::print_progress(&format!(
2243                "Using {}ms delay between conformance requests",
2244                self.conformance_delay_ms
2245            ));
2246        }
2247
2248        // Ensure output dir exists so canonicalize works for the report path
2249        std::fs::create_dir_all(&self.output)?;
2250
2251        let config = ConformanceConfig {
2252            target_url: self.target.clone(),
2253            api_key: self.conformance_api_key.clone(),
2254            basic_auth: self.conformance_basic_auth.clone(),
2255            skip_tls_verify: self.skip_tls_verify,
2256            categories,
2257            base_path: self.base_path.clone(),
2258            custom_headers,
2259            output_dir: Some(self.output.clone()),
2260            all_operations: self.conformance_all_operations,
2261            custom_checks_file: self.conformance_custom.clone(),
2262            request_delay_ms: self.conformance_delay_ms,
2263            custom_filter: self.conformance_custom_filter.clone(),
2264            export_requests: self.export_requests,
2265            validate_requests: self.validate_requests,
2266        };
2267
2268        // Branch: spec-driven mode vs reference mode
2269        // Annotate operations if spec is provided (used by both native and k6 paths)
2270        // Round 18.1 — resolve the spec's base path so the self-test
2271        // path can prepend it to every URL. Pre-fix, self-test
2272        // ignored `--base-path /api` and hit the bare spec path,
2273        // returning 404 for every request on specs whose server is
2274        // proxied behind a base prefix.
2275        let mut resolved_base_path: Option<String> = None;
2276        let annotated_ops = if !self.spec.is_empty() {
2277            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2278            let parser = SpecParser::from_file(&self.spec[0]).await?;
2279            resolved_base_path = self.resolve_base_path(&parser);
2280
2281            // Issue #79 round 12 — Srikanth ran `--conformance --operations "GET,POST"`
2282            // and saw DELETE/PATCH exercised anyway. Conformance silently ignored
2283            // the filter. Apply it (and `--exclude-operations`) the same way the
2284            // regular bench path does so users can scope the run.
2285            let mut operations = if let Some(filter) = &self.operations {
2286                parser.filter_operations(filter)?
2287            } else {
2288                parser.get_operations()
2289            };
2290            if let Some(exclude) = &self.exclude_operations {
2291                let before_count = operations.len();
2292                operations = parser.exclude_operations(operations, exclude)?;
2293                let excluded_count = before_count - operations.len();
2294                if excluded_count > 0 {
2295                    TerminalReporter::print_progress(&format!(
2296                        "Excluded {} operations matching '{}'",
2297                        excluded_count, exclude
2298                    ));
2299                }
2300            }
2301
2302            let annotated =
2303                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2304                    &operations,
2305                    parser.spec(),
2306                );
2307            TerminalReporter::print_success(&format!(
2308                "Analyzed {} operations, found {} feature annotations",
2309                operations.len(),
2310                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2311            ));
2312            Some(annotated)
2313        } else {
2314            None
2315        };
2316
2317        // Issue #79 round 13 (4) — `--conformance-self-test` replaces
2318        // the standard conformance run with a positive + per-category
2319        // negative driver that verifies the server actually rejects
2320        // bad requests with 4xx. Wires the spec-annotated operations
2321        // through `conformance::self_test::run_self_test` and prints
2322        // the resulting pass/fail matrix.
2323        if self.conformance_self_test {
2324            let Some(ops) = annotated_ops else {
2325                TerminalReporter::print_error(
2326                    "--conformance-self-test requires --spec; no operations to test",
2327                );
2328                return Ok(());
2329            };
2330            let cfg = crate::conformance::self_test::SelfTestConfig {
2331                target_url: self.target.clone(),
2332                skip_tls_verify: self.skip_tls_verify,
2333                timeout: std::time::Duration::from_secs(30),
2334                // `custom_headers` was already moved into the
2335                // `ConformanceConfig` above; re-derive from `self` so
2336                // we don't borrow it twice.
2337                extra_headers: self
2338                    .conformance_headers
2339                    .iter()
2340                    .filter_map(|h| {
2341                        let (n, v) = h.split_once(':')?;
2342                        Some((n.trim().to_string(), v.trim().to_string()))
2343                    })
2344                    .collect(),
2345                delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2346                // Round 18.1 — honour `--base-path` (or the spec's
2347                // own first server prefix) so a deployment served
2348                // under a path-prefix doesn't 404 every positive.
2349                base_path: resolved_base_path.clone(),
2350                // Round 18.5 — GEODB multi-source-IP. Parse CLI IP
2351                // lists (malformed entries log a warning and are
2352                // dropped). Empty lists keep pre-18.5 behaviour.
2353                source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2354                geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2355                geo_source_headers: if self.geo_source_headers.is_empty() {
2356                    crate::conformance::self_test::default_geo_source_headers()
2357                } else {
2358                    self.geo_source_headers.clone()
2359                },
2360            };
2361            TerminalReporter::print_progress(&format!(
2362                "Self-test mode: driving {} operations with positive + per-category negative cases",
2363                ops.len()
2364            ));
2365            let report = crate::conformance::self_test::run_self_test(&ops, &cfg)
2366                .await
2367                .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2368            TerminalReporter::print_progress(&report.render_summary());
2369            // Persist the JSON report alongside the regular conformance
2370            // report so it's grep-able next to the buffer dump from the
2371            // admin endpoint.
2372            let json_path = self.output.join("conformance-self-test.json");
2373            if let Ok(json) = serde_json::to_string_pretty(&report) {
2374                let _ = std::fs::write(&json_path, json);
2375                TerminalReporter::print_progress(&format!(
2376                    "Self-test report written to {}",
2377                    json_path.display()
2378                ));
2379            }
2380            // Round 18.1 — surface the "every positive failed with
2381            // the same status" case loudly. Without this, a user
2382            // who forgot `--base-path /api` saw 404 for every
2383            // request, but the per-category negative rollup looked
2384            // all-green (because 404 is in the 4xx range the
2385            // negatives expect). Now the run is correctly called
2386            // out as misconfigured before showing the (meaningless)
2387            // negative results.
2388            if let Some(status) = report.detect_target_misconfiguration() {
2389                let hint = match status {
2390                    404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2391                    401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2392                    _ => "",
2393                };
2394                TerminalReporter::print_warning(&format!(
2395                    "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2396                ));
2397            } else if !report.all_passed() {
2398                TerminalReporter::print_warning(
2399                    "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2400                );
2401            } else {
2402                TerminalReporter::print_success(
2403                    "Self-test passed — all positive cases accepted and all negative cases rejected",
2404                );
2405            }
2406            // Round 17.6 — emit a self-contained HTML report alongside
2407            // the JSON. Groups by category and surfaces the missed-
2408            // negative list directly so a user doesn't need to grep
2409            // through the JSON to find which routes failed which
2410            // checks. Optionally folds in a round-17.4 spec audit
2411            // report if one exists in the same output directory.
2412            let html_path = self.output.join("conformance-report.html");
2413            let audit_path = self.output.join("conformance-spec-audit.json");
2414            let audit_value = std::fs::read_to_string(&audit_path)
2415                .ok()
2416                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2417            // Round 21.1 — `--report-missed-cap N` lets the user
2418            // override the default 200-row HTML drill-down cap.
2419            // `--report-missed-cap 0` maps to `None` (no cap; show
2420            // everything). The JSON report always has the full set.
2421            let render_opts = crate::conformance::report_html::RenderOptions {
2422                missed_cap: match self.report_missed_cap {
2423                    Some(0) => None,
2424                    Some(n) => Some(n as usize),
2425                    None => Some(200),
2426                },
2427            };
2428            let html = crate::conformance::report_html::render_html_with_options(
2429                &report,
2430                audit_value.as_ref(),
2431                &render_opts,
2432            );
2433            if std::fs::write(&html_path, html).is_ok() {
2434                TerminalReporter::print_progress(&format!(
2435                    "HTML report written to {}",
2436                    html_path.display()
2437                ));
2438            }
2439            return Ok(());
2440        }
2441
2442        // Request validation against OpenAPI spec (if --validate-requests is set)
2443        if self.validate_requests && !self.spec.is_empty() {
2444            TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2445            let violation_count = crate::conformance::request_validator::run_request_validation(
2446                &self.spec,
2447                self.conformance_custom.as_deref(),
2448                self.base_path.as_deref(),
2449                &self.output,
2450            )
2451            .await?;
2452            if violation_count > 0 {
2453                TerminalReporter::print_warning(&format!(
2454                    "{} request validation violation(s) found — see conformance-request-violations.json",
2455                    violation_count
2456                ));
2457            } else {
2458                TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2459            }
2460        }
2461
2462        // If generate-only OR --use-k6, use the k6 script generation path
2463        if self.generate_only || self.use_k6 {
2464            let script = if let Some(annotated) = &annotated_ops {
2465                let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2466                    config,
2467                    annotated.clone(),
2468                );
2469                let op_count = gen.operation_count();
2470                let (script, check_count) = gen.generate()?;
2471                TerminalReporter::print_success(&format!(
2472                    "Conformance: {} operations analyzed, {} unique checks generated",
2473                    op_count, check_count
2474                ));
2475                script
2476            } else {
2477                let generator = ConformanceGenerator::new(config);
2478                generator.generate()?
2479            };
2480
2481            let script_path = self.output.join("k6-conformance.js");
2482            std::fs::write(&script_path, &script).map_err(|e| {
2483                BenchError::Other(format!("Failed to write conformance script: {}", e))
2484            })?;
2485            TerminalReporter::print_success(&format!(
2486                "Conformance script generated: {}",
2487                script_path.display()
2488            ));
2489
2490            if self.generate_only {
2491                println!("\nScript generated. Run with:");
2492                println!("  k6 run {}", script_path.display());
2493                return Ok(());
2494            }
2495
2496            // --use-k6: execute via k6
2497            if !K6Executor::is_k6_installed() {
2498                TerminalReporter::print_error("k6 is not installed");
2499                TerminalReporter::print_warning(
2500                    "Install k6 from: https://k6.io/docs/get-started/installation/",
2501                );
2502                return Err(BenchError::K6NotFound);
2503            }
2504
2505            TerminalReporter::print_progress("Running conformance tests via k6...");
2506            let executor = K6Executor::new()?;
2507            executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2508
2509            let report_path = self.output.join("conformance-report.json");
2510            if report_path.exists() {
2511                let report = ConformanceReport::from_file(&report_path)?;
2512                report.print_report_with_options(self.conformance_all_operations);
2513                self.save_conformance_report(&report, &report_path)?;
2514            } else {
2515                TerminalReporter::print_warning(
2516                    "Conformance report not generated (k6 handleSummary may not have run)",
2517                );
2518            }
2519
2520            return Ok(());
2521        }
2522
2523        // Default: Native Rust executor (no k6 dependency)
2524        TerminalReporter::print_progress("Running conformance tests (native executor)...");
2525
2526        let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2527
2528        executor = if let Some(annotated) = &annotated_ops {
2529            executor.with_spec_driven_checks(annotated)
2530        } else {
2531            executor.with_reference_checks()
2532        };
2533        executor = executor.with_custom_checks()?;
2534
2535        TerminalReporter::print_success(&format!(
2536            "Executing {} conformance checks...",
2537            executor.check_count()
2538        ));
2539
2540        let report = executor.execute().await?;
2541        report.print_report_with_options(self.conformance_all_operations);
2542
2543        // Save failure details to a separate file for easy debugging
2544        let failure_details = report.failure_details();
2545        if !failure_details.is_empty() {
2546            let details_path = self.output.join("conformance-failure-details.json");
2547            if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2548                let _ = std::fs::write(&details_path, json);
2549                TerminalReporter::print_success(&format!(
2550                    "Failure details saved to: {}",
2551                    details_path.display()
2552                ));
2553            }
2554        }
2555
2556        // Save report
2557        let report_path = self.output.join("conformance-report.json");
2558        let report_json = serde_json::to_string_pretty(&report.to_json())
2559            .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2560        std::fs::write(&report_path, &report_json)
2561            .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2562        TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2563
2564        self.save_conformance_report(&report, &report_path)?;
2565
2566        Ok(())
2567    }
2568
2569    /// Save conformance report in the requested format (SARIF or JSON copy)
2570    fn save_conformance_report(
2571        &self,
2572        report: &crate::conformance::report::ConformanceReport,
2573        report_path: &Path,
2574    ) -> Result<()> {
2575        if self.conformance_report_format == "sarif" {
2576            use crate::conformance::sarif::ConformanceSarifReport;
2577            ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2578            TerminalReporter::print_success(&format!(
2579                "SARIF report saved to: {}",
2580                self.conformance_report.display()
2581            ));
2582        } else if self.conformance_report != *report_path {
2583            std::fs::copy(report_path, &self.conformance_report)?;
2584            TerminalReporter::print_success(&format!(
2585                "Report saved to: {}",
2586                self.conformance_report.display()
2587            ));
2588        }
2589        Ok(())
2590    }
2591
2592    /// Execute conformance tests against multiple targets from a targets file.
2593    ///
2594    /// Uses the native `NativeConformanceExecutor` (no k6 dependency). Targets are
2595    /// tested sequentially to avoid overwhelming them, and per-target headers from
2596    /// the targets file are merged with the base `--conformance-header` headers.
2597    async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2598        use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2599        use crate::conformance::report::ConformanceReport;
2600        use crate::conformance::spec::ConformanceFeature;
2601
2602        TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2603
2604        // Parse targets file
2605        TerminalReporter::print_progress("Parsing targets file...");
2606        let targets = parse_targets_file(targets_file)?;
2607        let num_targets = targets.len();
2608        TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2609
2610        if targets.is_empty() {
2611            return Err(BenchError::Other("No targets found in file".to_string()));
2612        }
2613
2614        TerminalReporter::print_progress(
2615            "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2616        );
2617
2618        // Parse category filter (shared across all targets)
2619        let categories = self.conformance_categories.as_ref().map(|cats_str| {
2620            cats_str
2621                .split(',')
2622                .filter_map(|s| {
2623                    let trimmed = s.trim();
2624                    if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2625                        Some(canonical.to_string())
2626                    } else {
2627                        TerminalReporter::print_warning(&format!(
2628                            "Unknown conformance category: '{}'. Valid categories: {}",
2629                            trimmed,
2630                            ConformanceFeature::cli_category_names()
2631                                .iter()
2632                                .map(|(cli, _)| *cli)
2633                                .collect::<Vec<_>>()
2634                                .join(", ")
2635                        ));
2636                        None
2637                    }
2638                })
2639                .collect::<Vec<String>>()
2640        });
2641
2642        // Parse base custom headers from --conformance-header flags
2643        let base_custom_headers: Vec<(String, String)> = self
2644            .conformance_headers
2645            .iter()
2646            .filter_map(|h| {
2647                let (name, value) = h.split_once(':')?;
2648                Some((name.trim().to_string(), value.trim().to_string()))
2649            })
2650            .collect();
2651
2652        if !base_custom_headers.is_empty() {
2653            TerminalReporter::print_progress(&format!(
2654                "Using {} base custom header(s) for authentication",
2655                base_custom_headers.len()
2656            ));
2657        }
2658
2659        // Load spec once if provided (shared across all targets)
2660        let annotated_ops = if !self.spec.is_empty() {
2661            TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2662            let parser = SpecParser::from_file(&self.spec[0]).await?;
2663            let operations = parser.get_operations();
2664            let annotated =
2665                crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2666                    &operations,
2667                    parser.spec(),
2668                );
2669            TerminalReporter::print_success(&format!(
2670                "Analyzed {} operations, found {} feature annotations",
2671                operations.len(),
2672                annotated.iter().map(|a| a.features.len()).sum::<usize>()
2673            ));
2674            Some(annotated)
2675        } else {
2676            None
2677        };
2678
2679        // Ensure output dir exists
2680        std::fs::create_dir_all(&self.output)?;
2681
2682        // Collect per-target results for the summary
2683        struct TargetResult {
2684            url: String,
2685            passed: usize,
2686            failed: usize,
2687            elapsed: std::time::Duration,
2688            report_json: serde_json::Value,
2689            owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2690        }
2691
2692        let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2693        let total_start = std::time::Instant::now();
2694
2695        for (idx, target) in targets.iter().enumerate() {
2696            tracing::info!(
2697                "Running conformance tests against target {}/{}: {}",
2698                idx + 1,
2699                num_targets,
2700                target.url
2701            );
2702            TerminalReporter::print_progress(&format!(
2703                "\n--- Target {}/{}: {} ---",
2704                idx + 1,
2705                num_targets,
2706                target.url
2707            ));
2708
2709            // Merge base headers with per-target headers
2710            let mut merged_headers = base_custom_headers.clone();
2711            if let Some(ref target_headers) = target.headers {
2712                for (name, value) in target_headers {
2713                    // Per-target headers override base headers with the same name
2714                    if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2715                        existing.1 = value.clone();
2716                    } else {
2717                        merged_headers.push((name.clone(), value.clone()));
2718                    }
2719                }
2720            }
2721            // Add auth header if present on target
2722            if let Some(ref auth) = target.auth {
2723                if let Some(existing) =
2724                    merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2725                {
2726                    existing.1 = auth.clone();
2727                } else {
2728                    merged_headers.push(("Authorization".to_string(), auth.clone()));
2729                }
2730            }
2731
2732            // Per-target output dir (used by both native and k6 paths).
2733            // Created before the config so we can point the k6 script's
2734            // handleSummary at the per-target directory rather than the shared
2735            // parent output dir (otherwise every target would overwrite the
2736            // same conformance-report.json).
2737            let target_dir = self.output.join(format!("target_{}", idx));
2738            std::fs::create_dir_all(&target_dir)?;
2739
2740            let config = ConformanceConfig {
2741                target_url: target.url.clone(),
2742                api_key: self.conformance_api_key.clone(),
2743                basic_auth: self.conformance_basic_auth.clone(),
2744                skip_tls_verify: self.skip_tls_verify,
2745                categories: categories.clone(),
2746                base_path: self.base_path.clone(),
2747                custom_headers: merged_headers,
2748                output_dir: Some(target_dir.clone()),
2749                all_operations: self.conformance_all_operations,
2750                custom_checks_file: self.conformance_custom.clone(),
2751                request_delay_ms: self.conformance_delay_ms,
2752                custom_filter: self.conformance_custom_filter.clone(),
2753                export_requests: self.export_requests,
2754                validate_requests: self.validate_requests,
2755            };
2756
2757            let target_start = std::time::Instant::now();
2758            let report = if self.use_k6 {
2759                if !K6Executor::is_k6_installed() {
2760                    TerminalReporter::print_error("k6 is not installed");
2761                    TerminalReporter::print_warning(
2762                        "Install k6 from: https://k6.io/docs/get-started/installation/",
2763                    );
2764                    return Err(BenchError::K6NotFound);
2765                }
2766
2767                let script = if let Some(ref annotated) = annotated_ops {
2768                    let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2769                        config.clone(),
2770                        annotated.clone(),
2771                    );
2772                    let (script, _check_count) = gen.generate()?;
2773                    script
2774                } else {
2775                    let generator = ConformanceGenerator::new(config.clone());
2776                    generator.generate()?
2777                };
2778
2779                let script_path = target_dir.join("k6-conformance.js");
2780                std::fs::write(&script_path, &script).map_err(|e| {
2781                    BenchError::Other(format!("Failed to write conformance script: {}", e))
2782                })?;
2783                TerminalReporter::print_success(&format!(
2784                    "Conformance script generated: {}",
2785                    script_path.display()
2786                ));
2787
2788                TerminalReporter::print_progress(&format!(
2789                    "Running conformance tests via k6 against {}...",
2790                    target.url
2791                ));
2792                let k6 = K6Executor::new()?;
2793                // Unique k6 API port per target to avoid collisions.
2794                let api_port = 6565u16.saturating_add(idx as u16);
2795                k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2796                    .await?;
2797
2798                let report_path = target_dir.join("conformance-report.json");
2799                if report_path.exists() {
2800                    ConformanceReport::from_file(&report_path)?
2801                } else {
2802                    TerminalReporter::print_warning(&format!(
2803                        "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2804                        target.url
2805                    ));
2806                    continue;
2807                }
2808            } else {
2809                let mut executor =
2810                    crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2811
2812                executor = if let Some(ref annotated) = annotated_ops {
2813                    executor.with_spec_driven_checks(annotated)
2814                } else {
2815                    executor.with_reference_checks()
2816                };
2817                executor = executor.with_custom_checks()?;
2818
2819                TerminalReporter::print_success(&format!(
2820                    "Executing {} conformance checks against {}...",
2821                    executor.check_count(),
2822                    target.url
2823                ));
2824
2825                executor.execute().await?
2826            };
2827            let target_elapsed = target_start.elapsed();
2828
2829            let report_json = report.to_json();
2830
2831            // Extract pass/fail from the summary in the JSON
2832            let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2833            let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2834            let total_checks = passed + failed;
2835            let rate = if total_checks == 0 {
2836                0.0
2837            } else {
2838                (passed as f64 / total_checks as f64) * 100.0
2839            };
2840
2841            TerminalReporter::print_success(&format!(
2842                "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2843                target.url,
2844                passed,
2845                total_checks,
2846                rate,
2847                target_elapsed.as_secs_f64()
2848            ));
2849
2850            // Save per-target report (target_dir created above)
2851            let target_report_path = target_dir.join("conformance-report.json");
2852            let report_str = serde_json::to_string_pretty(&report_json)
2853                .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2854            std::fs::write(&target_report_path, &report_str)
2855                .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2856
2857            // Save failure details if any
2858            let failure_details = report.failure_details();
2859            if !failure_details.is_empty() {
2860                let details_path = target_dir.join("conformance-failure-details.json");
2861                if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2862                    let _ = std::fs::write(&details_path, json);
2863                }
2864            }
2865
2866            // Compute OWASP coverage for this target
2867            let owasp_coverage = report.owasp_coverage_data();
2868
2869            target_results.push(TargetResult {
2870                url: target.url.clone(),
2871                passed,
2872                failed,
2873                elapsed: target_elapsed,
2874                report_json,
2875                owasp_coverage,
2876            });
2877        }
2878
2879        let total_elapsed = total_start.elapsed();
2880
2881        // Print summary table
2882        println!("\n{}", "=".repeat(80));
2883        println!("  Multi-Target Conformance Summary");
2884        println!("{}", "=".repeat(80));
2885        println!(
2886            "  {:<40} {:>8} {:>8} {:>8} {:>8}",
2887            "Target URL", "Passed", "Failed", "Rate", "Time"
2888        );
2889        println!("  {}", "-".repeat(76));
2890
2891        let mut total_passed = 0usize;
2892        let mut total_failed = 0usize;
2893
2894        for result in &target_results {
2895            let total_checks = result.passed + result.failed;
2896            let rate = if total_checks == 0 {
2897                0.0
2898            } else {
2899                (result.passed as f64 / total_checks as f64) * 100.0
2900            };
2901
2902            // Truncate long URLs for display
2903            let display_url = if result.url.len() > 38 {
2904                format!("{}...", &result.url[..35])
2905            } else {
2906                result.url.clone()
2907            };
2908
2909            println!(
2910                "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2911                display_url,
2912                result.passed,
2913                result.failed,
2914                rate,
2915                result.elapsed.as_secs_f64()
2916            );
2917
2918            total_passed += result.passed;
2919            total_failed += result.failed;
2920        }
2921
2922        let grand_total = total_passed + total_failed;
2923        let overall_rate = if grand_total == 0 {
2924            0.0
2925        } else {
2926            (total_passed as f64 / grand_total as f64) * 100.0
2927        };
2928
2929        println!("  {}", "-".repeat(76));
2930        println!(
2931            "  {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2932            format!("TOTAL ({} targets)", num_targets),
2933            total_passed,
2934            total_failed,
2935            overall_rate,
2936            total_elapsed.as_secs_f64()
2937        );
2938        println!("{}", "=".repeat(80));
2939
2940        // Print per-target OWASP coverage
2941        for result in &target_results {
2942            println!("\n  OWASP API Security Top 10 Coverage for {}:", result.url);
2943            for entry in &result.owasp_coverage {
2944                let status = if !entry.tested {
2945                    "-"
2946                } else if entry.all_passed {
2947                    "pass"
2948                } else {
2949                    "FAIL"
2950                };
2951                let via = if entry.via_categories.is_empty() {
2952                    String::new()
2953                } else {
2954                    format!(" (via {})", entry.via_categories.join(", "))
2955                };
2956                println!("    {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
2957            }
2958        }
2959
2960        // Save combined summary
2961        let per_target_summaries: Vec<serde_json::Value> = target_results
2962            .iter()
2963            .enumerate()
2964            .map(|(idx, r)| {
2965                let total_checks = r.passed + r.failed;
2966                let rate = if total_checks == 0 {
2967                    0.0
2968                } else {
2969                    (r.passed as f64 / total_checks as f64) * 100.0
2970                };
2971                let owasp_json: Vec<serde_json::Value> = r
2972                    .owasp_coverage
2973                    .iter()
2974                    .map(|e| {
2975                        serde_json::json!({
2976                            "id": e.id,
2977                            "name": e.name,
2978                            "tested": e.tested,
2979                            "all_passed": e.all_passed,
2980                            "via_categories": e.via_categories,
2981                        })
2982                    })
2983                    .collect();
2984                serde_json::json!({
2985                    "target_url": r.url,
2986                    "target_index": idx,
2987                    "checks_passed": r.passed,
2988                    "checks_failed": r.failed,
2989                    "total_checks": total_checks,
2990                    "pass_rate": rate,
2991                    "elapsed_seconds": r.elapsed.as_secs_f64(),
2992                    "report": r.report_json,
2993                    "owasp_coverage": owasp_json,
2994                })
2995            })
2996            .collect();
2997
2998        let combined_summary = serde_json::json!({
2999            "total_targets": num_targets,
3000            "total_checks_passed": total_passed,
3001            "total_checks_failed": total_failed,
3002            "overall_pass_rate": overall_rate,
3003            "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3004            "targets": per_target_summaries,
3005        });
3006
3007        let summary_path = self.output.join("multi-target-conformance-summary.json");
3008        let summary_str = serde_json::to_string_pretty(&combined_summary)
3009            .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3010        std::fs::write(&summary_path, &summary_str)
3011            .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3012        TerminalReporter::print_success(&format!(
3013            "Combined summary saved to: {}",
3014            summary_path.display()
3015        ));
3016
3017        Ok(())
3018    }
3019
3020    /// Execute OWASP API Security Top 10 testing mode
3021    async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3022        TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3023
3024        // Parse custom headers from CLI
3025        let custom_headers = self.parse_headers()?;
3026
3027        // Build OWASP configuration from CLI options
3028        let mut config = OwaspApiConfig::new()
3029            .with_auth_header(&self.owasp_auth_header)
3030            .with_verbose(self.verbose)
3031            .with_insecure(self.skip_tls_verify)
3032            .with_concurrency(self.vus as usize)
3033            .with_iterations(self.owasp_iterations as usize)
3034            .with_base_path(self.base_path.clone())
3035            .with_custom_headers(custom_headers);
3036
3037        // Set valid auth token if provided
3038        if let Some(ref token) = self.owasp_auth_token {
3039            config = config.with_valid_auth_token(token);
3040        }
3041
3042        // Parse categories if provided
3043        if let Some(ref cats_str) = self.owasp_categories {
3044            let categories: Vec<OwaspCategory> = cats_str
3045                .split(',')
3046                .filter_map(|s| {
3047                    let trimmed = s.trim();
3048                    match trimmed.parse::<OwaspCategory>() {
3049                        Ok(cat) => Some(cat),
3050                        Err(e) => {
3051                            TerminalReporter::print_warning(&e);
3052                            None
3053                        }
3054                    }
3055                })
3056                .collect();
3057
3058            if !categories.is_empty() {
3059                config = config.with_categories(categories);
3060            }
3061        }
3062
3063        // Load admin paths from file if provided
3064        if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3065            config.admin_paths_file = Some(admin_paths_file.clone());
3066            if let Err(e) = config.load_admin_paths() {
3067                TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3068            }
3069        }
3070
3071        // Set ID fields if provided
3072        if let Some(ref id_fields_str) = self.owasp_id_fields {
3073            let id_fields: Vec<String> = id_fields_str
3074                .split(',')
3075                .map(|s| s.trim().to_string())
3076                .filter(|s| !s.is_empty())
3077                .collect();
3078            if !id_fields.is_empty() {
3079                config = config.with_id_fields(id_fields);
3080            }
3081        }
3082
3083        // Set report path and format
3084        if let Some(ref report_path) = self.owasp_report {
3085            config = config.with_report_path(report_path);
3086        }
3087        if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3088            config = config.with_report_format(format);
3089        }
3090
3091        // Print configuration summary
3092        let categories = config.categories_to_test();
3093        TerminalReporter::print_success(&format!(
3094            "Testing {} OWASP categories: {}",
3095            categories.len(),
3096            categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3097        ));
3098
3099        if config.valid_auth_token.is_some() {
3100            TerminalReporter::print_progress("Using provided auth token for baseline requests");
3101        }
3102
3103        // Create the OWASP generator
3104        TerminalReporter::print_progress("Generating OWASP security test script...");
3105        let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3106
3107        // Generate the script
3108        let script = generator.generate()?;
3109        TerminalReporter::print_success("OWASP security test script generated");
3110
3111        // Write script to file
3112        let script_path = if let Some(output) = &self.script_output {
3113            output.clone()
3114        } else {
3115            self.output.join("k6-owasp-security-test.js")
3116        };
3117
3118        if let Some(parent) = script_path.parent() {
3119            std::fs::create_dir_all(parent)?;
3120        }
3121        std::fs::write(&script_path, &script)?;
3122        TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3123
3124        // If generate-only mode, exit here
3125        if self.generate_only {
3126            println!("\nOWASP security test script generated. Run it with:");
3127            println!("  k6 run {}", script_path.display());
3128            return Ok(());
3129        }
3130
3131        // Execute k6
3132        TerminalReporter::print_progress("Executing OWASP security tests...");
3133        let executor = K6Executor::new()?;
3134        std::fs::create_dir_all(&self.output)?;
3135
3136        let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3137
3138        let duration_secs = Self::parse_duration(&self.duration)?;
3139        TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3140
3141        println!("\nOWASP security test results saved to: {}", self.output.display());
3142
3143        Ok(())
3144    }
3145}
3146
3147#[cfg(test)]
3148mod tests {
3149    use super::*;
3150    use tempfile::tempdir;
3151
3152    #[test]
3153    fn test_parse_duration() {
3154        assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3155        assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3156        assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3157        assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3158    }
3159
3160    /// Round 19 — single IPs and comma-separated lists already
3161    /// worked in 18.5; this regression-locks the parse paths.
3162    #[test]
3163    fn parse_ip_list_plain_and_comma() {
3164        let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
3165        assert_eq!(v.len(), 3);
3166        assert_eq!(v[0].to_string(), "10.0.0.5");
3167        assert_eq!(v[2].to_string(), "10.0.0.7");
3168    }
3169
3170    /// Round 19 — IPv4 CIDR expands to host count up to the cap.
3171    /// `/29` = 8 hosts (well under cap), all 8 enumerated.
3172    #[test]
3173    fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
3174        let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
3175        assert_eq!(v.len(), 8);
3176        assert_eq!(v[0].to_string(), "10.0.0.0");
3177        assert_eq!(v[7].to_string(), "10.0.0.7");
3178    }
3179
3180    /// Round 19 — IPv4 CIDR larger than the cap is truncated, not
3181    /// rejected. Cap is 256; `/8` would be 16M without the guard.
3182    #[test]
3183    fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
3184        let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
3185        assert_eq!(v.len(), 256);
3186        assert_eq!(v[0].to_string(), "10.0.0.0");
3187        assert_eq!(v[255].to_string(), "10.0.0.255");
3188    }
3189
3190    /// Round 19 — IPv6 CIDR also expands. `/126` = 4 hosts.
3191    #[test]
3192    fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
3193        let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
3194        assert_eq!(v.len(), 4);
3195        assert!(v[0].is_ipv6());
3196        assert_eq!(v[0].to_string(), "2001:db8::");
3197        assert_eq!(v[3].to_string(), "2001:db8::3");
3198    }
3199
3200    /// Round 19 — mixed IPv4 + IPv6 + CIDR in one call works.
3201    #[test]
3202    fn parse_ip_list_mixed_v4_v6_cidr() {
3203        let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
3204        assert_eq!(v.len(), 6); // 4 from /30 + 1 + 1
3205        assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
3206        assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
3207    }
3208
3209    /// Round 19 — malformed entries log and skip; the run continues
3210    /// with whatever resolved.
3211    #[test]
3212    fn parse_ip_list_skips_malformed() {
3213        let v = parse_ip_list(
3214            &[
3215                "10.0.0.5".into(),
3216                "not-an-ip".into(),
3217                "10.0.0.6".into(),
3218                "/24".into(),
3219                "1.2.3.4/200".into(),
3220            ],
3221            "source-ip",
3222        );
3223        assert_eq!(v.len(), 2);
3224        assert_eq!(v[0].to_string(), "10.0.0.5");
3225        assert_eq!(v[1].to_string(), "10.0.0.6");
3226    }
3227
3228    #[test]
3229    fn test_parse_duration_invalid() {
3230        assert!(BenchCommand::parse_duration("invalid").is_err());
3231        assert!(BenchCommand::parse_duration("30x").is_err());
3232    }
3233
3234    #[test]
3235    fn test_parse_headers() {
3236        let cmd = BenchCommand {
3237            spec: vec![PathBuf::from("test.yaml")],
3238            spec_dir: None,
3239            merge_conflicts: "error".to_string(),
3240            spec_mode: "merge".to_string(),
3241            dependency_config: None,
3242            target: "http://localhost".to_string(),
3243            base_path: None,
3244            duration: "1m".to_string(),
3245            vus: 10,
3246            scenario: "ramp-up".to_string(),
3247            operations: None,
3248            exclude_operations: None,
3249            auth: None,
3250            headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
3251            output: PathBuf::from("output"),
3252            generate_only: false,
3253            script_output: None,
3254            threshold_percentile: "p(95)".to_string(),
3255            threshold_ms: 500,
3256            max_error_rate: 0.05,
3257            verbose: false,
3258            skip_tls_verify: false,
3259            chunked_request_bodies: false,
3260            target_rps: None,
3261            no_keep_alive: false,
3262            targets_file: None,
3263            max_concurrency: None,
3264            results_format: "both".to_string(),
3265            params_file: None,
3266            crud_flow: false,
3267            flow_config: None,
3268            extract_fields: None,
3269            parallel_create: None,
3270            data_file: None,
3271            data_distribution: "unique-per-vu".to_string(),
3272            data_mappings: None,
3273            per_uri_control: false,
3274            error_rate: None,
3275            error_types: None,
3276            security_test: false,
3277            security_payloads: None,
3278            security_categories: None,
3279            security_target_fields: None,
3280            wafbench_dir: None,
3281            wafbench_cycle_all: false,
3282            owasp_api_top10: false,
3283            owasp_categories: None,
3284            owasp_auth_header: "Authorization".to_string(),
3285            owasp_auth_token: None,
3286            owasp_admin_paths: None,
3287            owasp_id_fields: None,
3288            owasp_report: None,
3289            owasp_report_format: "json".to_string(),
3290            owasp_iterations: 1,
3291            conformance: false,
3292            conformance_api_key: None,
3293            conformance_basic_auth: None,
3294            conformance_report: PathBuf::from("conformance-report.json"),
3295            conformance_categories: None,
3296            conformance_report_format: "json".to_string(),
3297            conformance_headers: vec![],
3298            conformance_all_operations: false,
3299            conformance_custom: None,
3300            conformance_delay_ms: 0,
3301            use_k6: false,
3302            conformance_custom_filter: None,
3303            export_requests: false,
3304            validate_requests: false,
3305            conformance_self_test: false,
3306            source_ips: Vec::new(),
3307            geo_source_ips: Vec::new(),
3308            geo_source_headers: Vec::new(),
3309            report_missed_cap: None,
3310        };
3311
3312        let headers = cmd.parse_headers().unwrap();
3313        assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
3314        assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
3315    }
3316
3317    #[test]
3318    fn test_get_spec_display_name() {
3319        let cmd = BenchCommand {
3320            spec: vec![PathBuf::from("test.yaml")],
3321            spec_dir: None,
3322            merge_conflicts: "error".to_string(),
3323            spec_mode: "merge".to_string(),
3324            dependency_config: None,
3325            target: "http://localhost".to_string(),
3326            base_path: None,
3327            duration: "1m".to_string(),
3328            vus: 10,
3329            scenario: "ramp-up".to_string(),
3330            operations: None,
3331            exclude_operations: None,
3332            auth: None,
3333            headers: None,
3334            output: PathBuf::from("output"),
3335            generate_only: false,
3336            script_output: None,
3337            threshold_percentile: "p(95)".to_string(),
3338            threshold_ms: 500,
3339            max_error_rate: 0.05,
3340            verbose: false,
3341            skip_tls_verify: false,
3342            chunked_request_bodies: false,
3343            target_rps: None,
3344            no_keep_alive: false,
3345            targets_file: None,
3346            max_concurrency: None,
3347            results_format: "both".to_string(),
3348            params_file: None,
3349            crud_flow: false,
3350            flow_config: None,
3351            extract_fields: None,
3352            parallel_create: None,
3353            data_file: None,
3354            data_distribution: "unique-per-vu".to_string(),
3355            data_mappings: None,
3356            per_uri_control: false,
3357            error_rate: None,
3358            error_types: None,
3359            security_test: false,
3360            security_payloads: None,
3361            security_categories: None,
3362            security_target_fields: None,
3363            wafbench_dir: None,
3364            wafbench_cycle_all: false,
3365            owasp_api_top10: false,
3366            owasp_categories: None,
3367            owasp_auth_header: "Authorization".to_string(),
3368            owasp_auth_token: None,
3369            owasp_admin_paths: None,
3370            owasp_id_fields: None,
3371            owasp_report: None,
3372            owasp_report_format: "json".to_string(),
3373            owasp_iterations: 1,
3374            conformance: false,
3375            conformance_api_key: None,
3376            conformance_basic_auth: None,
3377            conformance_report: PathBuf::from("conformance-report.json"),
3378            conformance_categories: None,
3379            conformance_report_format: "json".to_string(),
3380            conformance_headers: vec![],
3381            conformance_all_operations: false,
3382            conformance_custom: None,
3383            conformance_delay_ms: 0,
3384            use_k6: false,
3385            conformance_custom_filter: None,
3386            export_requests: false,
3387            validate_requests: false,
3388            conformance_self_test: false,
3389            source_ips: Vec::new(),
3390            geo_source_ips: Vec::new(),
3391            geo_source_headers: Vec::new(),
3392            report_missed_cap: None,
3393        };
3394
3395        assert_eq!(cmd.get_spec_display_name(), "test.yaml");
3396
3397        // Test multiple specs
3398        let cmd_multi = BenchCommand {
3399            spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
3400            spec_dir: None,
3401            merge_conflicts: "error".to_string(),
3402            spec_mode: "merge".to_string(),
3403            dependency_config: None,
3404            target: "http://localhost".to_string(),
3405            base_path: None,
3406            duration: "1m".to_string(),
3407            vus: 10,
3408            scenario: "ramp-up".to_string(),
3409            operations: None,
3410            exclude_operations: None,
3411            auth: None,
3412            headers: None,
3413            output: PathBuf::from("output"),
3414            generate_only: false,
3415            script_output: None,
3416            threshold_percentile: "p(95)".to_string(),
3417            threshold_ms: 500,
3418            max_error_rate: 0.05,
3419            verbose: false,
3420            skip_tls_verify: false,
3421            chunked_request_bodies: false,
3422            target_rps: None,
3423            no_keep_alive: false,
3424            targets_file: None,
3425            max_concurrency: None,
3426            results_format: "both".to_string(),
3427            params_file: None,
3428            crud_flow: false,
3429            flow_config: None,
3430            extract_fields: None,
3431            parallel_create: None,
3432            data_file: None,
3433            data_distribution: "unique-per-vu".to_string(),
3434            data_mappings: None,
3435            per_uri_control: false,
3436            error_rate: None,
3437            error_types: None,
3438            security_test: false,
3439            security_payloads: None,
3440            security_categories: None,
3441            security_target_fields: None,
3442            wafbench_dir: None,
3443            wafbench_cycle_all: false,
3444            owasp_api_top10: false,
3445            owasp_categories: None,
3446            owasp_auth_header: "Authorization".to_string(),
3447            owasp_auth_token: None,
3448            owasp_admin_paths: None,
3449            owasp_id_fields: None,
3450            owasp_report: None,
3451            owasp_report_format: "json".to_string(),
3452            owasp_iterations: 1,
3453            conformance: false,
3454            conformance_api_key: None,
3455            conformance_basic_auth: None,
3456            conformance_report: PathBuf::from("conformance-report.json"),
3457            conformance_categories: None,
3458            conformance_report_format: "json".to_string(),
3459            conformance_headers: vec![],
3460            conformance_all_operations: false,
3461            conformance_custom: None,
3462            conformance_delay_ms: 0,
3463            use_k6: false,
3464            conformance_custom_filter: None,
3465            export_requests: false,
3466            validate_requests: false,
3467            conformance_self_test: false,
3468            source_ips: Vec::new(),
3469            geo_source_ips: Vec::new(),
3470            geo_source_headers: Vec::new(),
3471            report_missed_cap: None,
3472        };
3473
3474        assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
3475    }
3476
3477    #[test]
3478    fn test_parse_extracted_values_from_output_dir() {
3479        let dir = tempdir().unwrap();
3480        let path = dir.path().join("extracted_values.json");
3481        std::fs::write(
3482            &path,
3483            r#"{
3484  "pool_id": "abc123",
3485  "count": 0,
3486  "enabled": false,
3487  "metadata": { "owner": "team-a" }
3488}"#,
3489        )
3490        .unwrap();
3491
3492        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3493        assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3494        assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3495        assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3496        assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3497    }
3498
3499    #[test]
3500    fn test_parse_extracted_values_missing_file() {
3501        let dir = tempdir().unwrap();
3502        let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3503        assert!(extracted.values.is_empty());
3504    }
3505}