use serde::{Deserialize, Serialize};
mod decimal_u64 {
use serde::{Deserialize, Deserializer, Serializer};
#[derive(Deserialize)]
#[serde(untagged)]
enum Repr {
Decimal(String),
LegacyNumber(u64),
}
pub fn serialize<S: Serializer>(value: &u64, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&value.to_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
match Repr::deserialize(deserializer)? {
Repr::Decimal(value) => value.parse().map_err(serde::de::Error::custom),
Repr::LegacyNumber(value) => Ok(value),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ResourceType {
File,
Ui,
Network,
Capability,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AccessType {
Read,
Write,
Execute,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeniedResource {
pub resource: String,
pub resource_type: ResourceType,
pub access_type: AccessType,
pub pid: u32,
#[serde(with = "decimal_u64")]
pub filetime: u64,
}
pub type DedupKey = (String, AccessType);
impl DeniedResource {
#[must_use]
pub fn dedup_key(&self) -> DedupKey {
(self.resource.clone(), self.access_type)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn denied_resource_serialises_camel_case() {
let r = DeniedResource {
resource: r"C:\Users\test\file.txt".to_string(),
resource_type: ResourceType::File,
access_type: AccessType::Read,
pid: 1234,
filetime: 132_847_890_123_456_789,
};
let json = serde_json::to_string(&r).unwrap();
assert!(json.contains("\"resource\":\"C:"), "got {json}");
assert!(json.contains("\"resourceType\":\"file\""), "got {json}");
assert!(json.contains("\"accessType\":\"read\""), "got {json}");
assert!(
json.contains("\"filetime\":\"132847890123456789\""),
"got {json}"
);
}
#[test]
fn denied_resource_round_trips_through_json() {
let r = DeniedResource {
resource: r"C:\foo\bar.txt".to_string(),
resource_type: ResourceType::Capability,
access_type: AccessType::Write,
pid: 9999,
filetime: 42,
};
let json = serde_json::to_string(&r).unwrap();
let parsed: DeniedResource = serde_json::from_str(&json).unwrap();
assert_eq!(r, parsed);
}
#[test]
fn denied_resource_accepts_legacy_numeric_filetime() {
let parsed: DeniedResource = serde_json::from_str(
r#"{"resource":"C:\\foo","resourceType":"file","accessType":"read","pid":1,"filetime":42}"#,
)
.unwrap();
assert_eq!(parsed.filetime, 42);
}
#[test]
fn resource_type_serialises_each_variant_to_lowercase() {
for (variant, expected) in [
(ResourceType::File, "\"file\""),
(ResourceType::Ui, "\"ui\""),
(ResourceType::Network, "\"network\""),
(ResourceType::Capability, "\"capability\""),
(ResourceType::Other, "\"other\""),
] {
assert_eq!(serde_json::to_string(&variant).unwrap(), expected);
}
}
#[test]
fn access_type_serialises_each_variant_to_lowercase() {
for (variant, expected) in [
(AccessType::Read, "\"read\""),
(AccessType::Write, "\"write\""),
(AccessType::Execute, "\"execute\""),
(AccessType::Unknown, "\"unknown\""),
] {
assert_eq!(serde_json::to_string(&variant).unwrap(), expected);
}
}
#[test]
fn dedup_key_pairs_path_and_access_type() {
let r = DeniedResource {
resource: r"C:\a".to_string(),
resource_type: ResourceType::File,
access_type: AccessType::Read,
pid: 1,
filetime: 1,
};
assert_eq!(r.dedup_key(), (r"C:\a".to_string(), AccessType::Read));
}
}