|
1 | 1 | #define CATCH_CONFIG_MAIN |
2 | 2 | #include "catch2/catch.hpp" |
3 | | -#include <string> |
4 | | -#include <set> |
5 | | -#include <mutex> |
| 3 | +#include "../FuncTracer.hpp" |
6 | 4 |
|
7 | | -// Updated is_Relevant logic (returns true if function is relevant, i.e., NOT blacklisted) |
8 | | -bool is_Relevant(const std::string& func_name) { |
9 | | - static const std::set<std::string> blacklist = { |
10 | | - "main", "_init", "_start", ".plt.got" |
11 | | - }; |
12 | | - if (func_name.length() >= 4 && func_name.compare(func_name.length() - 4, 4, "@plt") == 0) return false; |
13 | | - if (func_name.length() >= 2 && func_name.compare(0, 2, "__") == 0) return false; |
14 | | - return (blacklist.find(func_name) == blacklist.end()); |
15 | | -} |
16 | | - |
17 | | -// Deduplication logic for testing |
18 | | -static std::set<std::string> logged_functions; |
19 | | -static std::mutex log_mutex; |
20 | | - |
21 | | -bool log_function_call_test(const char* img_name, const char* func_name) |
22 | | -{ |
23 | | - std::string key = std::string(img_name) + ":" + std::string(func_name); |
24 | | - { |
25 | | - std::lock_guard<std::mutex> guard(log_mutex); |
26 | | - if (logged_functions.find(key) != logged_functions.end()) |
27 | | - return false; // Already logged, skip |
28 | | - logged_functions.insert(key); |
29 | | - } |
30 | | - return true; // Logged for the first time |
31 | | -} |
32 | | - |
33 | | -TEST_CASE("is_Relevant works as expected") { |
| 5 | +TEST_CASE("func_is_relevant works as expected") { |
34 | 6 | SECTION("PLT functions are not relevant") { |
35 | | - REQUIRE_FALSE(is_Relevant("foo@plt")); |
36 | | - REQUIRE_FALSE(is_Relevant("bar@plt")); |
| 7 | + REQUIRE_FALSE(func_is_relevant("foo@plt")); |
| 8 | + REQUIRE_FALSE(func_is_relevant("bar@plt")); |
37 | 9 | } |
38 | 10 | SECTION("Functions starting with __ are not relevant") { |
39 | | - REQUIRE_FALSE(is_Relevant("__internal")); |
40 | | - REQUIRE_FALSE(is_Relevant("__something")); |
| 11 | + REQUIRE_FALSE(func_is_relevant("__internal")); |
| 12 | + REQUIRE_FALSE(func_is_relevant("__something")); |
41 | 13 | } |
42 | 14 | SECTION("Explicit blacklist") { |
43 | | - REQUIRE_FALSE(is_Relevant("main")); |
44 | | - REQUIRE_FALSE(is_Relevant("_init")); |
45 | | - REQUIRE_FALSE(is_Relevant("_start")); |
46 | | - REQUIRE_FALSE(is_Relevant(".plt.got")); |
| 15 | + REQUIRE_FALSE(func_is_relevant("main")); |
| 16 | + REQUIRE_FALSE(func_is_relevant("_init")); |
| 17 | + REQUIRE_FALSE(func_is_relevant("_start")); |
| 18 | + REQUIRE_FALSE(func_is_relevant(".plt.got")); |
47 | 19 | } |
48 | 20 | SECTION("Normal functions are relevant") { |
49 | | - REQUIRE(is_Relevant("foo")); |
50 | | - REQUIRE(is_Relevant("bar")); |
51 | | - REQUIRE(is_Relevant("baz")); |
| 21 | + REQUIRE(func_is_relevant("foo")); |
| 22 | + REQUIRE(func_is_relevant("bar")); |
| 23 | + REQUIRE(func_is_relevant("baz")); |
52 | 24 | } |
53 | 25 | SECTION("Short names are relevant") { |
54 | | - REQUIRE(is_Relevant("a")); |
55 | | - REQUIRE(is_Relevant("b@p")); |
56 | | - REQUIRE(is_Relevant("_m")); |
| 26 | + REQUIRE(func_is_relevant("a")); |
| 27 | + REQUIRE(func_is_relevant("b@p")); |
| 28 | + REQUIRE(func_is_relevant("_m")); |
57 | 29 | } |
58 | 30 | } |
59 | 31 |
|
60 | | -TEST_CASE("log_function_call deduplicates function calls") { |
61 | | - // Clear the set before testing |
62 | | - logged_functions.clear(); |
63 | | - |
64 | | - SECTION("First call logs, second call skips") { |
65 | | - REQUIRE(log_function_call_test("img1", "funcA") == true); // First call, should log |
66 | | - REQUIRE(log_function_call_test("img1", "funcA") == false); // Second call, should skip |
67 | | - } |
68 | | - |
69 | | - SECTION("Different functions are logged separately") { |
70 | | - REQUIRE(log_function_call_test("img1", "funcB") == true); |
71 | | - REQUIRE(log_function_call_test("img1", "funcC") == true); |
72 | | - REQUIRE(log_function_call_test("img1", "funcB") == false); |
73 | | - REQUIRE(log_function_call_test("img1", "funcC") == false); |
74 | | - } |
75 | | - |
76 | | - SECTION("Same function name in different images are logged separately") { |
77 | | - REQUIRE(log_function_call_test("img1", "funcD") == true); |
78 | | - REQUIRE(log_function_call_test("img2", "funcD") == true); |
79 | | - REQUIRE(log_function_call_test("img1", "funcD") == false); |
80 | | - REQUIRE(log_function_call_test("img2", "funcD") == false); |
81 | | - } |
82 | | -} |
0 commit comments