use std::fmt;
use serde::{Deserialize, Serialize};
pub const MAX_LOG_BYTES: usize = 960 * 1024;
pub const MAX_NOTE_BYTES: usize = 8 * 1024;
pub const MAX_FIELD_BYTES: usize = 256;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct DiagnosticsReport {
pub device_model: String,
pub os_name: String,
pub os_version: String,
pub app_name: String,
pub app_version: String,
pub log: String,
pub note: Option<String>,
}
struct Bytes(usize);
impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{} bytes>", self.0)
}
}
impl fmt::Debug for DiagnosticsReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DiagnosticsReport")
.field("device_model", &self.device_model)
.field("os_name", &self.os_name)
.field("os_version", &self.os_version)
.field("app_name", &self.app_name)
.field("app_version", &self.app_version)
.field("log", &Bytes(self.log.len()))
.field("note", &self.note.as_ref().map(|n| Bytes(n.len())))
.finish()
}
}
impl DiagnosticsReport {
pub fn refusal(&self) -> Option<String> {
let parts = [
("device_model", self.device_model.len(), MAX_FIELD_BYTES),
("os_name", self.os_name.len(), MAX_FIELD_BYTES),
("os_version", self.os_version.len(), MAX_FIELD_BYTES),
("app_name", self.app_name.len(), MAX_FIELD_BYTES),
("app_version", self.app_version.len(), MAX_FIELD_BYTES),
("log", self.log.len(), MAX_LOG_BYTES),
(
"note",
self.note.as_ref().map_or(0, String::len),
MAX_NOTE_BYTES,
),
];
parts.into_iter().find(|&(_, len, max)| len > max).map(|(name, len, max)| {
format!(
"the diagnostics' {name} is {len} bytes; at most {max} are sent: keep the newest"
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::{MAX_FRAME, encode};
use crate::rpc::{Operation, RpcRequest};
use crate::{Request, RequestEnvelope, SessionId};
fn report(log: String) -> DiagnosticsReport {
DiagnosticsReport {
device_model: "iPhone16,2".into(),
os_name: "iOS".into(),
os_version: "26.1".into(),
app_name: "Spoolframe".into(),
app_version: "1.4.0 (212)".into(),
log,
note: Some("It froze at the credits".into()),
}
}
#[test]
fn debug_gives_the_log_s_length_not_the_log() {
let text = format!("{:?}", report("secret line\n".repeat(3)));
assert!(!text.contains("secret line"), "{text}");
assert!(text.contains("<36 bytes>"), "{text}");
assert!(text.contains("iPhone16,2"), "{text}");
}
#[test]
fn a_log_at_the_cap_is_sent() {
assert_eq!(report("a".repeat(MAX_LOG_BYTES)).refusal(), None);
}
#[test]
fn a_log_over_the_cap_is_refused_naming_the_log() {
let refusal = report("a".repeat(MAX_LOG_BYTES + 1)).refusal().unwrap();
assert!(refusal.contains("log is 983041 bytes"), "{refusal}");
}
#[test]
fn a_long_note_or_name_is_refused() {
let mut long_note = report(String::new());
long_note.note = Some("n".repeat(MAX_NOTE_BYTES + 1));
assert!(long_note.refusal().unwrap().contains("note"));
let mut long_model = report(String::new());
long_model.device_model = "m".repeat(MAX_FIELD_BYTES + 1);
assert!(long_model.refusal().unwrap().contains("device_model"));
}
#[test]
fn a_report_at_every_limit_fits_one_frame() {
let field = "f".repeat(MAX_FIELD_BYTES);
let full = DiagnosticsReport {
device_model: field.clone(),
os_name: field.clone(),
os_version: field.clone(),
app_name: field.clone(),
app_version: field,
log: "l".repeat(MAX_LOG_BYTES),
note: Some("n".repeat(MAX_NOTE_BYTES)),
};
assert_eq!(full.refusal(), None);
let wire = RpcRequest {
envelope: RequestEnvelope {
session: Some(SessionId("0".repeat(64))),
request: Request::SendDiagnostics { report: full },
},
operation: Some(Operation {
id: "0".repeat(64),
instance: "0".repeat(64),
expires_at_ms: u64::MAX,
}),
expected_revision: Some(u64::MAX),
};
let len = encode(&wire).unwrap().len();
assert!(len <= MAX_FRAME as usize, "{len} bytes");
}
}