首页 > 解决方案 > __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 使用模拟:

#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)

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

标签: cunit-testingcmocka

解决方案


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

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);
}

推荐阅读