Skip to main content

isr_dl_windows/
request.rs

1use std::path::PathBuf;
2
3use crate::{CodeView, ImageSignature};
4
5/// A single artifact to fetch from a Microsoft symbol server.
6pub enum SymbolRequest {
7    /// Request a PDB identified by its CodeView info.
8    Pdb(CodeView),
9
10    /// Request a PE binary identified by its image signature.
11    Image(ImageSignature),
12}
13
14impl SymbolRequest {
15    /// Returns the basename for the symbol server lookup.
16    pub fn name(&self) -> &str {
17        match self {
18            SymbolRequest::Pdb(request) => request.filename(),
19            SymbolRequest::Image(request) => &request.name,
20        }
21    }
22
23    /// Returns the second path segment for the symbol server.
24    pub fn hash(&self) -> String {
25        match self {
26            SymbolRequest::Pdb(request) => request.hash(),
27            SymbolRequest::Image(request) => request.hash(),
28        }
29    }
30
31    /// Returns the relative output directory for this artifact.
32    pub fn subdirectory(&self) -> PathBuf {
33        match self {
34            SymbolRequest::Pdb(request) => request.subdirectory(),
35            SymbolRequest::Image(request) => request.subdirectory(),
36        }
37    }
38}
39
40impl From<CodeView> for SymbolRequest {
41    fn from(value: CodeView) -> Self {
42        Self::Pdb(value)
43    }
44}
45
46impl From<ImageSignature> for SymbolRequest {
47    fn from(value: ImageSignature) -> Self {
48        Self::Image(value)
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    fn sample_pdb() -> CodeView {
57        CodeView {
58            name: r"D:\build\kernel32.pdb".into(),
59            guid: "1b72224d37b8179228200ed8994498b2".into(),
60            age: 1,
61        }
62    }
63
64    fn sample_image() -> ImageSignature {
65        ImageSignature {
66            name: "kernel32.dll".into(),
67            timestamp: 0x590285E9,
68            size_of_image: 0xE0000,
69        }
70    }
71
72    #[test]
73    fn pdb_request_strips_build_path_from_name() {
74        let req: SymbolRequest = sample_pdb().into();
75        assert_eq!(req.name(), "kernel32.pdb");
76    }
77
78    #[test]
79    fn pdb_request_delegates_hash_to_codeview() {
80        let req: SymbolRequest = sample_pdb().into();
81        assert_eq!(req.hash(), sample_pdb().hash());
82    }
83
84    #[test]
85    fn pdb_request_subdirectory_uses_stripped_filename() {
86        let req: SymbolRequest = sample_pdb().into();
87        assert_eq!(
88            req.subdirectory(),
89            PathBuf::from("kernel32.pdb").join("1b72224d37b8179228200ed8994498b21")
90        );
91    }
92
93    #[test]
94    fn image_request_exposes_full_name() {
95        let req: SymbolRequest = sample_image().into();
96        assert_eq!(req.name(), "kernel32.dll");
97    }
98
99    #[test]
100    fn image_request_delegates_hash_to_image_signature() {
101        let req: SymbolRequest = sample_image().into();
102        assert_eq!(req.hash(), "590285E9e0000");
103    }
104}