Skip to content

fix: Don't panic if the server returns 0 as the quota limit #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,78 @@ mod tests {
}
}

#[cfg_attr(feature = "runtime-tokio", tokio::test)]
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
async fn test_get_quota_root() {
{
let response = b"* QUOTAROOT Sent Userquota\r\n\
* QUOTA Userquota (STORAGE 4855 48576)\r\n\
A0001 OK Getquotaroot completed (0.004 + 0.000 + 0.004 secs).\r\n"
.to_vec();

let mock_stream = MockStream::new(response);
let mut session = mock_session!(mock_stream);
let (quotaroots, quota) = dbg!(session.get_quota_root("Sent").await.unwrap());
assert_eq!(
str::from_utf8(&session.stream.inner.written_buf).unwrap(),
"A0001 GETQUOTAROOT \"Sent\"\r\n"
);
assert_eq!(
quotaroots,
vec![QuotaRoot {
mailbox_name: "Sent".to_string(),
quota_root_names: vec!["Userquota".to_string(),],
},],
);
assert_eq!(
quota,
vec![Quota {
root_name: "Userquota".to_string(),
resources: vec![QuotaResource {
name: QuotaResourceName::Storage,
usage: 4855,
limit: 48576,
}],
}]
);
assert_eq!(quota[0].resources[0].get_usage_percentage(), 9);
}

{
let response = b"* QUOTAROOT \"INBOX\" \"#19\"\r\n\
* QUOTA \"#19\" (STORAGE 0 0)\r\n\
A0001 OK GETQUOTAROOT successful.\r\n"
.to_vec();

let mock_stream = MockStream::new(response);
let mut session = mock_session!(mock_stream);
let (quotaroots, quota) = session.get_quota_root("INBOX").await.unwrap();
assert_eq!(
str::from_utf8(&session.stream.inner.written_buf).unwrap(),
"A0001 GETQUOTAROOT \"INBOX\"\r\n"
);
assert_eq!(
quotaroots,
vec![QuotaRoot {
mailbox_name: "INBOX".to_string(),
quota_root_names: vec!["#19".to_string(),],
},],
);
assert_eq!(
quota,
vec![Quota {
root_name: "#19".to_string(),
resources: vec![QuotaResource {
name: QuotaResourceName::Storage,
usage: 0,
limit: 0,
}],
}]
);
assert_eq!(quota[0].resources[0].get_usage_percentage(), 0);
}
}

#[cfg_attr(feature = "runtime-tokio", tokio::test)]
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
async fn test_parsing_error() {
Expand Down
6 changes: 5 additions & 1 deletion src/types/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ impl From<QuotaResourceRef<'_>> for QuotaResource {
impl QuotaResource {
/// Returns the usage percentage of a QuotaResource.
pub fn get_usage_percentage(&self) -> u64 {
self.usage.saturating_mul(100) / self.limit
self.usage
.saturating_mul(100)
.checked_div(self.limit)
// Assume that if `limit` is 0, this means that storage is unlimited:
.unwrap_or(0)
}
}

Expand Down
Loading