Skip to main content

mockforge_bench/
command.rs

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