Skip to main content

wechat_protocol/
media.rs

1//! Bounded native CDN transport. This uses the observed HTTP carrier on port 443,
2//! not HTTPS. Media encryption and advertised size/digests are checked separately.
3use crate::errors::{Service, ServiceRejected};
4use crate::{
5    crypto::{md5, random, sha256},
6    protobuf as p, AccountState,
7};
8use anyhow::{bail, ensure, Result};
9use openssl::{
10    hash::{Hasher, MessageDigest},
11    symm::{Cipher, Crypter, Mode},
12};
13use serde::{Deserialize, Serialize};
14use std::{collections::BTreeMap, fmt, io::Read, net::Ipv4Addr, time::Duration};
15use zeroize::Zeroize;
16pub type CdnFields = BTreeMap<String, Vec<u8>>;
17const MAX_CDN_BODY: usize = 2 * 1024 * 1024;
18const UPLOAD_CHUNK: usize = 65536;
19pub const MAX_CDN_TRANSFER_BYTES: usize = 25 * 1024 * 1024;
20pub const MAX_NATIVE_UPLOAD_BYTES: usize = 25 * 1024 * 1024 - 17;
21pub const MAX_LARGE_FILE_BYTES: usize = wechat_media::MAX_FILE_BYTES;
22/// Incoming video plaintext limit, matching the ordinary-file download and
23/// automatic-download ceiling. Every such transfer stays bound to the
24/// advertised length and MD5. Outgoing preparation keeps the separate
25/// `wechat_media::MAX_VIDEO_SOURCE_BYTES` source limit.
26pub const MAX_VIDEO_DOWNLOAD_BYTES: usize = wechat_media::MAX_FILE_BYTES;
27/// A video advertised at or below the ordinary transfer threshold is fetched
28/// before its length is compared, so its reply keeps the earlier 50 MiB bound.
29const MAX_SMALL_VIDEO_REPLY_BYTES: usize = 50 * 1024 * 1024;
30#[path = "large_cdn_config.rs"]
31mod large_cdn_config;
32#[path = "large_file.rs"]
33mod large_file;
34pub use large_cdn_config::{decode_large_cdn_config, LargeCdnRoute};
35#[path = "avatar.rs"]
36mod avatar;
37pub use avatar::*;
38#[path = "link_thumbnail.rs"]
39mod link_thumbnail;
40pub use link_thumbnail::*;
41#[path = "sticker.rs"]
42mod sticker;
43pub use sticker::*;
44
45#[derive(Clone, Copy, Serialize, Deserialize)]
46pub struct CdnLocation {
47    pub province: u32,
48    pub city: u32,
49    pub isp: u32,
50}
51#[derive(Clone, Serialize, Deserialize)]
52pub struct CdnConfig {
53    pub version: u32,
54    pub uin: u64,
55    pub client_version: u32,
56    pub client_os_type: String,
57    pub auth_key: Vec<u8>,
58    pub server_ips: Vec<Ipv4Addr>,
59    #[serde(default)]
60    pub front_id: u32,
61    #[serde(default)]
62    pub location: Option<CdnLocation>,
63}
64impl Drop for CdnConfig {
65    fn drop(&mut self) {
66        self.auth_key.zeroize();
67    }
68}
69impl fmt::Debug for CdnConfig {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.debug_struct("CdnConfig")
72            .field("credentials", &"[redacted]")
73            .finish()
74    }
75}
76impl CdnConfig {
77    pub fn validate(&self) -> Result<()> {
78        ensure!(
79            self.uin != 0
80                && !self.auth_key.is_empty()
81                && self.auth_key.len() <= 65536
82                && !self.client_os_type.is_empty()
83                && self.client_os_type.len() <= 1024
84                && !self.server_ips.is_empty()
85                && self.server_ips.len() <= 32,
86            "Invalid CDN config"
87        );
88        for ip in &self.server_ips {
89            ensure!(public_ipv4(*ip), "CDN endpoint is not public IPv4");
90        }
91        Ok(())
92    }
93}
94pub fn decode_cdn_config(input: &[u8], account: &AccountState) -> Result<CdnConfig> {
95    let f = p::decode(input)?;
96    let code = p::base_code(&f)?;
97    ServiceRejected::check(Service::CdnBootstrap, code)?;
98    let group = p::decode(p::data(&f, 2)?)?;
99    let version = u32::try_from(p::scalar(&group, 1)?)?;
100    let uin = p::scalar(&group, 2)?;
101    ensure!(uin == account.uin as u64, "CDN account identity mismatch");
102    let auth_key = p::unbuffer(p::data(&group, 8)?)?;
103    let mut server_ips = vec![];
104    for field in group.iter().filter(|f| f.number == 6) {
105        let p::Value::Bytes(raw) = &field.value else {
106            bail!("Invalid CDN endpoint record");
107        };
108        let ip = p::decode(raw)?;
109        server_ips.push(std::str::from_utf8(p::data(&ip, 1)?)?.parse()?);
110    }
111    let conf = CdnConfig {
112        version,
113        uin,
114        client_version: account.device.client_version as u32,
115        client_os_type: account.device.device_type.clone(),
116        auth_key,
117        server_ips,
118        front_id: 0,
119        location: None,
120    };
121    conf.validate()?;
122    Ok(conf)
123}
124fn public_ipv4(ip: Ipv4Addr) -> bool {
125    let [a, b, c, _] = ip.octets();
126    !(a == 0
127        || a == 10
128        || a == 127
129        || a >= 224
130        || (a == 100 && (64..=127).contains(&b))
131        || (a == 169 && b == 254)
132        || (a == 172 && (16..=31).contains(&b))
133        || (a == 192 && (b == 0 || b == 168 || (b == 2 && c == 0)))
134        || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100)))
135        || (a == 203 && b == 0 && c == 113))
136}
137pub fn pack_cdn(fields: &CdnFields) -> Result<Vec<u8>> {
138    // A 512 MiB completion can contain 1025 ordered block etags. The byte
139    // envelope remains independently bounded, including individual values.
140    ensure!(fields.len() <= 2048, "Too many CDN fields");
141    let mut out = vec![];
142    for (k, v) in fields {
143        ensure!(
144            !k.is_empty()
145                && k.len() <= 128
146                && k.bytes()
147                    .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'),
148            "Invalid CDN key"
149        );
150        for b in [k.as_bytes(), v.as_slice()] {
151            ensure!(b.len() <= MAX_CDN_BODY, "CDN field too large");
152            out.extend((b.len() as u32).to_be_bytes());
153            out.extend(b);
154            ensure!(out.len() <= MAX_CDN_BODY, "CDN request exceeds limit");
155        }
156    }
157    Ok(out)
158}
159pub fn unpack_cdn(input: &[u8]) -> Result<CdnFields> {
160    ensure!(input.len() <= MAX_CDN_BODY, "CDN reply exceeds limit");
161    let mut c = crate::mmtls::Cursor::new(input);
162    let mut out = CdnFields::new();
163    let mut consumed = 0;
164    while consumed < input.len() {
165        ensure!(out.len() < 256, "Too many CDN reply fields");
166        let key = c.sized(4)?;
167        let value = c.sized(4)?;
168        consumed += 8 + key.len() + value.len();
169        ensure!(
170            !key.is_empty()
171                && key.len() <= 128
172                && key
173                    .iter()
174                    .all(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'-'),
175            "Invalid CDN reply key"
176        );
177        let key = std::str::from_utf8(key)?.to_string();
178        ensure!(
179            out.insert(key, value.to_vec()).is_none(),
180            "Duplicate CDN reply key"
181        );
182    }
183    c.end()?;
184    Ok(out)
185}
186fn integer(fields: &CdnFields, key: &str) -> Result<i64> {
187    let raw = fields
188        .get(key)
189        .ok_or_else(|| anyhow::anyhow!("Missing CDN integer {key}"))?;
190    let text = std::str::from_utf8(raw)?;
191    ensure!(
192        !text.is_empty()
193            && text.len() <= 16
194            && text
195                .bytes()
196                .enumerate()
197                .all(|(i, b)| b.is_ascii_digit() || (i == 0 && b == b'-')),
198        "Invalid CDN integer {key}"
199    );
200    Ok(text.parse()?)
201}
202fn put(fields: &mut CdnFields, key: &str, value: impl ToString) {
203    fields.insert(key.to_string(), value.to_string().into_bytes());
204}
205fn shared(conf: &CdnConfig, sequence: u32, filetype: u32) -> CdnFields {
206    let mut f = CdnFields::new();
207    for (k, v) in [
208        ("ver", conf.version as u64),
209        ("weixinnum", conf.uin),
210        ("seq", sequence as u64),
211        ("clientversion", conf.client_version as u64),
212        ("nettype", 1),
213        ("acceptdupack", 1),
214        ("safeproto", 1),
215        ("filetype", filetype as u64),
216        ("wxchattype", 0),
217        ("lastretcode", 0),
218        ("ipseq", 0),
219        ("largesvideo", 0),
220        ("sourceflag", 0),
221    ] {
222        put(&mut f, k, v);
223    }
224    f.insert(
225        "clientostype".into(),
226        conf.client_os_type.as_bytes().to_vec(),
227    );
228    f.insert("authkey".into(), conf.auth_key.clone());
229    f
230}
231pub fn encrypt_recipient(destination: &str) -> Result<String> {
232    ensure!(
233        !destination.is_empty()
234            && destination.len() <= 512
235            && !destination
236                .chars()
237                .any(|c| c.is_control() || c.is_whitespace()),
238        "Invalid CDN recipient"
239    );
240    let key = b"wxusrname2016cdn";
241    let encrypted = openssl::symm::encrypt(
242        Cipher::aes_128_cbc(),
243        key,
244        Some(key),
245        destination.as_bytes(),
246    )?;
247    Ok(format!("@cdn2_{}", hex::encode(encrypted)))
248}
249fn ecb_encrypt(raw: &[u8], key: &[u8]) -> Result<Vec<u8>> {
250    ensure!(key.len() == 16, "Invalid CDN key");
251    Ok(openssl::symm::encrypt(
252        Cipher::aes_128_ecb(),
253        key,
254        None,
255        raw,
256    )?)
257}
258pub fn decrypt_cdn(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> {
259    ensure!(
260        key.len() == 16
261            && !encrypted.is_empty()
262            && encrypted.len().is_multiple_of(16)
263            && encrypted.len() <= MAX_LARGE_FILE_BYTES + 16,
264        "Invalid CDN ciphertext"
265    );
266    Ok(openssl::symm::decrypt(
267        Cipher::aes_128_ecb(),
268        key,
269        None,
270        encrypted,
271    )?)
272}
273
274#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
275#[serde(rename_all = "snake_case")]
276pub enum MediaQuality {
277    Original,
278    Standard,
279    Thumbnail,
280    Video,
281    File,
282    LargeFile,
283}
284impl MediaQuality {
285    pub fn filetype(self) -> u32 {
286        match self {
287            Self::Original => 1,
288            Self::Standard => 2,
289            Self::Thumbnail => 3,
290            Self::Video => 4,
291            Self::File => 5,
292            Self::LargeFile => 7,
293        }
294    }
295}
296fn upload_plaintext_limit(kind: MediaQuality) -> usize {
297    match kind {
298        MediaQuality::File => MAX_CDN_TRANSFER_BYTES,
299        MediaQuality::LargeFile => MAX_LARGE_FILE_BYTES,
300        MediaQuality::Video => wechat_media::MAX_VIDEO_SOURCE_BYTES,
301        _ => MAX_NATIVE_UPLOAD_BYTES,
302    }
303}
304#[derive(Clone, Serialize, Deserialize)]
305pub struct MediaDescriptor {
306    pub file_id: String,
307    pub aes_key: Vec<u8>,
308    pub quality: MediaQuality,
309    pub expected_bytes: Option<u64>,
310    pub expected_md5: Option<String>,
311    pub strict_size: bool,
312    pub expires_at: Option<u64>,
313}
314impl Drop for MediaDescriptor {
315    fn drop(&mut self) {
316        self.aes_key.zeroize();
317        self.file_id.zeroize();
318    }
319}
320impl fmt::Debug for MediaDescriptor {
321    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322        f.debug_struct("MediaDescriptor")
323            .field("quality", &self.quality)
324            .field("metadata", &"[redacted]")
325            .finish()
326    }
327}
328pub struct DownloadedMedia {
329    pub bytes: Vec<u8>,
330    pub requested_quality: MediaQuality,
331    pub substituted_filetype: Option<u32>,
332    pub picture_format: Option<u32>,
333    pub md5: String,
334    pub sha256: String,
335    pub advertised_size_matches: Option<bool>,
336}
337#[derive(Clone, Serialize, Deserialize)]
338pub struct PreparedUpload {
339    pub kind: MediaQuality,
340    pub aes_key: Vec<u8>,
341    pub file_key: String,
342    pub encrypted: Vec<u8>,
343    pub plaintext_bytes: u32,
344    pub md5: String,
345    pub sha256: String,
346    pub file_name: String,
347    pub destination: String,
348}
349impl Drop for PreparedUpload {
350    fn drop(&mut self) {
351        self.aes_key.zeroize();
352        self.file_key.zeroize();
353        self.encrypted.zeroize();
354    }
355}
356impl fmt::Debug for PreparedUpload {
357    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
358        f.debug_struct("PreparedUpload")
359            .field("kind", &self.kind)
360            .field("plaintext_bytes", &self.plaintext_bytes)
361            .field("material", &"[redacted]")
362            .finish()
363    }
364}
365impl PreparedUpload {
366    /// Generate independent upload material. Persist either this result or the
367    /// source bytes and its key/file-key before the first upload request.
368    pub fn prepare(
369        raw: &[u8],
370        kind: MediaQuality,
371        file_name: &str,
372        destination: &str,
373    ) -> Result<Self> {
374        Self::with_material(
375            raw,
376            kind,
377            file_name,
378            destination,
379            &random::<16>()?,
380            &hex::encode(random::<16>()?),
381        )
382    }
383    /// Rebuild exactly the same encrypted upload from durable owned source bytes.
384    pub fn with_material(
385        raw: &[u8],
386        kind: MediaQuality,
387        file_name: &str,
388        destination: &str,
389        key: &[u8],
390        file_key: &str,
391    ) -> Result<Self> {
392        ensure!(
393            !raw.is_empty()
394                && raw.len() <= upload_plaintext_limit(kind)
395                && [
396                    MediaQuality::File,
397                    MediaQuality::LargeFile,
398                    MediaQuality::Original,
399                    MediaQuality::Video
400                ]
401                .contains(&kind),
402            "Unsupported upload profile"
403        );
404        ensure!(
405            !file_name.is_empty()
406                && file_name.len() <= 255
407                && file_name != "."
408                && file_name != ".."
409                && !file_name.contains(['/', '\\'])
410                && !file_name.chars().any(|c| c.is_control()),
411            "Invalid upload filename"
412        );
413        ensure!(
414            key.len() == 16
415                && file_key.len() == 32
416                && file_key.bytes().all(|b| b.is_ascii_hexdigit()),
417            "Invalid durable upload material"
418        );
419        encrypt_recipient(destination)?;
420        Ok(Self {
421            kind,
422            aes_key: key.to_vec(),
423            file_key: file_key.to_string(),
424            encrypted: ecb_encrypt(raw, key)?,
425            plaintext_bytes: raw.len() as u32,
426            md5: hex::encode(md5(raw)),
427            sha256: hex::encode(sha256(raw)),
428            file_name: file_name.into(),
429            destination: destination.into(),
430        })
431    }
432    fn validate(&self) -> Result<()> {
433        ensure!(
434            self.aes_key.len() == 16
435                && self.file_key.len() == 32
436                && self.file_key.bytes().all(|b| b.is_ascii_hexdigit())
437                && self.plaintext_bytes > 0
438                && self.plaintext_bytes as usize <= upload_plaintext_limit(self.kind)
439                && self.encrypted.len() == ((self.plaintext_bytes as usize / 16) + 1) * 16,
440            "Invalid prepared upload"
441        );
442        // Validate persisted ciphertext without allocating a second whole file.
443        let mut decryptor =
444            Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, &self.aes_key, None)?;
445        let mut buffer = zeroize::Zeroizing::new(vec![0; 128 * 1024 + 16]);
446        let mut md5 = Hasher::new(MessageDigest::md5())?;
447        let mut sha256 = Hasher::new(MessageDigest::sha256())?;
448        let mut plaintext_bytes = 0usize;
449        for chunk in self.encrypted.chunks(128 * 1024) {
450            let written = decryptor.update(chunk, &mut buffer)?;
451            md5.update(&buffer[..written])?;
452            sha256.update(&buffer[..written])?;
453            plaintext_bytes += written;
454        }
455        let written = decryptor.finalize(&mut buffer)?;
456        md5.update(&buffer[..written])?;
457        sha256.update(&buffer[..written])?;
458        plaintext_bytes += written;
459        ensure!(
460            plaintext_bytes == self.plaintext_bytes as usize
461                && hex::encode(md5.finish()?) == self.md5
462                && hex::encode(sha256.finish()?) == self.sha256,
463            "Prepared upload digest mismatch"
464        );
465        Ok(())
466    }
467}
468#[derive(Clone, Serialize, Deserialize)]
469pub struct UploadedMedia {
470    pub descriptor: MediaDescriptor,
471    pub plaintext_bytes: u32,
472    pub md5: String,
473    pub sha256: String,
474    pub file_name: String,
475    pub width: Option<u32>,
476    pub height: Option<u32>,
477}
478pub struct CdnClient {
479    config: CdnConfig,
480    timeout: Duration,
481}
482fn large_file_path(path: &str, uin: u64, sequence: u32) -> bool {
483    let parts: Vec<_> = path.split('/').collect();
484    parts.len() == 6
485        && parts[0].is_empty()
486        && parts[1] == "cdn"
487        && parts[2] == uin.to_string()
488        && sequence > 0
489        && parts[3] == sequence.to_string()
490        && matches!(parts[4], "4" | "5" | "7")
491        && matches!(parts[5], "apply" | "uploadblock" | "fin" | "complete")
492}
493impl CdnClient {
494    pub fn new(config: CdnConfig, timeout: Duration) -> Result<Self> {
495        config.validate()?;
496        ensure!(
497            !timeout.is_zero() && timeout <= Duration::from_secs(120),
498            "Invalid CDN timeout"
499        );
500        Ok(Self { config, timeout })
501    }
502    fn post(&self, path: &str, fields: &CdnFields) -> Result<CdnFields> {
503        let sequence = u32::try_from(integer(fields, "seq")?)?;
504        ensure!(sequence != 0, "Invalid CDN sequence");
505        ensure!(
506            ["/download", "/downloadbig", "/uploadv3"].contains(&path)
507                || large_file_path(path, self.config.uin, sequence),
508            "Invalid CDN path"
509        );
510        let body = pack_cdn(fields)?;
511        let client = reqwest::blocking::Client::builder()
512            .timeout(self.timeout)
513            .redirect(reqwest::redirect::Policy::none())
514            .no_proxy()
515            .build()?;
516        let mut response = client
517            .post(format!("http://{}:443{path}", self.config.server_ips[0]))
518            .header("Accept", "*/*")
519            .header("Content-Type", "application/octet-stream")
520            .header("User-Agent", "MicroMessenger Client")
521            .header("Connection", "close")
522            .header("C-Seq", sequence)
523            .body(body)
524            .send()?;
525        ensure!(
526            response.status() == 200,
527            "CDN HTTP status {}",
528            response.status().as_u16()
529        );
530        let mut raw = vec![];
531        response
532            .by_ref()
533            .take((MAX_CDN_BODY + 1) as u64)
534            .read_to_end(&mut raw)?;
535        unpack_cdn(&raw)
536    }
537    pub fn download(&self, descriptor: &MediaDescriptor) -> Result<DownloadedMedia> {
538        self.download_with_progress(descriptor, |_, _| Ok(()))
539    }
540    pub fn download_with_progress(
541        &self,
542        descriptor: &MediaDescriptor,
543        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
544    ) -> Result<DownloadedMedia> {
545        self.download_with_transport(descriptor, progress, |path, fields| self.post(path, fields))
546    }
547    fn download_with_transport(
548        &self,
549        descriptor: &MediaDescriptor,
550        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
551        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
552    ) -> Result<DownloadedMedia> {
553        ensure!(
554            !descriptor.file_id.is_empty()
555                && descriptor.file_id.len() <= 8192
556                && !descriptor.file_id.chars().any(|c| c.is_control())
557                && descriptor.aes_key.len() == 16,
558            "Invalid download descriptor"
559        );
560        if let Some(expiry) = descriptor.expires_at {
561            ensure!(expiry > crate::wire::unix_time()? as u64, "media_expired");
562        }
563        let large = descriptor.quality == MediaQuality::LargeFile;
564        let large_video = descriptor.quality == MediaQuality::Video
565            && descriptor.expected_bytes.unwrap_or(0) > MAX_CDN_TRANSFER_BYTES as u64;
566        let strict_transfer = large || large_video;
567        let plaintext_limit = if large {
568            MAX_LARGE_FILE_BYTES
569        } else if large_video {
570            MAX_VIDEO_DOWNLOAD_BYTES
571        } else if descriptor.quality == MediaQuality::Video && descriptor.expected_bytes.is_some() {
572            MAX_SMALL_VIDEO_REPLY_BYTES
573        } else {
574            MAX_CDN_TRANSFER_BYTES
575        };
576        let chunk_bytes = if strict_transfer { 512 * 1024 } else { 65536 };
577        let ciphertext_limit = (plaintext_limit / 16 + 1) * 16;
578        let expected_ciphertext = if strict_transfer {
579            let expected = usize::try_from(
580                descriptor
581                    .expected_bytes
582                    .ok_or_else(|| anyhow::anyhow!("Large file size is required"))?,
583            )?;
584            ensure!(
585                expected > 0 && expected <= plaintext_limit,
586                "Invalid large file size"
587            );
588            Some((expected / 16 + 1) * 16)
589        } else {
590            ensure!(
591                descriptor
592                    .expected_bytes
593                    .is_none_or(|size| size <= plaintext_limit as u64),
594                "Advertised media size outside limit"
595            );
596            None
597        };
598        if large_video {
599            ensure!(
600                descriptor.expected_md5.as_ref().is_some_and(
601                    |md5| md5.len() == 32 && md5.bytes().all(|b| b.is_ascii_hexdigit())
602                ),
603                "Large video source checksum is required"
604            );
605        }
606        let mut position = 0usize;
607        let mut total = 0usize;
608        let mut encrypted = vec![];
609        let mut substituted_filetype = None;
610        let mut picture_format = None;
611        let mut server_md5 = None;
612        let observe_video = descriptor.quality == MediaQuality::Video && !strict_transfer;
613        let mut video_reply = VideoReplyMetadata::default();
614        let mut video_selected_response: Option<bool> = None;
615        let mut video_response_md5: Option<String> = None;
616        let mut video_response_bytes: Option<u64> = None;
617        for sequence in 1..=u32::try_from(ciphertext_limit.div_ceil(chunk_bytes))? {
618            progress(
619                position as u64,
620                if total == 0 { None } else { Some(total as u64) },
621            )?;
622            let mut fields = shared(&self.config, sequence, descriptor.quality.filetype());
623            let requested_end = if total == 0 {
624                chunk_bytes - 1
625            } else {
626                (total - 1).min(position + chunk_bytes - 1)
627            };
628            for (k, v) in [
629                ("wxautostart", 0),
630                ("downpicformat", 1),
631                ("rangestart", position as u64),
632                ("rangeend", requested_end as u64),
633            ] {
634                put(&mut fields, k, v);
635            }
636            fields.insert("wxmsgflag".into(), vec![]);
637            fields.insert("fileid".into(), descriptor.file_id.as_bytes().to_vec());
638            let reply = post(if large { "/downloadbig" } else { "/download" }, &fields)?;
639            let code = integer(&reply, "retcode")?;
640            ServiceRejected::check_wide(Service::CdnDownload, code)?;
641            let response_digest = reply
642                .get("rawfilemd5")
643                .and_then(|v| std::str::from_utf8(v).ok())
644                .filter(|v| v.len() == 32 && v.bytes().all(|b| b.is_ascii_hexdigit()))
645                .map(|v| v.to_ascii_lowercase());
646            let server_selected = descriptor.quality == MediaQuality::Video
647                && !descriptor.strict_size
648                && matches!(integer(&reply, "istrans").ok(), Some(13 | 999))
649                && integer(&reply, "videoformat").ok() == Some(1)
650                && integer(&reply, "ver").ok() == Some(0)
651                && response_digest.is_some();
652            if server_selected {
653                validate_large_video_reply_fields(&reply)?;
654                if reply.contains_key("rawfilesize") {
655                    let size = u64::try_from(integer(&reply, "rawfilesize")?)?;
656                    ensure!(
657                        size > 0
658                            && size <= plaintext_limit as u64
659                            && video_response_bytes.is_none_or(|previous| previous == size),
660                        "Invalid video response size"
661                    );
662                    video_response_bytes = Some(size);
663                }
664                for key in [
665                    "retrysec",
666                    "isretry",
667                    "isoverload",
668                    "isgetcdn",
669                    "backsrcnum",
670                    "rsppicformat",
671                ] {
672                    ensure!(
673                        !reply.contains_key(key)
674                            || (integer(&reply, key)? == 0
675                                || (key == "backsrcnum" && matches!(integer(&reply, key)?, 0..=2))),
676                        "Unsupported server_selected-video control: {key}"
677                    );
678                }
679                ensure!(
680                    !reply.contains_key("substituteftype")
681                        || matches!(integer(&reply, "substituteftype")?, 0 | 4 | 9),
682                    "Unsupported server_selected-video representation"
683                );
684            }
685            if descriptor.quality == MediaQuality::Video {
686                ensure!(
687                    video_selected_response.is_none_or(|v| v == server_selected),
688                    "CDN representation changed during transfer"
689                );
690                video_selected_response = Some(server_selected);
691                if let Some(value) = response_digest {
692                    ensure!(
693                        video_response_md5.as_ref().is_none_or(|v| v == &value),
694                        "CDN representation changed during transfer"
695                    );
696                    video_response_md5 = Some(value);
697                } else if server_selected {
698                    anyhow::bail!("media_integrity_failed");
699                }
700            }
701            if strict_transfer || reply.contains_key("seq") {
702                ensure!(
703                    integer(&reply, "seq")? == i64::from(sequence),
704                    "CDN download reply sequence mismatch"
705                );
706            }
707            if large {
708                validate_large_download_reply(&reply, descriptor, &mut server_md5)?;
709            } else if large_video && !server_selected {
710                validate_large_video_download_reply(&reply, descriptor, &mut server_md5)?;
711            }
712            let start = usize::try_from(integer(&reply, "rangestart")?)?;
713            let end = usize::try_from(integer(&reply, "rangeend")?)?;
714            let size = usize::try_from(integer(&reply, "totalsize")?)?;
715            let data = reply
716                .get("filedata")
717                .ok_or_else(|| anyhow::anyhow!("Missing CDN payload"))?;
718            ensure!(
719                start == position
720                    && !data.is_empty()
721                    && size > 0
722                    && size <= ciphertext_limit
723                    && (server_selected
724                        || expected_ciphertext.is_none_or(|expected| expected == size))
725                    && size >= position
726                    && data.len() <= size - position
727                    && end <= requested_end
728                    && (!strict_transfer
729                        || server_selected
730                        || data.len() == (size - position).min(chunk_bytes))
731                    && (total == 0 || total == size)
732                    && end == start + data.len() - 1,
733                "Inconsistent CDN range response"
734            );
735            total = size;
736            if encrypted.is_empty() {
737                encrypted.try_reserve_exact(total)?;
738            }
739            position += data.len();
740            encrypted.extend(data);
741            if !large {
742                for (key, target) in [
743                    ("substituteftype", &mut substituted_filetype),
744                    ("rsppicformat", &mut picture_format),
745                ] {
746                    if reply.contains_key(key) {
747                        let value = u32::try_from(integer(&reply, key)?)?;
748                        ensure!(
749                            target.is_none_or(|previous| previous == value),
750                            "CDN representation changed during transfer"
751                        );
752                        *target = Some(value);
753                    }
754                }
755            }
756            if observe_video {
757                video_reply.observe(&reply);
758            }
759            progress(position as u64, Some(total as u64))?;
760            if position == total {
761                break;
762            }
763        }
764        ensure!(
765            total > 0 && position == total,
766            "CDN transfer did not finish within bound"
767        );
768        let bytes = decrypt_cdn(&encrypted, &descriptor.aes_key)?;
769        ensure!(bytes.len() <= plaintext_limit, "Decoded CDN body too large");
770        let size_matches = descriptor.expected_bytes.map(|n| n == bytes.len() as u64);
771        let size_rejected = video_selected_response != Some(true)
772            && (strict_transfer || descriptor.strict_size)
773            && size_matches == Some(false);
774        let digest = hex::encode(md5(&bytes));
775        let digest_matches = descriptor.expected_md5.as_ref().map(|expected| {
776            expected.len() == 32
777                && expected.bytes().all(|b| b.is_ascii_hexdigit())
778                && digest.eq_ignore_ascii_case(expected)
779        });
780        if observe_video
781            && video_selected_response != Some(true)
782            && (size_rejected || digest_matches == Some(false))
783        {
784            // Reject exactly as before, but keep comparison facts so a different
785            // server-selected representation can be told apart from corruption.
786            return Err(video_reply
787                .mismatch(
788                    descriptor,
789                    bytes.len(),
790                    &digest,
791                    digest_matches,
792                    substituted_filetype,
793                )
794                .into());
795        }
796        if video_selected_response == Some(true) {
797            ensure!(
798                video_response_bytes.is_none_or(|size| size == bytes.len() as u64),
799                "CDN video response size mismatch"
800            );
801            ensure!(
802                video_response_md5.as_deref() == Some(digest.as_str()),
803                "CDN video response checksum mismatch"
804            );
805        } else {
806            ensure!(!size_rejected, "Advertised media size mismatch");
807            ensure!(
808                digest_matches != Some(false),
809                "Advertised media checksum mismatch"
810            );
811        }
812        if let Some(expected) = server_md5 {
813            ensure!(
814                digest.eq_ignore_ascii_case(&expected),
815                "CDN source checksum mismatch"
816            );
817        }
818        let sha = hex::encode(sha256(&bytes));
819        Ok(DownloadedMedia {
820            bytes,
821            requested_quality: descriptor.quality,
822            substituted_filetype,
823            picture_format,
824            md5: digest,
825            sha256: sha,
826            advertised_size_matches: size_matches,
827        })
828    }
829    pub fn upload(&self, prepared: &PreparedUpload) -> Result<UploadedMedia> {
830        self.upload_with_progress(prepared, |_, _| Ok(()))
831    }
832    pub fn upload_with_progress(
833        &self,
834        prepared: &PreparedUpload,
835        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
836    ) -> Result<UploadedMedia> {
837        self.upload_with_transport(prepared, progress, |path, fields| self.post(path, fields))
838    }
839    fn upload_with_transport(
840        &self,
841        prepared: &PreparedUpload,
842        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
843        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
844    ) -> Result<UploadedMedia> {
845        prepared.validate()?;
846        let path = legacy_upload_path(
847            prepared.kind,
848            prepared.plaintext_bytes as usize,
849            prepared.encrypted.len(),
850        )?;
851        let mut file_id = None;
852        for (sequence, chunk) in prepared.encrypted.chunks(UPLOAD_CHUNK).enumerate() {
853            let start = sequence * UPLOAD_CHUNK;
854            progress(start as u64, Some(prepared.encrypted.len() as u64))?;
855            let end = start + chunk.len() - 1;
856            let mut fields =
857                upload_fields(&self.config, prepared, sequence as u32 + 1, start, end)?;
858            fields.insert("filedata".into(), chunk.to_vec());
859            let reply = post(path, &fields)?;
860            validate_upload_reply(&reply, sequence as u32 + 1, start, end, &mut file_id)?;
861            // The final correlated acknowledgement must reach the caller so it
862            // can save completion before applying cancellation/logout fences.
863            if end + 1 < prepared.encrypted.len() {
864                progress((end + 1) as u64, Some(prepared.encrypted.len() as u64))?;
865            }
866        }
867        let file_id =
868            file_id.ok_or_else(|| anyhow::anyhow!("CDN upload returned no file identifier"))?;
869        Ok(UploadedMedia {
870            descriptor: MediaDescriptor {
871                file_id,
872                aes_key: prepared.aes_key.clone(),
873                quality: prepared.kind,
874                expected_bytes: Some(prepared.plaintext_bytes as u64),
875                expected_md5: Some(prepared.md5.clone()),
876                strict_size: true,
877                expires_at: None,
878            },
879            plaintext_bytes: prepared.plaintext_bytes,
880            md5: prepared.md5.clone(),
881            sha256: prepared.sha256.clone(),
882            file_name: prepared.file_name.clone(),
883            width: None,
884            height: None,
885        })
886    }
887}
888/// Only these fixed, nonsecret numeric controls can enter a stored diagnostic.
889#[derive(Clone, Copy, Debug, PartialEq, Eq)]
890pub enum LargeFileDownloadControl {
891    Version,
892    VideoFormat,
893}
894impl LargeFileDownloadControl {
895    pub const fn field_name(self) -> &'static str {
896        match self {
897            Self::Version => "ver",
898            Self::VideoFormat => "videoformat",
899        }
900    }
901}
902/// Strict rejection details. Unknown keys and response bytes are never retained.
903#[derive(Clone, Copy, Debug, PartialEq, Eq)]
904pub enum LargeFileDownloadError {
905    UnknownField,
906    FixedControlMismatch {
907        control: LargeFileDownloadControl,
908        actual: i64,
909    },
910}
911impl fmt::Display for LargeFileDownloadError {
912    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
913        f.write_str(match self {
914            Self::UnknownField => "Unsupported large-file download response field",
915            Self::FixedControlMismatch { .. } => "Unsupported large-file download control",
916        })
917    }
918}
919impl std::error::Error for LargeFileDownloadError {}
920
921/// How an optional reply value relates to the selected message tuple and to
922/// the bytes actually delivered. Values are compared, never retained.
923#[derive(Clone, Copy, Debug, PartialEq, Eq)]
924pub struct SourceEvidence {
925    pub matches_advertised: bool,
926    pub matches_delivered: bool,
927}
928
929/// A complete video transfer at or below the ordinary transfer threshold
930/// decrypted successfully but did not match the selected message tuple. The
931/// download is still rejected. Only fixed numeric controls and comparison
932/// results are kept, so a caller can store a diagnostic without reply bytes or
933/// digests. Fields use wire key names; their meanings are not established.
934#[derive(Clone, Copy, Debug, PartialEq, Eq)]
935pub struct VideoRepresentationMismatch {
936    pub advertised_bytes: Option<u64>,
937    pub delivered_bytes: u64,
938    pub advertised_digest_matches: Option<bool>,
939    pub substituted_filetype: Option<u32>,
940    pub istrans: Option<i64>,
941    pub videoformat: Option<i64>,
942    pub rawfilesize: Option<SourceEvidence>,
943    pub rawfilemd5: Option<SourceEvidence>,
944}
945impl fmt::Display for VideoRepresentationMismatch {
946    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
947        f.write_str("Advertised video representation mismatch")
948    }
949}
950impl std::error::Error for VideoRepresentationMismatch {}
951
952/// First value of each optional video reply field on the ordinary path. This is
953/// diagnostic only: an absent or malformed value never rejects a reply.
954#[derive(Default)]
955struct VideoReplyMetadata {
956    istrans: Option<i64>,
957    videoformat: Option<i64>,
958    rawfilesize: Option<Option<u64>>,
959    rawfilemd5: Option<Option<String>>,
960}
961impl VideoReplyMetadata {
962    fn observe(&mut self, reply: &CdnFields) {
963        for (key, target) in [
964            ("istrans", &mut self.istrans),
965            ("videoformat", &mut self.videoformat),
966        ] {
967            if target.is_none() {
968                *target = integer(reply, key).ok();
969            }
970        }
971        if self.rawfilesize.is_none() && reply.contains_key("rawfilesize") {
972            self.rawfilesize = Some(
973                integer(reply, "rawfilesize")
974                    .ok()
975                    .and_then(|value| u64::try_from(value).ok()),
976            );
977        }
978        if self.rawfilemd5.is_none() {
979            if let Some(raw) = reply.get("rawfilemd5") {
980                self.rawfilemd5 = Some(
981                    (raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit))
982                        .then(|| String::from_utf8_lossy(raw).to_ascii_lowercase()),
983                );
984            }
985        }
986    }
987    fn mismatch(
988        &self,
989        descriptor: &MediaDescriptor,
990        delivered_bytes: usize,
991        delivered_md5: &str,
992        advertised_digest_matches: Option<bool>,
993        substituted_filetype: Option<u32>,
994    ) -> VideoRepresentationMismatch {
995        let delivered_bytes = delivered_bytes as u64;
996        VideoRepresentationMismatch {
997            advertised_bytes: descriptor.expected_bytes,
998            delivered_bytes,
999            advertised_digest_matches,
1000            substituted_filetype,
1001            istrans: self.istrans,
1002            videoformat: self.videoformat,
1003            rawfilesize: self.rawfilesize.map(|value| SourceEvidence {
1004                matches_advertised: value.is_some() && value == descriptor.expected_bytes,
1005                matches_delivered: value == Some(delivered_bytes),
1006            }),
1007            rawfilemd5: self.rawfilemd5.as_ref().map(|value| SourceEvidence {
1008                matches_advertised: value
1009                    .as_ref()
1010                    .zip(descriptor.expected_md5.as_ref())
1011                    .is_some_and(|(value, expected)| value.eq_ignore_ascii_case(expected)),
1012                matches_delivered: value.as_deref() == Some(delivered_md5),
1013            }),
1014        }
1015    }
1016}
1017
1018fn validate_large_video_reply_fields(reply: &CdnFields) -> Result<()> {
1019    const FIELDS: &[&str] = &[
1020        "ver",
1021        "seq",
1022        "retcode",
1023        "filedata",
1024        "rangestart",
1025        "rangeend",
1026        "totalsize",
1027        "retrysec",
1028        "isretry",
1029        "isoverload",
1030        "isgetcdn",
1031        "istrans",
1032        "backsrcnum",
1033        "rawfilesize",
1034        "rawfilemd5",
1035        "substituteftype",
1036        "rsppicformat",
1037        "videoformat",
1038        "x-ClientIp",
1039    ];
1040    ensure!(
1041        reply.keys().all(|key| FIELDS.contains(&key.as_str())),
1042        "Unsupported large-video download response field"
1043    );
1044    Ok(())
1045}
1046
1047// Type 4 retains the normal download endpoint. Unlike file7, representation
1048// and routing metadata cannot be ignored for a byte-exact original video.
1049fn validate_large_video_download_reply(
1050    reply: &CdnFields,
1051    descriptor: &MediaDescriptor,
1052    server_md5: &mut Option<String>,
1053) -> Result<()> {
1054    validate_large_video_reply_fields(reply)?;
1055    ensure!(
1056        integer(reply, "ver")? == 0,
1057        "Unsupported large-video download version"
1058    );
1059    for key in [
1060        "retrysec",
1061        "isretry",
1062        "isoverload",
1063        "isgetcdn",
1064        "backsrcnum",
1065        "rsppicformat",
1066    ] {
1067        if reply.contains_key(key) {
1068            ensure!(
1069                integer(reply, key)? == 0,
1070                "Unsupported large-video download control"
1071            );
1072        }
1073    }
1074    // Linux 4.1.13.26's normal pipeline response map discards `istrans`.
1075    // Its success handler's format lookup is literal `KEY_VIDEOFORMAT`
1076    // (CRC fb520bf9), not the retained wire key `videoformat` (dbe097e1).
1077    // Neither affects the selected ECB/range writer. Admit only the values
1078    // observed with byte-identical original-video ciphertext; retain the
1079    // independent representation, source size and digest checks below.
1080    for (key, observed) in [("istrans", 999), ("videoformat", 1)] {
1081        if reply.contains_key(key) {
1082            let value = integer(reply, key)?;
1083            ensure!(
1084                value == 0 || value == observed,
1085                "Unsupported large-video download control"
1086            );
1087        }
1088    }
1089    if reply.contains_key("substituteftype") {
1090        ensure!(
1091            matches!(integer(reply, "substituteftype")?, 0 | 4),
1092            "Unsupported large-video download representation"
1093        );
1094    }
1095    if reply.contains_key("rawfilesize") {
1096        ensure!(
1097            u64::try_from(integer(reply, "rawfilesize")?)?
1098                == descriptor.expected_bytes.unwrap_or(0),
1099            "Large-video download source size mismatch"
1100        );
1101    }
1102    if let Some(raw) = reply.get("rawfilemd5") {
1103        ensure!(
1104            raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1105            "Invalid CDN source checksum"
1106        );
1107        let value = std::str::from_utf8(raw)?;
1108        ensure!(
1109            server_md5
1110                .as_ref()
1111                .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1112                && descriptor
1113                    .expected_md5
1114                    .as_ref()
1115                    .is_some_and(|expected| expected.eq_ignore_ascii_case(value)),
1116            "CDN source checksum changed"
1117        );
1118        *server_md5 = Some(value.to_string());
1119    }
1120    Ok(())
1121}
1122
1123fn validate_large_download_reply(
1124    reply: &CdnFields,
1125    descriptor: &MediaDescriptor,
1126    server_md5: &mut Option<String>,
1127) -> Result<()> {
1128    const FIELDS: &[&str] = &[
1129        "ver",
1130        "seq",
1131        "retcode",
1132        "filedata",
1133        "rangestart",
1134        "rangeend",
1135        "totalsize",
1136        "retrysec",
1137        "isretry",
1138        "isoverload",
1139        "isgetcdn",
1140        "istrans",
1141        "backsrcnum",
1142        "rawfilesize",
1143        "rawfilemd5",
1144        "substituteftype",
1145        "rsppicformat",
1146        "videoformat",
1147        "x-ClientIp",
1148    ];
1149    if !reply.keys().all(|key| FIELDS.contains(&key.as_str())) {
1150        return Err(LargeFileDownloadError::UnknownField.into());
1151    }
1152    use LargeFileDownloadControl as Control;
1153    for (control, expected) in [(Control::Version, 0), (Control::VideoFormat, 0)] {
1154        let actual = integer(reply, control.field_name())?;
1155        if actual != expected {
1156            return Err(LargeFileDownloadError::FixedControlMismatch { control, actual }.into());
1157        }
1158    }
1159    // Linux 4.1.13.26's file7 pipeline (0x8d12d60 -> 0x8ce3000 ->
1160    // 0x8ca0ec0) does not consume these on a valid retcode-zero response.
1161    // The first four are optional template values; the remaining four never
1162    // enter its response template. They do not trigger retries or substitution.
1163    for key in [
1164        "retrysec",
1165        "isretry",
1166        "isoverload",
1167        "isgetcdn",
1168        "istrans",
1169        "backsrcnum",
1170        "substituteftype",
1171        "rsppicformat",
1172    ] {
1173        if let Some(raw) = reply.get(key) {
1174            // Keep the existing 16-byte integer parser bound, with unsigned
1175            // syntax. This is a native parsing policy, not a service limit.
1176            ensure!(
1177                raw.iter().all(u8::is_ascii_digit),
1178                "Invalid large-file metadata integer"
1179            );
1180            integer(reply, key)?;
1181        }
1182    }
1183    // File replies can omit this redundant source size. The transfer still
1184    // checks the encrypted length and the final decrypted length against the
1185    // message descriptor. Reject conflicting metadata when it is supplied.
1186    if reply.contains_key("rawfilesize") {
1187        ensure!(
1188            u64::try_from(integer(reply, "rawfilesize")?)?
1189                == descriptor.expected_bytes.unwrap_or(0),
1190            "Large-file download source size mismatch"
1191        );
1192    }
1193    let raw = reply
1194        .get("rawfilemd5")
1195        .ok_or_else(|| anyhow::anyhow!("Missing CDN source checksum"))?;
1196    ensure!(
1197        raw.len() == 32 && raw.iter().all(u8::is_ascii_hexdigit),
1198        "Invalid CDN source checksum"
1199    );
1200    let value = std::str::from_utf8(raw)?;
1201    ensure!(
1202        server_md5
1203            .as_ref()
1204            .is_none_or(|previous| previous.eq_ignore_ascii_case(value))
1205            && descriptor
1206                .expected_md5
1207                .as_ref()
1208                .is_none_or(|expected| expected.eq_ignore_ascii_case(value)),
1209        "CDN source checksum changed"
1210    );
1211    *server_md5 = Some(value.to_string());
1212    Ok(())
1213}
1214fn legacy_upload_path(
1215    kind: MediaQuality,
1216    plaintext: usize,
1217    ciphertext: usize,
1218) -> Result<&'static str> {
1219    ensure!(
1220        kind != MediaQuality::LargeFile
1221            && (kind != MediaQuality::File || plaintext <= MAX_NATIVE_UPLOAD_BYTES)
1222            && ciphertext < MAX_CDN_TRANSFER_BYTES,
1223        "File requires multiphase upload"
1224    );
1225    Ok("/uploadv3")
1226}
1227fn escaped_component(s: &str) -> String {
1228    s.bytes()
1229        .map(|b| {
1230            if b.is_ascii_alphanumeric() || b"-_.!~*'()".contains(&b) {
1231                (b as char).to_string()
1232            } else {
1233                format!("%{b:02X}")
1234            }
1235        })
1236        .collect()
1237}
1238fn upload_fields(
1239    conf: &CdnConfig,
1240    p: &PreparedUpload,
1241    sequence: u32,
1242    start: usize,
1243    end: usize,
1244) -> Result<CdnFields> {
1245    ensure!(
1246        sequence > 0 && start <= end && end < p.encrypted.len() && end - start < UPLOAD_CHUNK,
1247        "Invalid upload range"
1248    );
1249    let mut f = shared(conf, sequence, p.kind.filetype());
1250    for (k, v) in [
1251        ("hasthumb", 0),
1252        ("compresstype", 0),
1253        ("nocheckaeskey", 1),
1254        ("enablehit", 0),
1255        ("existancecheck", 0),
1256        ("apptype", 0),
1257        ("totalsize", p.encrypted.len() as u64),
1258        ("rawtotalsize", p.plaintext_bytes as u64),
1259        ("thumbtotalsize", 0),
1260        ("rawthumbsize", 0),
1261        ("encthumbcrc", 0),
1262        ("advideoflag", 0),
1263        ("rangestart", start as u64),
1264        ("rangeend", end as u64),
1265    ] {
1266        put(&mut f, k, v);
1267    }
1268    for (k, v) in [
1269        ("touser", encrypt_recipient(&p.destination)?),
1270        ("filekey", p.file_key.clone()),
1271        ("localname", escaped_component(&p.file_name)),
1272        ("filedatamd5", hex::encode(md5(&p.encrypted[start..=end]))),
1273        ("filemd5", p.md5.clone()),
1274        ("rawfilemd5", p.md5.clone()),
1275        ("blockmd5", hex::encode(md5(&p.encrypted[start..=end]))),
1276    ] {
1277        f.insert(k.into(), v.into_bytes());
1278    }
1279    f.insert("rawthumbmd5".into(), vec![]);
1280    Ok(f)
1281}
1282fn validate_upload_reply(
1283    reply: &CdnFields,
1284    sequence: u32,
1285    start: usize,
1286    end: usize,
1287    file_id: &mut Option<String>,
1288) -> Result<()> {
1289    let code = integer(reply, "retcode")?;
1290    ServiceRejected::check_wide(Service::CdnUpload, code)?;
1291    for (key, expected) in [
1292        ("seq", sequence as u64),
1293        ("rangestart", start as u64),
1294        ("rangeend", end as u64),
1295    ] {
1296        if reply.contains_key(key) {
1297            ensure!(
1298                u64::try_from(integer(reply, key)?)? == expected,
1299                "CDN upload correlation mismatch"
1300            );
1301        }
1302    }
1303    if let Some(raw) = reply.get("fileid") {
1304        let value = std::str::from_utf8(raw)?;
1305        ensure!(
1306            !value.is_empty()
1307                && value.len() <= 4096
1308                && value
1309                    .bytes()
1310                    .all(|b| b.is_ascii_alphanumeric() || b"-_@.+/=".contains(&b)),
1311            "Invalid uploaded file identifier"
1312        );
1313        ensure!(
1314            file_id.as_deref().is_none_or(|previous| previous == value),
1315            "CDN upload file identifier changed"
1316        );
1317        *file_id = Some(value.to_string());
1318    }
1319    Ok(())
1320}
1321
1322#[derive(Clone, Serialize, Deserialize)]
1323pub struct PreparedVariant {
1324    pub encrypted: Vec<u8>,
1325    pub plaintext_bytes: u32,
1326    pub width: u32,
1327    pub height: u32,
1328    pub md5: String,
1329    pub sha256: String,
1330    pub checksum: u32,
1331    pub encrypted_checksum: u32,
1332}
1333#[derive(Clone, Serialize, Deserialize)]
1334pub struct PreparedImageUpload {
1335    pub original: PreparedUpload,
1336    pub standard: PreparedVariant,
1337    pub thumbnail: PreparedVariant,
1338    pub width: u32,
1339    pub height: u32,
1340}
1341#[derive(Clone, Serialize, Deserialize)]
1342pub struct PreparedVideoUpload {
1343    pub video: PreparedUpload,
1344    pub thumbnail: PreparedVariant,
1345}
1346/// Uploaded copies offered by an image message, with an optional original.
1347#[derive(Clone, Serialize, Deserialize)]
1348pub struct UploadedImage {
1349    /// Original copy to offer alongside the previews; `None` offers only standard and thumbnail.
1350    #[serde(default)]
1351    pub original: Option<UploadedMedia>,
1352    /// Compressed main image copy.
1353    pub standard: UploadedMedia,
1354    /// Small preview image copy.
1355    pub thumbnail: UploadedMedia,
1356}
1357#[derive(Clone, Serialize, Deserialize)]
1358pub struct UploadedVideo {
1359    pub video: UploadedMedia,
1360    pub thumbnail: UploadedMedia,
1361    #[serde(default)]
1362    pub original: Option<UploadedMedia>,
1363}
1364/// Dimensions of an image original after decoding it once. An original is not
1365/// held to the 65,520-byte limit of the standard and thumbnail copies, and may
1366/// be an unchanged camera JPEG larger than their 4,096-pixel edge limit.
1367fn original_jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1368    ensure!(
1369        !bytes.is_empty() && bytes.len() <= MAX_NATIVE_UPLOAD_BYTES,
1370        "JPEG original outside upload limit"
1371    );
1372    let inspection = inspect_image(bytes)?;
1373    ensure!(
1374        inspection.content_type == "image/jpeg",
1375        "Expected JPEG original"
1376    );
1377    Ok((inspection.width, inspection.height))
1378}
1379/// Dimensions from the JPEG frame header, without decoding its pixels.
1380fn jpeg_header_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1381    Ok(
1382        image::ImageReader::with_format(std::io::Cursor::new(bytes), image::ImageFormat::Jpeg)
1383            .into_dimensions()?,
1384    )
1385}
1386fn jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32)> {
1387    use image::GenericImageView;
1388    ensure!(
1389        !bytes.is_empty() && bytes.len() <= 65520,
1390        "JPEG variant outside small-object profile"
1391    );
1392    ensure!(
1393        image::guess_format(bytes)? == image::ImageFormat::Jpeg,
1394        "Expected JPEG variant"
1395    );
1396    let mut reader =
1397        image::ImageReader::with_format(std::io::Cursor::new(bytes), image::ImageFormat::Jpeg);
1398    let mut limits = image::Limits::default();
1399    limits.max_image_width = Some(4096);
1400    limits.max_image_height = Some(4096);
1401    limits.max_alloc = Some(64 * 1024 * 1024);
1402    reader.limits(limits);
1403    let image = reader.decode()?;
1404    let (w, h) = image.dimensions();
1405    ensure!(
1406        w > 0 && h > 0 && w <= 4096 && h <= 4096,
1407        "JPEG dimensions outside limit"
1408    );
1409    Ok((w, h))
1410}
1411pub fn adler32_zero(bytes: &[u8]) -> u32 {
1412    let mut a = 0u32;
1413    let mut b = 0u32;
1414    for &v in bytes {
1415        a = (a + v as u32) % 65521;
1416        b = (b + a) % 65521;
1417    }
1418    (b << 16) | a
1419}
1420impl PreparedVariant {
1421    fn new(raw: &[u8], key: &[u8]) -> Result<Self> {
1422        let (width, height) = jpeg_dimensions(raw)?;
1423        let encrypted = ecb_encrypt(raw, key)?;
1424        Ok(Self {
1425            checksum: adler32_zero(raw),
1426            encrypted_checksum: adler32_zero(&encrypted),
1427            encrypted,
1428            plaintext_bytes: raw.len() as u32,
1429            width,
1430            height,
1431            md5: hex::encode(md5(raw)),
1432            sha256: hex::encode(sha256(raw)),
1433        })
1434    }
1435    fn validate(&self, key: &[u8]) -> Result<()> {
1436        let raw = decrypt_cdn(&self.encrypted, key)?;
1437        let (w, h) = jpeg_dimensions(&raw)?;
1438        ensure!(
1439            raw.len() == self.plaintext_bytes as usize
1440                && w == self.width
1441                && h == self.height
1442                && hex::encode(md5(&raw)) == self.md5
1443                && hex::encode(sha256(&raw)) == self.sha256
1444                && adler32_zero(&raw) == self.checksum
1445                && adler32_zero(&self.encrypted) == self.encrypted_checksum,
1446            "Prepared JPEG variant mismatch"
1447        );
1448        Ok(())
1449    }
1450}
1451impl PreparedImageUpload {
1452    pub fn with_material(
1453        original: &[u8],
1454        standard: &[u8],
1455        thumbnail: &[u8],
1456        file_name: &str,
1457        destination: &str,
1458        key: &[u8],
1459        file_key: &str,
1460    ) -> Result<Self> {
1461        let (width, height) = original_jpeg_dimensions(original)?;
1462        let original = PreparedUpload::with_material(
1463            original,
1464            MediaQuality::Original,
1465            file_name,
1466            destination,
1467            key,
1468            file_key,
1469        )?;
1470        let standard = PreparedVariant::new(standard, key)?;
1471        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1472        let p = Self {
1473            original,
1474            standard,
1475            thumbnail,
1476            width,
1477            height,
1478        };
1479        p.validate()?;
1480        Ok(p)
1481    }
1482    /// Supplied variants must be independent real JPEGs. This function decodes
1483    /// them fully but does not resize or invent an unsupplied representation.
1484    pub fn prepare(
1485        original: &[u8],
1486        standard: &[u8],
1487        thumbnail: &[u8],
1488        file_name: &str,
1489        destination: &str,
1490    ) -> Result<Self> {
1491        let (width, height) = original_jpeg_dimensions(original)?;
1492        let original =
1493            PreparedUpload::prepare(original, MediaQuality::Original, file_name, destination)?;
1494        let standard = PreparedVariant::new(standard, &original.aes_key)?;
1495        let thumbnail = PreparedVariant::new(thumbnail, &original.aes_key)?;
1496        let p = Self {
1497            original,
1498            standard,
1499            thumbnail,
1500            width,
1501            height,
1502        };
1503        p.validate()?;
1504        Ok(p)
1505    }
1506    fn validate(&self) -> Result<()> {
1507        self.original.validate()?;
1508        let raw = decrypt_cdn(&self.original.encrypted, &self.original.aes_key)?;
1509        ensure!(
1510            jpeg_header_dimensions(&raw)? == (self.width, self.height),
1511            "Prepared original dimensions differ"
1512        );
1513        self.standard.validate(&self.original.aes_key)?;
1514        self.thumbnail.validate(&self.original.aes_key)?;
1515        ensure!(
1516            self.original.plaintext_bytes <= MAX_NATIVE_UPLOAD_BYTES as u32
1517                // A preview of a rotated camera original is drawn upright, so its
1518                // edges may be the original's stored edges exchanged.
1519                && ((self.standard.width <= self.width && self.standard.height <= self.height)
1520                    || (self.standard.width <= self.height
1521                        && self.standard.height <= self.width))
1522                && self.thumbnail.width <= self.standard.width
1523                && self.thumbnail.height <= self.standard.height
1524                && self.thumbnail.width <= 320
1525                && self.thumbnail.height <= 320,
1526            "Invalid JPEG variant sizes"
1527        );
1528        Ok(())
1529    }
1530}
1531impl PreparedVideoUpload {
1532    pub fn with_material(
1533        video: &[u8],
1534        thumbnail: &[u8],
1535        file_name: &str,
1536        destination: &str,
1537        key: &[u8],
1538        file_key: &str,
1539    ) -> Result<Self> {
1540        ensure!(
1541            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1542            "Expected bounded MP4 container"
1543        );
1544        let video = PreparedUpload::with_material(
1545            video,
1546            MediaQuality::Video,
1547            file_name,
1548            destination,
1549            key,
1550            file_key,
1551        )?;
1552        let thumbnail = PreparedVariant::new(thumbnail, key)?;
1553        ensure!(
1554            thumbnail.width <= 320 && thumbnail.height <= 320,
1555            "Video thumbnail outside limit"
1556        );
1557        Ok(Self { video, thumbnail })
1558    }
1559    pub fn prepare(
1560        video: &[u8],
1561        thumbnail: &[u8],
1562        file_name: &str,
1563        destination: &str,
1564    ) -> Result<Self> {
1565        ensure!(
1566            video.len() >= 12 && video.len() <= MAX_NATIVE_UPLOAD_BYTES && video[4..8] == *b"ftyp",
1567            "Expected bounded MP4 container"
1568        );
1569        let video = PreparedUpload::prepare(video, MediaQuality::Video, file_name, destination)?;
1570        let thumbnail = PreparedVariant::new(thumbnail, &video.aes_key)?;
1571        ensure!(
1572            thumbnail.width <= 320 && thumbnail.height <= 320,
1573            "Video thumbnail outside limit"
1574        );
1575        Ok(Self { video, thumbnail })
1576    }
1577}
1578fn add_thumbnail(fields: &mut CdnFields, v: &PreparedVariant) {
1579    for (k, n) in [
1580        ("hasthumb", 1),
1581        ("thumbfiletype", 3),
1582        ("thumbtotalsize", v.encrypted.len() as u64),
1583        ("rawthumbsize", v.plaintext_bytes as u64),
1584        ("thumbcrc", v.checksum as u64),
1585        ("encthumbcrc", v.encrypted_checksum as u64),
1586    ] {
1587        put(fields, k, n);
1588    }
1589    fields.insert("rawthumbmd5".into(), v.md5.as_bytes().to_vec());
1590    fields.insert("thumbdata".into(), v.encrypted.clone());
1591}
1592fn uploaded_primary(
1593    prepared: &PreparedUpload,
1594    file_id: &str,
1595    width: Option<u32>,
1596    height: Option<u32>,
1597) -> UploadedMedia {
1598    UploadedMedia {
1599        descriptor: MediaDescriptor {
1600            file_id: file_id.to_string(),
1601            aes_key: prepared.aes_key.clone(),
1602            quality: prepared.kind,
1603            expected_bytes: Some(prepared.plaintext_bytes as u64),
1604            expected_md5: Some(prepared.md5.clone()),
1605            strict_size: true,
1606            expires_at: None,
1607        },
1608        plaintext_bytes: prepared.plaintext_bytes,
1609        md5: prepared.md5.clone(),
1610        sha256: prepared.sha256.clone(),
1611        file_name: prepared.file_name.clone(),
1612        width,
1613        height,
1614    }
1615}
1616fn uploaded_variant(
1617    primary: &PreparedUpload,
1618    v: &PreparedVariant,
1619    file_id: &str,
1620    quality: MediaQuality,
1621) -> UploadedMedia {
1622    UploadedMedia {
1623        descriptor: MediaDescriptor {
1624            file_id: file_id.to_string(),
1625            aes_key: primary.aes_key.clone(),
1626            quality,
1627            expected_bytes: Some(v.plaintext_bytes as u64),
1628            expected_md5: Some(v.md5.clone()),
1629            strict_size: true,
1630            expires_at: None,
1631        },
1632        plaintext_bytes: v.plaintext_bytes,
1633        md5: v.md5.clone(),
1634        sha256: v.sha256.clone(),
1635        file_name: primary.file_name.clone(),
1636        width: Some(v.width),
1637        height: Some(v.height),
1638    }
1639}
1640impl CdnClient {
1641    pub fn upload_image(&self, p: &PreparedImageUpload) -> Result<UploadedImage> {
1642        self.upload_image_with_progress(p, |_, _| Ok(()))
1643    }
1644    /// Upload an image's original, standard and thumbnail copies, reporting the encrypted bytes
1645    /// of the original sent so far and their total before each request.
1646    pub fn upload_image_with_progress(
1647        &self,
1648        p: &PreparedImageUpload,
1649        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1650    ) -> Result<UploadedImage> {
1651        self.upload_image_with_transport(p, progress, |path, fields| self.post(path, fields))
1652    }
1653    /// Upload the original in 64 KiB ranges like a video. The first request
1654    /// also carries the standard and thumbnail copies and the image metadata,
1655    /// so an original that fits one range is still a single request.
1656    fn upload_image_with_transport(
1657        &self,
1658        p: &PreparedImageUpload,
1659        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1660        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1661    ) -> Result<UploadedImage> {
1662        p.validate()?;
1663        let total = p.original.encrypted.len();
1664        let mut file_id = None;
1665        for (sequence, chunk) in p.original.encrypted.chunks(UPLOAD_CHUNK).enumerate() {
1666            let start = sequence * UPLOAD_CHUNK;
1667            let end = start + chunk.len() - 1;
1668            progress(start as u64, Some(total as u64))?;
1669            let mut f = upload_fields(&self.config, &p.original, sequence as u32 + 1, start, end)?;
1670            f.insert("filedata".into(), chunk.to_vec());
1671            if start == 0 {
1672                add_thumbnail(&mut f, &p.thumbnail);
1673                for (k, v) in [
1674                    ("imgheight", p.height as u64),
1675                    ("imgwidth", p.width as u64),
1676                    ("midimgrawsize", p.standard.plaintext_bytes as u64),
1677                    ("midimgtotalsize", p.standard.encrypted.len() as u64),
1678                    ("midimgchecksum", p.standard.encrypted_checksum as u64),
1679                    ("file_format", 0),
1680                    ("mid_format", 0),
1681                ] {
1682                    put(&mut f, k, v);
1683                }
1684                f.insert("midimgmd5".into(), p.standard.md5.as_bytes().to_vec());
1685                f.insert("midimgdata".into(), p.standard.encrypted.clone());
1686            }
1687            let reply = post("/uploadv3", &f)?;
1688            validate_upload_reply(&reply, sequence as u32 + 1, start, end, &mut file_id)?;
1689            if end + 1 < total {
1690                progress((end + 1) as u64, Some(total as u64))?;
1691            }
1692        }
1693        let id =
1694            file_id.ok_or_else(|| anyhow::anyhow!("Image upload returned no file identifier"))?;
1695        Ok(UploadedImage {
1696            original: Some(uploaded_primary(
1697                &p.original,
1698                &id,
1699                Some(p.width),
1700                Some(p.height),
1701            )),
1702            standard: uploaded_variant(&p.original, &p.standard, &id, MediaQuality::Standard),
1703            thumbnail: uploaded_variant(&p.original, &p.thumbnail, &id, MediaQuality::Thumbnail),
1704        })
1705    }
1706    pub fn upload_video(&self, p: &PreparedVideoUpload) -> Result<UploadedVideo> {
1707        self.upload_video_with_progress(p, |_, _| Ok(()))
1708    }
1709    pub fn upload_video_with_progress(
1710        &self,
1711        p: &PreparedVideoUpload,
1712        progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1713    ) -> Result<UploadedVideo> {
1714        self.upload_video_with_transport(p, progress, |path, fields| self.post(path, fields))
1715    }
1716    fn upload_video_with_transport(
1717        &self,
1718        p: &PreparedVideoUpload,
1719        mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
1720        mut post: impl FnMut(&str, &CdnFields) -> Result<CdnFields>,
1721    ) -> Result<UploadedVideo> {
1722        p.video.validate()?;
1723        ensure!(
1724            p.video.kind == MediaQuality::Video
1725                && p.video.plaintext_bytes as usize <= MAX_NATIVE_UPLOAD_BYTES,
1726            "Invalid normal video upload profile"
1727        );
1728        p.thumbnail.validate(&p.video.aes_key)?;
1729        ensure!(
1730            p.thumbnail.width <= 320 && p.thumbnail.height <= 320,
1731            "Video thumbnail outside limit"
1732        );
1733        let mut file_id = None;
1734        for (sequence, chunk) in p.video.encrypted.chunks(UPLOAD_CHUNK).enumerate() {
1735            let start = sequence * UPLOAD_CHUNK;
1736            let end = start + chunk.len() - 1;
1737            progress(start as u64, Some(p.video.encrypted.len() as u64))?;
1738            let mut fields =
1739                upload_fields(&self.config, &p.video, sequence as u32 + 1, start, end)?;
1740            fields.insert("filedata".into(), chunk.to_vec());
1741            if start == 0 {
1742                add_thumbnail(&mut fields, &p.thumbnail);
1743            }
1744            put(&mut fields, "smallvideoflag", 0);
1745            let reply = post("/uploadv3", &fields)?;
1746            validate_upload_reply(&reply, sequence as u32 + 1, start, end, &mut file_id)?;
1747            if end + 1 < p.video.encrypted.len() {
1748                progress((end + 1) as u64, Some(p.video.encrypted.len() as u64))?;
1749            }
1750        }
1751        let id =
1752            file_id.ok_or_else(|| anyhow::anyhow!("Video upload returned no file identifier"))?;
1753        Ok(UploadedVideo {
1754            video: uploaded_primary(&p.video, &id, None, None),
1755            thumbnail: uploaded_variant(&p.video, &p.thumbnail, &id, MediaQuality::Thumbnail),
1756            original: None,
1757        })
1758    }
1759}
1760
1761#[cfg(test)]
1762mod tests {
1763    use super::*;
1764    fn client() -> CdnClient {
1765        CdnClient::new(
1766            CdnConfig {
1767                version: 1,
1768                uin: 12345,
1769                client_version: 456,
1770                client_os_type: "synthetic-client".into(),
1771                auth_key: b"synthetic-auth".to_vec(),
1772                server_ips: vec![Ipv4Addr::new(1, 1, 1, 1)],
1773                front_id: 0,
1774                location: None,
1775            },
1776            Duration::from_secs(25),
1777        )
1778        .unwrap()
1779    }
1780    fn download_fixture(size: usize, quality: MediaQuality) -> (MediaDescriptor, Vec<u8>, Vec<u8>) {
1781        let raw = vec![0x5a; size];
1782        let key = vec![0x31; 16];
1783        let ciphertext = ecb_encrypt(&raw, &key).unwrap();
1784        let descriptor = MediaDescriptor {
1785            file_id: "synthetic-object".into(),
1786            aes_key: key,
1787            quality,
1788            expected_bytes: Some(size as u64),
1789            expected_md5: Some(hex::encode(md5(&raw))),
1790            strict_size: true,
1791            expires_at: None,
1792        };
1793        (descriptor, raw, ciphertext)
1794    }
1795    fn download_reply(
1796        fields: &CdnFields,
1797        descriptor: &MediaDescriptor,
1798        ciphertext: &[u8],
1799    ) -> CdnFields {
1800        let mut reply = CdnFields::new();
1801        let start = integer(fields, "rangestart").unwrap() as usize;
1802        let end = (integer(fields, "rangeend").unwrap() as usize).min(ciphertext.len() - 1);
1803        assert_eq!(fields["fileid"], descriptor.file_id.as_bytes());
1804        assert_eq!(integer(fields, "weixinnum").unwrap(), 12345);
1805        assert!(!fields.contains_key("aeskey"));
1806        for (key, value) in [
1807            ("ver", 0),
1808            ("seq", integer(fields, "seq").unwrap()),
1809            ("retcode", 0),
1810            ("rangestart", start as i64),
1811            ("rangeend", end as i64),
1812            ("totalsize", ciphertext.len() as i64),
1813        ] {
1814            put(&mut reply, key, value);
1815        }
1816        reply.insert("filedata".into(), ciphertext[start..=end].to_vec());
1817        if descriptor.quality == MediaQuality::LargeFile {
1818            for (key, value) in [
1819                ("retrysec", 0),
1820                ("isretry", 0),
1821                ("isoverload", 0),
1822                ("isgetcdn", 0),
1823                ("istrans", 999),
1824                ("backsrcnum", 0),
1825                ("substituteftype", 7),
1826                ("rsppicformat", 0),
1827                ("videoformat", 0),
1828                ("rawfilesize", descriptor.expected_bytes.unwrap() as i64),
1829            ] {
1830                put(&mut reply, key, value);
1831            }
1832            reply.insert(
1833                "rawfilemd5".into(),
1834                descriptor
1835                    .expected_md5
1836                    .as_ref()
1837                    .unwrap()
1838                    .as_bytes()
1839                    .to_vec(),
1840            );
1841        }
1842        reply
1843    }
1844    #[test]
1845    fn large_video_keeps_normal_endpoint_type_and_exact_source_bytes() {
1846        let (descriptor, raw, ciphertext) =
1847            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
1848        let mut requests = 0;
1849        let downloaded = client()
1850            .download_with_transport(
1851                &descriptor,
1852                |_, _| Ok(()),
1853                |path, fields| {
1854                    requests += 1;
1855                    assert_eq!(path, "/download");
1856                    assert_eq!(integer(fields, "filetype")?, 4);
1857                    assert_eq!(integer(fields, "seq")?, requests);
1858                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
1859                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
1860                    put(&mut reply, "substituteftype", 4);
1861                    put(&mut reply, "videoformat", 1);
1862                    put(&mut reply, "istrans", 999);
1863                    Ok(reply)
1864                },
1865            )
1866            .unwrap();
1867        assert_eq!(requests, 51);
1868        assert_eq!(downloaded.bytes, raw);
1869        assert_eq!(downloaded.md5, descriptor.expected_md5.as_deref().unwrap());
1870        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
1871        assert_eq!(downloaded.substituted_filetype, Some(4));
1872    }
1873
1874    #[test]
1875    fn large_video_rejects_unknown_controls_substitution_and_unbound_source() {
1876        let (mut descriptor, _, ciphertext) = download_fixture(16, MediaQuality::Video);
1877        descriptor.expected_bytes = Some(MAX_CDN_TRANSFER_BYTES as u64 + 1);
1878        let fields = BTreeMap::from([
1879            ("rangestart".into(), b"0".to_vec()),
1880            ("rangeend".into(), b"31".to_vec()),
1881            ("seq".into(), b"1".to_vec()),
1882            ("weixinnum".into(), b"12345".to_vec()),
1883            ("fileid".into(), descriptor.file_id.as_bytes().to_vec()),
1884        ]);
1885        let valid = download_reply(&fields, &descriptor, &ciphertext);
1886        for (key, value) in [
1887            ("unknown", "private"),
1888            ("ver", "1"),
1889            ("videoformat", "2"),
1890            ("istrans", "1"),
1891            ("KEY_VIDEOFORMAT", "1"),
1892            ("isretry", "1"),
1893            ("isgetcdn", "1"),
1894            ("rsppicformat", "3"),
1895            ("substituteftype", "7"),
1896            ("rawfilesize", "1"),
1897            ("rawfilemd5", "00000000000000000000000000000000"),
1898        ] {
1899            let mut reply = valid.clone();
1900            reply.insert(key.into(), value.as_bytes().to_vec());
1901            let error = validate_large_video_download_reply(&reply, &descriptor, &mut None)
1902                .unwrap_err()
1903                .to_string();
1904            assert!(!error.contains("private"));
1905        }
1906        for (size, checksum) in [
1907            (
1908                Some(MAX_VIDEO_DOWNLOAD_BYTES as u64 + 1),
1909                descriptor.expected_md5.clone(),
1910            ),
1911            (descriptor.expected_bytes, None),
1912        ] {
1913            descriptor.expected_bytes = size;
1914            descriptor.expected_md5 = checksum;
1915            let mut requests = 0;
1916            assert!(client()
1917                .download_with_transport(
1918                    &descriptor,
1919                    |_, _| Ok(()),
1920                    |_, _| {
1921                        requests += 1;
1922                        bail!("must not request")
1923                    }
1924                )
1925                .is_err());
1926            assert_eq!(requests, 0);
1927        }
1928    }
1929
1930    #[test]
1931    fn cold_large_descriptor_downloads_with_padding_and_hashes() {
1932        let (descriptor, raw, ciphertext) =
1933            download_fixture(32 * 1024 * 1024, MediaQuality::LargeFile);
1934        assert_eq!(ciphertext.len(), 32 * 1024 * 1024 + 16);
1935        let stored = serde_json::to_vec(&descriptor).unwrap();
1936        let cold: MediaDescriptor = serde_json::from_slice(&stored).unwrap();
1937        let events = std::cell::RefCell::new(Vec::new());
1938        let mut requests = 0;
1939        let mut previous = 0;
1940        let downloaded = client()
1941            .download_with_transport(
1942                &cold,
1943                |done, total| {
1944                    events.borrow_mut().push("progress");
1945                    assert!(done >= previous);
1946                    previous = done;
1947                    if let Some(total) = total {
1948                        assert_eq!(total, ciphertext.len() as u64);
1949                    }
1950                    Ok(())
1951                },
1952                |path, fields| {
1953                    events.borrow_mut().push("request");
1954                    requests += 1;
1955                    assert_eq!(path, "/downloadbig");
1956                    assert_eq!(integer(fields, "filetype")?, 7);
1957                    assert_eq!(integer(fields, "seq")?, requests);
1958                    assert_eq!(integer(fields, "rangestart")?, (requests - 1) * 512 * 1024);
1959                    Ok(download_reply(fields, &cold, &ciphertext))
1960                },
1961            )
1962            .unwrap();
1963        assert_eq!(requests, 65);
1964        assert_eq!(previous, ciphertext.len() as u64);
1965        for chunk in events.borrow().chunks(3) {
1966            assert_eq!(chunk, ["progress", "request", "progress"]);
1967        }
1968        assert_eq!(downloaded.bytes, raw);
1969        assert_eq!(
1970            downloaded.md5,
1971            descriptor.expected_md5.as_ref().unwrap().as_str()
1972        );
1973        assert_eq!(downloaded.sha256, hex::encode(sha256(&raw)));
1974        assert_eq!(downloaded.requested_quality, MediaQuality::LargeFile);
1975        assert_eq!(downloaded.substituted_filetype, None);
1976        assert_eq!(downloaded.picture_format, None);
1977        assert_eq!(downloaded.advertised_size_matches, Some(true));
1978    }
1979    #[test]
1980    fn large_download_accepts_missing_size_but_checks_supplied_size() {
1981        let (descriptor, raw, ciphertext) =
1982            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
1983        let downloaded = client()
1984            .download_with_transport(
1985                &descriptor,
1986                |_, _| Ok(()),
1987                |_, fields| {
1988                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
1989                    reply.remove("rawfilesize");
1990                    Ok(reply)
1991                },
1992            )
1993            .unwrap();
1994        assert_eq!(downloaded.bytes, raw);
1995        for value in ["", "invalid", "-1", "1"] {
1996            let result = client().download_with_transport(
1997                &descriptor,
1998                |_, _| Ok(()),
1999                |_, fields| {
2000                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2001                    reply.insert("rawfilesize".into(), value.as_bytes().to_vec());
2002                    Ok(reply)
2003                },
2004            );
2005            assert!(result.is_err(), "Accepted invalid source size {value}");
2006        }
2007    }
2008    #[test]
2009    fn large_download_rejections_expose_only_typed_static_control_details() {
2010        use LargeFileDownloadControl as Control;
2011        let (descriptor, _, ciphertext) =
2012            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2013        for (control, actual) in [(Control::Version, 1), (Control::VideoFormat, 1)] {
2014            let mut calls = 0;
2015            let error = client()
2016                .download_with_transport(
2017                    &descriptor,
2018                    |_, _| Ok(()),
2019                    |_, fields| {
2020                        calls += 1;
2021                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2022                        put(&mut reply, control.field_name(), actual);
2023                        Ok(reply)
2024                    },
2025                )
2026                .err()
2027                .unwrap();
2028            assert_eq!(calls, 1);
2029            assert_eq!(
2030                error.downcast_ref::<LargeFileDownloadError>(),
2031                Some(&LargeFileDownloadError::FixedControlMismatch { control, actual })
2032            );
2033            assert_eq!(error.to_string(), "Unsupported large-file download control");
2034        }
2035        let secret = "private-response-token-and-file-id";
2036        let error = client()
2037            .download_with_transport(
2038                &descriptor,
2039                |_, _| Ok(()),
2040                |_, fields| {
2041                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2042                    reply.insert(secret.into(), secret.as_bytes().to_vec());
2043                    Ok(reply)
2044                },
2045            )
2046            .err()
2047            .unwrap();
2048        assert_eq!(
2049            error.downcast_ref::<LargeFileDownloadError>(),
2050            Some(&LargeFileDownloadError::UnknownField)
2051        );
2052        assert_eq!(
2053            error.to_string(),
2054            "Unsupported large-file download response field"
2055        );
2056        assert!(!format!("{error:?}").contains(secret));
2057    }
2058    #[test]
2059    fn large_download_rejects_correlation_format_unknown_fields_and_ranges() {
2060        let (descriptor, _, ciphertext) =
2061            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2062        for (key, value) in [
2063            ("seq", 2),
2064            ("ver", 1),
2065            ("retcode", -1),
2066            ("videoformat", 1),
2067            ("rangestart", 1),
2068            ("rangeend", 512 * 1024),
2069            ("totalsize", ciphertext.len() as i64 + 16),
2070            ("rawfilesize", 1),
2071            ("futureauth", 1),
2072        ] {
2073            let mut requests = 0;
2074            let outcome = client().download_with_transport(
2075                &descriptor,
2076                |_, _| Ok(()),
2077                |_, fields| {
2078                    requests += 1;
2079                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2080                    put(&mut reply, key, value);
2081                    Ok(reply)
2082                },
2083            );
2084            assert!(outcome.is_err(), "Accepted invalid {key}");
2085            assert_eq!(requests, 1);
2086        }
2087        for missing in ["seq", "ver", "videoformat", "rawfilemd5", "filedata"] {
2088            assert!(
2089                client()
2090                    .download_with_transport(
2091                        &descriptor,
2092                        |_, _| Ok(()),
2093                        |_, fields| {
2094                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
2095                            reply.remove(missing);
2096                            Ok(reply)
2097                        }
2098                    )
2099                    .is_err(),
2100                "Accepted missing {missing}"
2101            );
2102        }
2103    }
2104    #[test]
2105    fn file7_success_metadata_is_optional_and_does_not_change_verified_bytes() {
2106        // These eight names come from the reference template/consumer join,
2107        // independently of the native validator's allowlist.
2108        let metadata = [
2109            "isretry",
2110            "retrysec",
2111            "isoverload",
2112            "isgetcdn",
2113            "istrans",
2114            "backsrcnum",
2115            "substituteftype",
2116            "rsppicformat",
2117        ];
2118        let (descriptor, raw, ciphertext) = download_fixture(1024 * 1024, MediaQuality::LargeFile);
2119        assert_eq!(ciphertext.len(), 1024 * 1024 + 16);
2120        // Each field may be absent or vary independently on the first, middle,
2121        // and final padding-only ranges, without creating another request.
2122        for selected in metadata.into_iter().map(Some).chain([None]) {
2123            for omit in [false, true] {
2124                let mut requests = 0;
2125                let result = client()
2126                    .download_with_transport(
2127                        &descriptor,
2128                        |_, _| Ok(()),
2129                        |path, fields| {
2130                            requests += 1;
2131                            assert_eq!(path, "/downloadbig");
2132                            assert_eq!(integer(fields, "seq")?, requests);
2133                            let mut reply = download_reply(fields, &descriptor, &ciphertext);
2134                            for key in metadata {
2135                                if selected.is_none_or(|selected| selected == key) {
2136                                    if omit {
2137                                        reply.remove(key);
2138                                    } else {
2139                                        put(
2140                                            &mut reply,
2141                                            key,
2142                                            match requests {
2143                                                1 => 1,
2144                                                2 => 999,
2145                                                3 => 9_999_999_999_999_999i64,
2146                                                _ => panic!("Unexpected retry or extra range"),
2147                                            },
2148                                        );
2149                                    }
2150                                }
2151                            }
2152                            Ok(reply)
2153                        },
2154                    )
2155                    .unwrap();
2156                assert_eq!(requests, 3);
2157                assert_eq!(result.bytes, raw);
2158                assert_eq!(result.md5, descriptor.expected_md5.as_deref().unwrap());
2159                assert_eq!(result.sha256, hex::encode(sha256(&raw)));
2160                assert_eq!(result.requested_quality, MediaQuality::LargeFile);
2161                assert_eq!(result.substituted_filetype, None);
2162                assert_eq!(result.picture_format, None);
2163            }
2164        }
2165    }
2166    #[test]
2167    fn file7_metadata_parser_bounds_and_errors_remain_strict() {
2168        let (descriptor, _, ciphertext) = download_fixture(512 * 1024, MediaQuality::LargeFile);
2169        for key in [
2170            "isretry",
2171            "retrysec",
2172            "isoverload",
2173            "isgetcdn",
2174            "istrans",
2175            "backsrcnum",
2176            "substituteftype",
2177            "rsppicformat",
2178        ] {
2179            for malformed in [
2180                b"".as_slice(),
2181                b"-1",
2182                b"-0",
2183                b"+1",
2184                b" 1",
2185                b"1x",
2186                b"99999999999999999",
2187                b"\xff",
2188            ] {
2189                let mut requests = 0;
2190                assert!(
2191                    client()
2192                        .download_with_transport(
2193                            &descriptor,
2194                            |_, _| Ok(()),
2195                            |_, fields| {
2196                                requests += 1;
2197                                let mut reply = download_reply(fields, &descriptor, &ciphertext);
2198                                reply.insert(key.into(), malformed.to_vec());
2199                                Ok(reply)
2200                            },
2201                        )
2202                        .is_err(),
2203                    "Accepted malformed {key}"
2204                );
2205                assert_eq!(requests, 1);
2206            }
2207        }
2208        let mut requests = 0;
2209        let error = client()
2210            .download_with_transport(
2211                &descriptor,
2212                |_, _| Ok(()),
2213                |_, fields| {
2214                    requests += 1;
2215                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2216                    for key in ["isretry", "retrysec", "isoverload", "isgetcdn"] {
2217                        put(&mut reply, key, 1);
2218                    }
2219                    put(&mut reply, "retcode", -1);
2220                    Ok(reply)
2221                },
2222            )
2223            .err()
2224            .unwrap();
2225        assert_eq!(error.to_string(), "CDN download returned -1");
2226        assert_eq!(requests, 1);
2227    }
2228    #[test]
2229    fn normal_download_keeps_representation_metadata_and_consistency_checks() {
2230        let (descriptor, raw, ciphertext) = download_fixture(65536, MediaQuality::File);
2231        let result = client()
2232            .download_with_transport(
2233                &descriptor,
2234                |_, _| Ok(()),
2235                |path, fields| {
2236                    assert_eq!(path, "/download");
2237                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2238                    put(&mut reply, "substituteftype", 5);
2239                    put(&mut reply, "rsppicformat", 3);
2240                    Ok(reply)
2241                },
2242            )
2243            .unwrap();
2244        assert_eq!(result.bytes, raw);
2245        assert_eq!(result.substituted_filetype, Some(5));
2246        assert_eq!(result.picture_format, Some(3));
2247        let mut requests = 0;
2248        assert!(client()
2249            .download_with_transport(
2250                &descriptor,
2251                |_, _| Ok(()),
2252                |_, fields| {
2253                    requests += 1;
2254                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2255                    put(&mut reply, "rsppicformat", requests);
2256                    Ok(reply)
2257                },
2258            )
2259            .is_err());
2260        assert_eq!(requests, 2);
2261    }
2262    #[test]
2263    fn download_refuses_truncated_overlong_and_changing_source_bytes() {
2264        let (descriptor, _, ciphertext) =
2265            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2266        for failure in [
2267            "empty",
2268            "overlong",
2269            "source-checksum",
2270            "changed-checksum",
2271            "changed-total",
2272            "short-nonfinal",
2273        ] {
2274            let mut requests = 0;
2275            let outcome = client().download_with_transport(
2276                &descriptor,
2277                |_, _| Ok(()),
2278                |_, fields| {
2279                    requests += 1;
2280                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2281                    match failure {
2282                        "empty" => {
2283                            reply.insert("filedata".into(), vec![]);
2284                        }
2285                        "overlong" => {
2286                            reply.get_mut("filedata").unwrap().push(1);
2287                            put(&mut reply, "rangeend", 512 * 1024);
2288                        }
2289                        "source-checksum" => {
2290                            reply.insert(
2291                                "rawfilemd5".into(),
2292                                b"0123456789abcdef0123456789abcdef".to_vec(),
2293                            );
2294                        }
2295                        "changed-checksum" if requests == 2 => {
2296                            reply.insert(
2297                                "rawfilemd5".into(),
2298                                b"0123456789abcdef0123456789abcdef".to_vec(),
2299                            );
2300                        }
2301                        "changed-total" if requests == 2 => {
2302                            put(&mut reply, "totalsize", ciphertext.len() + 16)
2303                        }
2304                        "short-nonfinal" => {
2305                            reply.insert("filedata".into(), vec![1]);
2306                            put(&mut reply, "rangeend", integer(fields, "rangestart")?);
2307                        }
2308                        _ => {}
2309                    }
2310                    Ok(reply)
2311                },
2312            );
2313            assert!(outcome.is_err(), "Accepted {failure}");
2314            assert!(requests <= 2);
2315        }
2316    }
2317    #[test]
2318    fn large_download_checks_padding_actual_size_and_digest_independently() {
2319        for omit_source_size in [false, true] {
2320            let reply_for_test =
2321                |fields: &CdnFields, descriptor: &MediaDescriptor, bytes: &[u8]| {
2322                    let mut reply = download_reply(fields, descriptor, bytes);
2323                    if omit_source_size {
2324                        reply.remove("rawfilesize");
2325                    }
2326                    reply
2327                };
2328            let (descriptor, _, ciphertext) =
2329                download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2330            let mut invalid_padding = ciphertext.clone();
2331            let mut encoder = openssl::symm::Crypter::new(
2332                Cipher::aes_128_ecb(),
2333                openssl::symm::Mode::Encrypt,
2334                &descriptor.aes_key,
2335                None,
2336            )
2337            .unwrap();
2338            encoder.pad(false);
2339            let mut block = [0u8; 32];
2340            let written = encoder.update(&[0u8; 16], &mut block).unwrap();
2341            assert_eq!(
2342                written + encoder.finalize(&mut block[written..]).unwrap(),
2343                16
2344            );
2345            let last = invalid_padding.len() - 16;
2346            invalid_padding[last..].copy_from_slice(&block[..16]);
2347            assert!(client()
2348                .download_with_transport(
2349                    &descriptor,
2350                    |_, _| Ok(()),
2351                    |_, fields| Ok(reply_for_test(fields, &descriptor, &invalid_padding))
2352                )
2353                .is_err());
2354            let mut without_advertised_md5 = descriptor.clone();
2355            without_advertised_md5.expected_md5 = None;
2356            for change_later in [false, true] {
2357                let mut requests = 0;
2358                assert!(client()
2359                    .download_with_transport(
2360                        &without_advertised_md5,
2361                        |_, _| Ok(()),
2362                        |_, fields| {
2363                            requests += 1;
2364                            let mut reply = reply_for_test(fields, &descriptor, &ciphertext);
2365                            if !change_later || requests == 2 {
2366                                reply.insert(
2367                                    "rawfilemd5".into(),
2368                                    b"0123456789abcdef0123456789abcdef".to_vec(),
2369                                );
2370                            }
2371                            Ok(reply)
2372                        }
2373                    )
2374                    .is_err());
2375                assert_eq!(requests, 2);
2376            }
2377            let mut wrong_source = ciphertext.clone();
2378            wrong_source[0] ^= 1;
2379            assert!(client()
2380                .download_with_transport(
2381                    &descriptor,
2382                    |_, _| Ok(()),
2383                    |_, fields| Ok(reply_for_test(fields, &descriptor, &wrong_source))
2384                )
2385                .is_err());
2386            let mut wrong_size = descriptor.clone();
2387            wrong_size.expected_bytes = Some(descriptor.expected_bytes.unwrap() + 1);
2388            wrong_size.strict_size = false;
2389            assert!(client()
2390                .download_with_transport(
2391                    &wrong_size,
2392                    |_, _| Ok(()),
2393                    |_, fields| Ok(reply_for_test(fields, &wrong_size, &ciphertext))
2394                )
2395                .is_err());
2396        }
2397    }
2398    #[test]
2399    fn cancellation_and_transport_failure_never_start_another_download_request() {
2400        let (descriptor, _, ciphertext) =
2401            download_fixture(512 * 1024 + 17, MediaQuality::LargeFile);
2402        for stop in [1, 2, 3, 4] {
2403            let mut callbacks = 0;
2404            let mut requests = 0;
2405            let outcome = client().download_with_transport(
2406                &descriptor,
2407                |_, _| {
2408                    callbacks += 1;
2409                    ensure!(callbacks != stop, "cancelled");
2410                    Ok(())
2411                },
2412                |_, fields| {
2413                    requests += 1;
2414                    Ok(download_reply(fields, &descriptor, &ciphertext))
2415                },
2416            );
2417            assert!(outcome.is_err());
2418            assert_eq!(callbacks, stop);
2419            assert_eq!(requests, stop / 2);
2420        }
2421        let mut requests = 0;
2422        assert!(client()
2423            .download_with_transport(
2424                &descriptor,
2425                |_, _| Ok(()),
2426                |_, _| {
2427                    requests += 1;
2428                    bail!("transport failure")
2429                }
2430            )
2431            .is_err());
2432        assert_eq!(requests, 1);
2433    }
2434    #[test]
2435    fn normal_download_keeps_optional_sequence_and_original_limits() {
2436        for quality in [
2437            MediaQuality::Original,
2438            MediaQuality::Standard,
2439            MediaQuality::Thumbnail,
2440            MediaQuality::Video,
2441            MediaQuality::File,
2442        ] {
2443            let (descriptor, raw, ciphertext) = download_fixture(65536 + 17, quality);
2444            let mut requests = 0;
2445            let downloaded = client()
2446                .download_with_transport(
2447                    &descriptor,
2448                    |_, _| Ok(()),
2449                    |path, fields| {
2450                        requests += 1;
2451                        assert_eq!(path, "/download");
2452                        assert_eq!(
2453                            integer(fields, "rangeend")? - integer(fields, "rangestart")? + 1,
2454                            if requests == 1 { 65536 } else { 32 }
2455                        );
2456                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2457                        reply.remove("seq");
2458                        Ok(reply)
2459                    },
2460                )
2461                .unwrap();
2462            assert_eq!(downloaded.bytes, raw);
2463            assert_eq!(requests, 2);
2464            assert!(client()
2465                .download_with_transport(
2466                    &descriptor,
2467                    |_, _| Ok(()),
2468                    |_, fields| {
2469                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2470                        put(&mut reply, "seq", 99);
2471                        Ok(reply)
2472                    }
2473                )
2474                .is_err());
2475            assert!(client()
2476                .download_with_transport(
2477                    &descriptor,
2478                    |_, _| Ok(()),
2479                    |_, fields| {
2480                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2481                        put(&mut reply, "totalsize", MAX_CDN_TRANSFER_BYTES + 32);
2482                        Ok(reply)
2483                    }
2484                )
2485                .is_err());
2486        }
2487        assert!(unpack_cdn(&vec![0; MAX_CDN_BODY + 1]).is_err());
2488    }
2489    #[test]
2490    fn large_descriptor_limits_fail_before_transport() {
2491        let (descriptor, _, _) = download_fixture(16, MediaQuality::LargeFile);
2492        for expected in [
2493            None,
2494            Some(0),
2495            Some(MAX_LARGE_FILE_BYTES as u64 + 1),
2496            Some(u64::MAX),
2497        ] {
2498            let mut descriptor = descriptor.clone();
2499            descriptor.expected_bytes = expected;
2500            let mut requests = 0;
2501            assert!(client()
2502                .download_with_transport(
2503                    &descriptor,
2504                    |_, _| Ok(()),
2505                    |_, _| {
2506                        requests += 1;
2507                        bail!("must not request")
2508                    }
2509                )
2510                .is_err());
2511            assert_eq!(requests, 0);
2512        }
2513    }
2514
2515    #[test]
2516    fn legacy_completion_receipt_survives_cancellation_after_final_ack() {
2517        let prepared = PreparedUpload::prepare(
2518            b"synthetic source",
2519            MediaQuality::File,
2520            "source.bin",
2521            "filehelper",
2522        )
2523        .unwrap();
2524        let cancelled = std::cell::Cell::new(false);
2525        let calls = std::cell::Cell::new(0);
2526        let upload = client()
2527            .upload_with_transport(
2528                &prepared,
2529                |_, _| {
2530                    ensure!(!cancelled.get(), "cancelled");
2531                    Ok(())
2532                },
2533                |path, fields| {
2534                    assert_eq!(path, "/uploadv3");
2535                    calls.set(calls.get() + 1);
2536                    let mut reply = CdnFields::new();
2537                    put(&mut reply, "retcode", 0);
2538                    for key in ["seq", "rangestart", "rangeend"] {
2539                        reply.insert(key.into(), fields[key].clone());
2540                    }
2541                    reply.insert("fileid".into(), b"synthetic-completed-object".to_vec());
2542                    cancelled.set(true);
2543                    Ok(reply)
2544                },
2545            )
2546            .unwrap();
2547        assert!(cancelled.get());
2548        assert_eq!(calls.get(), 1);
2549        assert_eq!(upload.descriptor.file_id, "synthetic-completed-object");
2550    }
2551
2552    #[test]
2553    fn paired_video_preserves_final_receipt_and_does_not_admit_large_normal() {
2554        let mut thumbnail = Vec::new();
2555        image::codecs::jpeg::JpegEncoder::new(&mut thumbnail)
2556            .encode_image(&image::DynamicImage::new_rgb8(16, 12))
2557            .unwrap();
2558        let p = PreparedVideoUpload::with_material(
2559            b"\0\0\0\x0cftypisom",
2560            &thumbnail,
2561            "normal.mp4",
2562            "filehelper",
2563            &[0x31; 16],
2564            "00112233445566778899aabbccddeeff",
2565        )
2566        .unwrap();
2567        let cancelled = std::cell::Cell::new(false);
2568        let uploaded = client()
2569            .upload_video_with_transport(
2570                &p,
2571                |_, _| {
2572                    ensure!(!cancelled.get(), "cancelled");
2573                    Ok(())
2574                },
2575                |path, fields| {
2576                    assert_eq!(path, "/uploadv3");
2577                    assert!(fields.contains_key("thumbdata"));
2578                    assert_eq!(integer(fields, "filetype")?, 4);
2579                    let mut reply = CdnFields::new();
2580                    put(&mut reply, "retcode", 0);
2581                    for key in ["seq", "rangestart", "rangeend"] {
2582                        reply.insert(key.into(), fields[key].clone());
2583                    }
2584                    reply.insert("fileid".into(), b"paired-object".to_vec());
2585                    cancelled.set(true);
2586                    Ok(reply)
2587                },
2588            )
2589            .unwrap();
2590        assert!(uploaded.original.is_none());
2591        assert_eq!(uploaded.video.descriptor.file_id, "paired-object");
2592        assert_eq!(uploaded.thumbnail.descriptor.file_id, "paired-object");
2593        let mut altered = p;
2594        altered.video.plaintext_bytes = MAX_NATIVE_UPLOAD_BYTES as u32 + 1;
2595        assert!(client()
2596            .upload_video_with_transport(
2597                &altered,
2598                |_, _| Ok(()),
2599                |_, _| { panic!("Large normal video reached legacy transport") }
2600            )
2601            .is_err());
2602    }
2603    #[test]
2604    fn boundary_matrix_keeps_ciphertext_and_media_kind_limits_separate() {
2605        let boundary = MAX_CDN_TRANSFER_BYTES;
2606        for (size, padded, legacy) in [
2607            (boundary - 17, boundary - 16, Some("/uploadv3")),
2608            (boundary - 16, boundary, None),
2609            (boundary - 1, boundary, None),
2610            (boundary, boundary + 16, None),
2611            (boundary + 1, boundary + 16, None),
2612            (MAX_LARGE_FILE_BYTES, MAX_LARGE_FILE_BYTES + 16, None),
2613            (MAX_LARGE_FILE_BYTES + 1, MAX_LARGE_FILE_BYTES + 16, None),
2614        ] {
2615            assert_eq!((size / 16 + 1) * 16, padded);
2616            let kind = if size > boundary {
2617                MediaQuality::LargeFile
2618            } else {
2619                MediaQuality::File
2620            };
2621            assert_eq!(legacy_upload_path(kind, size, padded).ok(), legacy);
2622            assert_eq!(
2623                size <= upload_plaintext_limit(kind),
2624                size <= MAX_LARGE_FILE_BYTES
2625            );
2626            assert_eq!(
2627                size <= upload_plaintext_limit(MediaQuality::Original),
2628                size <= MAX_NATIVE_UPLOAD_BYTES
2629            );
2630            assert_eq!(
2631                size <= upload_plaintext_limit(MediaQuality::Video),
2632                size <= wechat_media::MAX_VIDEO_SOURCE_BYTES
2633            );
2634        }
2635        assert_eq!(MAX_LARGE_FILE_BYTES, 536_870_912);
2636        assert_eq!(MAX_NATIVE_UPLOAD_BYTES, 26_214_383);
2637        assert_eq!(MAX_CDN_TRANSFER_BYTES, 26_214_400);
2638    }
2639    #[test]
2640    fn multiphase_paths_bind_account_sequence_type_and_phase() {
2641        for filetype in [4, 5, 7] {
2642            for phase in ["apply", "uploadblock", "fin", "complete"] {
2643                assert!(large_file_path(
2644                    &format!("/cdn/12345/3/{filetype}/{phase}"),
2645                    12345,
2646                    3
2647                ));
2648            }
2649        }
2650        for path in [
2651            "/cdn/999/3/7/apply",
2652            "/cdn/12345/2/7/apply",
2653            "/cdn/12345/03/7/apply",
2654            "/cdn/12345/0/7/apply",
2655            "/cdn/12345/3/3/apply",
2656            "/cdn/12345/3/7/unknown",
2657            "/cdn/12345/3/7/apply?x=1",
2658            "/cdn/12345/3/7/apply/extra",
2659            "//cdn/12345/3/7/apply",
2660            "/cdn/12345/3/7/../apply",
2661        ] {
2662            assert!(!large_file_path(path, 12345, 3), "Accepted {path}");
2663        }
2664        assert!(!large_file_path("/cdn/12345/0/7/apply", 12345, 0));
2665    }
2666    #[test]
2667    fn cdn_codec_rejects_duplicate_and_truncated() {
2668        let fields = BTreeMap::from([
2669            ("retcode".to_string(), b"0".to_vec()),
2670            ("filedata".into(), vec![0, 1, 2, 255]),
2671        ]);
2672        let packed = pack_cdn(&fields).unwrap();
2673        assert_eq!(unpack_cdn(&packed).unwrap(), fields);
2674        let one = pack_cdn(&BTreeMap::from([("x".into(), vec![1])])).unwrap();
2675        for n in 1..one.len() {
2676            assert!(unpack_cdn(&one[..n]).is_err());
2677        }
2678        let duplicate = [one.clone(), one].concat();
2679        assert!(unpack_cdn(&duplicate).is_err());
2680    }
2681    #[test]
2682    fn prepared_upload_roundtrip() {
2683        let p = PreparedUpload::prepare(
2684            b"synthetic attachment",
2685            MediaQuality::File,
2686            "test.txt",
2687            "filehelper",
2688        )
2689        .unwrap();
2690        p.validate().unwrap();
2691        assert_eq!(
2692            decrypt_cdn(&p.encrypted, &p.aes_key).unwrap(),
2693            b"synthetic attachment"
2694        );
2695        let mut corrupt = p.clone();
2696        corrupt.encrypted[0] ^= 1;
2697        assert!(corrupt.validate().is_err());
2698    }
2699    #[test]
2700    fn nonpublic_addresses_rejected() {
2701        for ip in [
2702            "0.1.2.3",
2703            "10.0.0.1",
2704            "100.64.0.1",
2705            "127.0.0.1",
2706            "169.254.1.1",
2707            "172.16.0.1",
2708            "192.168.0.1",
2709            "198.18.0.1",
2710            "192.0.2.1",
2711            "198.51.100.1",
2712            "203.0.113.1",
2713            "224.1.2.3",
2714        ] {
2715            assert!(!public_ipv4(ip.parse().unwrap()));
2716        }
2717        assert!(public_ipv4("8.8.8.8".parse().unwrap()));
2718    }
2719    #[test]
2720    fn upload_reply_correlates_ranges() {
2721        let mut f = CdnFields::from([
2722            ("retcode".into(), b"0".to_vec()),
2723            ("seq".into(), b"2".to_vec()),
2724        ]);
2725        assert!(validate_upload_reply(&f, 1, 0, 15, &mut None).is_err());
2726        put(&mut f, "seq", 1);
2727        put(&mut f, "rangestart", 0);
2728        put(&mut f, "rangeend", 15);
2729        f.insert("fileid".into(), b"synthetic-file-id".to_vec());
2730        let mut id = None;
2731        validate_upload_reply(&f, 1, 0, 15, &mut id).unwrap();
2732        assert_eq!(id, Some("synthetic-file-id".into()));
2733    }
2734    #[test]
2735    fn prepared_jpeg_variants_are_real_and_independent() {
2736        fn jpeg(w: u32, h: u32) -> Vec<u8> {
2737            let rgb = image::RgbImage::from_pixel(w, h, image::Rgb([12, 40, 80]));
2738            let mut out = vec![];
2739            image::codecs::jpeg::JpegEncoder::new(&mut out)
2740                .encode_image(&rgb)
2741                .unwrap();
2742            out
2743        }
2744        let p = PreparedImageUpload::prepare(
2745            &jpeg(64, 48),
2746            &jpeg(32, 24),
2747            &jpeg(16, 12),
2748            "image.jpg",
2749            "filehelper",
2750        )
2751        .unwrap();
2752        p.validate().unwrap();
2753        assert_eq!((p.width, p.height), (64, 48));
2754        assert_eq!((p.thumbnail.width, p.thumbnail.height), (16, 12));
2755        assert!(PreparedImageUpload::prepare(
2756            &jpeg(8, 8),
2757            &jpeg(16, 16),
2758            &jpeg(4, 4),
2759            "image.jpg",
2760            "filehelper"
2761        )
2762        .is_err());
2763        assert!(PreparedImageUpload::prepare(
2764            b"not image",
2765            &jpeg(4, 4),
2766            &jpeg(2, 2),
2767            "image.jpg",
2768            "filehelper"
2769        )
2770        .is_err());
2771    }
2772    #[test]
2773    fn image_original_uploads_in_ranges_with_previews_in_the_first_request() {
2774        fn jpeg(w: u32, h: u32, noisy: bool) -> Vec<u8> {
2775            let mut seed = 7u32;
2776            let rgb = image::RgbImage::from_fn(w, h, |_, _| {
2777                seed ^= seed << 13;
2778                seed ^= seed >> 17;
2779                seed ^= seed << 5;
2780                let [r, g, b, _] = if noisy { seed } else { 0x0050_280c }.to_le_bytes();
2781                image::Rgb([r, g, b])
2782            });
2783            let mut out = vec![];
2784            image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95)
2785                .encode_image(&rgb)
2786                .unwrap();
2787            out
2788        }
2789        // Wider than the 4,096-pixel preview limit and several ranges long.
2790        let original = jpeg(4200, 64, true);
2791        assert!(original.len() > 3 * UPLOAD_CHUNK);
2792        let p = PreparedImageUpload::prepare(
2793            &original,
2794            &jpeg(640, 10, false),
2795            &jpeg(160, 3, false),
2796            "wide.jpg",
2797            "filehelper",
2798        )
2799        .unwrap();
2800        assert_eq!((p.width, p.height), (4200, 64));
2801        let total = p.original.encrypted.len();
2802        let requests = std::cell::RefCell::new(vec![]);
2803        let reported = std::cell::RefCell::new(vec![]);
2804        let uploaded = client()
2805            .upload_image_with_transport(
2806                &p,
2807                |sent, all| {
2808                    assert_eq!(all, Some(total as u64));
2809                    reported.borrow_mut().push(sent);
2810                    Ok(())
2811                },
2812                |path, fields| {
2813                    assert_eq!(path, "/uploadv3");
2814                    assert_eq!(integer(fields, "filetype")?, 1);
2815                    assert_eq!(integer(fields, "totalsize")?, total as i64);
2816                    // Every request must fit the bounded CDN envelope.
2817                    pack_cdn(fields)?;
2818                    let start = integer(fields, "rangestart")? as usize;
2819                    let end = integer(fields, "rangeend")? as usize;
2820                    assert_eq!(fields["filedata"], p.original.encrypted[start..=end]);
2821                    let first = start == 0;
2822                    for key in ["thumbdata", "midimgdata", "midimgmd5", "imgwidth"] {
2823                        assert_eq!(fields.contains_key(key), first, "{key}");
2824                    }
2825                    assert_eq!(integer(fields, "hasthumb")?, i64::from(first));
2826                    requests.borrow_mut().push((start, end));
2827                    let mut reply = CdnFields::new();
2828                    put(&mut reply, "retcode", 0);
2829                    for key in ["seq", "rangestart", "rangeend"] {
2830                        reply.insert(key.into(), fields[key].clone());
2831                    }
2832                    reply.insert("fileid".into(), b"image-object".to_vec());
2833                    Ok(reply)
2834                },
2835            )
2836            .unwrap();
2837        let requests = requests.into_inner();
2838        assert_eq!(requests.len(), total.div_ceil(UPLOAD_CHUNK));
2839        assert_eq!(requests[0], (0, UPLOAD_CHUNK - 1));
2840        assert!(requests.windows(2).all(|pair| pair[0].1 + 1 == pair[1].0));
2841        assert_eq!(requests.last().unwrap().1, total - 1);
2842        assert_eq!(reported.borrow()[0], 0);
2843        assert!(reported.borrow().iter().all(|sent| *sent < total as u64));
2844        let uploaded_original = uploaded.original.as_ref().unwrap();
2845        assert_eq!(uploaded_original.plaintext_bytes as usize, original.len());
2846        assert_eq!(uploaded_original.width, Some(4200));
2847        for copy in [uploaded_original, &uploaded.standard, &uploaded.thumbnail] {
2848            assert_eq!(copy.descriptor.file_id, "image-object");
2849        }
2850        // A standard copy keeps its own limits.
2851        assert!(PreparedImageUpload::prepare(
2852            &original,
2853            &jpeg(4200, 64, false),
2854            &jpeg(160, 3, false),
2855            "wide.jpg",
2856            "filehelper"
2857        )
2858        .is_err());
2859    }
2860    fn video_mismatch(
2861        descriptor: &MediaDescriptor,
2862        ciphertext: &[u8],
2863        extra: &[(&str, &[u8])],
2864    ) -> VideoRepresentationMismatch {
2865        let error = client()
2866            .download_with_transport(
2867                descriptor,
2868                |_, _| Ok(()),
2869                |_, fields| {
2870                    let mut reply = download_reply(fields, descriptor, ciphertext);
2871                    for (key, value) in extra {
2872                        reply.insert((*key).into(), value.to_vec());
2873                    }
2874                    Ok(reply)
2875                },
2876            )
2877            .err()
2878            .expect("mismatched video must be rejected");
2879        let text = format!("{error:#} {error:?}");
2880        assert!(!text.contains(descriptor.expected_md5.as_deref().unwrap()));
2881        *error
2882            .downcast_ref::<VideoRepresentationMismatch>()
2883            .expect("typed video mismatch")
2884    }
2885    #[test]
2886    fn normal_video_of_another_length_is_rejected_with_comparison_facts_only() {
2887        // Synthetic values: the advertised tuple describes one object while the
2888        // CDN returns a complete, correctly encrypted object of another length.
2889        let (descriptor, _, _) = download_fixture(9_390, MediaQuality::Video);
2890        let delivered = vec![0x6b; 15_272];
2891        let delivered_md5 = hex::encode(md5(&delivered));
2892        let ciphertext = ecb_encrypt(&delivered, &descriptor.aes_key).unwrap();
2893        let bare = video_mismatch(&descriptor, &ciphertext, &[]);
2894        assert_eq!(
2895            bare,
2896            VideoRepresentationMismatch {
2897                advertised_bytes: Some(9_390),
2898                delivered_bytes: 15_272,
2899                advertised_digest_matches: Some(false),
2900                substituted_filetype: None,
2901                istrans: None,
2902                videoformat: None,
2903                rawfilesize: None,
2904                rawfilemd5: None,
2905            }
2906        );
2907        let delivered_size = delivered.len().to_string();
2908        let described = video_mismatch(
2909            &descriptor,
2910            &ciphertext,
2911            &[
2912                ("substituteftype", b"4"),
2913                ("istrans", b"7"),
2914                ("videoformat", b"3"),
2915                ("rawfilesize", delivered_size.as_bytes()),
2916                ("rawfilemd5", delivered_md5.to_uppercase().as_bytes()),
2917            ],
2918        );
2919        assert_eq!(described.substituted_filetype, Some(4));
2920        assert_eq!(
2921            (described.istrans, described.videoformat),
2922            (Some(7), Some(3))
2923        );
2924        let delivered_only = Some(SourceEvidence {
2925            matches_advertised: false,
2926            matches_delivered: true,
2927        });
2928        assert_eq!(described.rawfilesize, delivered_only);
2929        assert_eq!(described.rawfilemd5, delivered_only);
2930        let advertised_size = descriptor.expected_bytes.unwrap().to_string();
2931        let advertised_md5 = descriptor.expected_md5.clone().unwrap();
2932        let source = video_mismatch(
2933            &descriptor,
2934            &ciphertext,
2935            &[
2936                ("rawfilesize", advertised_size.as_bytes()),
2937                ("rawfilemd5", advertised_md5.as_bytes()),
2938            ],
2939        );
2940        let advertised_only = Some(SourceEvidence {
2941            matches_advertised: true,
2942            matches_delivered: false,
2943        });
2944        assert_eq!(source.rawfilesize, advertised_only);
2945        assert_eq!(source.rawfilemd5, advertised_only);
2946        let malformed = video_mismatch(
2947            &descriptor,
2948            &ciphertext,
2949            &[("rawfilesize", b"-1"), ("rawfilemd5", b"not-a-digest")],
2950        );
2951        let neither = Some(SourceEvidence {
2952            matches_advertised: false,
2953            matches_delivered: false,
2954        });
2955        assert_eq!(
2956            (malformed.rawfilesize, malformed.rawfilemd5),
2957            (neither, neither)
2958        );
2959        // A representation that only some chunks label is still refused.
2960        assert!(client()
2961            .download_with_transport(
2962                &descriptor,
2963                |_, _| Ok(()),
2964                |_, fields| {
2965                    let mut reply = download_reply(fields, &descriptor, &ciphertext);
2966                    put(&mut reply, "substituteftype", integer(fields, "seq")?);
2967                    Ok(reply)
2968                },
2969            )
2970            .is_err());
2971    }
2972    #[test]
2973    fn normal_video_digest_mismatch_and_valid_metadata_keep_existing_outcomes() {
2974        let (mut descriptor, raw, ciphertext) = download_fixture(9_390, MediaQuality::Video);
2975        // Optional metadata is diagnostic only; malformed values never reject
2976        // a transfer that matches its advertised tuple.
2977        for (key, value) in [
2978            ("rawfilesize", "-1"),
2979            ("rawfilemd5", "not-a-digest"),
2980            ("istrans", "x"),
2981            ("videoformat", "12345678901234567"),
2982        ] {
2983            let downloaded = client()
2984                .download_with_transport(
2985                    &descriptor,
2986                    |_, _| Ok(()),
2987                    |_, fields| {
2988                        let mut reply = download_reply(fields, &descriptor, &ciphertext);
2989                        reply.insert(key.into(), value.as_bytes().to_vec());
2990                        Ok(reply)
2991                    },
2992                )
2993                .unwrap();
2994            assert_eq!(downloaded.bytes, raw);
2995            assert_eq!(downloaded.advertised_size_matches, Some(true));
2996        }
2997        descriptor.expected_md5 = Some("0".repeat(32));
2998        let same_size = video_mismatch(&descriptor, &ciphertext, &[]);
2999        assert_eq!(same_size.delivered_bytes, 9_390);
3000        assert_eq!(same_size.advertised_digest_matches, Some(false));
3001        // Other kinds keep their plain messages.
3002        let (mut image, _, image_ciphertext) = download_fixture(9_390, MediaQuality::Original);
3003        image.expected_bytes = Some(9_391);
3004        let error = client()
3005            .download_with_transport(
3006                &image,
3007                |_, _| Ok(()),
3008                |_, fields| Ok(download_reply(fields, &image, &image_ciphertext)),
3009            )
3010            .err()
3011            .expect("download must fail");
3012        assert!(error
3013            .downcast_ref::<VideoRepresentationMismatch>()
3014            .is_none());
3015        assert_eq!(error.to_string(), "Advertised media size mismatch");
3016    }
3017    #[test]
3018    fn video_limits_admit_sizes_above_the_former_50_mib_bound() {
3019        assert_eq!(MAX_VIDEO_DOWNLOAD_BYTES, 536_870_912);
3020        assert_eq!(wechat_media::MAX_VIDEO_SOURCE_BYTES, 536_870_912);
3021        assert_eq!(
3022            upload_plaintext_limit(MediaQuality::Video),
3023            wechat_media::MAX_VIDEO_SOURCE_BYTES
3024        );
3025        // A small advertised video keeps its own bound on an unchecked reply.
3026        assert_eq!(MAX_SMALL_VIDEO_REPLY_BYTES, 52_428_800);
3027        let (mut descriptor, _, _) = download_fixture(16, MediaQuality::Video);
3028        for size in [MAX_SMALL_VIDEO_REPLY_BYTES + 1, MAX_VIDEO_DOWNLOAD_BYTES] {
3029            descriptor.expected_bytes = Some(size as u64);
3030            let mut requests = 0;
3031            let error = client()
3032                .download_with_transport(
3033                    &descriptor,
3034                    |_, _| Ok(()),
3035                    |path, fields| {
3036                        requests += 1;
3037                        assert_eq!(path, "/download");
3038                        assert_eq!(integer(fields, "filetype")?, 4);
3039                        assert_eq!(integer(fields, "rangeend")?, 512 * 1024 - 1);
3040                        bail!("synthetic transport stop")
3041                    },
3042                )
3043                .err()
3044                .expect("download must stop");
3045            assert_eq!(error.to_string(), "synthetic transport stop");
3046            assert_eq!(requests, 1);
3047        }
3048    }
3049
3050    #[test]
3051    fn observed_video_controls_do_not_bypass_large_response_validation() {
3052        let (mut descriptor, _, encrypted) =
3053            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3054        descriptor.strict_size = false;
3055        let mut admitted = Vec::new();
3056        for (key, value) in [("unknown", "1"), ("rawfilesize", "1")] {
3057            let result = client().download_with_transport(
3058                &descriptor,
3059                |_, _| Ok(()),
3060                |_, fields| {
3061                    let mut reply = download_reply(fields, &descriptor, &encrypted);
3062                    put(&mut reply, "istrans", 999);
3063                    put(&mut reply, "videoformat", 1);
3064                    put(&mut reply, "substituteftype", 4);
3065                    reply.insert(
3066                        "rawfilemd5".into(),
3067                        descriptor
3068                            .expected_md5
3069                            .as_ref()
3070                            .unwrap()
3071                            .as_bytes()
3072                            .to_vec(),
3073                    );
3074                    reply.insert(key.into(), value.as_bytes().to_vec());
3075                    Ok(reply)
3076                },
3077            );
3078            if result.is_ok() {
3079                admitted.push(key);
3080            }
3081        }
3082        assert!(
3083            admitted.is_empty(),
3084            "Accepted invalid large-video fields: {admitted:?}"
3085        );
3086    }
3087
3088    fn selected_video_reply(
3089        fields: &CdnFields,
3090        descriptor: &MediaDescriptor,
3091        encrypted: &[u8],
3092        raw: &[u8],
3093        control: i64,
3094    ) -> CdnFields {
3095        let mut reply = download_reply(fields, descriptor, encrypted);
3096        put(&mut reply, "istrans", control);
3097        put(&mut reply, "videoformat", 1);
3098        put(
3099            &mut reply,
3100            "backsrcnum",
3101            integer(fields, "seq").unwrap() % 3,
3102        );
3103        put(&mut reply, "substituteftype", 9);
3104        put(&mut reply, "rawfilesize", raw.len() as i64);
3105        reply.insert("rawfilemd5".into(), hex::encode(md5(raw)).into_bytes());
3106        reply
3107    }
3108
3109    #[test]
3110    fn selected_large_normal_video_checks_every_range_and_returned_identity() {
3111        let (mut descriptor, raw, encrypted) =
3112            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3113        descriptor.expected_bytes = Some(raw.len() as u64 + 32);
3114        descriptor.expected_md5 = Some("0".repeat(32));
3115        descriptor.strict_size = false;
3116        for control in [13, 999] {
3117            let mut calls = 0;
3118            let received = client()
3119                .download_with_transport(
3120                    &descriptor,
3121                    |_, _| Ok(()),
3122                    |path, fields| {
3123                        calls += 1;
3124                        assert_eq!(path, "/download");
3125                        assert_eq!(integer(fields, "filetype")?, 4);
3126                        assert_eq!(integer(fields, "seq")?, calls);
3127                        assert_eq!(integer(fields, "rangestart")?, (calls - 1) * 512 * 1024);
3128                        Ok(selected_video_reply(
3129                            fields,
3130                            &descriptor,
3131                            &encrypted,
3132                            &raw,
3133                            control,
3134                        ))
3135                    },
3136                )
3137                .unwrap();
3138            assert_eq!(calls, 51);
3139            assert_eq!(received.bytes, raw);
3140            assert_eq!(received.md5, hex::encode(md5(&raw)));
3141            assert_eq!(received.sha256, hex::encode(sha256(&raw)));
3142            assert_eq!(received.advertised_size_matches, Some(false));
3143            assert_eq!(received.substituted_filetype, Some(9));
3144        }
3145        descriptor.strict_size = true;
3146        for control in [13, 999] {
3147            assert!(
3148                client()
3149                    .download_with_transport(
3150                        &descriptor,
3151                        |_, _| Ok(()),
3152                        |_, fields| Ok(selected_video_reply(
3153                            fields,
3154                            &descriptor,
3155                            &encrypted,
3156                            &raw,
3157                            control
3158                        )),
3159                    )
3160                    .is_err(),
3161                "strict source accepted response-selected bytes"
3162            );
3163        }
3164    }
3165
3166    #[test]
3167    fn selected_video_rejects_mid_transfer_metadata_or_range_changes_without_retry() {
3168        let (mut descriptor, raw, encrypted) =
3169            download_fixture(MAX_CDN_TRANSFER_BYTES + 17, MediaQuality::Video);
3170        descriptor.strict_size = false;
3171        for fault in [
3172            "digest",
3173            "size",
3174            "range",
3175            "control",
3176            "missing_digest",
3177            "selection",
3178            "service",
3179        ] {
3180            let mut calls = 0;
3181            let result = client().download_with_transport(
3182                &descriptor,
3183                |_, _| Ok(()),
3184                |_, fields| {
3185                    calls += 1;
3186                    let mut reply =
3187                        selected_video_reply(fields, &descriptor, &encrypted, &raw, 999);
3188                    if calls == 2 {
3189                        match fault {
3190                            "digest" => {
3191                                reply.insert("rawfilemd5".into(), "0".repeat(32).into_bytes());
3192                            }
3193                            "size" => put(&mut reply, "rawfilesize", raw.len() as i64 + 1),
3194                            "range" => put(&mut reply, "rangestart", 0),
3195                            "control" => put(&mut reply, "backsrcnum", 3),
3196                            "missing_digest" => {
3197                                reply.remove("rawfilemd5");
3198                            }
3199                            "selection" => put(&mut reply, "istrans", 0),
3200                            "service" => {
3201                                reply.clear();
3202                                put(&mut reply, "retcode", -1);
3203                            }
3204                            _ => unreachable!(),
3205                        }
3206                    }
3207                    Ok(reply)
3208                },
3209            );
3210            assert!(result.is_err(), "accepted changed {fault}");
3211            if fault == "service" {
3212                assert!(result
3213                    .err()
3214                    .unwrap()
3215                    .downcast_ref::<ServiceRejected>()
3216                    .is_some());
3217            }
3218            assert_eq!(calls, 2, "retried changed {fault}");
3219        }
3220    }
3221
3222    #[test]
3223    fn server_selected_video_representation_is_accepted_with_validated_response_digest() {
3224        for istrans in [13, 999] {
3225            let (mut d, raw, encrypted) = download_fixture(103, MediaQuality::Video);
3226            // The advertised representation differs from the delivered bytes.
3227            d.expected_bytes = Some(90);
3228            d.expected_md5 = Some("0".repeat(32));
3229            d.strict_size = false;
3230            let received = client()
3231                .download_with_transport(
3232                    &d,
3233                    |_, _| Ok(()),
3234                    |_, fields| {
3235                        let mut r = download_reply(fields, &d, &encrypted);
3236                        put(&mut r, "istrans", istrans);
3237                        put(&mut r, "videoformat", 1);
3238                        put(&mut r, "ver", 0);
3239                        put(&mut r, "backsrcnum", 2);
3240                        put(&mut r, "substituteftype", 4);
3241                        r.insert("rawfilemd5".into(), hex::encode(md5(&raw)).into_bytes());
3242                        Ok(r)
3243                    },
3244                )
3245                .unwrap();
3246            assert_eq!(received.bytes, raw);
3247            assert_eq!(received.md5, hex::encode(md5(&raw)));
3248            assert_eq!(received.advertised_size_matches, Some(false));
3249        }
3250    }
3251
3252    #[test]
3253    fn server_selected_video_without_matching_response_digest_is_rejected() {
3254        let (mut d, _raw, encrypted) = download_fixture(103, MediaQuality::Video);
3255        d.expected_bytes = Some(90);
3256        d.strict_size = false;
3257        d.expected_md5 = Some("0".repeat(32));
3258        assert!(client()
3259            .download_with_transport(
3260                &d,
3261                |_, _| Ok(()),
3262                |_, fields| {
3263                    let mut r = download_reply(fields, &d, &encrypted);
3264                    put(&mut r, "istrans", 999);
3265                    put(&mut r, "videoformat", 1);
3266                    put(&mut r, "ver", 0);
3267                    r.insert("rawfilemd5".into(), "0".repeat(32).into_bytes());
3268                    Ok(r)
3269                },
3270            )
3271            .is_err());
3272    }
3273}