Cmockery macro demo hacking
/*********************************************************************
* Cmockery macro demo hacking
* 说明:
* 本文记录对Cmockery的宏使用的示例进行测试、跟踪。
*
* 2016-5-7 深圳 南山平山村 曾剑锋
********************************************************************/ 一、cat src/example/assert_macro.c
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h> static const char* status_code_strings[] = {
"Address not found",
"Connection dropped",
"Connection timed out",
}; const char* get_status_code_string(const unsigned int status_code) {
return status_code_strings[status_code];
}; unsigned int string_to_status_code(const char* const status_code_string) {
unsigned int i;
for (i = ; i < sizeof(status_code_strings) /
sizeof(status_code_strings[]); i++) {
if (strcmp(status_code_strings[i], status_code_string) == ) {
return i;
}
}
return ~0U;
} 二、cat src/example/assert_macro_test.c
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmockery.h> extern const char* get_status_code_string(const unsigned int status_code);
extern unsigned int string_to_status_code(
const char* const status_code_string); /* This test will fail since the string returned by get_status_code_string(0)
* doesn't match "Connection timed out". */
void get_status_code_string_test(void **state) {
assert_string_equal(get_status_code_string(), "Address not found"); --+
assert_string_equal(get_status_code_string(), "Connection timed out"); |
} |
|
// This test will fail since the status code of "Connection timed out" isn't 1 |
void string_to_status_code_test(void **state) { |
assert_int_equal(string_to_status_code("Address not found"), ); --*-+
assert_int_equal(string_to_status_code("Connection timed out"), ); | |
} | |
| |
int main(int argc, char *argv[]) { | |
const UnitTest tests[] = { | |
unit_test(get_status_code_string_test), | |
unit_test(string_to_status_code_test), | |
}; | |
return run_tests(tests); | |
} | |
| |
三、macro hacking | |
| |
// Assert that the two given strings are equal, otherwise fail. | |
#define assert_string_equal(a, b) \ <-----+ |
_assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \ ------+ |
__LINE__) | |
| |
void _assert_string_equal(const char * const a, const char * const b, <-----+ |
const char * const file, const int line) { |
if (!string_equal_display_error(a, b)) { ------+ |
_fail(file, line); | |
} | |
} | |
| |
/* Determine whether the specified strings are equal. If the strings are equal | |
* 1 is returned. If they're not equal an error is displayed and 0 is | |
* returned. */ | |
static int string_equal_display_error( <-----+ |
const char * const left, const char * const right) { ------+ |
if (strcmp(left, right) == ) { | |
return ; | |
} | |
print_error("\"%s\" != \"%s\"\n", left, right); | |
return ; | |
} | |
| |
// Assert that the two given integers are equal, otherwise fail. | |
#define assert_int_equal(a, b) \ <---------*-+
_assert_int_equal(cast_to_largest_integral_type(a), \ ------+ |
cast_to_largest_integral_type(b), \ | |
__FILE__, __LINE__) | |
| |
void _assert_int_equal( <-----+ |
const LargestIntegralType a, const LargestIntegralType b, |
const char * const file, const int line) { |
if (!values_equal_display_error(a, b)) { ----------+
_fail(file, line); ----------*-+
} | |
} | |
| |
/* Returns 1 if the specified values are equal. If the values are not equal | |
* an error is displayed and 0 is returned. */ | |
static int values_equal_display_error(const LargestIntegralType left, | |
const LargestIntegralType right) { | |
const int equal = left == right; | |
if (!equal) { | |
print_error(LargestIntegralTypePrintfFormat " != " | |
LargestIntegralTypePrintfFormat "\n", left, right); | |
} | |
return equal; | |
} | |
| |
void print_error(const char* const format, ...) { <-------+ |
va_list args; |
va_start(args, format); |
vprint_error(format, args); --------+ |
va_end(args); | |
} | |
| |
void vprint_error(const char* const format, va_list args) { <-------+ |
char buffer[]; |
vsnprintf(buffer, sizeof(buffer), format, args); |
fprintf(stderr, buffer); |
#ifdef _WIN32 |
OutputDebugString(buffer); |
#endif // _WIN32 |
} |
|
void _fail(const char * const file, const int line) { <--------+
print_error("ERROR: " SOURCE_LOCATION_FORMAT " Failure!\n", file, line);
exit_test(); --------+
} |
|
// Exit the currently executing test. |
static void exit_test(const int quit_application) { <------+
if (global_running_test) {
longjmp(global_run_test_env, );
} else if (quit_application) {
exit(-);
}
} 四、运行结果:
myzr@myzr:~/c_program/cmockery-master/src/example$ gcc assert_macro* -lcmockery
myzr@myzr:~/c_program/cmockery-master/src/example$ ./a.out
get_status_code_string_test: Starting test
"Connection dropped" != "Connection timed out"
ERROR: assert_macro_test.c: Failure!
get_status_code_string_test: Test failed.
string_to_status_code_test: Starting test
!=
ERROR: assert_macro_test.c: Failure!
string_to_status_code_test: Test failed.
out of tests failed!
get_status_code_string_test
string_to_status_code_test
myzr@myzr:~/c_program/cmockery-master/src/example$
Cmockery macro demo hacking的更多相关文章
- Qt QML referenceexamples attached Demo hacking
/********************************************************************************************* * Qt ...
- linux watchdog demo hacking
/********************************************************************** * linux watchdog demo hackin ...
- linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking ...
- Linux SocketCan client server demo hacking
/*********************************************************************** * Linux SocketCan client se ...
- am335x Qt SocketCAN Demo hacking
/*********************************************************************************** * am335x Qt Soc ...
- Linux watchdog
使用 watchdog 构建高可用性的 Linux 系统及应用https://www.ibm.com/developerworks/cn/linux/l-cn-watchdog/index.html ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- Android Mokoid Open Source Project hacking
/***************************************************************************** * Android Mokoid Open ...
- 黑客讲述渗透Hacking Team全过程(详细解说)
近期,黑客Phineas Fisher在pastebin.com上讲述了入侵Hacking Team的过程,以下为其讲述的原文情况,文中附带有相关文档.工具及网站的链接,请在安全环境下进行打开,并合理 ...
随机推荐
- 请教DotNetBar控件中的CalendarView控件如何拖动当前的时间轴
本人想拖动那个当前的时间轴或者让时间轴变动,因为那个时间轴默认的是当前时间.(就是那个黄色的线)
- node.js的npm详解
一.什么是npm呢 npm(Node Package Manager,node包管理器)是node的包管理器,他允许开发人员在node.js应用程序中创建,共享并重用模块.模块就是可以在不同的项目中重 ...
- oci.dll文件是用来干嘛的? 如果没有安装ORACLE客户端提示oci.dll未加载
oracle数据库开发编程中,没有找到oci.dll,一般是系统的 path 设置有问题, 查找oci.dll, 然后加入到系统路径.oci.dll 可下载解压到系统盘的system32目录下.然后打 ...
- C语言标准
1,K&R C 1978年, Dennis Ritchie和Brian Wilson Kernighan合作出版了<The C Programming Language>的第一版. ...
- iOS开发--泛型
一. 泛型函数 1.单一占位符泛型函数 下面就使用一个经典案例:两个数值进行交换.来使用泛型,写一个通用的函数,这个函数的功能就是交换两个变量的值.在Swift中不允许类型隐式转换,也就是说,如果你定 ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- AppDomain 应用程序域
应用程序域 一.什么是应用程序域? 应用程序域 (application domain) (AppDomain) 一种边界,它由公共语言运行库围绕同一应用程序范围内创建的对象建立(即,从应用程序入口点 ...
- ajax练习四留言板
留言界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- JavaScript DOM实战:创建和克隆元素
DOM来创建和克隆元素. createElement()和createTextNode() createElement()和createTextNode()做的事情正如它们的名字所说的那样.最常见的J ...
- C# 中通过API实现的打印类
using System;using System.Collections;using System.Text;using System.Runtime.InteropServices; using ...