Skip to content

Commit

Permalink
Ignore duplicated version number entries exist in VERSION response.
Browse files Browse the repository at this point in the history
Fix #151

Add duplicated entry check and ignore the duplicated entry when handling VERSION
  response payload.
Add unit test to check the version entries are successfully read when duplicated
  entries exist in version response.
Modify the relative unit test to check non-repetitive version entries of response
  payload.
  • Loading branch information
IntelCaisui authored and jyao1 committed Dec 23, 2024
1 parent fac5664 commit 1a55ab7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
22 changes: 11 additions & 11 deletions spdmlib/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,20 @@ mod tests {

#[test]
fn test_case0_spdm_message() {
let mut versions = gen_array_clone(SpdmVersionStruct::default(), MAX_SPDM_VERSION_COUNT);
versions[0].update = 100;
versions[0].version = SpdmVersion::SpdmVersion10;
versions[1].update = 100;
versions[1].version = SpdmVersion::SpdmVersion11;

let value = SpdmMessage {
header: SpdmMessageHeader {
version: SpdmVersion::SpdmVersion10,
request_response_code: SpdmRequestResponseCode::SpdmResponseVersion,
},
payload: SpdmMessagePayload::SpdmVersionResponse(SpdmVersionResponsePayload {
version_number_entry_count: 0x02,
versions: gen_array_clone(
SpdmVersionStruct {
update: 100,
version: SpdmVersion::SpdmVersion11,
},
MAX_SPDM_VERSION_COUNT,
),
versions,
}),
};

Expand All @@ -633,10 +633,10 @@ mod tests {
);
if let SpdmMessagePayload::SpdmVersionResponse(payload) = &spdm_message.payload {
assert_eq!(payload.version_number_entry_count, 0x02);
for i in 0..2 {
assert_eq!(payload.versions[i].update, 100);
assert_eq!(payload.versions[i].version, SpdmVersion::SpdmVersion11);
}
assert_eq!(payload.versions[0].update, 100);
assert_eq!(payload.versions[0].version, SpdmVersion::SpdmVersion10);
assert_eq!(payload.versions[1].update, 100);
assert_eq!(payload.versions[1].version, SpdmVersion::SpdmVersion11);
}
}
#[test]
Expand Down
39 changes: 22 additions & 17 deletions spdmlib/src/message/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ impl SpdmCodec for SpdmVersionResponsePayload {
},
MAX_SPDM_VERSION_COUNT,
);
let mut version_exist_map: [u8; MAX_SPDM_VERSION_COUNT] = [0; MAX_SPDM_VERSION_COUNT];

let mut version_count = 0;
let rest = r.take(version_number_entry_count as usize * 2)?;

for i in 0..version_number_entry_count {
if let Some(ver) = SpdmVersionStruct::read_bytes(&rest[i as usize * 2..]) {
if version_count < MAX_SPDM_VERSION_COUNT {
let index = ver.version as usize;
if version_exist_map[index] == 0 {
version_exist_map[index] = 1;
versions[version_count] = ver;
version_count += 1;
} else {
// the buffer is full now, stop for scaning more versions
break;
// for duplicated version, ignore it
}
} else {
// for unknown versions,
Expand Down Expand Up @@ -180,15 +182,15 @@ mod tests {
fn test_case0_spdm_version_response_payload() {
let u8_slice = &mut [0u8; 8];
let mut writer = Writer::init(u8_slice);
let mut versions = gen_array_clone(SpdmVersionStruct::default(), MAX_SPDM_VERSION_COUNT);
versions[0].update = 100;
versions[0].version = SpdmVersion::SpdmVersion10;
versions[1].update = 100;
versions[1].version = SpdmVersion::SpdmVersion11;

let value = SpdmVersionResponsePayload {
version_number_entry_count: 2u8,
versions: gen_array_clone(
SpdmVersionStruct {
update: 100u8,
version: SpdmVersion::SpdmVersion10,
},
MAX_SPDM_VERSION_COUNT,
),
versions,
};

create_spdm_context!(context);
Expand All @@ -200,13 +202,16 @@ mod tests {
SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader).unwrap();

assert_eq!(version_response.version_number_entry_count, 2u8);
for i in 0..2 {
assert_eq!(version_response.versions[i].update, 100u8);
assert_eq!(
version_response.versions[i].version,
SpdmVersion::SpdmVersion10
);
}
assert_eq!(version_response.versions[0].update, 100);
assert_eq!(
version_response.versions[0].version,
SpdmVersion::SpdmVersion10
);
assert_eq!(version_response.versions[1].update, 100);
assert_eq!(
version_response.versions[1].version,
SpdmVersion::SpdmVersion11
);
assert_eq!(0, reader.left());
}
#[test]
Expand Down
20 changes: 17 additions & 3 deletions spdmlib/src/message/version_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate alloc;

#[test]
fn test_case1_spdmversion_struct() {
// Validata VERSION response VersionNumberEntryCount beyond maximum allowed size.
// Validate VERSION response VersionNumberEntryCount beyond maximum allowed size.
let u8_slice = &mut [0u8; 100];

// VersionNumberEntryCount = 0xfe
Expand All @@ -19,13 +19,27 @@ fn test_case1_spdmversion_struct() {
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
assert!(res.is_none());

// Validata VERSION response VersionNumberEntryCount 0 size.
// Validate VERSION response VersionNumberEntryCount 0 size.
let u8_slice = &mut [0u8; 100];

// VersionNumberEntryCount = 0x0
u8_slice[3] = 0;
let mut reader = Reader::init(u8_slice);
create_spdm_context!(context);
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
assert!(res.is_none())
assert!(res.is_none());

// Validate VERSION response VersionNumberEntryCount beyond MAX_SPDM_VERSION_COUNT and with duplicated version entries.
let u8_slice: &mut [u8; 16] = &mut [
0x0, 0x0, 0x0, 0x6, 0x0, 0x10, 0x0, 0x10, 0x0, 0x11, 0x0, 0x12, 0x0, 0x13, 0x0, 0x13,
];
let mut reader = Reader::init(u8_slice);
create_spdm_context!(context);
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
let version = res.unwrap();
assert_eq!(version.version_number_entry_count, 4);
assert_eq!(version.versions[0].version, SpdmVersion::SpdmVersion10);
assert_eq!(version.versions[1].version, SpdmVersion::SpdmVersion11);
assert_eq!(version.versions[2].version, SpdmVersion::SpdmVersion12);
assert_eq!(version.versions[3].version, SpdmVersion::SpdmVersion13);
}

0 comments on commit 1a55ab7

Please sign in to comment.