ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
653 stars 269 forks source link

How to mock weak symbols with CMock? #391

Closed janaranjanahl closed 2 years ago

janaranjanahl commented 2 years ago

I am trying to mock functions from sys/socket.h. Where create a socket with socket(...), close a socket close(...) . #include <net/if.h> header has sys/socket.h and sys/types.h included

This is my implementation

can.c

int8_t close_socket(int32_t sock_id){
    printf("START can_close_socket\n");
    int8_t status = close(sock_id);
    if (status < 0) {
        perror("Close");
        return FAILURE;
    }
    printf("END can_close_socket\n");
    return SUCCESS;
}

can.h

#include <unistd.h>
#include <net/if.h>

can_test.c I have not included following headers just yet for this test

//#include "mock_socket.h"
//#include "mock_types.h"
//#include "mock_unistd.h"
void test_can_close_socket(void){
    TEST_ASSERT_EQUAL(-1, close_socket(-1));
}

when I run the test

TEST OUTPUT

[test_can.c]

well. it works with real interface.

But I need to mock symbolic function - close(socket_id)

For that I tried added back the commented headers in test_can.c. I am getting following error

now test_can.c look like below

#ifdef CEEDLING_TESTS
#include "unity.h"
#include "can.h"
#include "logger.h"
#include "mock_libsocketcan.h"
#include "mock_pthread.h"
#include "mock_if.h"
#include "mock_socket.h"
#include "mock_types.h"
#include "mock_unistd.h"

void setUp(void)
{

}

void tearDown(void)
{

}

void test_can_close_socket(void){

    TEST_ASSERT_EQUAL(-1, close_socket(-1));
}

Now I am trying to execute the test with following command ceedling clobber; ceedling test:test_can.c

Test 'test_can.c'

Generating include list for libsocketcan.h... Generating include list for pthread.h... ERROR: Found no file 'socket.h' in search paths. rake aborted!

this is my project.yml file looks like

---
:project:
  :use_exceptions: FALSE
  :use_test_preprocessor: TRUE
  :use_auxiliary_dependencies: TRUE
  :build_root: build
  :test_file_prefix: test_
  :which_ceedling: gem
  :ceedling_version: 0.31.1
  :default_tasks:
    - test:all
:environment:
:extension:
  :executable: .out
:paths:
  :test:
    - +:test/**
  :source:
    - src/**

  :include:
    - include/**   
  :support:
  :libraries: []
:files:
  :include:
    - +:/usr/include/**
:defines:
  :common: &common_defines []
  :test:
    - *common_defines
    - CEEDLING_TESTS
  :test_preprocess:
    - *common_defines
    - CEEDLING_TESTS
:cmock:
  :mock_prefix: mock_
  :when_no_prototypes: :warn
  :enforce_strict_ordering: TRUE
  :weak: __attribute ((weak))
  :unity_helper_path: test/support/unity_helper.h
  :plugins:
    - :ignore
    - :callback
  :treat_as:
    uint8:     HEX8
    uint16:    HEX16
    uint32:    UINT32
    int8:      INT8
    bool:      UINT8

:files: :include:

janaranjanahl commented 2 years ago

Solution is here

https://github.com/ThrowTheSwitch/Ceedling/issues/673