Skip to content

Commit c502b00

Browse files
authored
Add back the image format tests which had been previously removed (#266)
* Test the ESP8266 image format * Test the IDF bootloader image format * Add a README documenting how to generate the test resources
1 parent 0f6d8fd commit c502b00

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

espflash/src/image_format/esp8266.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,26 @@ fn encode_flash_size(size: FlashSize) -> Result<u8, FlashDetectError> {
161161
_ => Err(FlashDetectError::from(size as u8)),
162162
}
163163
}
164+
165+
#[cfg(test)]
166+
mod tests {
167+
use std::fs;
168+
169+
use super::*;
170+
use crate::elf::ElfFirmwareImage;
171+
172+
#[test]
173+
fn test_esp8266_image_format() {
174+
let input_bytes = fs::read("tests/resources/esp8266_hal_blinky").unwrap();
175+
let expected_bin = fs::read("tests/resources/esp8266_hal_blinky.bin").unwrap();
176+
177+
let image = ElfFirmwareImage::try_from(input_bytes.as_slice()).unwrap();
178+
let flash_image = Esp8266Format::new(&image, None, None, None).unwrap();
179+
180+
let segments = flash_image.flash_segments().collect::<Vec<_>>();
181+
let buf = segments[0].data.as_ref();
182+
183+
assert_eq!(expected_bin.len(), buf.len());
184+
assert_eq!(expected_bin.as_slice(), buf);
185+
}
186+
}

espflash/src/image_format/idf_bootloader.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,38 @@ fn save_segment(data: &mut Vec<u8>, segment: &CodeSegment, checksum: u8) -> Resu
327327

328328
Ok(update_checksum(segment.data(), checksum))
329329
}
330+
331+
#[cfg(test)]
332+
pub mod tests {
333+
use std::fs;
334+
335+
use super::*;
336+
use crate::elf::ElfFirmwareImage;
337+
338+
// Copied from: src/targets/esp32.rs
339+
const PARAMS: Esp32Params = Esp32Params::new(
340+
0x1000,
341+
0x1_0000,
342+
0x3f_0000,
343+
0,
344+
include_bytes!("../../resources/bootloaders/esp32-bootloader.bin"),
345+
);
346+
347+
#[test]
348+
fn test_idf_bootloader_format() {
349+
let input_bytes = fs::read("tests/resources/esp32_hal_blinky").unwrap();
350+
let expected_bin = fs::read("tests/resources/esp32_hal_blinky.bin").unwrap();
351+
352+
let image = ElfFirmwareImage::try_from(input_bytes.as_slice()).unwrap();
353+
let flash_image =
354+
IdfBootloaderFormat::new(&image, Chip::Esp32, PARAMS, None, None, None, None, None)
355+
.unwrap();
356+
357+
let segments = flash_image.flash_segments().collect::<Vec<_>>();
358+
assert_eq!(segments.len(), 3);
359+
360+
let buf = segments[2].data.as_ref();
361+
assert_eq!(expected_bin.len(), buf.len());
362+
assert_eq!(expected_bin.as_slice(), buf);
363+
}
364+
}

espflash/tests/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test Resources
2+
3+
This document describes how the test files under `tests/resources` were generated, so that they can be re-generated in the future if needed.
4+
5+
## IDF Bootloader
6+
7+
```bash
8+
$ git clone https://github.com/esp-rs/esp-hal
9+
$ cd esp32-hal/
10+
$ cargo build --release --example=blinky
11+
```
12+
13+
The ELF file is located at `target/xtensa-esp32-none-elf/examples/blinky`
14+
15+
```bash
16+
$ espflash save-image --chip=esp32 esp32_hal_blinky.bin esp32_hal_blinky
17+
```
18+
19+
## ESP8266
20+
21+
```bash
22+
$ git clone https://github.com/esp-rs/esp8266-hal
23+
$ cd esp8266-hal/
24+
$ cargo build --release --example=blinky
25+
```
26+
27+
The ELF file is located at `target/xtensa-esp8266-none-elf/examples/blinky`
28+
29+
```bash
30+
$ espflash save-image --chip=esp8266 esp8266_hal_blinky.bin esp8266_hal_blinky
31+
```
70.1 KB
Binary file not shown.
83 KB
Binary file not shown.
17.6 KB
Binary file not shown.
820 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)