Skip to main content

radio_cli/speech/
models.rs

1use crate::{
2    config::Config,
3    error::{DomainError, ErrorCode},
4};
5use anyhow::{Context, Result, bail, ensure};
6use clap::Subcommand;
7use fs2::FileExt;
8use serde::{Deserialize, Serialize};
9use sha2::{Digest, Sha256};
10use std::{
11    collections::HashMap,
12    fs::{self, File, OpenOptions},
13    io::{Read, Write},
14    os::unix::fs::MetadataExt,
15    path::{Path, PathBuf},
16    process::Command,
17    sync::atomic::{AtomicBool, Ordering},
18    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
19};
20
21#[derive(Subcommand)]
22pub enum Action {
23    /// Install an isolated Python 3.12 speech runtime using uv.
24    Setup,
25    /// Check the runtime and selected model without downloading.
26    Doctor,
27    Models {
28        #[command(subcommand)]
29        action: ModelAction,
30    },
31}
32
33#[derive(Subcommand)]
34pub enum ModelAction {
35    List,
36    Install { id: String },
37    Verify { id: String },
38    Remove { id: String },
39}
40
41#[derive(Debug, Deserialize, Serialize)]
42pub(super) struct Model {
43    pub id: String,
44    pub license: String,
45    pub license_url: String,
46    pub files: Vec<ModelFile>,
47}
48
49#[derive(Debug, Clone, Deserialize, Serialize)]
50pub(super) struct ModelFile {
51    pub path: String,
52    pub url: String,
53    pub size: u64,
54    pub sha256: String,
55}
56
57pub(super) fn manifest() -> Vec<Model> {
58    serde_json::from_str(include_str!("models.json")).expect("embedded speech manifest")
59}
60
61fn required_models(id: &str) -> Result<[Model; 2]> {
62    required_from(manifest(), id)
63}
64
65pub(crate) fn validate_model(id: &str) -> Result<()> {
66    required_models(id).map(|_| ())
67}
68
69fn required_from(models: Vec<Model>, id: &str) -> Result<[Model; 2]> {
70    let mut selected = None;
71    let mut vad = None;
72    for model in models {
73        if model.id == "silero-vad" {
74            vad = Some(model);
75        } else if model.id == id {
76            selected = Some(model);
77        }
78    }
79    Ok([
80        selected.with_context(|| DomainError::new(ErrorCode::SubtitleModel).with("id", id))?,
81        vad.context("embedded speech manifest has no silero-vad")?,
82    ])
83}
84
85pub(super) fn root(config: &Config) -> PathBuf {
86    config.data_dir.join("speech")
87}
88pub(super) fn runtime(config: &Config) -> PathBuf {
89    root(config).join("runtime-2.0.1-v1")
90}
91pub(super) fn python(config: &Config) -> PathBuf {
92    runtime(config).join("bin/python")
93}
94
95fn supports_runtime(os: &str, arch: &str, glibc: bool) -> bool {
96    matches!((os, arch), ("macos", "x86_64" | "aarch64"))
97        || (os == "linux" && arch == "x86_64" && glibc)
98}
99
100fn ensure_runtime_platform() -> Result<()> {
101    // A statically linked musl radio binary can still use glibc Python on a
102    // glibc host. Check the host loader rather than rejecting that binary.
103    let glibc = cfg!(target_env = "gnu")
104        || [
105            "/lib64/ld-linux-x86-64.so.2",
106            "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2",
107        ]
108        .iter()
109        .any(|path| Path::new(path).exists());
110    ensure!(
111        supports_runtime(std::env::consts::OS, std::env::consts::ARCH, glibc),
112        DomainError::new(ErrorCode::SubtitlePlatform).with(
113            "platform",
114            format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH)
115        )
116    );
117    Ok(())
118}
119pub(super) fn model_dir(config: &Config, id: &str) -> PathBuf {
120    root(config).join("models").join(id)
121}
122
123pub(super) struct ResourceLock(File);
124impl Drop for ResourceLock {
125    fn drop(&mut self) {
126        let _ = FileExt::unlock(&self.0);
127    }
128}
129
130pub(super) fn lock(config: &Config, exclusive: bool) -> Result<ResourceLock> {
131    let path = root(config).join("resources.lock");
132    let create = || -> Result<File> {
133        fs::create_dir_all(root(config))?;
134        Ok(OpenOptions::new()
135            .create(true)
136            .truncate(false)
137            .read(true)
138            .write(true)
139            .open(&path)?)
140    };
141    let file = if exclusive {
142        create()
143    } else {
144        match File::open(&path) {
145            Ok(file) => Ok(file),
146            Err(error) if error.kind() == std::io::ErrorKind::NotFound => create(),
147            Err(error) => Err(error.into()),
148        }
149    }
150    .with_context(|| format!("cannot open speech resource lock {}", path.display()))?;
151    if exclusive {
152        FileExt::try_lock_exclusive(&file)
153    } else {
154        FileExt::try_lock_shared(&file)
155    }
156    .context(DomainError::new(ErrorCode::SubtitleBusy))?;
157    Ok(ResourceLock(file))
158}
159
160fn cancelled(stop: &AtomicBool) -> Result<()> {
161    ensure!(
162        !stop.load(Ordering::Relaxed),
163        DomainError::new(ErrorCode::Cancelled)
164    );
165    Ok(())
166}
167
168fn digest(path: &Path, stop: &AtomicBool) -> Result<String> {
169    let mut file = File::open(path)?;
170    let mut hash = Sha256::new();
171    let mut buf = [0; 65536];
172    loop {
173        cancelled(stop)?;
174        let n = file.read(&mut buf)?;
175        if n == 0 {
176            break;
177        }
178        hash.update(&buf[..n]);
179    }
180    Ok(format!("{:x}", hash.finalize()))
181}
182
183fn present(path: &Path, file: &ModelFile) -> Result<bool> {
184    match fs::symlink_metadata(path) {
185        Ok(s) => Ok(s.is_file() && s.len() == file.size),
186        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
187        Err(e) => Err(e).with_context(|| format!("cannot inspect {}", path.display())),
188    }
189}
190
191fn valid(path: &Path, file: &ModelFile, stop: &AtomicBool) -> Result<bool> {
192    cancelled(stop)?;
193    Ok(present(path, file)? && digest(path, stop)? == file.sha256)
194}
195
196#[derive(Debug, PartialEq, Eq)]
197struct FileStamp {
198    device: u64,
199    inode: u64,
200    size: u64,
201    mode: u32,
202    modified: (i64, i64),
203    changed: (i64, i64),
204}
205
206impl FileStamp {
207    fn read(path: &Path) -> Result<Option<Self>> {
208        match fs::symlink_metadata(path) {
209            Ok(meta) if meta.is_file() => Ok(Some(Self {
210                device: meta.dev(),
211                inode: meta.ino(),
212                size: meta.len(),
213                mode: meta.mode(),
214                modified: (meta.mtime(), meta.mtime_nsec()),
215                changed: (meta.ctime(), meta.ctime_nsec()),
216            })),
217            Ok(_) => Ok(None),
218            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
219            Err(error) => Err(error).with_context(|| format!("cannot inspect {}", path.display())),
220        }
221    }
222
223    fn cacheable_at(&self, started: SystemTime) -> bool {
224        // Filesystems can reuse a timestamp for successive writes. Do not
225        // cache a hash until ctime is beyond that potentially racy second.
226        started
227            .duration_since(UNIX_EPOCH)
228            .is_ok_and(|now| i128::from(self.changed.0) + 1 < i128::from(now.as_secs()))
229    }
230}
231
232/// Successful hashes are reusable only within this controller's lifetime.
233/// Never trust persisted cache data or populate entries from metadata alone.
234#[derive(Default)]
235pub(super) struct VerificationCache {
236    files: HashMap<PathBuf, (FileStamp, String)>,
237    #[cfg(test)]
238    hashes: usize,
239}
240
241impl VerificationCache {
242    fn valid(&mut self, path: &Path, file: &ModelFile, stop: &AtomicBool) -> Result<bool> {
243        self.valid_at(path, file, stop, SystemTime::now())
244    }
245
246    fn valid_at(
247        &mut self,
248        path: &Path,
249        file: &ModelFile,
250        stop: &AtomicBool,
251        started: SystemTime,
252    ) -> Result<bool> {
253        cancelled(stop)?;
254        let stamp = FileStamp::read(path)?;
255        if let Some(stamp) = &stamp
256            && stamp.size == file.size
257            && self
258                .files
259                .get(path)
260                .is_some_and(|(old, hash)| old == stamp && hash == &file.sha256)
261        {
262            return Ok(true);
263        }
264        self.files.remove(path);
265        let Some(stamp) = stamp.filter(|stamp| stamp.size == file.size) else {
266            return Ok(false);
267        };
268        #[cfg(test)]
269        {
270            self.hashes += 1;
271        }
272        if digest(path, stop)? != file.sha256 || FileStamp::read(path)?.as_ref() != Some(&stamp) {
273            return Ok(false);
274        }
275        if stamp.cacheable_at(started) {
276            self.files
277                .insert(path.to_owned(), (stamp, file.sha256.clone()));
278        }
279        Ok(true)
280    }
281
282    pub(super) fn verify(&mut self, config: &Config, id: &str, stop: &AtomicBool) -> Result<()> {
283        verify_with(config, id, |path, file| self.valid(path, file, stop))
284    }
285}
286
287pub(crate) fn check_available(config: &Config, stop: &std::sync::Arc<AtomicBool>) -> Result<()> {
288    let _lock = lock(config, false)?;
289    runtime_check(config, stop)?;
290    verify_with(config, &config.settings.subtitles.model, present)
291}
292
293pub(super) fn verify(config: &Config, id: &str, stop: &AtomicBool) -> Result<()> {
294    verify_with(config, id, |path, file| valid(path, file, stop))
295}
296
297fn verify_with(
298    config: &Config,
299    id: &str,
300    mut valid_file: impl FnMut(&Path, &ModelFile) -> Result<bool>,
301) -> Result<()> {
302    for m in required_models(id)? {
303        for file in &m.files {
304            let path = model_dir(config, &m.id).join(&file.path);
305            ensure!(
306                valid_file(&path, file)?,
307                DomainError::new(ErrorCode::SubtitleIntegrity)
308                    .with("id", id)
309                    .with("path", path.display())
310            );
311        }
312    }
313    Ok(())
314}
315
316/// A Range response is appended only when its complete range matches our file.
317fn download(
318    client: &reqwest::Client,
319    id: &str,
320    path: &Path,
321    file: &ModelFile,
322    stop: &AtomicBool,
323) -> Result<()> {
324    let runtime = tokio::runtime::Builder::new_current_thread()
325        .enable_all()
326        .build()?;
327    let _enter = runtime.enter();
328    if valid(path, file, stop)? {
329        return Ok(());
330    }
331    fs::create_dir_all(path.parent().unwrap())?;
332    let partial = partial_path(path);
333    let mut have = partial_len(&partial)?;
334    if have == file.size && valid(&partial, file, stop)? {
335        fs::rename(partial, path)?;
336        return Ok(());
337    }
338    if have >= file.size {
339        have = 0;
340    }
341    cancelled(stop)?;
342    let mut request = client
343        .get(&file.url)
344        .header(reqwest::header::ACCEPT_ENCODING, "identity");
345    if have > 0 {
346        request = request.header(reqwest::header::RANGE, format!("bytes={have}-"));
347    }
348    let mut response = runtime
349        .block_on(network(request.send(), stop))??
350        .error_for_status()?;
351    if response.status() == reqwest::StatusCode::PARTIAL_CONTENT {
352        let expected = format!("bytes {have}-{}/{}", file.size - 1, file.size);
353        ensure!(
354            response
355                .headers()
356                .get(reqwest::header::CONTENT_RANGE)
357                .and_then(|v| v.to_str().ok())
358                == Some(expected.as_str()),
359            "invalid Content-Range"
360        );
361    } else {
362        ensure!(
363            response.status() == reqwest::StatusCode::OK,
364            "unexpected model download status"
365        );
366        have = 0;
367    }
368    let mut output = OpenOptions::new()
369        .create(true)
370        .write(true)
371        .truncate(have == 0)
372        .append(have > 0)
373        .open(&partial)?;
374    // A server may ignore Range, so recheck after any truncation as well as
375    // checking the complete installation plan before the first request.
376    check_space(
377        file.size - have,
378        fs2::available_space(path.parent().unwrap())?,
379    )?;
380    let mut last = Instant::now() - Duration::from_secs(2);
381    loop {
382        cancelled(stop)?;
383        let Some(bytes) = runtime.block_on(network(response.chunk(), stop))?? else {
384            break;
385        };
386        let n = bytes.len();
387        ensure!(
388            have + n as u64 <= file.size,
389            "model download exceeds manifest size"
390        );
391        output.write_all(&bytes)?;
392        have += n as u64;
393        if last.elapsed() >= Duration::from_secs(1) {
394            eprintln!(
395                "{}",
396                crate::t!("subtitle_install_progress", "file" => &file.path, "done" => have, "total" => file.size)
397            );
398            last = Instant::now();
399        }
400    }
401    output.sync_all()?;
402    drop(output);
403    if have != file.size {
404        bail!("model download truncated: {have}/{}", file.size);
405    }
406    if !valid(&partial, file, stop)? {
407        return reject_corrupt_download(id, path);
408    }
409    fs::rename(partial, path)?;
410    Ok(())
411}
412
413fn reject_corrupt_download(id: &str, path: &Path) -> Result<()> {
414    let partial = partial_path(path);
415    let integrity = DomainError::new(ErrorCode::SubtitleIntegrity)
416        .with("id", id)
417        .with("path", path.display());
418    match fs::remove_file(&partial) {
419        Ok(()) => {}
420        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
421        Err(error) => {
422            return Err(anyhow::Error::new(error)
423                .context(format!(
424                    "cannot remove corrupt download {}",
425                    partial.display()
426                ))
427                .context(integrity));
428        }
429    }
430    Err(integrity.into())
431}
432
433fn partial_path(path: &Path) -> PathBuf {
434    path.with_extension(format!(
435        "{}.part",
436        path.extension().unwrap_or_default().to_string_lossy()
437    ))
438}
439
440fn partial_len(path: &Path) -> Result<u64> {
441    match fs::metadata(path) {
442        Ok(meta) => Ok(meta.len()),
443        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(0),
444        Err(e) => Err(e.into()),
445    }
446}
447
448fn sources(config: &Config, id: &str, file: &ModelFile) -> Result<Vec<String>> {
449    let mut urls = Vec::new();
450    if let Some(base) = &config.settings.subtitles.model_mirror {
451        let mut url = reqwest::Url::parse(base)?;
452        url.path_segments_mut()
453            .map_err(|_| DomainError::new(ErrorCode::SubtitleSettings))?
454            .pop_if_empty()
455            .push(id)
456            .extend(file.path.split('/'));
457        urls.push(url.into());
458    }
459    urls.push(file.url.clone());
460    Ok(urls)
461}
462
463fn install_file(
464    config: &Config,
465    client: &reqwest::Client,
466    id: &str,
467    file: &ModelFile,
468    stop: &AtomicBool,
469) -> Result<()> {
470    let path = model_dir(config, id).join(&file.path);
471    let mut last_error = None;
472    let mut failures = Vec::new();
473    for url in sources(config, id, file)? {
474        let source = ModelFile {
475            url,
476            ..file.clone()
477        };
478        let resumed = partial_len(&partial_path(&path))? > 0;
479        let mut result = download(client, id, &path, &source, stop);
480        if resumed
481            && source.url == file.url
482            && result
483                .as_ref()
484                .is_err_and(|e| e.downcast_ref::<std::io::Error>().is_none())
485            && result.as_ref().is_err_and(|e| {
486                e.downcast_ref::<DomainError>()
487                    .is_some_and(|e| e.code == ErrorCode::SubtitleIntegrity)
488            })
489        {
490            // A failed mirror may have left a corrupt prefix. Only retry the
491            // canonical source from scratch after a resumed digest mismatch;
492            // a mirror mismatch should fall back to the canonical source.
493            result = download(client, id, &path, &source, stop);
494        }
495        match result {
496            Ok(()) => return Ok(()),
497            Err(error) => {
498                cancelled(stop)?;
499                failures.push(crate::redact::urls(&format!("{}: {error:#}", source.url)));
500                // A second server cannot fix a local filesystem failure.
501                if error.downcast_ref::<std::io::Error>().is_some()
502                    || error
503                        .downcast_ref::<DomainError>()
504                        .is_some_and(|e| e.code == ErrorCode::SubtitleSpace)
505                {
506                    return Err(error.context(failures.join("; ")));
507                }
508                last_error = Some(error);
509            }
510        }
511    }
512    Err(last_error
513        .expect("canonical source")
514        .context(failures.join("; ")))
515}
516
517fn pending_bytes(config: &Config, models: &[Model], stop: &AtomicBool) -> Result<u64> {
518    let mut pending = 0_u64;
519    for model in models {
520        for file in &model.files {
521            let path = model_dir(config, &model.id).join(&file.path);
522            if valid(&path, file, stop)? {
523                continue;
524            }
525            let partial = partial_path(&path);
526            let mut have = partial_len(&partial)?;
527            if have >= file.size {
528                if valid(&partial, file, stop)? {
529                    continue;
530                }
531                have = 0;
532            }
533            pending = pending
534                .checked_add(file.size - have)
535                .context("model installation size overflow")?;
536        }
537    }
538    Ok(pending)
539}
540
541fn check_space(pending: u64, available: u64) -> Result<()> {
542    if pending > 0 {
543        let required = pending.saturating_add(64 * 1024 * 1024);
544        ensure!(
545            available >= required,
546            DomainError::new(ErrorCode::SubtitleSpace)
547                .with("available", available)
548                .with("required", required)
549        );
550    }
551    Ok(())
552}
553
554fn model_list(config: &Config) -> Result<serde_json::Value> {
555    let _lock = lock(config, false)?;
556    let mut rows = Vec::new();
557    for model in manifest().into_iter().filter(|m| m.id != "silero-vad") {
558        let mut bytes = 0_u64;
559        let mut all_present = true;
560        for required in required_models(&model.id)? {
561            for file in &required.files {
562                bytes = bytes
563                    .checked_add(file.size)
564                    .context("model size overflow")?;
565                all_present &= present(&model_dir(config, &required.id).join(&file.path), file)?;
566            }
567        }
568        rows.push(serde_json::json!({"id":model.id,"bytes":bytes,"license":model.license,"license_url":model.license_url,"present":all_present}));
569    }
570    Ok(serde_json::json!(rows))
571}
572
573async fn network<T>(future: impl std::future::Future<Output = T>, stop: &AtomicBool) -> Result<T> {
574    let timeout = tokio::time::sleep(Duration::from_secs(20));
575    tokio::pin!(future, timeout);
576    loop {
577        cancelled(stop)?;
578        tokio::select! {
579            result = &mut future => return Ok(result),
580            _ = &mut timeout => bail!("model download stalled"),
581            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
582        }
583    }
584}
585
586pub(super) fn runtime_check(config: &Config, stop: &std::sync::Arc<AtomicBool>) -> Result<()> {
587    cancelled(stop)?;
588    ensure_runtime_platform()?;
589    let result = crate::process::output(Command::new(python(config)).args(["-I", "-c", "import sherpa_onnx,numpy; assert sherpa_onnx.__version__ == '2.0.1'; assert numpy.__version__ == '2.2.6'"]), Duration::from_secs(15), stop);
590    cancelled(stop)?;
591    result.context(DomainError::new(ErrorCode::SubtitleRuntime))?;
592    Ok(())
593}
594
595pub fn manage(
596    config: &Config,
597    action: Action,
598    stop: &std::sync::Arc<AtomicBool>,
599) -> Result<serde_json::Value> {
600    use serde_json::json;
601    match action {
602        Action::Setup => {
603            cancelled(stop)?;
604            ensure_runtime_platform()?;
605            let _lock = lock(config, true)?;
606            if runtime_check(config, stop).is_err() {
607                cancelled(stop)?;
608                let requirements = root(config).join("requirements-2.0.1.lock");
609                fs::write(&requirements, include_str!("requirements.lock"))?;
610                let run = |command: &mut Command| -> Result<()> {
611                    command.env_remove("UV_EXCLUDE_NEWER");
612                    let bytes = crate::process::output(command, Duration::from_secs(600), stop)?;
613                    eprintln!("{}", String::from_utf8_lossy(&bytes));
614                    Ok(())
615                };
616                run(Command::new(&config.settings.subtitles.uv)
617                    .args([
618                        "--no-config",
619                        "venv",
620                        "--python",
621                        "3.12",
622                        "--managed-python",
623                        "--clear",
624                    ])
625                    .arg(runtime(config)))?;
626                run(Command::new(&config.settings.subtitles.uv)
627                    .args([
628                        "--no-config",
629                        "pip",
630                        "sync",
631                        "--require-hashes",
632                        "--only-binary",
633                        ":all:",
634                        "--default-index",
635                        "https://pypi.org/simple/",
636                        "--python",
637                    ])
638                    .arg(python(config))
639                    .arg(requirements))?;
640                runtime_check(config, stop)?;
641            }
642            Ok(json!({"runtime":runtime(config),"ready":true}))
643        }
644        Action::Doctor => {
645            let _lock = lock(config, false)?;
646            runtime_check(config, stop)?;
647            verify(config, &config.settings.subtitles.model, stop)?;
648            crate::process::output(
649                Command::new(&config.settings.ffmpeg).arg("-version"),
650                Duration::from_secs(5),
651                stop,
652            )?;
653            Ok(
654                json!({"ready":true,"runtime":runtime(config),"model":config.settings.subtitles.model}),
655            )
656        }
657        Action::Models {
658            action: ModelAction::List,
659        } => model_list(config),
660        Action::Models { action } => {
661            if matches!(action, ModelAction::Install { .. }) {
662                cancelled(stop)?;
663                ensure_runtime_platform()?;
664            }
665            let id = match &action {
666                ModelAction::Install { id }
667                | ModelAction::Verify { id }
668                | ModelAction::Remove { id } => id,
669                ModelAction::List => unreachable!(),
670            };
671            let required = required_models(id)?;
672            let _lock = lock(config, !matches!(action, ModelAction::Verify { .. }))?;
673            match &action {
674                ModelAction::Install { .. } => {
675                    let pending = pending_bytes(config, &required, stop)?;
676                    check_space(pending, fs2::available_space(root(config))?)?;
677                    eprintln!(
678                        "{}",
679                        crate::t!("subtitle_install_plan", "id" => id, "bytes" => pending)
680                    );
681                    let client = reqwest::Client::builder()
682                        .user_agent(crate::USER_AGENT)
683                        .no_gzip()
684                        .connect_timeout(Duration::from_secs(config.settings.connect_timeout_secs))
685                        // network() bounds each wait and polls cancellation;
686                        // a progressing large download has no total deadline.
687                        .build()?;
688                    for m in required {
689                        for f in &m.files {
690                            install_file(config, &client, &m.id, f, stop)?;
691                        }
692                    }
693                    verify(config, id, stop)?;
694                }
695                ModelAction::Verify { .. } => verify(config, id, stop)?,
696                ModelAction::Remove { .. } => {
697                    let dir = model_dir(config, id);
698                    if dir.exists() {
699                        fs::remove_dir_all(dir)?;
700                    }
701                }
702                ModelAction::List => unreachable!(),
703            }
704            Ok(json!({"id":id,"removed":matches!(action, ModelAction::Remove{..})}))
705        }
706    }
707}
708
709#[cfg(test)]
710mod tests {
711    use super::*;
712    use crate::test_support::{Server, response};
713
714    fn verification_fixture(directory: &Path) -> (PathBuf, ModelFile) {
715        let file = ModelFile {
716            path: "model.onnx".into(),
717            url: "http://127.0.0.1:1".into(),
718            size: 6,
719            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
720        };
721        let path = directory.join(&file.path);
722        fs::write(&path, b"abcdef").unwrap();
723        let stamp = FileStamp::read(&path).unwrap().unwrap();
724        let deadline = Instant::now() + Duration::from_secs(5);
725        while !stamp.cacheable_at(SystemTime::now()) {
726            assert!(Instant::now() < deadline, "file timestamp did not age");
727            std::thread::sleep(Duration::from_millis(10));
728        }
729        (path, file)
730    }
731
732    #[test]
733    fn verification_cache_does_not_trust_recent_timestamps() {
734        let dir = tempfile::tempdir().unwrap();
735        let (path, file) = verification_fixture(dir.path());
736        // A fresh write must not be cached even when its digest is correct.
737        fs::write(&path, b"abcdef").unwrap();
738        let stamp = FileStamp::read(&path).unwrap().unwrap();
739        let second = u64::try_from(stamp.changed.0).unwrap();
740        for offset in [0, 1, 2] {
741            assert_eq!(
742                stamp.cacheable_at(UNIX_EPOCH + Duration::from_secs(second + offset)),
743                offset == 2
744            );
745        }
746        let mut cache = VerificationCache::default();
747        assert!(
748            cache
749                .valid_at(
750                    &path,
751                    &file,
752                    &AtomicBool::new(false),
753                    UNIX_EPOCH + Duration::from_secs(second)
754                )
755                .unwrap()
756        );
757        assert!(cache.files.is_empty());
758    }
759
760    #[test]
761    fn verification_cache_reuses_hashes_but_checks_manifest_and_cancellation() {
762        let dir = tempfile::tempdir().unwrap();
763        let (path, file) = verification_fixture(dir.path());
764        let stop = AtomicBool::new(false);
765        let mut cache = VerificationCache::default();
766        assert!(cache.valid(&path, &file, &stop).unwrap());
767        assert!(cache.valid(&path, &file, &stop).unwrap());
768        assert_eq!(cache.hashes, 1);
769
770        stop.store(true, Ordering::Relaxed);
771        let error = cache.valid(&path, &file, &stop).unwrap_err();
772        assert_eq!(
773            error.downcast_ref::<DomainError>().unwrap().code,
774            ErrorCode::Cancelled
775        );
776        stop.store(false, Ordering::Relaxed);
777        let other = ModelFile {
778            sha256: format!("{:x}", Sha256::digest(b"xxxxxx")),
779            ..file.clone()
780        };
781        assert!(!cache.valid(&path, &other, &stop).unwrap());
782        assert!(cache.files.is_empty());
783        assert_eq!(cache.hashes, 2);
784        assert!(cache.valid(&path, &file, &stop).unwrap());
785        assert_eq!(cache.hashes, 3);
786
787        let mut fresh = VerificationCache::default();
788        assert!(fresh.valid(&path, &file, &stop).unwrap());
789        assert_eq!(fresh.hashes, 1);
790    }
791
792    #[test]
793    fn verification_cache_rehashes_changed_files_even_with_restored_mtime() {
794        use std::fs::FileTimes;
795        for replacement in [false, true] {
796            let dir = tempfile::tempdir().unwrap();
797            let (path, file) = verification_fixture(dir.path());
798            let stop = AtomicBool::new(false);
799            let mut cache = VerificationCache::default();
800            assert!(cache.valid(&path, &file, &stop).unwrap());
801            let original = fs::metadata(&path).unwrap();
802            if replacement {
803                let other = dir.path().join("replacement");
804                fs::write(&other, b"xxxxxx").unwrap();
805                fs::rename(other, &path).unwrap();
806            } else {
807                fs::write(&path, b"xxxxxx").unwrap();
808            }
809            File::options()
810                .write(true)
811                .open(&path)
812                .unwrap()
813                .set_times(FileTimes::new().set_modified(original.modified().unwrap()))
814                .unwrap();
815            let changed = fs::metadata(&path).unwrap();
816            assert_eq!(changed.modified().unwrap(), original.modified().unwrap());
817            assert_eq!(changed.len(), original.len());
818            assert!(!cache.valid(&path, &file, &stop).unwrap());
819            assert_eq!(cache.hashes, 2);
820            assert!(cache.files.is_empty());
821            // A failed verification must not become a reusable result.
822            assert!(!cache.valid(&path, &file, &stop).unwrap());
823            assert_eq!(cache.hashes, 3);
824        }
825    }
826
827    #[test]
828    fn verification_cache_rejects_missing_resized_and_nonregular_files() {
829        use std::os::unix::fs::symlink;
830        for mode in 0..4 {
831            let dir = tempfile::tempdir().unwrap();
832            let (path, file) = verification_fixture(dir.path());
833            let stop = AtomicBool::new(false);
834            let mut cache = VerificationCache::default();
835            assert!(cache.valid(&path, &file, &stop).unwrap());
836            fs::remove_file(&path).unwrap();
837            match mode {
838                0 => {}
839                1 => fs::write(&path, b"short").unwrap(),
840                2 => fs::create_dir(&path).unwrap(),
841                _ => {
842                    let target = dir.path().join("target");
843                    fs::write(&target, b"abcdef").unwrap();
844                    symlink(target, &path).unwrap();
845                }
846            }
847            assert!(!cache.valid(&path, &file, &stop).unwrap());
848            assert!(cache.files.is_empty());
849            assert_eq!(cache.hashes, 1);
850        }
851    }
852
853    #[test]
854    fn verification_cache_preserves_permission_errors() {
855        use std::os::unix::fs::PermissionsExt;
856        if unsafe { libc::geteuid() } == 0 {
857            return;
858        }
859        let dir = tempfile::tempdir().unwrap();
860        let (path, file) = verification_fixture(dir.path());
861        let stop = AtomicBool::new(false);
862        let mut cache = VerificationCache::default();
863        assert!(cache.valid(&path, &file, &stop).unwrap());
864        let permissions = fs::metadata(&path).unwrap().permissions();
865        fs::set_permissions(&path, fs::Permissions::from_mode(0o000)).unwrap();
866        let cached = cache.valid(&path, &file, &stop);
867        let full = valid(&path, &file, &stop);
868        fs::set_permissions(&path, permissions).unwrap();
869        for result in [cached, full] {
870            assert_eq!(
871                result
872                    .unwrap_err()
873                    .downcast_ref::<std::io::Error>()
874                    .unwrap()
875                    .kind(),
876                std::io::ErrorKind::PermissionDenied
877            );
878        }
879        assert!(cache.files.is_empty());
880        assert!(cache.valid(&path, &file, &stop).unwrap());
881    }
882
883    #[test]
884    fn shared_locks_open_existing_resources_without_write_access() {
885        use std::os::{fd::AsRawFd, unix::fs::PermissionsExt};
886        let dir = tempfile::tempdir().unwrap();
887        let config = crate::test_support::config(dir.path());
888        drop(lock(&config, true).unwrap());
889        let reader = lock(&config, false).unwrap();
890        let flags = unsafe { libc::fcntl(reader.0.as_raw_fd(), libc::F_GETFL) };
891        assert_ne!(flags, -1);
892        assert_eq!(flags & libc::O_ACCMODE, libc::O_RDONLY);
893        assert!(lock(&config, true).is_err());
894        drop(reader);
895        assert!(lock(&config, true).is_ok());
896
897        let directory = root(&config);
898        let path = directory.join("resources.lock");
899        let dir_permissions = fs::metadata(&directory).unwrap().permissions();
900        let file_permissions = fs::metadata(&path).unwrap().permissions();
901        fs::set_permissions(&directory, fs::Permissions::from_mode(0o555)).unwrap();
902        fs::set_permissions(&path, fs::Permissions::from_mode(0o444)).unwrap();
903        let result = model_list(&config);
904        fs::set_permissions(&directory, dir_permissions).unwrap();
905        fs::set_permissions(&path, file_permissions).unwrap();
906        assert!(result.is_ok(), "{result:?}");
907    }
908
909    #[test]
910    fn read_only_directory_without_a_lock_reports_why_it_cannot_coordinate() {
911        use std::os::unix::fs::PermissionsExt;
912        if unsafe { libc::geteuid() } == 0 {
913            return; // Root bypasses the permission boundary this test exercises.
914        }
915        let dir = tempfile::tempdir().unwrap();
916        let config = crate::test_support::config(dir.path());
917        let directory = root(&config);
918        fs::create_dir_all(&directory).unwrap();
919        let permissions = fs::metadata(&directory).unwrap().permissions();
920        fs::set_permissions(&directory, fs::Permissions::from_mode(0o555)).unwrap();
921        let result = model_list(&config);
922        fs::set_permissions(&directory, permissions).unwrap();
923        let error = result.unwrap_err();
924        assert_eq!(
925            error.downcast_ref::<std::io::Error>().unwrap().kind(),
926            std::io::ErrorKind::PermissionDenied
927        );
928        assert!(error.to_string().contains("resources.lock"));
929        assert!(!directory.join("resources.lock").exists());
930    }
931
932    #[test]
933    fn corrupt_download_cleanup_keeps_integrity_context_and_tolerates_missing_files() {
934        use std::os::unix::fs::PermissionsExt;
935        let dir = tempfile::tempdir().unwrap();
936        let path = dir.path().join("model.onnx");
937        let error = reject_corrupt_download("test-model", &path).unwrap_err();
938        let integrity = error.downcast_ref::<DomainError>().unwrap();
939        assert_eq!(integrity.code, ErrorCode::SubtitleIntegrity);
940        assert_eq!(integrity.params["id"], "test-model");
941        assert_eq!(integrity.params["path"], path.to_string_lossy());
942        assert!(error.downcast_ref::<std::io::Error>().is_none());
943
944        if unsafe { libc::geteuid() } == 0 {
945            return;
946        }
947        let partial = partial_path(&path);
948        fs::write(&partial, b"corrupt").unwrap();
949        let permissions = fs::metadata(dir.path()).unwrap().permissions();
950        fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o555)).unwrap();
951        let result = reject_corrupt_download("test-model", &path);
952        fs::set_permissions(dir.path(), permissions).unwrap();
953        let error = result.unwrap_err();
954        assert_eq!(
955            error.downcast_ref::<DomainError>().unwrap().code,
956            ErrorCode::SubtitleIntegrity
957        );
958        assert_eq!(
959            error.downcast_ref::<std::io::Error>().unwrap().kind(),
960            std::io::ErrorKind::PermissionDenied
961        );
962        assert!(format!("{error:#}").contains("cannot remove corrupt download"));
963        assert!(partial.exists());
964    }
965
966    #[test]
967    fn cleanup_permission_errors_keep_source_diagnostics_without_retrying() {
968        use std::{
969            os::unix::fs::PermissionsExt,
970            sync::{Arc, atomic::AtomicUsize},
971        };
972        if unsafe { libc::geteuid() } == 0 {
973            return;
974        }
975        let dir = tempfile::tempdir().unwrap();
976        let mut config = crate::test_support::config(dir.path());
977        let path = model_dir(&config, "test-model").join("model.onnx");
978        let directory = path.parent().unwrap().to_path_buf();
979        fs::create_dir_all(&directory).unwrap();
980        fs::write(partial_path(&path), b"xxx").unwrap();
981        let permissions = fs::metadata(&directory).unwrap().permissions();
982        let protected = directory.clone();
983        let calls = Arc::new(AtomicUsize::new(0));
984        let counter = calls.clone();
985        let origin = Server::new(move |_, stream| {
986            counter.fetch_add(1, Ordering::Relaxed);
987            fs::set_permissions(&protected, fs::Permissions::from_mode(0o555)).unwrap();
988            response(stream, 200, "application/octet-stream", b"xxxxxx");
989        });
990        let mirror = Server::new(|_, stream| response(stream, 404, "text/plain", b"missing"));
991        config.settings.subtitles.model_mirror = Some(mirror.url.clone());
992        let file = ModelFile {
993            path: "model.onnx".into(),
994            url: origin.url.clone(),
995            size: 6,
996            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
997        };
998        let result = install_file(
999            &config,
1000            &reqwest::Client::new(),
1001            "test-model",
1002            &file,
1003            &AtomicBool::new(false),
1004        );
1005        fs::set_permissions(&directory, permissions).unwrap();
1006        let error = result.unwrap_err();
1007        assert_eq!(
1008            error.downcast_ref::<DomainError>().unwrap().params["id"],
1009            "test-model"
1010        );
1011        assert_eq!(
1012            error.downcast_ref::<std::io::Error>().unwrap().kind(),
1013            std::io::ErrorKind::PermissionDenied
1014        );
1015        let diagnostic = format!("{error:#}");
1016        assert!(diagnostic.contains(&mirror.url));
1017        assert!(diagnostic.contains(&origin.url));
1018        assert!(diagnostic.contains(&path.to_string_lossy().to_string()));
1019        assert!(diagnostic.contains("cannot remove corrupt download"));
1020        assert_eq!(calls.load(Ordering::Relaxed), 1);
1021    }
1022
1023    #[test]
1024    fn runtime_platform_support_checks_host_libc() {
1025        assert!(supports_runtime("linux", "x86_64", true));
1026        assert!(!supports_runtime("linux", "x86_64", false));
1027        assert!(!supports_runtime("linux", "aarch64", true));
1028        assert!(supports_runtime("macos", "x86_64", false));
1029        assert!(supports_runtime("macos", "aarch64", false));
1030        assert!(!supports_runtime("windows", "x86_64", false));
1031    }
1032
1033    #[test]
1034    fn cancelled_runtime_checks_do_not_start_installation_or_mask_the_error() {
1035        let dir = tempfile::tempdir().unwrap();
1036        let config = crate::test_support::config(dir.path());
1037        let stop = std::sync::Arc::new(AtomicBool::new(true));
1038        for error in [
1039            runtime_check(&config, &stop).unwrap_err(),
1040            manage(&config, Action::Setup, &stop).unwrap_err(),
1041            manage(
1042                &config,
1043                Action::Models {
1044                    action: ModelAction::Install {
1045                        id: "sensevoice-small-yue-int8".into(),
1046                    },
1047                },
1048                &stop,
1049            )
1050            .unwrap_err(),
1051        ] {
1052            assert_eq!(
1053                error.downcast_ref::<DomainError>().unwrap().code,
1054                ErrorCode::Cancelled
1055            );
1056        }
1057        assert!(!root(&config).exists());
1058    }
1059
1060    #[test]
1061    fn model_install_checks_platform_before_model_lookup_or_disk_changes() {
1062        let dir = tempfile::tempdir().unwrap();
1063        let config = crate::test_support::config(dir.path());
1064        let expected = if ensure_runtime_platform().is_ok() {
1065            ErrorCode::SubtitleModel
1066        } else {
1067            ErrorCode::SubtitlePlatform
1068        };
1069        let error = manage(
1070            &config,
1071            Action::Models {
1072                action: ModelAction::Install {
1073                    id: "unknown-model".into(),
1074                },
1075            },
1076            &std::sync::Arc::new(AtomicBool::new(false)),
1077        )
1078        .unwrap_err();
1079        assert_eq!(error.downcast_ref::<DomainError>().unwrap().code, expected);
1080        assert!(!root(&config).exists());
1081    }
1082
1083    #[test]
1084    fn network_waits_remain_cancellable_and_abort_when_stalled() {
1085        let runtime = tokio::runtime::Builder::new_current_thread()
1086            .enable_all()
1087            .build()
1088            .unwrap();
1089        runtime.block_on(async {
1090            let stop = AtomicBool::new(false);
1091            let cancel = async {
1092                tokio::time::sleep(Duration::from_millis(100)).await;
1093                stop.store(true, Ordering::Relaxed);
1094            };
1095            let wait = network(std::future::pending::<()>(), &stop);
1096            let (result, ()) =
1097                tokio::time::timeout(Duration::from_secs(5), async { tokio::join!(wait, cancel) })
1098                    .await
1099                    .expect("network cancellation must remain responsive");
1100            assert_eq!(
1101                result
1102                    .unwrap_err()
1103                    .downcast_ref::<DomainError>()
1104                    .unwrap()
1105                    .code,
1106                ErrorCode::Cancelled
1107            );
1108
1109            stop.store(false, Ordering::Relaxed);
1110            let result = tokio::time::timeout(
1111                Duration::from_secs(25),
1112                network(std::future::pending::<()>(), &stop),
1113            )
1114            .await
1115            .expect("stalled network waits must time out");
1116            assert_eq!(result.unwrap_err().to_string(), "model download stalled");
1117        });
1118    }
1119
1120    #[test]
1121    fn mirrors_preserve_base_paths_and_fall_back_without_changing_hashes() {
1122        use std::sync::{Arc, atomic::AtomicUsize};
1123        // Successful mirror, bad digest, missing file, corrupt prefix left by
1124        // a mirror, and resumed mirror mismatches with Range honored/ignored.
1125        for mode in 0..6 {
1126            let origin_calls = Arc::new(AtomicUsize::new(0));
1127            let calls = origin_calls.clone();
1128            let origin = Server::new(move |request, stream| {
1129                let attempt = calls.fetch_add(1, Ordering::Relaxed);
1130                if mode == 3 && attempt == 0 {
1131                    assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1132                    stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap();
1133                } else {
1134                    assert!(!request.to_ascii_lowercase().contains("range:"));
1135                    response(stream, 200, "application/octet-stream", b"abcdef");
1136                }
1137            });
1138            let mirror_calls = Arc::new(AtomicUsize::new(0));
1139            let calls = mirror_calls.clone();
1140            let mirror = Server::new(move |request, stream| {
1141                calls.fetch_add(1, Ordering::Relaxed);
1142                assert!(request.starts_with("GET /base/test-model/tokenizer/vocab.json "));
1143                match mode {
1144                    0 => response(stream, 200, "application/octet-stream", b"abcdef"),
1145                    1 | 5 => response(stream, 200, "application/octet-stream", b"xxxxxx"),
1146                    2 => response(stream, 404, "text/plain", b"missing"),
1147                    3 => response(stream, 200, "application/octet-stream", b"xxx"),
1148                    _ => {
1149                        assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1150                        stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\nxxx").unwrap();
1151                    }
1152                }
1153            });
1154            let dir = tempfile::tempdir().unwrap();
1155            let mut config = crate::test_support::config(dir.path());
1156            config.settings.subtitles.model_mirror = Some(format!("{}/base/", mirror.url));
1157            let file = ModelFile {
1158                path: "tokenizer/vocab.json".into(),
1159                url: origin.url.clone(),
1160                size: 6,
1161                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1162            };
1163            if mode >= 4 {
1164                let path = model_dir(&config, "test-model").join(&file.path);
1165                fs::create_dir_all(path.parent().unwrap()).unwrap();
1166                fs::write(partial_path(&path), b"abc").unwrap();
1167            }
1168            install_file(
1169                &config,
1170                &reqwest::Client::new(),
1171                "test-model",
1172                &file,
1173                &AtomicBool::new(false),
1174            )
1175            .unwrap();
1176            assert_eq!(
1177                fs::read(model_dir(&config, "test-model").join(&file.path)).unwrap(),
1178                b"abcdef"
1179            );
1180            assert_eq!(
1181                origin_calls.load(Ordering::Relaxed),
1182                match mode {
1183                    0 => 0,
1184                    3 => 2,
1185                    _ => 1,
1186                }
1187            );
1188            assert_eq!(mirror_calls.load(Ordering::Relaxed), 1);
1189        }
1190    }
1191
1192    #[test]
1193    fn installation_space_accounts_for_verified_files_and_partial_downloads() {
1194        let dir = tempfile::tempdir().unwrap();
1195        let config = crate::test_support::config(dir.path());
1196        let model = Model {
1197            id: "test-model".into(),
1198            license: "MIT".into(),
1199            license_url: String::new(),
1200            files: vec![ModelFile {
1201                path: "model.onnx".into(),
1202                url: "http://127.0.0.1:1".into(),
1203                size: 6,
1204                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1205            }],
1206        };
1207        let models = [model];
1208        let path = model_dir(&config, "test-model").join("model.onnx");
1209        let stop = AtomicBool::new(false);
1210        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 6);
1211        fs::create_dir_all(path.parent().unwrap()).unwrap();
1212        fs::write(partial_path(&path), b"abc").unwrap();
1213        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 3);
1214        fs::write(&path, b"xxxxxx").unwrap();
1215        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 3);
1216        fs::write(partial_path(&path), b"xxxxxx").unwrap();
1217        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 6);
1218        fs::write(partial_path(&path), b"abcdef").unwrap();
1219        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 0);
1220        fs::write(&path, b"abcdef").unwrap();
1221        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 0);
1222        assert!(check_space(0, 0).is_ok());
1223        assert!(check_space(6, 6 + 64 * 1024 * 1024).is_ok());
1224        let error = check_space(6, 6 + 64 * 1024 * 1024 - 1).unwrap_err();
1225        assert_eq!(
1226            error.downcast_ref::<DomainError>().unwrap().code,
1227            ErrorCode::SubtitleSpace
1228        );
1229    }
1230
1231    #[test]
1232    fn vad_dependencies_are_independent_of_manifest_order() {
1233        let mut models = manifest();
1234        models.reverse();
1235        models.push(Model {
1236            id: "future-model".into(),
1237            license: "MIT".into(),
1238            license_url: String::new(),
1239            files: vec![],
1240        });
1241        let required = required_from(models, "qwen3-asr-0.6b-int8").unwrap();
1242        assert_eq!(required[0].id, "qwen3-asr-0.6b-int8");
1243        assert_eq!(required[1].id, "silero-vad");
1244    }
1245
1246    #[test]
1247    fn list_requires_vad_and_a_shared_resource_lock() {
1248        let dir = tempfile::tempdir().unwrap();
1249        let config = crate::test_support::config(dir.path());
1250        let required = required_models("sensevoice-small-int8").unwrap();
1251        let create = |model: &Model| {
1252            for file in &model.files {
1253                let path = model_dir(&config, &model.id).join(&file.path);
1254                fs::create_dir_all(path.parent().unwrap()).unwrap();
1255                File::create(path).unwrap().set_len(file.size).unwrap();
1256            }
1257        };
1258        create(&required[0]);
1259        let listed = model_list(&config).unwrap();
1260        assert_eq!(listed[0]["present"], false);
1261        assert_eq!(
1262            listed[0]["bytes"],
1263            required
1264                .iter()
1265                .flat_map(|m| &m.files)
1266                .map(|f| f.size)
1267                .sum::<u64>()
1268        );
1269        create(&required[1]);
1270        assert_eq!(model_list(&config).unwrap()[0]["present"], true);
1271        let writer = lock(&config, true).unwrap();
1272        assert_eq!(
1273            model_list(&config)
1274                .unwrap_err()
1275                .downcast_ref::<DomainError>()
1276                .unwrap()
1277                .code,
1278            ErrorCode::SubtitleBusy
1279        );
1280        drop(writer);
1281    }
1282
1283    #[test]
1284    fn validation_preserves_cancellation_and_io_errors() {
1285        let dir = tempfile::tempdir().unwrap();
1286        let path = dir.path().join("model.onnx");
1287        fs::write(&path, b"abcdef").unwrap();
1288        let file = ModelFile {
1289            path: "model.onnx".into(),
1290            url: "http://127.0.0.1:1".into(),
1291            size: 6,
1292            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1293        };
1294        let stop = AtomicBool::new(true);
1295        assert_eq!(
1296            valid(&path, &file, &stop)
1297                .unwrap_err()
1298                .downcast_ref::<DomainError>()
1299                .unwrap()
1300                .code,
1301            ErrorCode::Cancelled
1302        );
1303        assert_eq!(
1304            download(&reqwest::Client::new(), "test-model", &path, &file, &stop)
1305                .unwrap_err()
1306                .downcast_ref::<DomainError>()
1307                .unwrap()
1308                .code,
1309            ErrorCode::Cancelled
1310        );
1311        assert!(valid(&path.join("child"), &file, &AtomicBool::new(false)).is_err());
1312        assert!(valid(&path, &file, &AtomicBool::new(false)).unwrap());
1313    }
1314
1315    #[test]
1316    fn downloads_resume_only_a_matching_range_and_restart_on_full_response() {
1317        for ranged in [true, false] {
1318            let server = Server::new(move |request, stream| {
1319                assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1320                if ranged {
1321                    stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap();
1322                } else {
1323                    response(stream, 200, "application/octet-stream", b"abcdef");
1324                }
1325            });
1326            let dir = tempfile::tempdir().unwrap();
1327            let path = dir.path().join("model.onnx");
1328            fs::write(dir.path().join("model.onnx.part"), b"abc").unwrap();
1329            let file = ModelFile {
1330                path: "model.onnx".into(),
1331                url: server.url.clone(),
1332                size: 6,
1333                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1334            };
1335            download(
1336                &reqwest::Client::builder()
1337                    .timeout(Duration::from_secs(5))
1338                    .build()
1339                    .unwrap(),
1340                "test-model",
1341                &path,
1342                &file,
1343                &AtomicBool::new(false),
1344            )
1345            .unwrap();
1346            assert_eq!(fs::read(path).unwrap(), b"abcdef");
1347        }
1348    }
1349
1350    #[test]
1351    fn bad_range_truncation_digest_and_cancellation_never_publish() {
1352        for mode in 0..4 {
1353            let server = Server::new(move |_, stream| {
1354                match mode {
1355                0 => stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 0-2/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap(),
1356                1 => response(stream, 200, "application/octet-stream", b"abc"),
1357                _ => response(stream, 200, "application/octet-stream", b"xxxxxx"),
1358            }
1359            });
1360            let dir = tempfile::tempdir().unwrap();
1361            let path = dir.path().join("model.onnx");
1362            let partial = dir.path().join("model.onnx.part");
1363            fs::write(&partial, b"abc").unwrap();
1364            let file = ModelFile {
1365                path: "model.onnx".into(),
1366                url: server.url.clone(),
1367                size: 6,
1368                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1369            };
1370            assert!(
1371                download(
1372                    &reqwest::Client::builder()
1373                        .timeout(Duration::from_secs(5))
1374                        .build()
1375                        .unwrap(),
1376                    "test-model",
1377                    &path,
1378                    &file,
1379                    &AtomicBool::new(mode == 3)
1380                )
1381                .is_err()
1382            );
1383            assert!(!path.exists());
1384            if mode == 2 {
1385                assert!(!partial.exists());
1386            }
1387        }
1388    }
1389
1390    #[test]
1391    fn resources_cannot_be_modified_during_recognition() {
1392        let dir = tempfile::tempdir().unwrap();
1393        let config = crate::test_support::config(dir.path());
1394        let reader = lock(&config, false).unwrap();
1395        assert!(lock(&config, true).is_err());
1396        assert!(lock(&config, false).is_ok());
1397        drop(reader);
1398        assert!(lock(&config, true).is_ok());
1399        assert!(required_models("../../outside").is_err());
1400    }
1401}