未验证 提交 3403ce44 编写于 作者: O openharmony_ci 提交者: Gitee

!99 Modified the Fuzzer's File

Merge pull request !99 from stivn/master
......@@ -19,7 +19,7 @@ module_output_path = "developertest/calculator"
##############################fuzztest##########################################
ohos_fuzztest("CalculatorFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "//test/developertest/examples/calculator/test/fuzztest/common/Calculator_fuzzer"
include_dirs = []
cflags = [
"-g",
......@@ -27,7 +27,7 @@ ohos_fuzztest("CalculatorFuzzTest") {
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "parse_fuzzer.cpp" ]
sources = [ "Calculator_fuzzer.cpp" ]
}
###############################################################################
......
......@@ -13,9 +13,9 @@
* limitations under the License.
*/
#ifndef _EXAMPLE_CALCULATOR_PARSE_FUZZER_H_
#define _EXAMPLE_CALCULATOR_PARSE_FUZZER_H_
#ifndef EXAMPLE_CALCULATOR_FUZZER_H_
#define EXAMPLE_CALCULATOR_FUZZER_H_
#define FUZZ_PROJECT_NAME "parse_fuzzer"
#define FUZZ_PROJECT_NAME "Calculator_fuzzer"
#endif // _EXAMPLE_CALCULATOR_PARSE_FUZZER_H_
\ No newline at end of file
#endif // EXAMPLE_CALCULATOR_FUZZER_H_
\ No newline at end of file
......@@ -53,12 +53,12 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
执行gen命令用于fuzzer源文件生成,会自动生成fuzzer源文件、fuzzer配置文件和corpus语料,目录结构如下
```
parse_fuzzer/
Calculator_fuzzer/
├── corpus # Fuzz语料目录
│ ├── init # Fuzz语料
├── BUILD.gn # Fuzz用例编译配置
├── parse_fuzzer.cpp # Fuzz用例源文件
├── parse_fuzzer.h # Fuzz用例头文件
├── Calculator_fuzzer.cpp # Fuzz用例源文件
├── Calculator_fuzzer.h # Fuzz用例头文件
├── project.xml # Fuzz选项配置文件
```
......@@ -71,13 +71,13 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
| 参数 | 描述 | 说明 | 备注 |
| ---- | ---------- | -------------- | ------------------------------------------ |
| -t | testtype | 测试类型 | 目前仅支持"FUZZ" |
| -fn | fuzzername | fuzzer名称 | 为显式区分Fuzz用例,名称必须以"fuzzer"结尾 |
| -fn | fuzzername | fuzzer名称 | 为显式区分Fuzz用例,名称必须以测试套前缀 + _fuzzer形式命名 |
| -dp | dirpath | fuzzer生成路径 | 路径不存在则自动创建目录 |
3. gen命令示例,-t、-fn和-dp均为必选项
```
gen -t FUZZ -fn parse_fuzzer -dp test/developertest/example/calculator/test/fuzztest/common
gen -t FUZZ -fn Calculator_fuzzer -dp test/developertest/example/calculator/test/fuzztest/common
```
执行完毕后会在test/developertest/example/calculator/test/fuzztest/common目录下生成一个Fuzz用例demo。
......@@ -100,7 +100,7 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
![img](../../public_sys-resources/icon-note.gif) **说明:** DoSomethingInterestingWithMyAPI接口名称允许依据业务逻辑修改。两接口参数data和size为fuzz测试标准化参数,不可修改。
```
#include "parse_fuzzer.h"
#include "Calculator_fuzzer.h"
#include <stddef.h>
#include <stdint.h>
......@@ -143,7 +143,7 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
```
ohos_fuzztest("CalculatorFuzzTest") { #定义测试套名称CalculatorFuzzTest
module_out_path = module_output_path
fuzz_config_file = "//test/developertest/examples/calculator/test/fuzztest/common/Calculator_fuzzer"
include_dirs = []
cflags = [
"-g",
......@@ -151,7 +151,7 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "parse_fuzzer.cpp" ]
sources = [ "Calculator_fuzzer.cpp" ]
}
```
......@@ -209,7 +209,7 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or
testonly = true
deps = []
deps += [ "fuzztest/common/parse_fuzzer:fuzztest" ]
deps += [ "fuzztest/common/Calculator_fuzzer:fuzztest" ]
}
```
......@@ -233,55 +233,29 @@ run -t FUZZ -ss developertest -tm calculator
Windows环境可通过归档DTFuzz用例配置文件project.xml、语料corpus和可执行文件执行DTFuzz。
1. 归档用例配置文件、语料
1. 归档用例配置文件、语料以及用例可执行文件
新建目录,如:
```
D:\test\res\parse_fuzzer
D:\test\tests
```
parse_fuzzer用例的配置文件project.xml、语料corpus拷贝到该路径下。如有多个需要执行的用例,在res目录下新建多个xxx_fuzzer目录。
2. 归档用例可执行文件
用例可执行文件为DTFuzz源文件编译产出文件,为DTFuzz用例在设备中实际执行文件,以二进制形式存储在out/release/package/phone/tests/fuzztest下,名称为对应的测试套名。测试套的配置文件均编译输出在out/release/package/phone/tests/res目录下对应的XXXX_fuzzer目录中。将编译生成的配置文件res目录以及用例可执行文件fuzztest目录直接拷贝到该路径下即可。
用例可执行文件为DTFuzz源文件编译产出文件,为DTFuzz用例在设备中实际执行文件,以二进制形式存储在out/release/package/phone/tests/fuzztest下,名称为对应的测试套名。
新建目录,如:
```
D:\test\cases
```
将fuzztest目录拷贝到该路径下。
3. 配置执行用例
libs\fuzzlib中新建fuzzer_list.txt,其中配置需要执行的DTFuzz用例,如需要执行parse_fuzzer用例:
```
#格式:用例配置文件和语料归档路径 可执行文件名
D:\test\res\parse_fuzzer CalculatorFuzzTest
```
![img](../../public_sys-resources/icon-note.gif) **说明:**
1.fuzzer_list.txt中可配置多行,每行对应一个DTFuzz用例
2.路径与可执行文件名严格一一对应,如例子中路径归档的是parse_fuzzer用例的配置文件和语料,CalculatorFuzzTest为parse_fuzzer用例的可执行文件。
4. 配置用例路径
2. 配置用例路径
config\user_config.xml中配置用例归档路径:
```
<!-- configure test cases path -->
<test_cases>
<dir>D:\test\cases</dir> #用例可执行文件归档路径
<dir>D:\test\tests</dir> #用例可执行文件归档路径
</test_cases>
```
5. 执行用例
3. 执行用例
执行DTFuzz命令示例,无需参数-ss、-tm
......
......@@ -481,12 +481,13 @@ class CppTestDriver(IDriver):
self.config.testlevel,
self.config.testtype,
self.config.target_test_path,
suite_file,
filename)
is_coverage_test = True if self.config.coverage else False
# push testsuite file
self.config.device.push_file(suite_file, self.config.target_test_path)
self._push_corpus_if_exist(filename)
self._push_corpus_if_exist(suite_file)
# push resource files
resource_manager = ResourceManager()
......@@ -533,9 +534,9 @@ class CppTestDriver(IDriver):
resource_dir,
self.config.device)
def _push_corpus_if_exist(self, filename):
def _push_corpus_if_exist(self, suite_file):
if "fuzztest" == self.config.testtype[0]:
corpus_path = os.path.join(get_fuzzer_path(filename), "corpus")
corpus_path = os.path.join(get_fuzzer_path(suite_file), "corpus")
self.config.device.push_file(corpus_path,
os.path.join(self.config.target_test_path, "corpus"))
......@@ -544,6 +545,7 @@ class CppTestDriver(IDriver):
testlevel,
testtype,
target_test_path,
suite_file,
filename):
if "benchmark" == testtype[0]:
test_para = (" --benchmark_out_format=json"
......@@ -561,7 +563,7 @@ class CppTestDriver(IDriver):
if "fuzztest" == testtype[0]:
cfg_list = FuzzerConfigManager(os.path.join(get_fuzzer_path(
filename), "project.xml")).get_fuzzer_config("fuzztest")
suite_file), "project.xml")).get_fuzzer_config("fuzztest")
LOG.info("config list :%s" % str(cfg_list))
test_para += "corpus -max_len=" + cfg_list[0] + \
" -max_total_time=" + cfg_list[1] + \
......
......@@ -173,29 +173,17 @@ def get_decode(stream):
return ret
def parse_fuzzer_info():
path_list = []
bin_list = []
list_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
os.path.realpath(__file__)))), "libs", "fuzzlib", "fuzzer_list.txt")
with open(list_path, 'r') as list_file:
for line in list_file.readlines():
striped_str = line.strip()
if platform.system() == "Windows":
path_list.append(striped_str.split(" ")[0])
bin_list.append(striped_str.split(" ")[1])
else:
path_list.append(striped_str.split(":")[0][3:])
bin_list.append(striped_str.split(":")[1].split("(")[0])
return path_list, bin_list
def get_fuzzer_path(filename):
path_list, bin_list = parse_fuzzer_info()
for i, name in enumerate(bin_list):
if name == filename:
return os.path.join(sys.source_code_root_path, path_list[i])
return ""
def get_fuzzer_path(suite_file):
filename = os.path.basename(suite_file)
suitename = filename.split("FuzzTest")[0]
current_dir = os.path.dirname(suite_file)
while True:
if os.path.exists(os.path.join(current_dir, "tests")):
res_path = os.path.join(os.path.join(current_dir, "tests"), "res")
break
current_dir = os.path.dirname(current_dir)
fuzzer_path = os.path.join(res_path, "%s_fuzzer" % suitename)
return fuzzer_path
def is_lite_product(product_form, code_root_path):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册