|
| 1 | +mod utils; |
| 2 | + |
| 3 | +#[derive(Debug, Clone)] |
| 4 | +struct ExpectedDirEntry { |
| 5 | + name: String, |
| 6 | + mtime: String, |
| 7 | + ctime: String, |
| 8 | + size: u32, |
| 9 | + is_dir: bool, |
| 10 | +} |
| 11 | + |
| 12 | +impl PartialEq<embedded_sdmmc::DirEntry> for ExpectedDirEntry { |
| 13 | + fn eq(&self, other: &embedded_sdmmc::DirEntry) -> bool { |
| 14 | + if other.name.to_string() != self.name { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if format!("{}", other.mtime) != self.mtime { |
| 18 | + return false; |
| 19 | + } |
| 20 | + if format!("{}", other.ctime) != self.ctime { |
| 21 | + return false; |
| 22 | + } |
| 23 | + if other.size != self.size { |
| 24 | + return false; |
| 25 | + } |
| 26 | + if other.attributes.is_directory() != self.is_dir { |
| 27 | + return false; |
| 28 | + } |
| 29 | + true |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn fat16_root_directory_listing() { |
| 35 | + let time_source = utils::make_time_source(); |
| 36 | + let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap(); |
| 37 | + let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); |
| 38 | + |
| 39 | + let fat16_volume = volume_mgr |
| 40 | + .open_volume(embedded_sdmmc::VolumeIdx(0)) |
| 41 | + .expect("open volume 0"); |
| 42 | + let root_dir = volume_mgr |
| 43 | + .open_root_dir(fat16_volume) |
| 44 | + .expect("open root dir"); |
| 45 | + |
| 46 | + let expected = [ |
| 47 | + ExpectedDirEntry { |
| 48 | + name: String::from("README.TXT"), |
| 49 | + mtime: String::from("2018-12-09 19:22:34"), |
| 50 | + ctime: String::from("2018-12-09 19:22:34"), |
| 51 | + size: 258, |
| 52 | + is_dir: false, |
| 53 | + }, |
| 54 | + ExpectedDirEntry { |
| 55 | + name: String::from("EMPTY.DAT"), |
| 56 | + mtime: String::from("2018-12-09 19:21:16"), |
| 57 | + ctime: String::from("2018-12-09 19:21:16"), |
| 58 | + size: 0, |
| 59 | + is_dir: false, |
| 60 | + }, |
| 61 | + ExpectedDirEntry { |
| 62 | + name: String::from("TEST"), |
| 63 | + mtime: String::from("2018-12-09 19:23:16"), |
| 64 | + ctime: String::from("2018-12-09 19:23:16"), |
| 65 | + size: 0, |
| 66 | + is_dir: true, |
| 67 | + }, |
| 68 | + ExpectedDirEntry { |
| 69 | + name: String::from("64MB.DAT"), |
| 70 | + mtime: String::from("2018-12-09 19:21:38"), |
| 71 | + ctime: String::from("2018-12-09 19:21:38"), |
| 72 | + size: 64 * 1024 * 1024, |
| 73 | + is_dir: false, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + |
| 77 | + let mut listing = Vec::new(); |
| 78 | + |
| 79 | + volume_mgr |
| 80 | + .iterate_dir(root_dir, |d| { |
| 81 | + listing.push(d.clone()); |
| 82 | + }) |
| 83 | + .expect("iterate directory"); |
| 84 | + |
| 85 | + assert_eq!(expected.len(), listing.len()); |
| 86 | + for (expected_entry, given_entry) in expected.iter().zip(listing.iter()) { |
| 87 | + assert_eq!( |
| 88 | + expected_entry, given_entry, |
| 89 | + "{:#?} does not match {:#?}", |
| 90 | + given_entry, expected_entry |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn fat16_sub_directory_listing() { |
| 97 | + let time_source = utils::make_time_source(); |
| 98 | + let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap(); |
| 99 | + let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); |
| 100 | + |
| 101 | + let fat16_volume = volume_mgr |
| 102 | + .open_volume(embedded_sdmmc::VolumeIdx(0)) |
| 103 | + .expect("open volume 0"); |
| 104 | + let root_dir = volume_mgr |
| 105 | + .open_root_dir(fat16_volume) |
| 106 | + .expect("open root dir"); |
| 107 | + let test_dir = volume_mgr |
| 108 | + .open_dir(root_dir, "TEST") |
| 109 | + .expect("open test dir"); |
| 110 | + |
| 111 | + let expected = [ |
| 112 | + ExpectedDirEntry { |
| 113 | + name: String::from("."), |
| 114 | + mtime: String::from("2018-12-09 19:21:02"), |
| 115 | + ctime: String::from("2018-12-09 19:21:02"), |
| 116 | + size: 0, |
| 117 | + is_dir: true, |
| 118 | + }, |
| 119 | + ExpectedDirEntry { |
| 120 | + name: String::from(".."), |
| 121 | + mtime: String::from("2018-12-09 19:21:02"), |
| 122 | + ctime: String::from("2018-12-09 19:21:02"), |
| 123 | + size: 0, |
| 124 | + is_dir: true, |
| 125 | + }, |
| 126 | + ExpectedDirEntry { |
| 127 | + name: String::from("TEST.DAT"), |
| 128 | + mtime: String::from("2018-12-09 19:22:12"), |
| 129 | + ctime: String::from("2018-12-09 19:22:12"), |
| 130 | + size: 3500, |
| 131 | + is_dir: false, |
| 132 | + }, |
| 133 | + ]; |
| 134 | + |
| 135 | + let mut listing = Vec::new(); |
| 136 | + |
| 137 | + volume_mgr |
| 138 | + .iterate_dir(test_dir, |d| { |
| 139 | + listing.push(d.clone()); |
| 140 | + }) |
| 141 | + .expect("iterate directory"); |
| 142 | + |
| 143 | + assert_eq!(expected.len(), listing.len()); |
| 144 | + for (expected_entry, given_entry) in expected.iter().zip(listing.iter()) { |
| 145 | + assert_eq!( |
| 146 | + expected_entry, given_entry, |
| 147 | + "{:#?} does not match {:#?}", |
| 148 | + given_entry, expected_entry |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#[test] |
| 154 | +fn fat32_root_directory_listing() { |
| 155 | + let time_source = utils::make_time_source(); |
| 156 | + let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap(); |
| 157 | + let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); |
| 158 | + |
| 159 | + let fat32_volume = volume_mgr |
| 160 | + .open_volume(embedded_sdmmc::VolumeIdx(1)) |
| 161 | + .expect("open volume 1"); |
| 162 | + let root_dir = volume_mgr |
| 163 | + .open_root_dir(fat32_volume) |
| 164 | + .expect("open root dir"); |
| 165 | + |
| 166 | + let expected = [ |
| 167 | + ExpectedDirEntry { |
| 168 | + name: String::from("64MB.DAT"), |
| 169 | + mtime: String::from("2018-12-09 19:22:56"), |
| 170 | + ctime: String::from("2018-12-09 19:22:56"), |
| 171 | + size: 64 * 1024 * 1024, |
| 172 | + is_dir: false, |
| 173 | + }, |
| 174 | + ExpectedDirEntry { |
| 175 | + name: String::from("EMPTY.DAT"), |
| 176 | + mtime: String::from("2018-12-09 19:22:56"), |
| 177 | + ctime: String::from("2018-12-09 19:22:56"), |
| 178 | + size: 0, |
| 179 | + is_dir: false, |
| 180 | + }, |
| 181 | + ExpectedDirEntry { |
| 182 | + name: String::from("README.TXT"), |
| 183 | + mtime: String::from("2023-09-21 09:48:06"), |
| 184 | + ctime: String::from("2018-12-09 19:22:56"), |
| 185 | + size: 258, |
| 186 | + is_dir: false, |
| 187 | + }, |
| 188 | + ExpectedDirEntry { |
| 189 | + name: String::from("TEST"), |
| 190 | + mtime: String::from("2018-12-09 19:23:20"), |
| 191 | + ctime: String::from("2018-12-09 19:23:20"), |
| 192 | + size: 0, |
| 193 | + is_dir: true, |
| 194 | + }, |
| 195 | + ]; |
| 196 | + |
| 197 | + let mut listing = Vec::new(); |
| 198 | + |
| 199 | + volume_mgr |
| 200 | + .iterate_dir(root_dir, |d| { |
| 201 | + listing.push(d.clone()); |
| 202 | + }) |
| 203 | + .expect("iterate directory"); |
| 204 | + |
| 205 | + assert_eq!(expected.len(), listing.len()); |
| 206 | + for (expected_entry, given_entry) in expected.iter().zip(listing.iter()) { |
| 207 | + assert_eq!( |
| 208 | + expected_entry, given_entry, |
| 209 | + "{:#?} does not match {:#?}", |
| 210 | + given_entry, expected_entry |
| 211 | + ); |
| 212 | + } |
| 213 | +} |
0 commit comments