Fundamentals 11 min read

Frida-gum Based Non-intrusive C++ Mock Framework

This open‑source C++ mock framework leverages Frida‑gum to replace any function—including static, member, virtual, and library calls—at runtime without source changes, offering simple MOCK and MOCK_RETURN macros, call‑count expectations, automatic scope‑based rollback, and cross‑platform support for reliable, non‑intrusive unit testing.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Frida-gum Based Non-intrusive C++ Mock Framework

In unit testing, external dependencies such as network or database calls need to be isolated. Existing C++ mock tools are few and often rely on polymorphism (e.g., gmock), requiring code changes. To achieve truly non‑intrusive mocking, we built a simple C++ mock utility on top of the open‑source Frida‑gum hook framework, using C++ templates.

Frida is a dynamic instrumentation tool that can inject code into native apps on Windows, macOS, Linux, Android, and iOS. Our mock tool leverages Frida’s ability to replace function implementations at runtime.

Implemented Features

The tool supports function replacement for all function types: member functions, virtual functions, static functions, global functions from system libraries, and functions from shared libraries. Anonymous functions are not supported because a function pointer is required.

Macros

MOCK(target, alter)

The first argument is the function pointer to be mocked; the second argument is a lambda or function pointer that provides the replacement.

MOCK_RETURN(target, alter)

A wrapper of MOCK that only replaces the return value of the target function (value types only).

ExpectTimes

MOCK_RETURN(&MyTest::testMember, 0)->ExpectTimes(1); // test run count

Specifies the expected number of calls; the test fails if the actual count differs.

Automatic Rollback

TEST(mock, mock脱离作用域自动失效) {
    int input = 10; // input
    {
        MOCK_RETURN(&globalFunction, 100);
        ASSERT_EQ(globalFunction(input), 100); // modified return
    }
    ASSERT_EQ(globalFunction(input), input); // mock reverted
}

The mock is automatically reverted when the MockHandler object goes out of scope, keeping test cases independent.

Cross‑Platform Support

Works on macOS, Linux, Windows, Android, and iOS.

Usage Example

Define a class to be mocked:

class MyTest {
public:
    static int testStatic() { return 0; }
    int testMember(int input) { return input; }
    int constMemberFun() const { return 0; }
    static int overloadFunction(int intInput) { return 0; }
    static int overloadFunction(double doubleInput) { return 0; }
};

Mock a static function:

// Change testStatic return value to 10
MOCK(&MyTest::testStatic, []() { return 10; });
MOCK_RETURN(&MyTest::testStatic, 10);

Mock a member function:

// Increment the original return value by 1
MOCK(&MyTest::testMember, [](auto&& self, auto&& input) {
    return input + 1;
});
int input = 10;
ASSERT_EQ(MyTest().testMember(input), input + 1);

Mock an overloaded function using static_cast to select the correct overload:

MOCK(static_cast
(&MyTest::overloadFunction), [](...args) { return 100; });
ASSERT_EQ(MyTest().overloadFunction(10), 100);
ASSERT_EQ(MyTest().overloadFunction(10.0), 0);

When to Use Mock

Use this tool when the test does not care about certain external functionalities (e.g., DB access, network requests), when the test depends on complex data sources, when functions are non‑idempotent or unstable, when reproducing error states is difficult, or when you need to verify intermediate values.

Precautions

Member functions receive the this pointer as the first argument.

Exceptions thrown inside the mock cannot be caught by the original caller.

Line numbers may be mismatched during debugging; removing macro wrappers can help.

Avoid enabling compiler optimizations in the test project, as they may prevent mocking.

Frida may be reported as a memory leak by leak detection tools.

When mocking a dynamic library, compile it with -fPIC .

References

Frida GitHub: https://github.com/frida/frida

Frida working principle: https://www.wangan.com/p/7fy7f8bd4ab57950

Frida‑gum code reading notes: https://o0xmuhe.github.io/2019/11/15/frida-gum%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/

FRIDA‑GUM source analysis: https://jmpews.github.io/2017/06/27/pwn/frida-gum%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB/

cross-platformC++unit testingmockingFridaHook
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.