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