@@ -1767,31 +1767,36 @@ TEST_CASE("ebpf_verification_apis", "[ebpf_api]")
17671767// Test eBPF object management APIs
17681768TEST_CASE (" ebpf_object_apis" , " [ebpf_api]" )
17691769{
1770- // Test object_unpin with invalid path - should fail gracefully
1771- ebpf_result_t result = ebpf_object_unpin ( " /invalid/path/that/does/not/exist " );
1772- REQUIRE (result != EBPF_SUCCESS );
1770+ // Create a simple map object.
1771+ fd_t map_fd = bpf_map_create (BPF_MAP_TYPE_ARRAY, " test_map_pin " , sizeof ( uint32_t ), sizeof ( uint32_t ), 1 , nullptr );
1772+ REQUIRE (map_fd > 0 );
17731773
1774- // Test get_pinned_map_info
1774+ // Pin the map object.
1775+ const char * pin_path = " BPF:\\ test_map_pin" ;
1776+ int pin_result = bpf_obj_pin (map_fd, pin_path);
1777+ REQUIRE (pin_result == 0 );
1778+
1779+ // Call ebpf_api_get_pinned_map_info to get pinned map info.
17751780 uint16_t map_count = 0 ;
17761781 ebpf_map_info_t * map_info = nullptr ;
1777- result = ebpf_api_get_pinned_map_info (&map_count, &map_info);
1782+ ebpf_result_t result = ebpf_api_get_pinned_map_info (&map_count, &map_info);
1783+ REQUIRE (result == EBPF_SUCCESS);
1784+ REQUIRE (map_count > 0 );
1785+ REQUIRE (map_info != nullptr );
17781786
1779- // Should succeed even if no maps are pinned (count would be 0)
1780- if (result == EBPF_SUCCESS) {
1781- // Clean up if we got any map info
1782- if (map_info != nullptr ) {
1783- ebpf_api_map_info_free (map_count, map_info);
1784- }
1785- }
1787+ // Clean up pinned map info returned by the API.
1788+ ebpf_api_map_info_free (map_count, map_info);
1789+
1790+ // Unpin the map object.
1791+ result = ebpf_object_unpin (pin_path);
1792+ REQUIRE (result == EBPF_SUCCESS);
17861793
1787- // Test api_close_handle with invalid handle - should fail gracefully
1788- result = ebpf_api_close_handle (( ebpf_handle_t ) 0 );
1794+ // Verify that unpinning the object a second time fails.
1795+ result = ebpf_object_unpin (pin_path );
17891796 REQUIRE (result != EBPF_SUCCESS);
17901797
1791- // Test invalid handle
1792- result = ebpf_api_close_handle ((ebpf_handle_t )-1 );
1793- // -6 through -1 are NT pseudo-handles and are expected to silently fail.
1794- REQUIRE (result == EBPF_SUCCESS);
1798+ // Close the map fd.
1799+ _close (map_fd);
17951800}
17961801
17971802// Test eBPF pinned object path APIs
@@ -1800,24 +1805,32 @@ TEST_CASE("ebpf_pinned_path_apis", "[ebpf_api]")
18001805 char next_path[EBPF_MAX_PIN_PATH_LENGTH];
18011806 ebpf_object_type_t object_type = EBPF_OBJECT_UNKNOWN;
18021807
1803- // Test get_next_pinned_object_path starting from empty string
1804- ebpf_result_t result = ebpf_get_next_pinned_object_path (" " , next_path, sizeof (next_path), &object_type);
1808+ // 1) Create and pin a map.
1809+ const char * pin_path = " BPF:\\ test_get_next_pinned_object_path" ;
1810+ fd_t map_fd = bpf_map_create (
1811+ BPF_MAP_TYPE_ARRAY, " test_get_next_pinned_object_path" , sizeof (uint32_t ), sizeof (uint32_t ), 1 , nullptr );
1812+ REQUIRE (map_fd > 0 );
18051813
1806- // Should succeed (even if no objects are pinned) or fail gracefully
1807- REQUIRE ((result == EBPF_SUCCESS || result == EBPF_NO_MORE_KEYS || result != EBPF_SUCCESS) );
1814+ int pin_result = bpf_obj_pin (map_fd, pin_path);
1815+ REQUIRE (pin_result == 0 );
18081816
1809- if (result == EBPF_SUCCESS) {
1810- // If we got a path, it should be valid
1811- REQUIRE (strlen (next_path) > 0 );
1812- // Object type should be set to something valid
1813- REQUIRE (object_type != EBPF_OBJECT_UNKNOWN);
1814- }
1817+ // 2) Verify the map can be found via ebpf_get_next_pinned_object_path
1818+ ebpf_result_t result = ebpf_get_next_pinned_object_path (" " , next_path, sizeof (next_path), &object_type);
1819+ REQUIRE (result == EBPF_SUCCESS);
1820+ REQUIRE (object_type == EBPF_OBJECT_MAP);
1821+ // Ensure returned path contains our pin name.
1822+ REQUIRE (strstr (next_path, " test_get_next_pinned_object_path" ) != nullptr );
1823+
1824+ // 3) Verify that there are no more pinned objects after the current one
1825+ result = ebpf_get_next_pinned_object_path (
1826+ " test_get_next_pinned_object_path" , next_path, sizeof (next_path), &object_type);
1827+ REQUIRE (result == EBPF_NO_MORE_KEYS);
18151828
1816- // Test with a specific starting path that likely doesn't exist
1817- result = ebpf_get_next_pinned_object_path (" /non/existent/path" , next_path, sizeof (next_path), &object_type);
1829+ // 4) Unpin and free the map
1830+ ebpf_result_t unpin_result = ebpf_object_unpin (pin_path);
1831+ REQUIRE (unpin_result == EBPF_SUCCESS);
18181832
1819- // Should handle gracefully - either succeed with next path or report no more keys
1820- REQUIRE ((result == EBPF_SUCCESS || result == EBPF_NO_MORE_KEYS || result != EBPF_SUCCESS));
1833+ _close (map_fd);
18211834}
18221835
18231836// Test eBPF program synchronization API
@@ -1869,8 +1882,8 @@ TEST_CASE("ebpf_perf_event_array_api", "[ebpf_api]")
18691882 const char test_data[] = " test perf event data" ;
18701883 ebpf_result_t result = ebpf_perf_event_array_map_write (map_fd, test_data, sizeof (test_data));
18711884
1872- // Should either succeed or fail gracefully (e.g., if no consumers are attached)
1873- REQUIRE (( result == EBPF_SUCCESS || result != EBPF_SUCCESS) );
1885+ // Should succeed
1886+ REQUIRE (result == EBPF_SUCCESS);
18741887
18751888 (void )ebpf_close_fd (map_fd);
18761889 }
@@ -1883,32 +1896,31 @@ TEST_CASE("ebpf_object_info_api", "[ebpf_api]")
18831896
18841897 // Create a simple map to test object info API
18851898 fd_t map_fd = bpf_map_create (BPF_MAP_TYPE_ARRAY, " test_map" , sizeof (uint32_t ), sizeof (uint32_t ), 1 , nullptr );
1886- if (map_fd > 0 ) {
1887- struct bpf_map_info info = {0 };
1888- uint32_t info_size = sizeof (info);
1889- ebpf_object_type_t object_type;
1899+ REQUIRE (map_fd > 0 );
18901900
1891- ebpf_result_t result = ebpf_object_get_info_by_fd (map_fd, &info, &info_size, &object_type);
1892- REQUIRE (result == EBPF_SUCCESS);
1893- REQUIRE (object_type == EBPF_OBJECT_MAP);
1894- REQUIRE (info_size > 0 );
1895- REQUIRE (info.type == BPF_MAP_TYPE_ARRAY);
1896- REQUIRE (info.key_size == sizeof (uint32_t ));
1897- REQUIRE (info.value_size == sizeof (uint32_t ));
1898- REQUIRE (info.max_entries == 1 );
1899- REQUIRE (info.id != 0 );
1900- REQUIRE (std::string (info.name ) == " test_map" );
1901+ struct bpf_map_info info = {0 };
1902+ uint32_t info_size = sizeof (info);
1903+ ebpf_object_type_t object_type;
19011904
1902- (void )ebpf_close_fd (map_fd);
1903- }
1905+ ebpf_result_t result = ebpf_object_get_info_by_fd (map_fd, &info, &info_size, &object_type);
1906+ REQUIRE (result == EBPF_SUCCESS);
1907+ REQUIRE (object_type == EBPF_OBJECT_MAP);
1908+ REQUIRE (info_size > 0 );
1909+ REQUIRE (info.type == BPF_MAP_TYPE_ARRAY);
1910+ REQUIRE (info.key_size == sizeof (uint32_t ));
1911+ REQUIRE (info.value_size == sizeof (uint32_t ));
1912+ REQUIRE (info.max_entries == 1 );
1913+ REQUIRE (info.id != 0 );
1914+ REQUIRE (std::string (info.name ) == " test_map" );
1915+
1916+ (void )ebpf_close_fd (map_fd);
19041917
19051918 // Test with invalid fd
1906- uint32_t info_size = 0 ;
1907- ebpf_object_type_t object_type;
1908- ebpf_result_t result = ebpf_object_get_info_by_fd (-1 , nullptr , &info_size, &object_type);
1919+ result = ebpf_object_get_info_by_fd (-1 , nullptr , &info_size, &object_type);
19091920 REQUIRE (result != EBPF_SUCCESS);
19101921}
19111922
1923+ #if !defined(CONFIG_BPF_JIT_DISABLED) || !defined(CONFIG_BPF_INTERPRETER_DISABLED)
19121924// Test eBPF program attach APIs with graceful error handling
19131925TEST_CASE (" ebpf_program_attach_apis_basic" , " [ebpf_api]" )
19141926{
@@ -1942,6 +1954,7 @@ TEST_CASE("ebpf_program_attach_apis_basic", "[ebpf_api]")
19421954 result = ebpf_program_attach_by_fd (-1 , &sample_attach_type, nullptr , 0 , &link);
19431955 REQUIRE (result != EBPF_SUCCESS);
19441956}
1957+ #endif
19451958
19461959// Test eBPF native object loading API
19471960TEST_CASE (" ebpf_object_load_native_api" , " [ebpf_api]" )
@@ -2033,13 +2046,8 @@ TEST_CASE("ebpf_get_next_pinned_program_path_deprecated", "[ebpf_api]")
20332046 ebpf_result_t result = ebpf_get_next_pinned_program_path (" " , next_path);
20342047#pragma warning(pop)
20352048
2036- // Should succeed (even if no programs are pinned) or fail gracefully
2037- REQUIRE ((result == EBPF_SUCCESS || result == EBPF_NO_MORE_KEYS || result != EBPF_SUCCESS));
2038-
2039- if (result == EBPF_SUCCESS) {
2040- // If we got a path, it should be valid
2041- REQUIRE (strlen (next_path) > 0 );
2042- }
2049+ // Should return EBPF_NO_MORE_KEYS as no objects are pinned
2050+ REQUIRE (result == EBPF_NO_MORE_KEYS);
20432051}
20442052
20452053// Test eBPF memory-based verification APIs
0 commit comments