diff --git a/libpff/libpff_descriptors_index.c b/libpff/libpff_descriptors_index.c index 6aa474cb..1d5c3591 100644 --- a/libpff/libpff_descriptors_index.c +++ b/libpff/libpff_descriptors_index.c @@ -156,6 +156,7 @@ int libpff_descriptors_index_initialize( return( -1 ); } + /* Frees a descriptors index * Returns 1 if successful or -1 on error */ @@ -345,13 +346,21 @@ int libpff_descriptors_index_get_index_value_by_identifier( goto on_error; } - lookup_index_value->data_identifier = lookup_index_value->data_identifier; - lookup_index_value->local_descriptors_identifier = lookup_index_value->local_descriptors_identifier; - lookup_index_value->parent_identifier = lookup_index_value->parent_identifier; + lookup_index_value->data_identifier = safe_index_value->data_identifier; + lookup_index_value->local_descriptors_identifier = safe_index_value->local_descriptors_identifier; + lookup_index_value->parent_identifier = safe_index_value->parent_identifier; *index_value = lookup_index_value; + + lookup_index_value = NULL; } } + if( lookup_index_value != NULL ) + { + libpff_index_value_free( + &lookup_index_value, + NULL ); + } return( result ); on_error: @@ -483,4 +492,3 @@ int libpff_descriptors_index_insert_recovered_index_value( } return( -1 ); } - diff --git a/libpff/libpff_offsets_index.c b/libpff/libpff_offsets_index.c index b669c5f1..05309bf1 100644 --- a/libpff/libpff_offsets_index.c +++ b/libpff/libpff_offsets_index.c @@ -155,6 +155,7 @@ int libpff_offsets_index_initialize( return( -1 ); } + /* Frees an offsets index * Returns 1 if successful or -1 on error */ @@ -339,13 +340,21 @@ int libpff_offsets_index_get_index_value_by_identifier( goto on_error; } - lookup_index_value->file_offset = lookup_index_value->file_offset; - lookup_index_value->data_size = lookup_index_value->data_size; - lookup_index_value->reference_count = lookup_index_value->reference_count; + lookup_index_value->file_offset = safe_index_value->file_offset; + lookup_index_value->data_size = safe_index_value->data_size; + lookup_index_value->reference_count = safe_index_value->reference_count; *index_value = lookup_index_value; + + lookup_index_value = NULL; } } + if( lookup_index_value != NULL ) + { + libpff_index_value_free( + &lookup_index_value, + NULL ); + } return( result ); on_error: @@ -477,4 +486,3 @@ int libpff_offsets_index_insert_recovered_index_value( } return( -1 ); } - diff --git a/tests/pff_test_descriptors_index.c b/tests/pff_test_descriptors_index.c index cc3efac1..28aefbab 100644 --- a/tests/pff_test_descriptors_index.c +++ b/tests/pff_test_descriptors_index.c @@ -34,6 +34,8 @@ #include "pff_test_unused.h" #include "../libpff/libpff_descriptors_index.h" +#include "../libpff/libpff_index_value.h" +#include "../libpff/libpff_io_handle.h" #if defined( __GNUC__ ) && !defined( LIBPFF_DLL_IMPORT ) @@ -75,6 +77,168 @@ int pff_test_descriptors_index_free( return( 0 ); } +/* Tests recovered descriptor lookup field preservation. + * Returns 1 if successful or 0 if not. + */ +int pff_test_descriptors_index_get_index_value_by_identifier( + void ) +{ + libcerror_error_t *error = NULL; + libpff_descriptors_index_t *descriptors_index = NULL; + libpff_index_value_t *index_value = NULL; + libpff_index_value_t *recovered_value = NULL; + libpff_io_handle_t *io_handle = NULL; + int result = 0; + + result = libpff_io_handle_initialize( + &io_handle, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + result = libpff_descriptors_index_initialize( + &descriptors_index, + 0, + 0, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + result = libpff_index_value_initialize( + &recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value->identifier = 0x1234; + recovered_value->data_identifier = 0x5678; + recovered_value->local_descriptors_identifier = 0x9abc; + recovered_value->parent_identifier = 0xdef0; + + result = libpff_descriptors_index_insert_recovered_index_value( + descriptors_index, + recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value = NULL; + + result = libpff_descriptors_index_get_index_value_by_identifier( + descriptors_index, + io_handle, + NULL, + 0x1234, + 1, + &index_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + PFF_TEST_ASSERT_IS_NOT_NULL( + "index_value", + index_value ); + + PFF_TEST_ASSERT_EQUAL_UINT64( + "index_value->data_identifier", + index_value->data_identifier, + (uint64_t) 0x5678 ); + + PFF_TEST_ASSERT_EQUAL_UINT64( + "index_value->local_descriptors_identifier", + index_value->local_descriptors_identifier, + (uint64_t) 0x9abc ); + + PFF_TEST_ASSERT_EQUAL_UINT32( + "index_value->parent_identifier", + index_value->parent_identifier, + (uint32_t) 0xdef0 ); + + PFF_TEST_ASSERT_IS_NULL( + "error", + error ); + + result = libpff_index_value_free( + &index_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + result = libpff_descriptors_index_free( + &descriptors_index, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + result = libpff_io_handle_free( + &io_handle, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + PFF_TEST_ASSERT_IS_NULL( + "error", + error ); + + return( 1 ); + +on_error: + if( error != NULL ) + { + libcerror_error_free( + &error ); + } + if( index_value != NULL ) + { + libpff_index_value_free( + &index_value, + NULL ); + } + if( recovered_value != NULL ) + { + libpff_index_value_free( + &recovered_value, + NULL ); + } + if( descriptors_index != NULL ) + { + libpff_descriptors_index_free( + &descriptors_index, + NULL ); + } + if( io_handle != NULL ) + { + libpff_io_handle_free( + &io_handle, + NULL ); + } + return( 0 ); +} + #endif /* defined( __GNUC__ ) && !defined( LIBPFF_DLL_IMPORT ) */ /* The main program @@ -102,7 +266,9 @@ int main( /* TODO: add tests for libpff_descriptors_index_set_root_node */ - /* TODO: add tests for libpff_descriptors_index_get_index_value_by_identifier */ + PFF_TEST_RUN( + "libpff_descriptors_index_get_index_value_by_identifier", + pff_test_descriptors_index_get_index_value_by_identifier ); #endif /* defined( __GNUC__ ) && !defined( LIBPFF_DLL_IMPORT ) */ @@ -111,4 +277,3 @@ int main( on_error: return( EXIT_FAILURE ); } - diff --git a/tests/pff_test_offsets_index.c b/tests/pff_test_offsets_index.c index f1203807..91f2aa12 100644 --- a/tests/pff_test_offsets_index.c +++ b/tests/pff_test_offsets_index.c @@ -290,6 +290,7 @@ int pff_test_offsets_index_get_index_value_by_identifier( { libcerror_error_t *error = NULL; libpff_index_value_t *index_value = NULL; + libpff_index_value_t *recovered_value = NULL; libpff_io_handle_t *io_handle = NULL; libpff_offsets_index_t *offsets_index = NULL; int result = 0; @@ -332,30 +333,107 @@ int pff_test_offsets_index_get_index_value_by_identifier( "error", error ); - /* Test regular cases + /* Test retrieving a selected recovered value preserves its location and + * size metadata. */ -/* TODO implement + result = libpff_index_value_initialize( + &recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value->identifier = 0x1234; + recovered_value->file_offset = 4096; + recovered_value->data_size = 512; + recovered_value->reference_count = 7; + + result = libpff_offsets_index_insert_recovered_index_value( + offsets_index, + recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value = NULL; + + result = libpff_index_value_initialize( + &recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value->identifier = 0x1234; + recovered_value->file_offset = 8192; + recovered_value->data_size = 1024; + recovered_value->reference_count = 9; + + result = libpff_offsets_index_insert_recovered_index_value( + offsets_index, + recovered_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); + + recovered_value = NULL; + result = libpff_offsets_index_get_index_value_by_identifier( offsets_index, io_handle, NULL, - 0, - 0, - 0, + 0x1234, + 1, + 1, &index_value, &error ); -PFF_TEST_FPRINT_ERROR( error ); - PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); + PFF_TEST_ASSERT_IS_NOT_NULL( + "index_value", + index_value ); + + PFF_TEST_ASSERT_EQUAL_INT64( + "index_value->file_offset", + index_value->file_offset, + (int64_t) 8192 ); + + PFF_TEST_ASSERT_EQUAL_UINT32( + "index_value->data_size", + index_value->data_size, + 1024 ); + + PFF_TEST_ASSERT_EQUAL_UINT16( + "index_value->reference_count", + index_value->reference_count, + 9 ); + PFF_TEST_ASSERT_IS_NULL( "error", error ); -*/ + + result = libpff_index_value_free( + &index_value, + &error ); + + PFF_TEST_ASSERT_EQUAL_INT( + "result", + result, + 1 ); /* Test error cases */ @@ -425,6 +503,18 @@ PFF_TEST_FPRINT_ERROR( error ); libcerror_error_free( &error ); } + if( index_value != NULL ) + { + libpff_index_value_free( + &index_value, + NULL ); + } + if( recovered_value != NULL ) + { + libpff_index_value_free( + &recovered_value, + NULL ); + } if( offsets_index != NULL ) { libpff_offsets_index_free( @@ -482,4 +572,3 @@ int main( #endif /* defined( __GNUC__ ) && !defined( LIBPFF_DLL_IMPORT ) */ } -