c-__wrap_strcpy()有剩余的未返回值



我是新手,使用cmocka进行单元测试时遇到问题。我有以下功能:

#include <string.h>
#include <stdlib.h>
#include "function.h"

void copy_ip_addr(char *ip_addr_ascii)
{
char ip_ascii[50];
strcpy(ip_ascii, ip_addr_ascii);
}

我想为该函数编写单元测试,并对strcpy使用mock,如下所示:

#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmocka.h"
#include <stdio.h>
#include <string.h>
#include "function.h"

char* __real_strcpy(char *dest,const char *src);
char* __wrap_strcpy(char *dest,const char *src){
check_expected(src);
return (char*)mock();
}
static void test_convert_ip_addr(void **state){
(void) state; /* unused */
expect_string(__wrap_strcpy,src,"abc"); // line 42
will_return(__wrap_strcpy,"abc");       // line 43
}
const struct CMUnitTest test_copy_ip_addr_group[] = {
cmocka_unit_test(test_convert_ip_addr),
};
int main()
{
return cmocka_run_group_tests(test_copy_ip_addr_group, NULL, NULL);
}
gcc -g function.c test_function.c -Wl,--wrap=strcpy -o test -l cmocka

程序给出的错误如下:

[==========] Running 1 test(s).
[ RUN      ] test_convert_ip_addr
__wrap_strcpy() has remaining non-returned values.
test_function.c:43: note: remaining item was declared here
__wrap_strcpy.src parameter still has values that haven't been checked.
test_function.c:42: note: remaining item was declared here
[  FAILED  ] test_convert_ip_addr
[==========] 1 test(s) run.
[  PASSED  ] 0 test(s).
[  FAILED  ] 1 test(s), listed below:
[  FAILED  ] test_convert_ip_addr
1 FAILED TEST(S)

我应该如何修复它以获得准确的结果?

问题是您没有在测试函数中调用被测函数。你应该这样做:

static void test_convert_ip_addr(void **state){
(void) state; /* unused */
char* ip_addr_ascii = "abc";
expect_string(__wrap_strcpy,src,"abc"); // line 42
will_return(__wrap_strcpy,"abc");       // line 43
copy_ip_addr(ip_addr_ascii);
}

相关内容

  • 没有找到相关文章

最新更新