Skip to content

Commit c88c507

Browse files
Placebo27jakezhu9
authored andcommitted
tests: add some test cases for check and process functions.
Signed-off-by: DongQing <placebo27@hust.edu.cn>
1 parent b568321 commit c88c507

File tree

3 files changed

+170
-20
lines changed

3 files changed

+170
-20
lines changed

sophgo-rom-tool/src/lib.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
use core::mem::{size_of, size_of_val};
22
use crc::{Crc, CRC_16_XMODEM};
33

4-
#[derive(Debug)]
4+
#[derive(Clone, Debug, PartialEq, Eq)]
55
pub enum Error {
6-
HeadLength {
7-
wrong_length: usize,
8-
},
9-
MagicNumber {
10-
wrong_magic: u32,
11-
},
12-
RawBlobMagic {
13-
wrong_magic: [u8; 32],
14-
},
15-
ImageContentLength {
16-
wrong_content_length: usize,
17-
wrong_full_length: usize,
18-
},
19-
OutputBufferLength {
20-
wrong_length: usize,
21-
},
6+
HeadLength { wrong_length: usize },
7+
MagicNumber { wrong_magic: u32 },
8+
RawBlobMagic { wrong_magic: [u8; 32] },
9+
ImageFullLength { wrong_full_length: usize },
10+
ImageContentLength { wrong_content_length: usize },
11+
OutputBufferLength { wrong_length: usize },
2212
}
2313

2414
pub type Result<T> = core::result::Result<T, Error>;
2515

16+
#[derive(Clone, Debug, PartialEq, Eq)]
2617
pub struct Operations<'a> {
2718
pub refill_header: Option<HeaderInfo>,
2819
pub set_image_content: Option<&'a [u8]>,
2920
pub resize_image_full_length: usize,
3021
}
3122

23+
#[derive(Clone, Debug, PartialEq, Eq)]
3224
pub struct HeaderInfo {
3325
pub blcp_image_checksum: u32,
3426
pub bl2_image_checksum: u32,
@@ -124,15 +116,19 @@ pub fn process(buf: &mut [u8], ops: &Operations) -> Result<()> {
124116
let param_checksum = 0xCAFE0000u32 + crc.checksum(&buf[0x10..0x800]) as u32;
125117
buf[0xC..0x10].copy_from_slice(&param_checksum.to_le_bytes())
126118
}
119+
if ops.resize_image_full_length > u32::MAX as usize
120+
|| ops.resize_image_full_length < HEADER_LENGTH
121+
{
122+
return Err(Error::ImageFullLength {
123+
wrong_full_length: ops.resize_image_full_length,
124+
});
125+
}
127126
if let Some(image) = &ops.set_image_content {
128127
if image.len() > u32::MAX as usize
129-
|| ops.resize_image_full_length > u32::MAX as usize
130-
|| ops.resize_image_full_length < HEADER_LENGTH
131128
|| image.len() + HEADER_LENGTH > ops.resize_image_full_length
132129
{
133130
return Err(Error::ImageContentLength {
134131
wrong_content_length: image.len(),
135-
wrong_full_length: ops.resize_image_full_length,
136132
});
137133
}
138134
buf[HEADER_LENGTH..][..image.len()].copy_from_slice(image);

sophgo-rom-tool/tests/check-test.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use sophgo_rom_tool::Error;
2+
3+
#[test]
4+
fn check_success_raw_blob() {
5+
let content = vec![
6+
0x6F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8+
0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
9+
];
10+
let ans = sophgo_rom_tool::check(&content).unwrap();
11+
assert_eq!(
12+
ans.refill_header,
13+
Some(sophgo_rom_tool::HeaderInfo {
14+
blcp_image_checksum: 0xCAFE0000,
15+
bl2_image_checksum: 0xCAFE8761,
16+
bl2_image_size: 512
17+
})
18+
);
19+
assert_eq!(ans.resize_image_full_length, 4608);
20+
}
21+
22+
#[test]
23+
fn check_error_head_length() {
24+
let content = vec![0x6F, 0x00, 0x00];
25+
let ans = sophgo_rom_tool::check(&content);
26+
assert_eq!(ans, Err(Error::HeadLength { wrong_length: 3 }))
27+
}
28+
29+
#[test]
30+
fn check_error_magic_number() {
31+
let content = vec![0x44, 0x33, 0x22, 0x11];
32+
let ans = sophgo_rom_tool::check(&content);
33+
assert_eq!(
34+
ans,
35+
Err(Error::MagicNumber {
36+
wrong_magic: 0x11223344
37+
})
38+
)
39+
}
40+
41+
#[test]
42+
fn check_error_raw_blob_head_length() {
43+
let content = vec![
44+
0x6F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x02, 0x00,
45+
0x00, 0x00, 0x00, 0x00, 0x00,
46+
];
47+
let ans = sophgo_rom_tool::check(&content);
48+
assert_eq!(ans, Err(Error::HeadLength { wrong_length: 20 }))
49+
}
50+
51+
#[test]
52+
fn check_error_raw_blob_magic() {
53+
let content = vec![
54+
0x6F, 0x00, 0x00, 0x02, 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x11, 0x45, 0x14, 0x19, 0x19,
55+
0x81, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x0, 0x1,
56+
];
57+
let ans = sophgo_rom_tool::check(&content);
58+
assert_eq!(
59+
ans,
60+
Err(Error::RawBlobMagic {
61+
wrong_magic: [
62+
0x6F, 0x00, 0x00, 0x02, 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x11, 0x45, 0x14, 0x19,
63+
0x19, 0x81, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x11, 0x45, 0x14, 0x19, 0x19,
64+
0x81, 0x0, 0x1
65+
]
66+
})
67+
)
68+
}

sophgo-rom-tool/tests/process-test.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use sophgo_rom_tool::{Error, HeaderInfo};
2+
3+
#[test]
4+
fn process_success() {
5+
let mut content = vec![0u8; 4098];
6+
let ops = sophgo_rom_tool::Operations {
7+
refill_header: None,
8+
set_image_content: Some(&[0x11, 0x22]),
9+
resize_image_full_length: 4098,
10+
};
11+
let ans = sophgo_rom_tool::process(&mut content, &ops);
12+
assert_eq!(ans, Ok(()));
13+
let mut expected = vec![0u8; 4096];
14+
expected.push(0x11);
15+
expected.push(0x22);
16+
assert_eq!(content, expected);
17+
let mut content = vec![0u8; 4098];
18+
let ops = sophgo_rom_tool::Operations {
19+
refill_header: Some(HeaderInfo {
20+
blcp_image_checksum: 0x11112222,
21+
bl2_image_checksum: 0x33334444,
22+
bl2_image_size: 0x55556666,
23+
}),
24+
set_image_content: Some(&[0x11, 0x22]),
25+
resize_image_full_length: 4098,
26+
};
27+
let ans = sophgo_rom_tool::process(&mut content, &ops);
28+
assert_eq!(ans, Ok(()));
29+
let mut expected = vec![0u8; 4098];
30+
expected[..12].copy_from_slice(&[
31+
0x43, 0x56, 0x42, 0x4C, 0x30, 0x31, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00,
32+
]);
33+
expected[0xBC..0xC0].copy_from_slice(&[0xF8, 0x02, 0x00, 0x00]);
34+
expected[0xC0..0xC4].copy_from_slice(&[0x22, 0x22, 0x11, 0x11]);
35+
expected[0xD4..0xD8].copy_from_slice(&[0x44, 0x44, 0x33, 0x33]);
36+
expected[0xD8..0xDC].copy_from_slice(&[0x66, 0x66, 0x55, 0x55]);
37+
expected[0xC..0x10].copy_from_slice(&[0xA5, 0xAC, 0xFE, 0xCA]);
38+
expected[4096..].copy_from_slice(&[0x11, 0x22]);
39+
assert_eq!(content, expected);
40+
}
41+
42+
#[test]
43+
fn process_error_output_buffer_length() {
44+
let mut content = vec![0u8; 50];
45+
let ops = sophgo_rom_tool::Operations {
46+
refill_header: None,
47+
set_image_content: None,
48+
resize_image_full_length: 100,
49+
};
50+
let ans = sophgo_rom_tool::process(&mut content, &ops);
51+
assert_eq!(ans, Err(Error::OutputBufferLength { wrong_length: 50 }))
52+
}
53+
54+
#[test]
55+
fn process_error_image_full_length() {
56+
let mut content = vec![0u8; 501];
57+
let ops = sophgo_rom_tool::Operations {
58+
refill_header: None,
59+
set_image_content: None,
60+
resize_image_full_length: 500,
61+
};
62+
let ans = sophgo_rom_tool::process(&mut content, &ops);
63+
assert_eq!(
64+
ans,
65+
Err(Error::ImageFullLength {
66+
wrong_full_length: 500
67+
})
68+
);
69+
}
70+
71+
#[test]
72+
fn process_error_image_content_length() {
73+
let mut content = vec![0u8; 5000];
74+
let ops = sophgo_rom_tool::Operations {
75+
refill_header: None,
76+
set_image_content: Some(&[0x11, 0x22]),
77+
resize_image_full_length: 600,
78+
};
79+
let ans = sophgo_rom_tool::process(&mut content, &ops);
80+
assert_eq!(
81+
ans,
82+
Err(Error::ImageFullLength {
83+
wrong_full_length: 600
84+
})
85+
);
86+
}

0 commit comments

Comments
 (0)