chio-conformance 0.1.2

Scenario loading and report generation for Chio cross-language conformance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Peer-binary discovery and lockfile parsing.
//!
//! External implementers cannot rely on having Python, Node.js, Go, or C++
//! toolchains available locally to build the conformance peer adapters. The
//! `chio conformance fetch-peers` subcommand downloads pre-built binaries
//! whose URLs and integrity digests are pinned in
//! `crates/chio-conformance/peers.lock.toml`.
//!
//! This module owns the parsing and validation of that lockfile. Actual
//! download / extraction lives in the CLI handler so that this crate stays
//! free of network code paths during unit tests.

use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;

/// Schema identifier for the v1 peers lockfile.
pub const PEERS_LOCK_SCHEMA: &str = "chio.conformance.peers/v1";

/// Filename used for the bundled lockfile when published or installed.
pub const PEERS_LOCK_FILENAME: &str = "peers.lock.toml";

/// Recognised peer-language identifiers. New languages must be added here
/// and to the conformance harness in lock-step.
pub const SUPPORTED_LANGUAGES: &[&str] = &["python", "js", "go", "cpp"];

/// Parsed representation of `peers.lock.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct PeersLock {
    /// Schema discriminator. MUST equal `chio.conformance.peers/v1`.
    pub schema: String,
    /// Pinned peer-binary entries.
    #[serde(default, rename = "peer")]
    pub peers: Vec<PeerEntry>,
}

/// A single pinned peer-binary release artifact.
#[derive(Debug, Clone, Deserialize)]
pub struct PeerEntry {
    /// Language adapter identifier (one of `SUPPORTED_LANGUAGES`).
    pub language: String,
    /// Fully-qualified https URL of the release artifact.
    pub url: String,
    /// 64-character lowercase hex sha256 of the artifact bytes.
    pub sha256: String,
    /// Rust target triple the binary was built for.
    pub target: String,
    /// Executable name inside the archive.
    pub binary: String,
    /// Whether this entry has been published with a real sha256 pin and a
    /// reachable url. Defaults to `true` so existing entries do not need
    /// to be edited; placeholder entries MUST set `published = false`.
    /// `chio conformance fetch-peers` skips unpublished entries only when at
    /// least one selected entry is published.
    #[serde(default = "default_published")]
    pub published: bool,
}

fn default_published() -> bool {
    true
}

/// Errors surfaced when loading or validating `peers.lock.toml`.
#[derive(Debug, thiserror::Error)]
pub enum PeersLockError {
    /// Filesystem failure reading the lockfile.
    #[error("i/o error reading {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    /// Lockfile bytes are not valid TOML matching the v1 schema.
    #[error("toml parse error in {path}: {source}")]
    Toml {
        path: PathBuf,
        #[source]
        source: toml::de::Error,
    },

    /// Lockfile parsed but failed schema validation (see `PeersLock::validate`).
    #[error("invalid peers.lock.toml: {0}")]
    Validation(String),
}

impl PeersLock {
    /// Parse `peers.lock.toml` from disk. Does NOT validate; call
    /// `PeersLock::validate` after loading to enforce schema invariants.
    pub fn load(path: &Path) -> Result<Self, PeersLockError> {
        let bytes = fs::read_to_string(path).map_err(|source| PeersLockError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::parse_str(&bytes).map_err(|source| match source {
            PeersLockError::Toml { source, .. } => PeersLockError::Toml {
                path: path.to_path_buf(),
                source,
            },
            other => other,
        })
    }

    /// Parse `peers.lock.toml` from an in-memory string. Named `parse_str`
    /// rather than `from_str` to avoid colliding with `std::str::FromStr`.
    pub fn parse_str(content: &str) -> Result<Self, PeersLockError> {
        toml::from_str::<PeersLock>(content).map_err(|source| PeersLockError::Toml {
            path: PathBuf::from("<memory>"),
            source,
        })
    }

    /// Validate the parsed lockfile:
    /// - schema MUST equal `chio.conformance.peers/v1`,
    /// - every `url` MUST be https,
    /// - every `sha256` MUST be 64 chars of lowercase hex,
    /// - every `language` MUST appear in `SUPPORTED_LANGUAGES`,
    /// - `target` MUST be non-empty,
    /// - `binary` MUST be a non-empty relative path made only of normal
    ///   path components.
    pub fn validate(&self) -> Result<(), PeersLockError> {
        if self.schema != PEERS_LOCK_SCHEMA {
            return Err(PeersLockError::Validation(format!(
                "unexpected schema `{}`, expected `{}`",
                self.schema, PEERS_LOCK_SCHEMA
            )));
        }
        if self.peers.is_empty() {
            return Err(PeersLockError::Validation(
                "lockfile has no [[peer]] entries".to_string(),
            ));
        }
        for (idx, entry) in self.peers.iter().enumerate() {
            entry
                .validate()
                .map_err(|message| PeersLockError::Validation(format!("peer[{idx}]: {message}")))?;
        }
        Ok(())
    }

    /// Filter entries by language and target triple. Both filters are
    /// case-sensitive; pass an empty string to skip a filter.
    pub fn entries_for(&self, language: &str, target: &str) -> Vec<&PeerEntry> {
        self.peers
            .iter()
            .filter(|entry| language.is_empty() || entry.language == language)
            .filter(|entry| target.is_empty() || entry.target == target)
            .collect()
    }

    /// Filter entries by language only.
    pub fn entries_for_language(&self, language: &str) -> Vec<&PeerEntry> {
        self.entries_for(language, "")
    }

    /// Partition entries into `(published, skipped)` based on the
    /// `published` flag. The CLI fails a selected set with zero published
    /// entries; this partition lets mixed selections still print a friendly
    /// "skipping unpublished entry" line per skipped row.
    pub fn partition_by_published<'a>(
        entries: &[&'a PeerEntry],
    ) -> (Vec<&'a PeerEntry>, Vec<&'a PeerEntry>) {
        let mut published = Vec::new();
        let mut skipped = Vec::new();
        for entry in entries {
            if entry.published {
                published.push(*entry);
            } else {
                skipped.push(*entry);
            }
        }
        (published, skipped)
    }
}

impl PeerEntry {
    fn validate(&self) -> Result<(), String> {
        if !SUPPORTED_LANGUAGES.contains(&self.language.as_str()) {
            return Err(format!(
                "unsupported language `{}`; expected one of {:?}",
                self.language, SUPPORTED_LANGUAGES,
            ));
        }
        if !self.url.starts_with("https://") {
            return Err(format!("url `{}` is not https", self.url));
        }
        if !is_lowercase_hex_64(&self.sha256) {
            return Err(format!(
                "sha256 `{}` is not 64 lowercase hex chars",
                self.sha256
            ));
        }
        if self.target.trim().is_empty() {
            return Err("target must be non-empty".to_string());
        }
        if self.binary.trim().is_empty() {
            return Err("binary must be non-empty".to_string());
        }
        if !is_safe_relative_binary_path(&self.binary) {
            return Err(format!(
                "binary `{}` must be a relative path inside the archive",
                self.binary
            ));
        }
        Ok(())
    }
}

fn is_safe_relative_binary_path(value: &str) -> bool {
    let path = Path::new(value);
    !path.is_absolute()
        && path
            .components()
            .all(|component| matches!(component, std::path::Component::Normal(_)))
}

fn is_lowercase_hex_64(value: &str) -> bool {
    value.len() == 64
        && value
            .chars()
            .all(|ch| ch.is_ascii_digit() || ('a'..='f').contains(&ch))
}

/// Resolve the default `peers.lock.toml` path at runtime.
///
/// External consumers obtain `chio` via `cargo install chio-cli` (or build
/// the chio-conformance crate). The compile-time `CARGO_MANIFEST_DIR` of
/// either crate is gone after install, so this helper consults a layered
/// list of candidates so the caller does not have to pass `--lockfile`
/// for the common cases. Order:
///
/// 1. `$CHIO_PEERS_LOCK` (explicit override; honoured first so CI and
///    sandboxes can pin a vendored copy).
/// 2. `$XDG_CONFIG_HOME/chio/peers.lock.toml` (XDG default).
/// 3. `$HOME/.config/chio/peers.lock.toml` (XDG fallback).
/// 4. `<repo-root>/crates/chio-conformance/peers.lock.toml` (in-repo
///    default for `cargo run` from a fresh checkout).
/// 5. `./peers.lock.toml` (cwd-relative; useful for sandboxed CI).
///
/// Returns the first candidate that exists. When none exist this returns
/// the in-repo default (candidate 4) so the error message points users at
/// the canonical path.
#[must_use]
pub fn default_peers_lock_path() -> PathBuf {
    if let Some(path) = std::env::var_os("CHIO_PEERS_LOCK") {
        return PathBuf::from(path);
    }

    if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME") {
        let candidate = PathBuf::from(xdg).join("chio").join(PEERS_LOCK_FILENAME);
        if candidate.exists() {
            return candidate;
        }
    }

    if let Some(home) = std::env::var_os("HOME") {
        let candidate = PathBuf::from(home)
            .join(".config")
            .join("chio")
            .join(PEERS_LOCK_FILENAME);
        if candidate.exists() {
            return candidate;
        }
    }

    let in_repo = in_repo_default_lock_path();
    if in_repo.exists() {
        return in_repo;
    }

    let cwd = PathBuf::from(PEERS_LOCK_FILENAME);
    if cwd.exists() {
        return cwd;
    }

    in_repo
}

/// In-repo default. Resolved relative to the chio-conformance crate at
/// compile time; only useful when the binary still has the workspace
/// checkout next to it (i.e. `cargo run --bin chio` from the repo).
fn in_repo_default_lock_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(PEERS_LOCK_FILENAME)
}

/// Compute the lowercase hex sha256 digest of `bytes`. Used by the CLI
/// download path to verify integrity after fetching a release artifact.
///
/// Thin re-export of [`chio_core::sha256_hex`]; kept here for callers that
/// already imported `chio_conformance::sha256_hex`.
pub fn sha256_hex(bytes: &[u8]) -> String {
    chio_core::sha256_hex(bytes)
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    const VALID: &str = r#"
schema = "chio.conformance.peers/v1"

[[peer]]
language = "python"
url = "https://example.com/py.tar.gz"
sha256 = "0000000000000000000000000000000000000000000000000000000000000000"
target = "x86_64-unknown-linux-gnu"
binary = "chio-py-peer"

[[peer]]
language = "js"
url = "https://example.com/js.tar.gz"
sha256 = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
target = "aarch64-apple-darwin"
binary = "chio-js-peer"
"#;

    #[test]
    fn parses_valid_lockfile() {
        let lock = PeersLock::parse_str(VALID).expect("parse valid lockfile");
        assert_eq!(lock.schema, PEERS_LOCK_SCHEMA);
        assert_eq!(lock.peers.len(), 2);
        lock.validate().expect("valid lockfile passes validation");
    }

    #[test]
    fn rejects_wrong_schema() {
        let bad = VALID.replace("chio.conformance.peers/v1", "chio.conformance.peers/v2");
        let lock = PeersLock::parse_str(&bad).expect("still parses");
        let err = lock.validate().expect_err("wrong schema rejected");
        assert!(format!("{err}").contains("unexpected schema"));
    }

    #[test]
    fn rejects_non_https_url() {
        let bad = VALID.replace(
            "https://example.com/py.tar.gz",
            "http://example.com/py.tar.gz",
        );
        let lock = PeersLock::parse_str(&bad).expect("still parses");
        let err = lock.validate().expect_err("http rejected");
        assert!(format!("{err}").contains("not https"));
    }

    #[test]
    fn rejects_short_sha256() {
        let bad = VALID.replace(
            "0000000000000000000000000000000000000000000000000000000000000000",
            "deadbeef",
        );
        let lock = PeersLock::parse_str(&bad).expect("still parses");
        let err = lock.validate().expect_err("short sha256 rejected");
        assert!(format!("{err}").contains("64 lowercase hex"));
    }

    #[test]
    fn rejects_uppercase_sha256() {
        let bad = VALID.replace(
            "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
            "ABCDEF0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
        );
        let lock = PeersLock::parse_str(&bad).expect("still parses");
        let err = lock.validate().expect_err("uppercase sha256 rejected");
        assert!(format!("{err}").contains("64 lowercase hex"));
    }

    #[test]
    fn rejects_unsupported_language() {
        let bad = VALID.replace(r#"language = "python""#, r#"language = "ruby""#);
        let lock = PeersLock::parse_str(&bad).expect("still parses");
        let err = lock.validate().expect_err("ruby rejected");
        assert!(format!("{err}").contains("unsupported language"));
    }

    #[test]
    fn rejects_unsafe_binary_paths() {
        for binary in ["../chio-py-peer", "/tmp/chio-py-peer"] {
            let bad = VALID.replace(
                r#"binary = "chio-py-peer""#,
                &format!(r#"binary = "{binary}""#),
            );
            let lock = PeersLock::parse_str(&bad).expect("still parses");
            let err = lock.validate().expect_err("unsafe binary path rejected");
            assert!(format!("{err}").contains("relative path inside the archive"));
        }
    }

    #[test]
    fn rejects_empty_peer_list() {
        let bad = "schema = \"chio.conformance.peers/v1\"\n";
        let lock = PeersLock::parse_str(bad).expect("still parses");
        let err = lock.validate().expect_err("empty list rejected");
        assert!(format!("{err}").contains("no [[peer]] entries"));
    }

    #[test]
    fn entries_for_filters_by_language_and_target() {
        let lock = PeersLock::parse_str(VALID).expect("parse");
        let py_only = lock.entries_for_language("python");
        assert_eq!(py_only.len(), 1);
        assert_eq!(py_only[0].language, "python");

        let py_linux = lock.entries_for("python", "x86_64-unknown-linux-gnu");
        assert_eq!(py_linux.len(), 1);

        let py_darwin = lock.entries_for("python", "aarch64-apple-darwin");
        assert!(py_darwin.is_empty());

        let all = lock.entries_for("", "");
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn shipped_lockfile_validates() {
        // The lockfile shipped at crates/chio-conformance/peers.lock.toml is
        // the source of truth. This test prevents accidental schema drift.
        let manifest_dir =
            std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set during cargo test");
        let path = Path::new(&manifest_dir).join("peers.lock.toml");
        let lock = PeersLock::load(&path).expect("load shipped peers.lock.toml");
        lock.validate().expect("shipped lockfile validates");
        assert!(!lock.peers.is_empty());
    }

    #[test]
    fn sha256_hex_matches_known_vector() {
        // sha256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        assert_eq!(
            sha256_hex(b""),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
        );
        // sha256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
        assert_eq!(
            sha256_hex(b"abc"),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
        );
    }

    #[test]
    fn published_defaults_to_true() {
        // Omitting `published` treats every entry as published.
        let lock = PeersLock::parse_str(VALID).expect("parse");
        assert!(lock.peers[0].published);
        assert!(lock.peers[1].published);
    }

    #[test]
    fn partition_by_published_separates_skipped_entries() {
        let raw = r#"
schema = "chio.conformance.peers/v1"

[[peer]]
language = "python"
url = "https://example.com/py.tar.gz"
sha256 = "0000000000000000000000000000000000000000000000000000000000000000"
target = "x86_64-unknown-linux-gnu"
binary = "chio-py-peer"
published = false

[[peer]]
language = "js"
url = "https://example.com/js.tar.gz"
sha256 = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
target = "aarch64-apple-darwin"
binary = "chio-js-peer"
published = true
"#;
        let lock = PeersLock::parse_str(raw).expect("parse");
        let all = lock.entries_for("", "");
        let (published, skipped) = PeersLock::partition_by_published(&all);
        assert_eq!(published.len(), 1);
        assert_eq!(skipped.len(), 1);
        assert_eq!(published[0].language, "js");
        assert_eq!(skipped[0].language, "python");
    }

    #[test]
    fn shipped_lockfile_marks_placeholders_as_unpublished() {
        // Every entry MUST be flagged `published = false` until the release pipeline
        // replaces the placeholder sha256 pins with real values.
        let manifest_dir =
            std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set during cargo test");
        let path = Path::new(&manifest_dir).join("peers.lock.toml");
        let lock = PeersLock::load(&path).expect("load shipped peers.lock.toml");
        for (idx, entry) in lock.peers.iter().enumerate() {
            assert!(
                !entry.published,
                "shipped peer[{idx}] ({} / {}) is flagged published but the lockfile carries placeholder pins; flip published=true only when the release pipeline cuts a real binary",
                entry.language,
                entry.target,
            );
        }
    }
}