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 使用模拟:
#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);
}
推荐阅读
- python - 格式化多维数组 Numpy Python
- json - 有没有办法以编程方式读取 Reddit 图库中的所有图像 URL?
- c# - 带有 C# 应用程序的 Microchip Harmony CRYPT ECC 公钥
- scala - 为什么 checkpoint() 比 persist() 快
- excel - ClearContents 会影响其旁边的其他单元格中的公式
- python - 使用数据帧时如何在python中返回元组列表?
- javascript - 如何确定是什么工具创建了如下所示的 javascript 代码?
- azure - Azure Logic App Customer Connector 无法使用 oAuth2(震惊!) - 这次使用 Pinterest
- javascript - 为什么输入值是这种形式的空字符串?
- kubernetes - 尝试使用 EKS 集群将 configmap 应用于身份验证时出错