Mocking Opaque Types

Hi guys, another question here regarding Cmock and opaque types. I am currently trying to mock a function with an argument that contains the type "struct bt_conn". However when building this mock, I get the following error:

In file included from c:\nordicsemi\v2.5.0\test\cmock\vendor\unity\src\unity.h:21,
                 from c:\nordicsemi\v2.5.0\test\cmock\src\cmock_internals.h:10,
                 from c:\nordicsemi\v2.5.0\test\cmock\src\cmock.h:10,
                 from C:/PP_Unit_Tests/twister/ProPatch_test/unity.example_test/mocks/LWservice/cmock_lwl_service_support.c:4:
C:/PP_Unit_Tests/twister/ProPatch_test/unity.example_test/mocks/LWservice/cmock_lwl_service_support.c: In function '__cmock_on_sent':
C:/PP_Unit_Tests/twister/ProPatch_test/unity.example_test/mocks/LWservice/cmock_lwl_service_support.c:187:113: error: invalid application of 'sizeof' to incomplete type 'const struct bt_conn'
  187 |       { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(cmock_call_instance->Expected_conn), (void*)(conn), sizeof(const struct bt_conn), cmock_call_instance->Expected_conn_Depth, cmock_line, CMockStringMismatch); }
      |                                                                                                                 ^~~~~
c:\nordicsemi\v2.5.0\test\cmock\vendor\unity\src\unity_internals.h:941:198: note: in definition of macro 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY'
  941 | #define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY)
      |                                                                                                                                                                                                      ^~~

 After doing some digging, it looks like struct bt_conn is an opaque type (I know very little about opaque types). Is there any way I can mock this function despite the use of an opaque type?

Parents
  • Hello,

    Sorry for the delay in responding. I'm also not very familiar with opaque types and Cmock. The log you uploaded indicates there was a build issue because the code attempted to determine the size of a type, const struct bt_conn, which was not fully defined at that point. This problem occurred in the mock function __cmock_on_sent generated by CMock. To potentially resolve issues related to incomplete type definitions, could you try using a pointer to struct bt_conn (struct bt_conn *conn) instead of (struct bt_conn conn)?

    Please let me know if this solution works for you. Otherwise, I will escalate the issue internally to someone with more experience.

    Kind regards,

    Abhijith

  • Hi Abhijith,

    So the actual function call already calls a pointer. Thats on me for not being specific enough. Here's the function:

    void on_sent(struct bt_conn *conn, void *user_data);
    

    I have many more functions that I'll need to test later that also call bt_conn as a pointer.

    I tried defining the argument as not a pointer as well, but still get the same issue. When I try defining the type struct bt_conn within my test file, and even give it some meaningless content, the error still persists. When I brought this up on the discord, they suggested making my own mocks of all of the functions and even mocking the struct itself by hand, however I don't believe this will allow me to continue using Unity they way that I already have been. If you could please escalate this ASAP I would greatly appreciate it.
    -Jesse

  • I also came across this post, but I cannot add a fake definition of bt_conn in the autogenerated cmock header that will exist across builds. 

  • I've come to a temporary solution by defining the struct bt_conn within my source file, with compiler tags around it so that this definition is only used when I'm testing. I have yet to actually test this, but it at least compiles for now.

    #ifdef TESTING
    typedef struct bt_conn {
        int dummy;
    } bt_conn;
    #endif

Reply Children
No Data
Related