Skip to main content

purple_ssh/ssh_config/
parser.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::{Context, Result};
4use log::debug;
5
6use super::model::{
7    ConfigElement, Directive, HostBlock, IncludeDirective, IncludedFile, SshConfigFile,
8};
9
10const MAX_INCLUDE_DEPTH: usize = 16;
11
12impl SshConfigFile {
13    /// Parse an SSH config file from the given path. Convenience wrapper that
14    /// captures the process environment once for `${VAR}` / `~` expansion.
15    /// Production code threads its injected `Env` via
16    /// [`SshConfigFile::parse_with_env`]; this shim keeps a path-only entry
17    /// point for tests and external callers without scattering ambient reads.
18    pub fn parse(path: &Path) -> Result<Self> {
19        Self::parse_with_env(path, &crate::runtime::env::Env::from_process())
20    }
21
22    /// Parse with `${VAR}` expansion resolved from an injected environment
23    /// instead of the process env, so tests control Include expansion without
24    /// mutating `std::env`. Production `parse` delegates here with the real env.
25    pub fn parse_with_env(path: &Path, env: &crate::runtime::env::Env) -> Result<Self> {
26        Self::parse_with_depth(path, 0, &|n| env.var(n).map(str::to_string))
27    }
28
29    fn parse_with_depth(
30        path: &Path,
31        depth: usize,
32        lookup: &dyn Fn(&str) -> Option<String>,
33    ) -> Result<Self> {
34        // Track every config file we've already opened along the include chain.
35        // OpenSSH's `readconf.c` aborts with "Too many recursive configuration
36        // includes" the moment it detects a cycle; without this set, purple
37        // would silently re-read the same file up to MAX_INCLUDE_DEPTH times,
38        // producing N copies of every host (verified: A→B→A produces 17 host
39        // duplicates). Insert the canonical top-level path before recursing
40        // so Include directives that loop back to the main config are caught.
41        let mut visited = std::collections::HashSet::new();
42        if let Ok(canonical) = std::fs::canonicalize(path) {
43            visited.insert(canonical);
44        }
45        Self::parse_with_depth_visited(path, depth, &mut visited, lookup)
46    }
47
48    fn parse_with_depth_visited(
49        path: &Path,
50        depth: usize,
51        visited: &mut std::collections::HashSet<PathBuf>,
52        lookup: &dyn Fn(&str) -> Option<String>,
53    ) -> Result<Self> {
54        let content = if path.exists() {
55            std::fs::read_to_string(path)
56                .with_context(|| format!("Failed to read SSH config at {}", path.display()))?
57        } else {
58            String::new()
59        };
60
61        // Strip UTF-8 BOM if present (Windows editors like Notepad add this).
62        let (bom, content) = match content.strip_prefix('\u{FEFF}') {
63            Some(stripped) => (true, stripped),
64            None => (false, content.as_str()),
65        };
66
67        let crlf = detect_crlf_majority(content);
68        // Relative Include paths resolve against this directory for the
69        // entire include tree, matching OpenSSH's readconf.c which fixes the
70        // base to the user's ~/.ssh (or /etc/ssh for the system file) once
71        // at entry and reuses it for every nested Include.
72        let config_dir = path.parent().map(|p| p.to_path_buf());
73        let elements = Self::parse_content_with_includes(
74            content,
75            config_dir.as_deref(),
76            depth,
77            Some(path),
78            Some(visited),
79            lookup,
80        );
81
82        let host_count = elements
83            .iter()
84            .filter(|e| matches!(e, super::model::ConfigElement::HostBlock(_)))
85            .count();
86        debug!(
87            "SSH config loaded: {} ({} hosts)",
88            path.display(),
89            host_count
90        );
91
92        Ok(SshConfigFile {
93            elements,
94            path: path.to_path_buf(),
95            crlf,
96            bom,
97        })
98    }
99
100    /// Create an SshConfigFile from raw content string (for demo/test use).
101    /// Uses a synthetic path; the file is never read from or written to disk.
102    pub fn from_content(content: &str, synthetic_path: PathBuf) -> Self {
103        let elements = Self::parse_content_with_includes(
104            content,
105            None,
106            MAX_INCLUDE_DEPTH,
107            None,
108            None,
109            &|_: &str| None,
110        );
111        SshConfigFile {
112            elements,
113            path: synthetic_path,
114            crlf: false,
115            bom: false,
116        }
117    }
118
119    /// Parse SSH config content from a string (without Include resolution).
120    /// Used by tests to create SshConfigFile from inline strings.
121    #[allow(dead_code)]
122    pub fn parse_content(content: &str) -> Vec<ConfigElement> {
123        Self::parse_content_with_includes(
124            content,
125            None,
126            MAX_INCLUDE_DEPTH,
127            None,
128            None,
129            &|_: &str| None,
130        )
131    }
132
133    /// Parse SSH config content, optionally resolving Include directives.
134    /// `visited` carries the canonical paths of files already opened so an
135    /// Include cycle aborts cleanly. `None` disables cycle tracking and is
136    /// used by the demo/test entry points that pass `depth = MAX_INCLUDE_DEPTH`
137    /// (which gates Include resolution off anyway).
138    fn parse_content_with_includes(
139        content: &str,
140        config_dir: Option<&Path>,
141        depth: usize,
142        config_path: Option<&Path>,
143        mut visited: Option<&mut std::collections::HashSet<PathBuf>>,
144        lookup: &dyn Fn(&str) -> Option<String>,
145    ) -> Vec<ConfigElement> {
146        let mut elements = Vec::new();
147        let mut current_block: Option<HostBlock> = None;
148
149        for (line_idx, raw_line) in content.lines().enumerate() {
150            let line_num = line_idx + 1;
151            // Strip trailing \r characters that may be left when a file mixes
152            // line endings or contains lone \r (old Mac style). Rust's
153            // str::lines() splits on \n and strips \r from \r\n pairs, but
154            // lone \r (not followed by \n) stays in the line. Stripping
155            // prevents stale \r from leaking into raw_line and breaking
156            // round-trip idempotency.
157            let line = raw_line.trim_end_matches('\r');
158            let trimmed = line.trim();
159
160            // OpenSSH's readconf.c treats keywords as significant regardless
161            // of leading whitespace; indentation is purely cosmetic. We follow
162            // the same rule: Include and Match always start a new section, even
163            // when indented inside a previous Host block.
164            if let Some(pattern) = Self::parse_include_line(trimmed) {
165                if let Some(block) = current_block.take() {
166                    elements.push(ConfigElement::HostBlock(block));
167                }
168                let resolved = if depth < MAX_INCLUDE_DEPTH {
169                    Self::resolve_include(
170                        pattern,
171                        config_dir,
172                        depth,
173                        visited.as_deref_mut(),
174                        lookup,
175                    )
176                } else {
177                    Vec::new()
178                };
179                elements.push(ConfigElement::Include(IncludeDirective {
180                    raw_line: line.to_string(),
181                    pattern: pattern.to_string(),
182                    resolved_files: resolved,
183                }));
184                continue;
185            }
186
187            // Match line = block boundary (flush current Host block).
188            // Match blocks are stored as GlobalLines (inert, never edited/deleted).
189            // Recognizes `Match foo`, `Match=foo`, `Match = foo`, with optional
190            // leading whitespace.
191            if Self::is_match_line(trimmed) {
192                if let Some(block) = current_block.take() {
193                    elements.push(ConfigElement::HostBlock(block));
194                }
195                elements.push(ConfigElement::GlobalLine(line.to_string()));
196                continue;
197            }
198
199            // Non-indented purple:group comment = block boundary (visual separator
200            // between provider groups, written as GlobalLine by the sync engine).
201            let is_indented = line.starts_with(' ') || line.starts_with('\t');
202            if !is_indented && trimmed.starts_with("# purple:group ") {
203                if let Some(block) = current_block.take() {
204                    elements.push(ConfigElement::HostBlock(block));
205                }
206                elements.push(ConfigElement::GlobalLine(line.to_string()));
207                continue;
208            }
209
210            // Check if this line starts a new Host block
211            if let Some(pattern) = Self::parse_host_line(trimmed) {
212                // Flush the previous block if any
213                if let Some(block) = current_block.take() {
214                    elements.push(ConfigElement::HostBlock(block));
215                }
216                current_block = Some(HostBlock {
217                    host_pattern: pattern,
218                    raw_host_line: line.to_string(),
219                    directives: Vec::new(),
220                });
221                continue;
222            }
223
224            // If we're inside a Host block, add this line as a directive
225            if let Some(ref mut block) = current_block {
226                if trimmed.is_empty() || trimmed.starts_with('#') {
227                    // Comment or blank line inside a host block
228                    block.directives.push(Directive {
229                        key: String::new(),
230                        value: String::new(),
231                        raw_line: line.to_string(),
232                        is_non_directive: true,
233                    });
234                } else if let Some((key, value)) = Self::parse_directive(trimmed) {
235                    block.directives.push(Directive {
236                        key,
237                        value,
238                        raw_line: line.to_string(),
239                        is_non_directive: false,
240                    });
241                } else {
242                    // Unrecognized line format — preserve verbatim
243                    if let Some(p) = config_path {
244                        debug!(
245                            "[config] SSH config: unrecognized line {} in {}",
246                            line_num,
247                            p.display()
248                        );
249                    }
250                    block.directives.push(Directive {
251                        key: String::new(),
252                        value: String::new(),
253                        raw_line: line.to_string(),
254                        is_non_directive: true,
255                    });
256                }
257            } else {
258                // Global line (before any Host block)
259                elements.push(ConfigElement::GlobalLine(line.to_string()));
260            }
261        }
262
263        // Flush the last block
264        if let Some(block) = current_block {
265            elements.push(ConfigElement::HostBlock(block));
266        }
267
268        elements
269    }
270
271    /// Parse an Include directive line. Returns the pattern if it matches.
272    /// Handles space, tab and `=` between keyword and value (SSH allows all three).
273    /// Matches OpenSSH behavior: skip whitespace, optional `=`, more whitespace.
274    fn parse_include_line(trimmed: &str) -> Option<&str> {
275        let bytes = trimmed.as_bytes();
276        // "include" is 7 ASCII bytes; byte 7 must be whitespace or '='
277        if bytes.len() > 7 && bytes[..7].eq_ignore_ascii_case(b"include") {
278            let sep = bytes[7];
279            if sep.is_ascii_whitespace() || sep == b'=' {
280                // Skip whitespace, optional '=', and more whitespace after keyword.
281                // All bytes 0..7 are ASCII so byte 7 onward is a valid slice point.
282                let rest = trimmed[7..].trim_start();
283                let rest = rest.strip_prefix('=').unwrap_or(rest).trim_start();
284                if !rest.is_empty() {
285                    return Some(rest);
286                }
287            }
288        }
289        None
290    }
291
292    /// Split Include patterns respecting double-quoted paths.
293    /// OpenSSH supports `Include "path with spaces" other_path`.
294    pub(crate) fn split_include_patterns(pattern: &str) -> Vec<&str> {
295        let mut result = Vec::new();
296        let mut chars = pattern.char_indices().peekable();
297        while let Some(&(i, c)) = chars.peek() {
298            if c.is_whitespace() {
299                chars.next();
300                continue;
301            }
302            if c == '"' {
303                chars.next(); // skip opening quote
304                let start = i + 1;
305                let mut end = pattern.len();
306                for (j, ch) in chars.by_ref() {
307                    if ch == '"' {
308                        end = j;
309                        break;
310                    }
311                }
312                let token = &pattern[start..end];
313                if !token.is_empty() {
314                    result.push(token);
315                }
316            } else {
317                let start = i;
318                let mut end = pattern.len();
319                for (j, ch) in chars.by_ref() {
320                    if ch.is_whitespace() {
321                        end = j;
322                        break;
323                    }
324                }
325                result.push(&pattern[start..end]);
326            }
327        }
328        result
329    }
330
331    /// Resolve an Include pattern to a list of included files.
332    /// Supports multiple space-separated patterns on one line (SSH spec).
333    /// Handles quoted paths for paths containing spaces.
334    ///
335    /// `visited` carries the canonical path of every file already opened up
336    /// the include chain. When a candidate file's canonical path is already
337    /// present, the file is skipped with a warning and its branch is pruned.
338    /// This breaks cycles (A→B→A, A→B→C→A) cleanly instead of letting them
339    /// fan out exponentially until MAX_INCLUDE_DEPTH bottoms out.
340    fn resolve_include(
341        pattern: &str,
342        config_dir: Option<&Path>,
343        depth: usize,
344        mut visited: Option<&mut std::collections::HashSet<PathBuf>>,
345        lookup: &dyn Fn(&str) -> Option<String>,
346    ) -> Vec<IncludedFile> {
347        let mut files = Vec::new();
348        let mut seen = std::collections::HashSet::new();
349
350        for single in Self::split_include_patterns(pattern) {
351            let expanded = Self::expand_env_vars_with(&Self::expand_tilde(single, lookup), lookup);
352
353            // If relative path, resolve against config dir
354            let glob_pattern = if expanded.starts_with('/') {
355                expanded
356            } else if let Some(dir) = config_dir {
357                dir.join(&expanded).to_string_lossy().to_string()
358            } else {
359                continue;
360            };
361
362            if let Ok(paths) = glob::glob(&glob_pattern) {
363                let mut matched: Vec<PathBuf> = paths.filter_map(|p| p.ok()).collect();
364                matched.sort();
365                for path in matched {
366                    if path.is_file() && seen.insert(path.clone()) {
367                        // Cycle detection: compare canonical paths so a
368                        // symlink and its target are treated as the same
369                        // file. Falls back to the lexical path when
370                        // canonicalize fails (e.g. permission denied).
371                        let canonical =
372                            std::fs::canonicalize(&path).unwrap_or_else(|_| path.clone());
373                        if let Some(ref mut v) = visited {
374                            if !v.insert(canonical) {
375                                log::warn!(
376                                    "[config] Include cycle detected, skipping {} (already visited up the chain)",
377                                    path.display()
378                                );
379                                continue;
380                            }
381                        }
382                        match std::fs::read_to_string(&path) {
383                            Ok(content) => {
384                                // Strip UTF-8 BOM if present (same as main config)
385                                let content = content.strip_prefix('\u{FEFF}').unwrap_or(&content);
386                                // Pass `config_dir` (the user-config base for
387                                // the entire include tree) through to nested
388                                // parses, NOT `path.parent()`. OpenSSH resolves
389                                // every relative Include against the original
390                                // base, not against the includer's directory.
391                                let elements = Self::parse_content_with_includes(
392                                    content,
393                                    config_dir,
394                                    depth + 1,
395                                    Some(&path),
396                                    visited.as_deref_mut(),
397                                    lookup,
398                                );
399                                files.push(IncludedFile {
400                                    path: path.clone(),
401                                    elements,
402                                });
403                            }
404                            Err(e) => {
405                                log::warn!(
406                                    "[config] Could not read Include file {}: {}",
407                                    path.display(),
408                                    e
409                                );
410                            }
411                        }
412                    }
413                }
414            }
415        }
416        files
417    }
418
419    /// Expand `~` to the home directory, resolved from the injected `lookup`
420    /// (`$HOME`) so expansion stays free of ambient `dirs::home_dir()`. On the
421    /// supported Unix platforms `$HOME` is the same source `dirs` reads.
422    pub(crate) fn expand_tilde(pattern: &str, lookup: &dyn Fn(&str) -> Option<String>) -> String {
423        if let Some(rest) = pattern.strip_prefix("~/") {
424            if let Some(home) = lookup("HOME") {
425                return format!("{}/{}", home, rest);
426            }
427        }
428        pattern.to_string()
429    }
430
431    /// Expand `${VAR}` references using the real process environment. Test-only
432    /// convenience; production paths build the lookup at the edge and call
433    /// `expand_env_vars_with` so expansion stays injectable.
434    #[cfg(test)]
435    pub(crate) fn expand_env_vars(s: &str) -> String {
436        Self::expand_env_vars_with(s, &|n| std::env::var(n).ok())
437    }
438
439    /// Expand `${VAR}` references via an injected lookup (matches OpenSSH behavior).
440    /// Unknown variables are preserved as-is so that SSH itself can report the error.
441    /// Tests pass a closure instead of mutating the process environment.
442    pub(crate) fn expand_env_vars_with(s: &str, lookup: &dyn Fn(&str) -> Option<String>) -> String {
443        let mut result = String::with_capacity(s.len());
444        let mut chars = s.char_indices().peekable();
445        while let Some((i, c)) = chars.next() {
446            if c == '$' {
447                if let Some(&(_, '{')) = chars.peek() {
448                    chars.next(); // consume '{'
449                    if let Some(close) = s[i + 2..].find('}') {
450                        let var_name = &s[i + 2..i + 2 + close];
451                        if let Some(val) = lookup(var_name) {
452                            result.push_str(&val);
453                        } else {
454                            // Preserve unknown vars as-is
455                            result.push_str(&s[i..i + 2 + close + 1]);
456                        }
457                        // Advance past the closing '}'
458                        while let Some(&(j, _)) = chars.peek() {
459                            if j <= i + 2 + close {
460                                chars.next();
461                            } else {
462                                break;
463                            }
464                        }
465                        continue;
466                    }
467                    // No closing '}' — preserve literally
468                    result.push('$');
469                    result.push('{');
470                    continue;
471                }
472            }
473            result.push(c);
474        }
475        result
476    }
477
478    /// Check if a line is a `Host <pattern>` line.
479    /// Returns the pattern if it is.
480    /// Handles space, tab and `=` between keyword and value (SSH allows all three).
481    /// Matches OpenSSH behavior: skip whitespace, optional `=`, more whitespace.
482    /// Strips inline comments (`# ...` preceded by whitespace) from the pattern.
483    fn parse_host_line(trimmed: &str) -> Option<String> {
484        let bytes = trimmed.as_bytes();
485        // "host" is 4 ASCII bytes; byte 4 must be whitespace or '='
486        if bytes.len() > 4 && bytes[..4].eq_ignore_ascii_case(b"host") {
487            let sep = bytes[4];
488            if sep.is_ascii_whitespace() || sep == b'=' {
489                // Reject "hostname", "hostkey" etc: after "host" + separator,
490                // the keyword must end. If sep is alphanumeric, it's a different keyword.
491                // Skip whitespace, optional '=', and more whitespace after keyword.
492                let rest = trimmed[4..].trim_start();
493                let rest = rest.strip_prefix('=').unwrap_or(rest).trim_start();
494                let pattern = strip_inline_comment(rest).to_string();
495                if !pattern.is_empty() {
496                    return Some(pattern);
497                }
498            }
499        }
500        None
501    }
502
503    /// Check if a line is a `Match ...` line (block boundary).
504    /// Recognizes `Match foo`, `Match=foo` and `Match = foo` per OpenSSH's
505    /// strdelim() rule that any keyword can use `=` as separator.
506    fn is_match_line(trimmed: &str) -> bool {
507        let bytes = trimmed.as_bytes();
508        // "match" is 5 ASCII bytes; byte 5 must be whitespace or '=' (or absent
509        // for the bare `Match` keyword, which OpenSSH itself doesn't accept,
510        // so we require at least the separator).
511        if bytes.len() > 5 && bytes[..5].eq_ignore_ascii_case(b"match") {
512            let sep = bytes[5];
513            return sep.is_ascii_whitespace() || sep == b'=';
514        }
515        false
516    }
517
518    /// Parse a "Key Value" directive line.
519    /// Matches OpenSSH behavior: keyword ends at first whitespace or `=`.
520    /// An `=` in the value portion (e.g. `IdentityFile ~/.ssh/id=prod`) is
521    /// NOT treated as a separator.
522    fn parse_directive(trimmed: &str) -> Option<(String, String)> {
523        // Find end of keyword: first whitespace or '='
524        let key_end = trimmed.find(|c: char| c.is_whitespace() || c == '=')?;
525        let key = &trimmed[..key_end];
526        if key.is_empty() {
527            return None;
528        }
529
530        // Skip whitespace, optional '=', and more whitespace after the keyword
531        let rest = trimmed[key_end..].trim_start();
532        let rest = rest.strip_prefix('=').unwrap_or(rest);
533        let value = rest.trim_start();
534
535        // Strip inline comments (# preceded by whitespace) from parsed value,
536        // but only outside quoted strings. Raw_line is untouched for round-trip fidelity.
537        let value = strip_inline_comment(value);
538
539        Some((key.to_string(), value.to_string()))
540    }
541}
542
543/// Detect CRLF only when the MAJORITY of lines use `\r\n`. A single
544/// Windows-edited line in an otherwise LF file must not flip the entire
545/// output to CRLF on the next save; that would produce a wholesale diff
546/// against version control for an unrelated edit.
547pub fn detect_crlf_majority(content: &str) -> bool {
548    let mut crlf_lines = 0usize;
549    let mut lf_lines = 0usize;
550    for line in content.split('\n') {
551        if line.ends_with('\r') {
552            crlf_lines += 1;
553        } else if !line.is_empty() {
554            lf_lines += 1;
555        }
556    }
557    crlf_lines > lf_lines
558}
559
560/// Strip an inline comment (`# ...` preceded by whitespace) from a parsed value,
561/// respecting double-quoted strings.
562fn strip_inline_comment(value: &str) -> &str {
563    let bytes = value.as_bytes();
564    let mut in_quote = false;
565    for i in 0..bytes.len() {
566        if bytes[i] == b'"' {
567            in_quote = !in_quote;
568        } else if !in_quote
569            && bytes[i] == b'#'
570            && i > 0
571            && (bytes[i - 1] == b' ' || bytes[i - 1] == b'\t')
572        {
573            return value[..i].trim_end();
574        }
575    }
576    value
577}
578
579#[cfg(test)]
580mod tests {
581    use super::*;
582    #[allow(unused_imports)]
583    use std::path::PathBuf;
584
585    fn parse_str(content: &str) -> SshConfigFile {
586        SshConfigFile {
587            elements: SshConfigFile::parse_content(content),
588            path: tempfile::tempdir()
589                .expect("tempdir")
590                .keep()
591                .join("test_config"),
592            crlf: crate::ssh_config::parser::detect_crlf_majority(content),
593            bom: false,
594        }
595    }
596
597    #[test]
598    fn test_empty_config() {
599        let config = parse_str("");
600        assert!(config.host_entries().is_empty());
601    }
602
603    #[test]
604    fn test_basic_host() {
605        let config =
606            parse_str("Host myserver\n  HostName 192.168.1.10\n  User admin\n  Port 2222\n");
607        let entries = config.host_entries();
608        assert_eq!(entries.len(), 1);
609        assert_eq!(entries[0].alias, "myserver");
610        assert_eq!(entries[0].hostname, "192.168.1.10");
611        assert_eq!(entries[0].user, "admin");
612        assert_eq!(entries[0].port, 2222);
613    }
614
615    #[test]
616    fn test_multiple_hosts() {
617        let content = "\
618Host alpha
619  HostName alpha.example.com
620  User deploy
621
622Host beta
623  HostName beta.example.com
624  User root
625  Port 22022
626";
627        let config = parse_str(content);
628        let entries = config.host_entries();
629        assert_eq!(entries.len(), 2);
630        assert_eq!(entries[0].alias, "alpha");
631        assert_eq!(entries[1].alias, "beta");
632        assert_eq!(entries[1].port, 22022);
633    }
634
635    #[test]
636    fn test_wildcard_host_filtered() {
637        let content = "\
638Host *
639  ServerAliveInterval 60
640
641Host myserver
642  HostName 10.0.0.1
643";
644        let config = parse_str(content);
645        let entries = config.host_entries();
646        assert_eq!(entries.len(), 1);
647        assert_eq!(entries[0].alias, "myserver");
648    }
649
650    #[test]
651    fn test_comments_preserved() {
652        let content = "\
653# Global comment
654Host myserver
655  # This is a comment
656  HostName 10.0.0.1
657  User admin
658";
659        let config = parse_str(content);
660        // Check that the global comment is preserved
661        assert!(
662            matches!(&config.elements[0], ConfigElement::GlobalLine(s) if s == "# Global comment")
663        );
664        // Check that the host block has the comment directive
665        if let ConfigElement::HostBlock(block) = &config.elements[1] {
666            assert!(block.directives[0].is_non_directive);
667            assert_eq!(block.directives[0].raw_line, "  # This is a comment");
668        } else {
669            panic!("Expected HostBlock");
670        }
671    }
672
673    #[test]
674    fn test_identity_file_and_proxy_jump() {
675        let content = "\
676Host bastion
677  HostName bastion.example.com
678  User admin
679  IdentityFile ~/.ssh/id_ed25519
680  ProxyJump gateway
681";
682        let config = parse_str(content);
683        let entries = config.host_entries();
684        assert_eq!(entries[0].identity_file, "~/.ssh/id_ed25519");
685        assert_eq!(entries[0].proxy_jump, "gateway");
686    }
687
688    #[test]
689    fn test_unknown_directives_preserved() {
690        let content = "\
691Host myserver
692  HostName 10.0.0.1
693  ForwardAgent yes
694  LocalForward 8080 localhost:80
695";
696        let config = parse_str(content);
697        if let ConfigElement::HostBlock(block) = &config.elements[0] {
698            assert_eq!(block.directives.len(), 3);
699            assert_eq!(block.directives[1].key, "ForwardAgent");
700            assert_eq!(block.directives[1].value, "yes");
701            assert_eq!(block.directives[2].key, "LocalForward");
702        } else {
703            panic!("Expected HostBlock");
704        }
705    }
706
707    #[test]
708    fn test_include_directive_parsed() {
709        let content = "\
710Include config.d/*
711
712Host myserver
713  HostName 10.0.0.1
714";
715        let config = parse_str(content);
716        // parse_content uses no config_dir, so Include resolves to no files
717        assert!(
718            matches!(&config.elements[0], ConfigElement::Include(inc) if inc.raw_line == "Include config.d/*")
719        );
720        // Blank line becomes a GlobalLine between Include and HostBlock
721        assert!(matches!(&config.elements[1], ConfigElement::GlobalLine(s) if s.is_empty()));
722        assert!(matches!(&config.elements[2], ConfigElement::HostBlock(_)));
723    }
724
725    #[test]
726    fn test_include_round_trip() {
727        let content = "\
728Include ~/.ssh/config.d/*
729
730Host myserver
731  HostName 10.0.0.1
732";
733        let config = parse_str(content);
734        assert_eq!(config.serialize(), content);
735    }
736
737    #[test]
738    fn test_ssh_command() {
739        use crate::ssh_config::model::HostEntry;
740        use std::path::PathBuf;
741        let entry = HostEntry {
742            alias: "myserver".to_string(),
743            hostname: "10.0.0.1".to_string(),
744            ..Default::default()
745        };
746        let paths = crate::runtime::env::Paths::new("/home/testuser");
747        let default_path = paths.ssh_dir().join("config");
748        assert_eq!(
749            entry.ssh_command(Some(&paths), &default_path),
750            "ssh -- 'myserver'"
751        );
752        let custom_path = PathBuf::from("/tmp/my_config");
753        assert_eq!(
754            entry.ssh_command(Some(&paths), &custom_path),
755            "ssh -F '/tmp/my_config' -- 'myserver'"
756        );
757    }
758
759    #[test]
760    fn test_unicode_comment_no_panic() {
761        // "# abcdeé" has byte 8 mid-character (é starts at byte 7, is 2 bytes)
762        // This must not panic in parse_include_line
763        let content = "# abcde\u{00e9} test\n\nHost myserver\n  HostName 10.0.0.1\n";
764        let config = parse_str(content);
765        let entries = config.host_entries();
766        assert_eq!(entries.len(), 1);
767        assert_eq!(entries[0].alias, "myserver");
768    }
769
770    #[test]
771    fn test_unicode_multibyte_line_no_panic() {
772        // Three 3-byte CJK characters: byte 8 falls mid-character
773        let content = "# \u{3042}\u{3042}\u{3042}xyz\n\nHost myserver\n  HostName 10.0.0.1\n";
774        let config = parse_str(content);
775        let entries = config.host_entries();
776        assert_eq!(entries.len(), 1);
777    }
778
779    #[test]
780    fn test_host_with_tab_separator() {
781        let content = "Host\tmyserver\n  HostName 10.0.0.1\n";
782        let config = parse_str(content);
783        let entries = config.host_entries();
784        assert_eq!(entries.len(), 1);
785        assert_eq!(entries[0].alias, "myserver");
786    }
787
788    #[test]
789    fn test_include_with_tab_separator() {
790        let content = "Include\tconfig.d/*\n\nHost myserver\n  HostName 10.0.0.1\n";
791        let config = parse_str(content);
792        assert!(
793            matches!(&config.elements[0], ConfigElement::Include(inc) if inc.pattern == "config.d/*")
794        );
795    }
796
797    #[test]
798    fn test_include_with_equals_separator() {
799        let content = "Include=config.d/*\n\nHost myserver\n  HostName 10.0.0.1\n";
800        let config = parse_str(content);
801        assert!(
802            matches!(&config.elements[0], ConfigElement::Include(inc) if inc.pattern == "config.d/*")
803        );
804    }
805
806    #[test]
807    fn test_include_with_space_equals_separator() {
808        let content = "Include =config.d/*\n\nHost myserver\n  HostName 10.0.0.1\n";
809        let config = parse_str(content);
810        assert!(
811            matches!(&config.elements[0], ConfigElement::Include(inc) if inc.pattern == "config.d/*")
812        );
813    }
814
815    #[test]
816    fn test_include_with_space_equals_space_separator() {
817        let content = "Include = config.d/*\n\nHost myserver\n  HostName 10.0.0.1\n";
818        let config = parse_str(content);
819        assert!(
820            matches!(&config.elements[0], ConfigElement::Include(inc) if inc.pattern == "config.d/*")
821        );
822    }
823
824    #[test]
825    fn test_hostname_not_confused_with_host() {
826        // "HostName" should not be parsed as a Host line
827        let content = "Host myserver\n  HostName example.com\n";
828        let config = parse_str(content);
829        let entries = config.host_entries();
830        assert_eq!(entries.len(), 1);
831        assert_eq!(entries[0].hostname, "example.com");
832    }
833
834    #[test]
835    fn test_equals_in_value_not_treated_as_separator() {
836        let content = "Host myserver\n  IdentityFile ~/.ssh/id=prod\n";
837        let config = parse_str(content);
838        let entries = config.host_entries();
839        assert_eq!(entries.len(), 1);
840        assert_eq!(entries[0].identity_file, "~/.ssh/id=prod");
841    }
842
843    #[test]
844    fn test_equals_syntax_key_value() {
845        let content = "Host myserver\n  HostName=10.0.0.1\n  User = admin\n";
846        let config = parse_str(content);
847        let entries = config.host_entries();
848        assert_eq!(entries.len(), 1);
849        assert_eq!(entries[0].hostname, "10.0.0.1");
850        assert_eq!(entries[0].user, "admin");
851    }
852
853    #[test]
854    fn test_inline_comment_inside_quotes_preserved() {
855        let content = "Host myserver\n  ProxyCommand ssh -W \"%h #test\" gateway\n";
856        let config = parse_str(content);
857        let entries = config.host_entries();
858        assert_eq!(entries.len(), 1);
859        // The value should preserve the # inside quotes
860        if let ConfigElement::HostBlock(block) = &config.elements[0] {
861            let proxy_cmd = block
862                .directives
863                .iter()
864                .find(|d| d.key == "ProxyCommand")
865                .unwrap();
866            assert_eq!(proxy_cmd.value, "ssh -W \"%h #test\" gateway");
867        } else {
868            panic!("Expected HostBlock");
869        }
870    }
871
872    #[test]
873    fn test_inline_comment_outside_quotes_stripped() {
874        let content = "Host myserver\n  HostName 10.0.0.1 # production\n";
875        let config = parse_str(content);
876        let entries = config.host_entries();
877        assert_eq!(entries[0].hostname, "10.0.0.1");
878    }
879
880    #[test]
881    fn test_host_inline_comment_stripped() {
882        let content = "Host alpha # this is a comment\n  HostName 10.0.0.1\n";
883        let config = parse_str(content);
884        let entries = config.host_entries();
885        assert_eq!(entries.len(), 1);
886        assert_eq!(entries[0].alias, "alpha");
887        // Raw line is preserved for round-trip fidelity
888        if let ConfigElement::HostBlock(block) = &config.elements[0] {
889            assert_eq!(block.raw_host_line, "Host alpha # this is a comment");
890            assert_eq!(block.host_pattern, "alpha");
891        } else {
892            panic!("Expected HostBlock");
893        }
894    }
895
896    #[test]
897    fn test_match_block_is_global_line() {
898        let content = "\
899Host myserver
900  HostName 10.0.0.1
901
902Match host *.example.com
903  ForwardAgent yes
904";
905        let config = parse_str(content);
906        // Match line should flush the Host block and become a GlobalLine
907        let host_count = config
908            .elements
909            .iter()
910            .filter(|e| matches!(e, ConfigElement::HostBlock(_)))
911            .count();
912        assert_eq!(host_count, 1);
913        // Match line itself
914        assert!(
915            config.elements.iter().any(
916                |e| matches!(e, ConfigElement::GlobalLine(s) if s == "Match host *.example.com")
917            )
918        );
919        // Indented lines after Match (no current_block) become GlobalLines
920        assert!(
921            config
922                .elements
923                .iter()
924                .any(|e| matches!(e, ConfigElement::GlobalLine(s) if s.contains("ForwardAgent")))
925        );
926    }
927
928    #[test]
929    fn test_match_block_survives_host_deletion() {
930        let content = "\
931Host myserver
932  HostName 10.0.0.1
933
934Match host *.example.com
935  ForwardAgent yes
936
937Host other
938  HostName 10.0.0.2
939";
940        let mut config = parse_str(content);
941        config.delete_host("myserver");
942        let output = config.serialize();
943        assert!(output.contains("Match host *.example.com"));
944        assert!(output.contains("ForwardAgent yes"));
945        assert!(output.contains("Host other"));
946        assert!(!output.contains("Host myserver"));
947    }
948
949    #[test]
950    fn test_match_block_round_trip() {
951        let content = "\
952Host myserver
953  HostName 10.0.0.1
954
955Match host *.example.com
956  ForwardAgent yes
957";
958        let config = parse_str(content);
959        assert_eq!(config.serialize(), content);
960    }
961
962    #[test]
963    fn test_match_at_start_of_file() {
964        let content = "\
965Match all
966  ServerAliveInterval 60
967
968Host myserver
969  HostName 10.0.0.1
970";
971        let config = parse_str(content);
972        assert!(matches!(&config.elements[0], ConfigElement::GlobalLine(s) if s == "Match all"));
973        assert!(
974            matches!(&config.elements[1], ConfigElement::GlobalLine(s) if s.contains("ServerAliveInterval"))
975        );
976        let entries = config.host_entries();
977        assert_eq!(entries.len(), 1);
978        assert_eq!(entries[0].alias, "myserver");
979    }
980
981    #[test]
982    fn test_host_equals_syntax() {
983        let config = parse_str("Host=foo\n  HostName 10.0.0.1\n");
984        let entries = config.host_entries();
985        assert_eq!(entries.len(), 1);
986        assert_eq!(entries[0].alias, "foo");
987    }
988
989    #[test]
990    fn test_host_space_equals_syntax() {
991        let config = parse_str("Host =foo\n  HostName 10.0.0.1\n");
992        let entries = config.host_entries();
993        assert_eq!(entries.len(), 1);
994        assert_eq!(entries[0].alias, "foo");
995    }
996
997    #[test]
998    fn test_host_equals_space_syntax() {
999        let config = parse_str("Host= foo\n  HostName 10.0.0.1\n");
1000        let entries = config.host_entries();
1001        assert_eq!(entries.len(), 1);
1002        assert_eq!(entries[0].alias, "foo");
1003    }
1004
1005    #[test]
1006    fn test_host_space_equals_space_syntax() {
1007        let config = parse_str("Host = foo\n  HostName 10.0.0.1\n");
1008        let entries = config.host_entries();
1009        assert_eq!(entries.len(), 1);
1010        assert_eq!(entries[0].alias, "foo");
1011    }
1012
1013    #[test]
1014    fn test_host_equals_case_insensitive() {
1015        let config = parse_str("HOST=foo\n  HostName 10.0.0.1\n");
1016        let entries = config.host_entries();
1017        assert_eq!(entries.len(), 1);
1018        assert_eq!(entries[0].alias, "foo");
1019    }
1020
1021    #[test]
1022    fn test_hostname_equals_not_parsed_as_host() {
1023        // "HostName=example.com" must NOT be parsed as a Host line
1024        let config = parse_str("Host myserver\n  HostName=example.com\n");
1025        let entries = config.host_entries();
1026        assert_eq!(entries.len(), 1);
1027        assert_eq!(entries[0].alias, "myserver");
1028        assert_eq!(entries[0].hostname, "example.com");
1029    }
1030
1031    #[test]
1032    fn test_host_multi_pattern_with_inline_comment() {
1033        // Multi-pattern host with inline comment: "prod staging # servers"
1034        // The comment should be stripped, but "prod staging" is still multi-pattern
1035        // and gets filtered by host_entries()
1036        let content = "Host prod staging # servers\n  HostName 10.0.0.1\n";
1037        let config = parse_str(content);
1038        if let ConfigElement::HostBlock(block) = &config.elements[0] {
1039            assert_eq!(block.host_pattern, "prod staging");
1040        } else {
1041            panic!("Expected HostBlock");
1042        }
1043        // Multi-pattern hosts are filtered out of host_entries
1044        assert_eq!(config.host_entries().len(), 0);
1045    }
1046
1047    #[test]
1048    fn test_expand_env_vars_basic() {
1049        let result =
1050            SshConfigFile::expand_env_vars_with("${_PURPLE_TEST_VAR}/.ssh/config", &|name| {
1051                match name {
1052                    "_PURPLE_TEST_VAR" => Some("/custom/path".to_string()),
1053                    _ => None,
1054                }
1055            });
1056        assert_eq!(result, "/custom/path/.ssh/config");
1057    }
1058
1059    #[test]
1060    fn test_expand_env_vars_multiple() {
1061        let result =
1062            SshConfigFile::expand_env_vars_with("${_PURPLE_TEST_A}/${_PURPLE_TEST_B}", &|name| {
1063                match name {
1064                    "_PURPLE_TEST_A" => Some("hello".to_string()),
1065                    "_PURPLE_TEST_B" => Some("world".to_string()),
1066                    _ => None,
1067                }
1068            });
1069        assert_eq!(result, "hello/world");
1070    }
1071
1072    #[test]
1073    fn test_expand_env_vars_unknown_preserved() {
1074        let result = SshConfigFile::expand_env_vars("${_PURPLE_NONEXISTENT_VAR}/path");
1075        assert_eq!(result, "${_PURPLE_NONEXISTENT_VAR}/path");
1076    }
1077
1078    #[test]
1079    fn test_expand_env_vars_no_vars() {
1080        let result = SshConfigFile::expand_env_vars("~/.ssh/config.d/*");
1081        assert_eq!(result, "~/.ssh/config.d/*");
1082    }
1083
1084    #[test]
1085    fn test_expand_env_vars_unclosed_brace() {
1086        let result = SshConfigFile::expand_env_vars("${UNCLOSED/path");
1087        assert_eq!(result, "${UNCLOSED/path");
1088    }
1089
1090    #[test]
1091    fn test_expand_env_vars_dollar_without_brace() {
1092        let result = SshConfigFile::expand_env_vars("$HOME/.ssh/config");
1093        // Only ${VAR} syntax should be expanded, not bare $VAR
1094        assert_eq!(result, "$HOME/.ssh/config");
1095    }
1096
1097    #[test]
1098    fn test_max_include_depth_matches_openssh() {
1099        assert_eq!(MAX_INCLUDE_DEPTH, 16);
1100    }
1101
1102    #[test]
1103    fn test_split_include_patterns_single_unquoted() {
1104        let result = SshConfigFile::split_include_patterns("config.d/*");
1105        assert_eq!(result, vec!["config.d/*"]);
1106    }
1107
1108    #[test]
1109    fn test_split_include_patterns_quoted_with_spaces() {
1110        let result = SshConfigFile::split_include_patterns("\"/path/with spaces/config\"");
1111        assert_eq!(result, vec!["/path/with spaces/config"]);
1112    }
1113
1114    #[test]
1115    fn test_split_include_patterns_mixed() {
1116        let result =
1117            SshConfigFile::split_include_patterns("\"/path/with spaces/*\" ~/.ssh/config.d/*");
1118        assert_eq!(result, vec!["/path/with spaces/*", "~/.ssh/config.d/*"]);
1119    }
1120
1121    #[test]
1122    fn test_split_include_patterns_quoted_no_spaces() {
1123        let result = SshConfigFile::split_include_patterns("\"config.d/*\"");
1124        assert_eq!(result, vec!["config.d/*"]);
1125    }
1126
1127    #[test]
1128    fn test_split_include_patterns_multiple_unquoted() {
1129        let result = SshConfigFile::split_include_patterns("~/.ssh/conf.d/* /etc/ssh/config.d/*");
1130        assert_eq!(result, vec!["~/.ssh/conf.d/*", "/etc/ssh/config.d/*"]);
1131    }
1132}