From 87d4554f8a4394371ae79348472d38db03587947 Mon Sep 17 00:00:00 2001 From: Xiao Date: Fri, 28 Jun 2024 16:23:31 +0800 Subject: [PATCH] Add unit tests for MEASUREMENT_EXTENSION_LOG Signed-off-by: Xiao --- .../get_measurement_extension_log.c | 422 ++++++++++++++++++ .../test_spdm_requester/test_spdm_requester.c | 10 +- .../measurement_extension_log.c | 284 ++++++++++++ .../test_spdm_responder/test_spdm_responder.c | 10 +- 4 files changed, 722 insertions(+), 4 deletions(-) diff --git a/unit_test/test_spdm_requester/get_measurement_extension_log.c b/unit_test/test_spdm_requester/get_measurement_extension_log.c index e3154345c7e..0b70234f941 100644 --- a/unit_test/test_spdm_requester/get_measurement_extension_log.c +++ b/unit_test/test_spdm_requester/get_measurement_extension_log.c @@ -16,6 +16,63 @@ uint8_t m_libspdm_mel_test[LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE]; size_t m_libspdm_mel_len; uint8_t m_libspdm_mel_number; +void generate_mel_entry_test() +{ + spdm_measurement_extension_log_dmtf_t *measurement_extension_log; + uint32_t mel_index; + spdm_mel_entry_dmtf_t *mel_entry; + size_t mel_entry_size; + uint8_t rom_infomational[] = "ROM"; + + /*generate MEL*/ + libspdm_zero_mem(m_libspdm_mel_test, sizeof(m_libspdm_mel_test)); + measurement_extension_log = (spdm_measurement_extension_log_dmtf_t *)m_libspdm_mel_test; + + measurement_extension_log->number_of_entries = 0; + measurement_extension_log->mel_entries_len = 0; + measurement_extension_log->reserved = 0; + + m_libspdm_mel_len = sizeof(spdm_measurement_extension_log_dmtf_t); + mel_entry = (spdm_mel_entry_dmtf_t *)((uint8_t *)measurement_extension_log + + sizeof(spdm_measurement_extension_log_dmtf_t)); + + mel_index = 0; + mel_entry_size = 0; + + while (1) + { + if((m_libspdm_mel_len + sizeof(spdm_mel_entry_dmtf_t) + sizeof(rom_infomational)) > + sizeof(m_libspdm_mel_test)) { + break; + } + + mel_entry->mel_index = mel_index; + mel_entry->meas_index = LIBSPDM_MEASUREMENT_INDEX_HEM; + + libspdm_write_uint24(mel_entry->reserved, 0); + mel_entry->measurement_block_dmtf_header.dmtf_spec_measurement_value_type = + SPDM_MEASUREMENT_BLOCK_MEASUREMENT_TYPE_VERSION | + SPDM_MEASUREMENT_BLOCK_MEASUREMENT_TYPE_RAW_BIT_STREAM; + mel_entry->measurement_block_dmtf_header.dmtf_spec_measurement_value_size = + sizeof(rom_infomational); + libspdm_copy_mem((void *)(mel_entry + 1), sizeof(rom_infomational), + rom_infomational, sizeof(rom_infomational)); + + mel_entry_size = (sizeof(spdm_mel_entry_dmtf_t) + sizeof(rom_infomational)); + m_libspdm_mel_len += mel_entry_size; + + measurement_extension_log->number_of_entries = mel_index; + measurement_extension_log->mel_entries_len += (uint32_t)mel_entry_size; + measurement_extension_log->reserved = 0; + + mel_entry = (spdm_mel_entry_dmtf_t *) + ((uint8_t *)mel_entry + sizeof(spdm_mel_entry_dmtf_t)+ + mel_entry->measurement_block_dmtf_header.dmtf_spec_measurement_value_size); + + mel_index++; + } +} + /*generate different long mel according to the m_libspdm_mel_number*/ void libspdm_generate_long_mel(uint32_t measurement_hash_algo) { @@ -111,6 +168,10 @@ libspdm_return_t libspdm_requester_get_measurement_extension_log_test_send_messa case 0x3: case 0x4: case 0x5: + case 0x6: + case 0x7: + case 0x8: + case 0x9: return LIBSPDM_STATUS_SUCCESS; default: return LIBSPDM_STATUS_SEND_FAIL; @@ -433,6 +494,179 @@ libspdm_return_t libspdm_requester_get_measurement_extension_log_test_receive_me } } return LIBSPDM_STATUS_SUCCESS; + case 0x6: + { + spdm_measurement_extension_log_response_t *spdm_response; + size_t spdm_response_size; + size_t transport_header_size; + uint32_t portion_length; + uint32_t remainder_length; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + + static size_t count = 0; + static size_t calling_index = 0; + + if (calling_index == 0) { + count = (m_libspdm_mel_len / LIBSPDM_MAX_MEL_BLOCK_LEN) + 1; + } + + spdm_mel = (spdm_measurement_extension_log_dmtf_t *)m_libspdm_mel_test; + + transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + spdm_response = (void *)((uint8_t *)*response + transport_header_size); + + spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13; + spdm_response->header.request_response_code = SPDM_MEASUREMENT_EXTENSION_LOG; + spdm_response->header.param1 = 0; + spdm_response->header.param2 = 0; + + if(m_libspdm_mel_len > LIBSPDM_MAX_MEL_BLOCK_LEN) { + portion_length = LIBSPDM_MAX_MEL_BLOCK_LEN; + remainder_length = + (uint32_t)(m_libspdm_mel_len - LIBSPDM_MAX_MEL_BLOCK_LEN); + } else { + portion_length = (uint32_t)m_libspdm_mel_len; + remainder_length = 0; + } + + spdm_response->portion_length = portion_length; + spdm_response->remainder_length = remainder_length; + + libspdm_copy_mem(spdm_response + 1, + (size_t)(*response) + *response_size - (size_t)(spdm_response + 1), + (uint8_t *)spdm_mel + + LIBSPDM_MAX_MEL_BLOCK_LEN * calling_index, + portion_length); + + spdm_response_size = sizeof(spdm_measurement_extension_log_response_t) + portion_length; + + libspdm_transport_test_encode_message(spdm_context, NULL, false, + false, spdm_response_size, + spdm_response, response_size, + response); + + m_libspdm_mel_len -= portion_length;; + calling_index++; + + if (calling_index == count) { + calling_index = 0; + m_libspdm_mel_len = 0; + } + } + return LIBSPDM_STATUS_SUCCESS; + + case 0x7: + { + spdm_measurement_extension_log_response_t *spdm_response; + size_t spdm_response_size; + size_t transport_header_size; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + + static size_t calling_index = 0; + + spdm_mel = (spdm_measurement_extension_log_dmtf_t *)m_libspdm_mel_test; + + transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + spdm_response = (void *)((uint8_t *)*response + transport_header_size); + + spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13; + spdm_response->header.request_response_code = SPDM_MEASUREMENT_EXTENSION_LOG; + spdm_response->header.param1 = 0; + spdm_response->header.param2 = 0; + if (calling_index == 0) { + spdm_response->portion_length = LIBSPDM_MAX_MEL_BLOCK_LEN; + spdm_response->remainder_length = LIBSPDM_MAX_MEL_BLOCK_LEN; + } else { + /* The total amount of messages actually sent by the responder is less than the negotiated total mel len*/ + spdm_response->portion_length = LIBSPDM_MAX_MEL_BLOCK_LEN / 2; + spdm_response->remainder_length = 0; + } + + libspdm_copy_mem(spdm_response + 1, + (size_t)(*response) + *response_size - (size_t)(spdm_response + 1), + (uint8_t *)spdm_mel, + LIBSPDM_MAX_MEL_BLOCK_LEN); + + spdm_response_size = sizeof(spdm_measurement_extension_log_response_t) + + LIBSPDM_MAX_MEL_BLOCK_LEN; + + libspdm_transport_test_encode_message(spdm_context, NULL, false, + false, spdm_response_size, + spdm_response, response_size, + response); + calling_index++; + } + return LIBSPDM_STATUS_SUCCESS; + case 0x8: + { + spdm_measurement_extension_log_response_t *spdm_response; + size_t spdm_response_size; + size_t transport_header_size; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + + spdm_mel = (spdm_measurement_extension_log_dmtf_t *)m_libspdm_mel_test; + + transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + spdm_response = (void *)((uint8_t *)*response + transport_header_size); + + spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13; + spdm_response->header.request_response_code = SPDM_MEASUREMENT_EXTENSION_LOG; + spdm_response->header.param1 = 0; + spdm_response->header.param2 = 0; + /* Portion_length is greater than the LIBSPDM_MAX_MEL_BLOCK_LEN */ + spdm_response->portion_length = LIBSPDM_MAX_MEL_BLOCK_LEN + 1; + spdm_response->remainder_length = 0; + + libspdm_copy_mem(spdm_response + 1, + (size_t)(*response) + *response_size - (size_t)(spdm_response + 1), + (uint8_t *)spdm_mel, + LIBSPDM_MAX_MEL_BLOCK_LEN); + + spdm_response_size = sizeof(spdm_measurement_extension_log_response_t) + + LIBSPDM_MAX_MEL_BLOCK_LEN; + + libspdm_transport_test_encode_message(spdm_context, NULL, false, + false, spdm_response_size, + spdm_response, response_size, + response); + } + return LIBSPDM_STATUS_SUCCESS; + + case 0x9: + { + spdm_measurement_extension_log_response_t *spdm_response; + size_t spdm_response_size; + size_t transport_header_size; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + + spdm_mel = (spdm_measurement_extension_log_dmtf_t *)m_libspdm_mel_test; + + transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + spdm_response = (void *)((uint8_t *)*response + transport_header_size); + + spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13; + spdm_response->header.request_response_code = SPDM_MEASUREMENT_EXTENSION_LOG; + spdm_response->header.param1 = 0; + spdm_response->header.param2 = 0; + /* The total MEL length is larger than SPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE*/ + spdm_response->portion_length = LIBSPDM_MAX_MEL_BLOCK_LEN; + spdm_response->remainder_length = SPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE; + + libspdm_copy_mem(spdm_response + 1, + (size_t)(*response) + *response_size - (size_t)(spdm_response + 1), + (uint8_t *)spdm_mel, + LIBSPDM_MAX_MEL_BLOCK_LEN); + + spdm_response_size = sizeof(spdm_measurement_extension_log_response_t) + + LIBSPDM_MAX_MEL_BLOCK_LEN; + + libspdm_transport_test_encode_message(spdm_context, NULL, false, + false, spdm_response_size, + spdm_response, response_size, + response); + } + return LIBSPDM_STATUS_SUCCESS; + default: return LIBSPDM_STATUS_RECEIVE_FAIL; } @@ -657,6 +891,186 @@ void libspdm_test_requester_get_measurement_extension_log_case5(void **state) assert_int_equal(spdm_mel_size, m_libspdm_mel_len); } +/** + * Test 6: Normal case, request a LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE MEL + * Expected Behavior: receives a valid MEL + **/ +void libspdm_test_requester_get_measurement_extension_log_case6(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t spdm_mel_size; + uint8_t spdm_mel[LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE]; + size_t mel_buffer_size; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x6; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->local_context.algorithm.measurement_spec = + SPDM_MEASUREMENT_SPECIFICATION_DMTF; + + libspdm_reset_message_b(spdm_context); + spdm_mel_size = sizeof(spdm_mel); + libspdm_zero_mem(spdm_mel, sizeof(spdm_mel)); + + generate_mel_entry_test(); + mel_buffer_size = m_libspdm_mel_len; + + status = libspdm_get_measurement_extension_log(spdm_context, NULL, + &spdm_mel_size, spdm_mel); + + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(spdm_mel_size, mel_buffer_size); + assert_memory_equal(m_libspdm_mel_test, (void *)spdm_mel, spdm_mel_size); +} + +/** + * Test 7: The total amount of messages actually sent by the responder is less than the negotiated total mel len. + * Expected Behavior: returns with status LIBSPDM_STATUS_INVALID_MSG_FIELD. + **/ +void libspdm_test_requester_get_measurement_extension_log_case7(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t spdm_mel_size; + uint8_t spdm_mel[LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE]; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x7; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->local_context.algorithm.measurement_spec = + SPDM_MEASUREMENT_SPECIFICATION_DMTF; + + libspdm_reset_message_b(spdm_context); + spdm_mel_size = sizeof(spdm_mel); + libspdm_zero_mem(spdm_mel, sizeof(spdm_mel)); + + generate_mel_entry_test(); + + status = libspdm_get_measurement_extension_log(spdm_context, NULL, + &spdm_mel_size, spdm_mel); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); +} + +/** + * Test 8: Portion_length is greater than the LIBSPDM_MAX_MEL_BLOCK_LEN + * Expected Behavior: returns with status LIBSPDM_STATUS_INVALID_MSG_FIELD. + **/ +void libspdm_test_requester_get_measurement_extension_log_case8(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t spdm_mel_size; + uint8_t spdm_mel[LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE]; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x8; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->local_context.algorithm.measurement_spec = + SPDM_MEASUREMENT_SPECIFICATION_DMTF; + + libspdm_reset_message_b(spdm_context); + spdm_mel_size = sizeof(spdm_mel); + libspdm_zero_mem(spdm_mel, sizeof(spdm_mel)); + + generate_mel_entry_test(); + + status = libspdm_get_measurement_extension_log(spdm_context, NULL, + &spdm_mel_size, spdm_mel); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); +} + +/** + * Test 9: The total MEL length is larger than SPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE + * Expected Behavior: returns with status LIBSPDM_STATUS_INVALID_MSG_FIELD. + **/ +void libspdm_test_requester_get_measurement_extension_log_case9(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t spdm_mel_size; + uint8_t spdm_mel[LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE]; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x9; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->local_context.algorithm.measurement_spec = + SPDM_MEASUREMENT_SPECIFICATION_DMTF; + + libspdm_reset_message_b(spdm_context); + spdm_mel_size = sizeof(spdm_mel); + libspdm_zero_mem(spdm_mel, sizeof(spdm_mel)); + + generate_mel_entry_test(); + + status = libspdm_get_measurement_extension_log(spdm_context, NULL, + &spdm_mel_size, spdm_mel); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); +} + libspdm_test_context_t m_libspdm_requester_get_measurement_extension_log_test_context = { LIBSPDM_TEST_CONTEXT_VERSION, true, @@ -677,6 +1091,14 @@ int libspdm_requester_get_measurement_extension_log_test_main(void) cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case4), /* Successful response, the MEL size become more bigger when get MEL. MEL number change from 100 to 200*/ cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case5), + /* Successful response , LIBSPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE*/ + cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case6), + /* Failed response , The total amount of messages actually sent by the responder is less than the negotiated total mel len*/ + cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case7), + /* Failed response , Portion_length is greater than the LIBSPDM_MAX_MEL_BLOCK_LEN*/ + cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case8), + /* Failed response , The total MEL length is larger than SPDM_MAX_MEASUREMENT_EXTENSION_LOG_SIZE*/ + cmocka_unit_test(libspdm_test_requester_get_measurement_extension_log_case9), }; diff --git a/unit_test/test_spdm_requester/test_spdm_requester.c b/unit_test/test_spdm_requester/test_spdm_requester.c index c89e5859d60..257f5039b50 100644 --- a/unit_test/test_spdm_requester/test_spdm_requester.c +++ b/unit_test/test_spdm_requester/test_spdm_requester.c @@ -27,9 +27,12 @@ int libspdm_requester_challenge_test_main(void); #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP int libspdm_requester_get_measurements_test_main(void); int libspdm_requester_get_measurements_error_test_main(void); -int libspdm_requester_get_measurement_extension_log_test_main(void); #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/ +#if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP +int libspdm_requester_get_measurement_extension_log_test_main(void); +#endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP*/ + #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP int libspdm_requester_key_exchange_test_main(void); int libspdm_requester_key_exchange_error_test_main(void); @@ -128,10 +131,13 @@ int main(void) if (libspdm_requester_get_measurements_error_test_main() != 0) { return_value = 1; } + #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */ + + #if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP if (libspdm_requester_get_measurement_extension_log_test_main() != 0) { return_value = 1; } - #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */ + #endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP */ #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP if (libspdm_requester_key_exchange_test_main() != 0) { diff --git a/unit_test/test_spdm_responder/measurement_extension_log.c b/unit_test/test_spdm_responder/measurement_extension_log.c index f4c1349c0bb..c6bdd7d3605 100644 --- a/unit_test/test_spdm_responder/measurement_extension_log.c +++ b/unit_test/test_spdm_responder/measurement_extension_log.c @@ -17,6 +17,30 @@ spdm_get_measurement_extension_log_request_t m_libspdm_get_measurement_extension size_t m_libspdm_get_measurement_extension_log_request1_size = sizeof(m_libspdm_get_measurement_extension_log_request1); +spdm_get_measurement_extension_log_request_t m_libspdm_get_measurement_extension_log_request2 = { + { SPDM_MESSAGE_VERSION_13, SPDM_GET_MEASUREMENT_EXTENSION_LOG, 0, 0 }, + 0, + 0 +}; +size_t m_libspdm_get_measurement_extension_log_request2_size = + sizeof(m_libspdm_get_measurement_extension_log_request2); + +spdm_get_measurement_extension_log_request_t m_libspdm_get_measurement_extension_log_request3 = { + { SPDM_MESSAGE_VERSION_13, SPDM_GET_MEASUREMENT_EXTENSION_LOG, 0, 0 }, + 0, + LIBSPDM_MAX_MEL_BLOCK_LEN + 1 +}; +size_t m_libspdm_get_measurement_extension_log_request3_size = + sizeof(m_libspdm_get_measurement_extension_log_request3); + +spdm_get_measurement_extension_log_request_t m_libspdm_get_measurement_extension_log_request4 = { + { SPDM_MESSAGE_VERSION_13, SPDM_GET_MEASUREMENT_EXTENSION_LOG, 0, 0 }, + LIBSPDM_MAX_MEL_BLOCK_LEN, + LIBSPDM_MAX_MEL_BLOCK_LEN +}; +size_t m_libspdm_get_measurement_extension_log_request4_size = + sizeof(m_libspdm_get_measurement_extension_log_request4); + /** * Test 1: request the first LIBSPDM_MAX_MEL_BLOCK_LEN bytes of the MEL * Expected Behavior: generate a correctly formed MEL message, including its portion_length and remainder_length fields @@ -79,6 +103,258 @@ void libspdm_test_responder_measurement_extension_log_case1(void **state) assert_memory_equal(spdm_response + 1, spdm_mel, spdm_mel_len); } +/** + * Test 2: request.length is less than the MEL len + * Expected Behavior: generate a correctly formed MEL message, including its portion_length and remainder_length fields + **/ +void libspdm_test_responder_measurement_extension_log_case2(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t response_size; + uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE]; + spdm_measurement_extension_log_response_t *spdm_response; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + size_t spdm_mel_len; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x2; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.mel_spec = + m_libspdm_use_mel_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + + spdm_mel = NULL; + spdm_mel_len = 0; + libspdm_measurement_extension_log_collection(spdm_context, + m_libspdm_use_mel_spec, + m_libspdm_use_measurement_spec, + m_libspdm_use_measurement_hash_algo, + (void **)&spdm_mel, &spdm_mel_len); + + /* Test the validity of the request.length.*/ + m_libspdm_get_measurement_extension_log_request2.length = (uint32_t)spdm_mel_len / 2; + + response_size = sizeof(response); + status = libspdm_get_response_measurement_extension_log( + spdm_context, m_libspdm_get_measurement_extension_log_request2_size, + &m_libspdm_get_measurement_extension_log_request2, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + + assert_int_equal(response_size, + sizeof(spdm_measurement_extension_log_response_t) + + spdm_mel_len / 2); + spdm_response = (void *)response; + assert_int_equal(spdm_response->header.request_response_code, + SPDM_MEASUREMENT_EXTENSION_LOG); + assert_int_equal(spdm_response->header.param1, 0); + assert_int_equal(spdm_response->portion_length, spdm_mel_len / 2); + assert_int_equal(spdm_response->remainder_length, spdm_mel_len / 2 + 1); + assert_memory_equal(spdm_response + 1, (void *)spdm_mel, + response_size - sizeof(spdm_measurement_extension_log_response_t)); +} + +/** + * Test 3: When the request.length is greater than LIBSPDM_MAX_MEL_BLOCK_LEN. + * Expected Behavior: generate a correctly formed MEL message, including its portion_length and remainder_length fields + **/ +void libspdm_test_responder_measurement_extension_log_case3(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t response_size; + uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE]; + spdm_measurement_extension_log_response_t *spdm_response; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + size_t spdm_mel_len; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x3; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.mel_spec = + m_libspdm_use_mel_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + + response_size = sizeof(response); + status = libspdm_get_response_measurement_extension_log( + spdm_context, m_libspdm_get_measurement_extension_log_request3_size, + &m_libspdm_get_measurement_extension_log_request3, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + + spdm_mel = NULL; + spdm_mel_len = 0; + libspdm_measurement_extension_log_collection(spdm_context, + m_libspdm_use_mel_spec, + m_libspdm_use_measurement_spec, + m_libspdm_use_measurement_hash_algo, + (void **)&spdm_mel, &spdm_mel_len); + + assert_int_equal(response_size, + sizeof(spdm_measurement_extension_log_response_t) + + spdm_mel_len); + spdm_response = (void *)response; + assert_int_equal(spdm_response->header.request_response_code, + SPDM_MEASUREMENT_EXTENSION_LOG); + assert_int_equal(spdm_response->header.param1, 0); + assert_int_equal(spdm_response->portion_length, spdm_mel_len); + assert_int_equal(spdm_response->remainder_length, 0); + assert_memory_equal(spdm_response + 1, (void *)spdm_mel, + response_size - sizeof(spdm_measurement_extension_log_response_t)); +} + +/** + * Test 4: request.offset > spdm mel len , wrong request message + * Expected Behavior: Generate error response message + **/ +void libspdm_test_responder_measurement_extension_log_case4(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t response_size; + uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE]; + spdm_measurement_extension_log_response_t *spdm_response; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + size_t spdm_mel_len; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x4; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.mel_spec = + m_libspdm_use_mel_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + + spdm_mel = NULL; + spdm_mel_len = 0; + libspdm_measurement_extension_log_collection(spdm_context, + m_libspdm_use_mel_spec, + m_libspdm_use_measurement_spec, + m_libspdm_use_measurement_hash_algo, + (void **)&spdm_mel, &spdm_mel_len); + /* request.offset > total MEL len*/ + m_libspdm_get_measurement_extension_log_request4.offset = (uint32_t)spdm_mel_len + + LIBSPDM_MAX_MEL_BLOCK_LEN; + + response_size = sizeof(response); + status = libspdm_get_response_measurement_extension_log( + spdm_context, m_libspdm_get_measurement_extension_log_request4_size, + &m_libspdm_get_measurement_extension_log_request4, &response_size, response); + + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(response_size, sizeof(spdm_error_response_t)); + spdm_response = (void *)response; + assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR); + assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST); + assert_int_equal(spdm_response->header.param2, 0); +} + +/** + * Test 5: A correct and not zero request.offset. + * Expected Behavior: generate a correctly formed MEL message, including its portion_length and remainder_length fields + **/ +void libspdm_test_responder_measurement_extension_log_case5(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + size_t response_size; + uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE]; + spdm_measurement_extension_log_response_t *spdm_response; + spdm_measurement_extension_log_dmtf_t *spdm_mel; + size_t spdm_mel_len; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x5; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + spdm_context->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP; + spdm_context->connection_info.algorithm.base_hash_algo = + m_libspdm_use_hash_algo; + spdm_context->connection_info.algorithm.base_asym_algo = + m_libspdm_use_asym_algo; + spdm_context->connection_info.algorithm.measurement_spec = + m_libspdm_use_measurement_spec; + spdm_context->connection_info.algorithm.mel_spec = + m_libspdm_use_mel_spec; + spdm_context->connection_info.algorithm.measurement_hash_algo = + m_libspdm_use_measurement_hash_algo; + + spdm_mel = NULL; + spdm_mel_len = 0; + libspdm_measurement_extension_log_collection(spdm_context, + m_libspdm_use_mel_spec, + m_libspdm_use_measurement_spec, + m_libspdm_use_measurement_hash_algo, + (void **)&spdm_mel, &spdm_mel_len); + + /* Test the validity of the request.offset.*/ + m_libspdm_get_measurement_extension_log_request2.offset = (uint32_t)spdm_mel_len / 2; + m_libspdm_get_measurement_extension_log_request2.length = LIBSPDM_MAX_MEL_BLOCK_LEN; + + response_size = sizeof(response); + status = libspdm_get_response_measurement_extension_log( + spdm_context, m_libspdm_get_measurement_extension_log_request2_size, + &m_libspdm_get_measurement_extension_log_request2, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + + assert_int_equal(response_size, + sizeof(spdm_measurement_extension_log_response_t) + + spdm_mel_len / 2 + 1); + spdm_response = (void *)response; + assert_int_equal(spdm_response->header.request_response_code, + SPDM_MEASUREMENT_EXTENSION_LOG); + assert_int_equal(spdm_response->header.param1, 0); + assert_int_equal(spdm_response->portion_length, spdm_mel_len / 2 + 1); + assert_int_equal(spdm_response->remainder_length, 0); + assert_memory_equal(spdm_response + 1, (void *)((uint8_t *)spdm_mel + spdm_mel_len / 2), + response_size - sizeof(spdm_measurement_extension_log_response_t)); +} + libspdm_test_context_t m_libspdm_responder_measurement_extension_log_test_context = { LIBSPDM_TEST_CONTEXT_VERSION, false, @@ -89,6 +365,14 @@ int libspdm_responder_measurement_extension_log_test_main(void) const struct CMUnitTest spdm_responder_measurement_extension_log_tests[] = { /* Success Case*/ cmocka_unit_test(libspdm_test_responder_measurement_extension_log_case1), + /* Success Case, request.length < total MEL len*/ + cmocka_unit_test(libspdm_test_responder_measurement_extension_log_case2), + /* Success Case, request.length > LIBSPDM_MAX_MEL_BLOCK_LEN*/ + cmocka_unit_test(libspdm_test_responder_measurement_extension_log_case3), + /* failed Case, request.offset > total MEL len*/ + cmocka_unit_test(libspdm_test_responder_measurement_extension_log_case4), + /* Success Case, request.offset < total MEL len*/ + cmocka_unit_test(libspdm_test_responder_measurement_extension_log_case5), }; libspdm_setup_test_context(&m_libspdm_responder_measurement_extension_log_test_context); diff --git a/unit_test/test_spdm_responder/test_spdm_responder.c b/unit_test/test_spdm_responder/test_spdm_responder.c index 82b707f37df..40643f0112f 100644 --- a/unit_test/test_spdm_responder/test_spdm_responder.c +++ b/unit_test/test_spdm_responder/test_spdm_responder.c @@ -22,9 +22,12 @@ int libspdm_responder_challenge_auth_test_main(void); #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP int libspdm_responder_measurements_test_main(void); -int libspdm_responder_measurement_extension_log_test_main(void); #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/ +#if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP +int libspdm_responder_measurement_extension_log_test_main(void); +#endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP*/ + #if LIBSPDM_RESPOND_IF_READY_SUPPORT #if (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP || LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP || \ LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP || LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP || \ @@ -118,10 +121,13 @@ int main(void) if (libspdm_responder_measurements_test_main() != 0) { return_value = 1; } + #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/ + + #if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP if (libspdm_responder_measurement_extension_log_test_main() != 0) { return_value = 1; } - #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/ + #endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP*/ #if LIBSPDM_RESPOND_IF_READY_SUPPORT #if (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP || LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP || \