From db938fdcf77433f10723bd21740b4afaa149d1f5 Mon Sep 17 00:00:00 2001 From: stivn Date: Mon, 11 Apr 2022 10:38:28 +0800 Subject: [PATCH 01/15] Modified file Signed-off-by: stivn --- src/core/driver/drivers.py | 10 ++++++---- src/core/utils.py | 34 +++++++++++----------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index ac93f2b..ca08bc4 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -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] + \ diff --git a/src/core/utils.py b/src/core/utils.py index 45d10b2..70bac9e 100755 --- a/src/core/utils.py +++ b/src/core/utils.py @@ -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): -- Gitee From 4e68733c44d5ea44979506607bdba6af6ab9e17c Mon Sep 17 00:00:00 2001 From: stivn Date: Mon, 11 Apr 2022 11:02:41 +0800 Subject: [PATCH 02/15] Modified fuzz file Signed-off-by: stivn --- .../BUILD.gn | 4 +- .../Calculator_fuzzer.cpp} | 0 .../Calculator_fuzzer.h} | 0 .../corpus/init | 0 .../project.xml | 0 libs/fuzzlib/README_zh.md | 56 +++++-------------- 6 files changed, 17 insertions(+), 43 deletions(-) rename examples/calculator/test/fuzztest/common/{parse_fuzzer => Calculator_fuzzer}/BUILD.gn (89%) rename examples/calculator/test/fuzztest/common/{parse_fuzzer/parse_fuzzer.cpp => Calculator_fuzzer/Calculator_fuzzer.cpp} (100%) rename examples/calculator/test/fuzztest/common/{parse_fuzzer/parse_fuzzer.h => Calculator_fuzzer/Calculator_fuzzer.h} (100%) rename examples/calculator/test/fuzztest/common/{parse_fuzzer => Calculator_fuzzer}/corpus/init (100%) rename examples/calculator/test/fuzztest/common/{parse_fuzzer => Calculator_fuzzer}/project.xml (100%) diff --git a/examples/calculator/test/fuzztest/common/parse_fuzzer/BUILD.gn b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn similarity index 89% rename from examples/calculator/test/fuzztest/common/parse_fuzzer/BUILD.gn rename to examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn index bfdc220..fa678bc 100644 --- a/examples/calculator/test/fuzztest/common/parse_fuzzer/BUILD.gn +++ b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn @@ -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" ] } ############################################################################### diff --git a/examples/calculator/test/fuzztest/common/parse_fuzzer/parse_fuzzer.cpp b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.cpp similarity index 100% rename from examples/calculator/test/fuzztest/common/parse_fuzzer/parse_fuzzer.cpp rename to examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.cpp diff --git a/examples/calculator/test/fuzztest/common/parse_fuzzer/parse_fuzzer.h b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h similarity index 100% rename from examples/calculator/test/fuzztest/common/parse_fuzzer/parse_fuzzer.h rename to examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h diff --git a/examples/calculator/test/fuzztest/common/parse_fuzzer/corpus/init b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init similarity index 100% rename from examples/calculator/test/fuzztest/common/parse_fuzzer/corpus/init rename to examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init diff --git a/examples/calculator/test/fuzztest/common/parse_fuzzer/project.xml b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/project.xml similarity index 100% rename from examples/calculator/test/fuzztest/common/parse_fuzzer/project.xml rename to examples/calculator/test/fuzztest/common/Calculator_fuzzer/project.xml diff --git a/libs/fuzzlib/README_zh.md b/libs/fuzzlib/README_zh.md index 3d52e2c..a95fcd8 100644 --- a/libs/fuzzlib/README_zh.md +++ b/libs/fuzzlib/README_zh.md @@ -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 #include @@ -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中配置用例归档路径: ``` - D:\test\cases #用例可执行文件归档路径 + D:\test\tests #用例可执行文件归档路径 ``` - 5. 执行用例 + 3. 执行用例 执行DTFuzz命令示例,无需参数-ss、-tm -- Gitee From e55fe2c2aee3c29132d9164c23e7fa4d72283916 Mon Sep 17 00:00:00 2001 From: stivn Date: Mon, 11 Apr 2022 11:48:56 +0800 Subject: [PATCH 03/15] Modified Signed-off-by: stivn --- .../fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h index 9089dfa..85fd4dc 100644 --- a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h +++ b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h @@ -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 -- Gitee From f0a925fe4342b29dd513b0a9f8b49f6455229312 Mon Sep 17 00:00:00 2001 From: stivn Date: Mon, 11 Apr 2022 14:37:34 +0800 Subject: [PATCH 04/15] Modified Signed-off-by: stivn --- examples/calculator/test/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/calculator/test/BUILD.gn b/examples/calculator/test/BUILD.gn index c63fcc5..f4f6ae1 100755 --- a/examples/calculator/test/BUILD.gn +++ b/examples/calculator/test/BUILD.gn @@ -29,7 +29,7 @@ group("fuzztest") { testonly = true deps = [] - deps += [ "fuzztest/common/parse_fuzzer:fuzztest" ] + deps += [ "fuzztest/common/Calculator_fuzzer:fuzztest" ] } group("benchmarktest") { -- Gitee From 1b6a7077ae793a98dd0b6489fbacd89ab8a53505 Mon Sep 17 00:00:00 2001 From: stivn Date: Mon, 11 Apr 2022 15:07:49 +0800 Subject: [PATCH 05/15] Modified file Signed-off-by: stivn --- examples/distributedb/test/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/distributedb/test/BUILD.gn b/examples/distributedb/test/BUILD.gn index bad399b..0bf3b10 100755 --- a/examples/distributedb/test/BUILD.gn +++ b/examples/distributedb/test/BUILD.gn @@ -51,7 +51,7 @@ ohos_distributedtest("DistributeDemo") { ] deps = [ - "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/:app_distributeddata", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//third_party/googletest:gtest", "//utils/native/base:utils", @@ -73,7 +73,7 @@ ohos_distributedtest("DistributeDemoAgent") { ] deps = [ - "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/:app_distributeddata", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//third_party/googletest:gtest", "//utils/native/base:utils", -- Gitee From 702c3e27cef75b7479e09e4a94afed3d56a3453f Mon Sep 17 00:00:00 2001 From: stivn Date: Tue, 12 Apr 2022 14:27:51 +0800 Subject: [PATCH 06/15] Modified the fuzz file Signed-off-by: stivn --- examples/calculator/test/BUILD.gn | 2 +- .../common/Calculator_fuzzer/BUILD.gn | 4 +-- .../Calculator_fuzzer/Calculator_fuzzer.h | 2 +- libs/fuzzlib/README_zh.md | 29 ++++++++++--------- src/core/utils.py | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/examples/calculator/test/BUILD.gn b/examples/calculator/test/BUILD.gn index f4f6ae1..00aa30c 100755 --- a/examples/calculator/test/BUILD.gn +++ b/examples/calculator/test/BUILD.gn @@ -29,7 +29,7 @@ group("fuzztest") { testonly = true deps = [] - deps += [ "fuzztest/common/Calculator_fuzzer:fuzztest" ] + deps += [ "fuzztest/common/calculator_fuzzer:fuzztest" ] } group("benchmarktest") { diff --git a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn index fa678bc..45b78fd 100644 --- a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn +++ b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/BUILD.gn @@ -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" + 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 = [ "Calculator_fuzzer.cpp" ] + sources = [ "calculator_fuzzer.cpp" ] } ############################################################################### diff --git a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h index 85fd4dc..83d6f55 100644 --- a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h +++ b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/Calculator_fuzzer.h @@ -16,6 +16,6 @@ #ifndef EXAMPLE_CALCULATOR_FUZZER_H_ #define EXAMPLE_CALCULATOR_FUZZER_H_ -#define FUZZ_PROJECT_NAME "Calculator_fuzzer" +#define FUZZ_PROJECT_NAME "calculator_fuzzer" #endif // EXAMPLE_CALCULATOR_FUZZER_H_ \ No newline at end of file diff --git a/libs/fuzzlib/README_zh.md b/libs/fuzzlib/README_zh.md index a95fcd8..83424a1 100644 --- a/libs/fuzzlib/README_zh.md +++ b/libs/fuzzlib/README_zh.md @@ -53,12 +53,12 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or 执行gen命令用于fuzzer源文件生成,会自动生成fuzzer源文件、fuzzer配置文件和corpus语料,目录结构如下 ``` - Calculator_fuzzer/ + calculator_fuzzer/ ├── corpus # Fuzz语料目录 │ ├── init # Fuzz语料 ├── BUILD.gn # Fuzz用例编译配置 - ├── Calculator_fuzzer.cpp # Fuzz用例源文件 - ├── Calculator_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 Calculator_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 "Calculator_fuzzer.h" + #include "calculator_fuzzer.h" #include #include @@ -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" + 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 = [ "Calculator_fuzzer.cpp" ] + sources = [ "calculator_fuzzer.cpp" ] } ``` @@ -190,10 +190,10 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or 添加Fuzz用例编译到模块的test_list中: -1. 在需要DTFuzz测试的对应模块ohos.build添加Fuzz用例路径,如在examples/ohos.build添加: +1. 在需要DTFuzz测试的对应模块bundle.json中添加Fuzz用例路径,如在bundle.json添加: ``` - "test_list": [ + "tests": [ "//test/developertest/examples/calculator/test:unittest", "//test/developertest/examples/calculator/test:fuzztest", #添加DTFuzz用例路径 "//test/developertest/examples/detector/test:unittest", @@ -209,7 +209,7 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or testonly = true deps = [] - deps += [ "fuzztest/common/Calculator_fuzzer:fuzztest" ] + deps += [ "fuzztest/common/calculator_fuzzer:fuzztest" ] } ``` @@ -241,7 +241,8 @@ run -t FUZZ -ss developertest -tm calculator D:\test\tests ``` - 用例可执行文件为DTFuzz源文件编译产出文件,为DTFuzz用例在设备中实际执行文件,以二进制形式存储在out/release/package/phone/tests/fuzztest下,名称为对应的测试套名。测试套的配置文件均编译输出在out/release/package/phone/tests/res目录下对应的XXXX_fuzzer目录中。将编译生成的配置文件res目录以及用例可执行文件fuzztest目录直接拷贝到该路径下即可。 + 用例可执行文件为DTFuzz源文件编译产出文件,以二进制形式存储在out/release/package/phone/tests/fuzztest下。测试用例的配置文件均编译输出在out/release/package/phone/tests/res目录下对应的xxxx_fuzzer目录中。 + 将fuzztest目录以及res目录直接拷贝到该路径下即可。 2. 配置用例路径 @@ -257,10 +258,10 @@ run -t FUZZ -ss developertest -tm calculator 3. 执行用例 - 执行DTFuzz命令示例,无需参数-ss、-tm + 执行DTFuzz命令示例 ``` - run -t FUZZ + run -t FUZZ -ts CalculatorFuzzTest ``` diff --git a/src/core/utils.py b/src/core/utils.py index 70bac9e..fc0a589 100755 --- a/src/core/utils.py +++ b/src/core/utils.py @@ -182,7 +182,7 @@ def get_fuzzer_path(suite_file): 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) + fuzzer_path = os.path.join(res_path, "%s_fuzzer" % suitename.lower()) return fuzzer_path -- Gitee From 9764025ae0161e86f3506de2b5e58a3434748d59 Mon Sep 17 00:00:00 2001 From: stivn Date: Tue, 12 Apr 2022 15:03:49 +0800 Subject: [PATCH 07/15] modified Signed-off-by: stivn --- .../test/fuzztest/common/Calculator_fuzzer/corpus/init | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init index 4dc63c8..59aefb5 100644 --- a/examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init +++ b/examples/calculator/test/fuzztest/common/Calculator_fuzzer/corpus/init @@ -9,4 +9,5 @@ # 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. \ No newline at end of file +# limitations under the License. +FUZZ \ No newline at end of file -- Gitee From a2c13b58134fd77d1b3e1751d36b06e51e18a6c1 Mon Sep 17 00:00:00 2001 From: stivn Date: Wed, 13 Apr 2022 10:51:36 +0800 Subject: [PATCH 08/15] Modified the js framework Signed-off-by: stivn --- src/core/config/resource_manager.py | 2 +- src/core/driver/drivers.py | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/core/config/resource_manager.py b/src/core/config/resource_manager.py index 538b813..c78c21f 100755 --- a/src/core/config/resource_manager.py +++ b/src/core/config/resource_manager.py @@ -24,7 +24,7 @@ from xdevice import DeviceTestType from core.constants import ConfigFileConst LOG = platform_logger("ResourceManager") -DEFAULT_TIMEOUT = "300" +DEFAULT_TIMEOUT = "1" ############################################################################## diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index ca08bc4..14a32f3 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -639,7 +639,7 @@ class JSUnitTestDriver(IDriver): with os.fdopen(hilog_open, "a") as hilog_file_pipe: self.config.device.start_catch_device_log(hilog_file_pipe) self._init_jsunit_test() - self._run_jsunit(suite_file) + self._run_jsunit(suite_file, hilog) hilog_file_pipe.flush() self.generate_console_output(hilog, request) finally: @@ -654,13 +654,13 @@ class JSUnitTestDriver(IDriver): self.config.device.execute_shell_command( "mount -o rw,remount,rw /") - - def _run_jsunit(self, suite_file): + def _run_jsunit(self, suite_file, device_log_file): filename = os.path.basename(suite_file) resource_manager = ResourceManager() resource_data_dic, resource_dir = \ resource_manager.get_resource_data_dic(suite_file) + timeout = ResourceManager.get_nodeattrib_data(resource_data_dic) resource_manager.process_preparer_data(resource_data_dic, resource_dir, self.config.device) @@ -669,6 +669,25 @@ class JSUnitTestDriver(IDriver): if main_result: self._execute_hapfile_jsunittest() + try: + if timeout: + time.sleep(float(timeout)) + device_log_file_open = os.open(device_log_file, os.O_RDONLY, + stat.S_IWUSR | stat.S_IRUSR) + with os.fdopen(device_log_file_open, "r", encoding='utf-8') \ + as file_read_pipe: + start_time = int(time.time()) + while True: + data = file_read_pipe.readline() + if data.find("JSApp:") != -1 and data.find("[end] run suites end") != -1: + LOG.info("execute testcase successfully.") + break + if int(time.time()) - start_time > 300: + LOG.info("execute testcase timeout.") + break + + finally: + _lock_screen(self.config.device) self._uninstall_hap(self.package_name) else: self.result = result.get_test_results("Error: install hap failed") @@ -725,7 +744,6 @@ class JSUnitTestDriver(IDriver): except (ExecuteTerminate, DeviceError) as exception: return_message = str(exception.args) - _lock_screen(self.config.device) return return_message def _install_hap(self, suite_file): @@ -754,7 +772,6 @@ class JSUnitTestDriver(IDriver): if "success" in str(result_value).lower(): LOG.info("execute %s's testcase success. result value=%s" % (self.package_name, result_value)) - time.sleep(30) else: LOG.info("execute %s's testcase failed. result value=%s" % (self.package_name, result_value)) -- Gitee From a2417fc280a13dc9a26cb2db20d126698ecd361f Mon Sep 17 00:00:00 2001 From: stivn Date: Wed, 13 Apr 2022 11:34:17 +0800 Subject: [PATCH 09/15] Modified Signed-off-by: stivn --- src/core/config/resource_manager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/config/resource_manager.py b/src/core/config/resource_manager.py index c78c21f..69c6463 100755 --- a/src/core/config/resource_manager.py +++ b/src/core/config/resource_manager.py @@ -24,7 +24,6 @@ from xdevice import DeviceTestType from core.constants import ConfigFileConst LOG = platform_logger("ResourceManager") -DEFAULT_TIMEOUT = "1" ############################################################################## @@ -245,7 +244,7 @@ class ResourceManager(object): @classmethod def get_nodeattrib_data(cls, data_dic): - curr_timeout = DEFAULT_TIMEOUT + curr_timeout = "" if "nodeattrib" in data_dic.keys(): LOG.info("++++++++++++++nodeattrib+++++++++++++++") nodeattrib_list = data_dic["nodeattrib"] -- Gitee From 69363f16c3e39d2d5470ae07c17173fa31feee82 Mon Sep 17 00:00:00 2001 From: stivn Date: Thu, 14 Apr 2022 11:27:56 +0800 Subject: [PATCH 10/15] change name Signed-off-by: stivn --- .../common/calculator_fuzzer/BUILD.gn | 43 ++++++++++++++++ .../calculator_fuzzer/calculator_fuzzer.cpp | 50 +++++++++++++++++++ .../calculator_fuzzer/calculator_fuzzer.h | 21 ++++++++ .../common/calculator_fuzzer/corpus/init | 13 +++++ .../common/calculator_fuzzer/project.xml | 25 ++++++++++ 5 files changed, 152 insertions(+) create mode 100644 examples/calculator/test/fuzztest/common/calculator_fuzzer/BUILD.gn create mode 100644 examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.cpp create mode 100644 examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.h create mode 100644 examples/calculator/test/fuzztest/common/calculator_fuzzer/corpus/init create mode 100644 examples/calculator/test/fuzztest/common/calculator_fuzzer/project.xml diff --git a/examples/calculator/test/fuzztest/common/calculator_fuzzer/BUILD.gn b/examples/calculator/test/fuzztest/common/calculator_fuzzer/BUILD.gn new file mode 100644 index 0000000..45b78fd --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +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", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "calculator_fuzzer.cpp" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":CalculatorFuzzTest", + ] +} +############################################################################### diff --git a/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.cpp b/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.cpp new file mode 100644 index 0000000..beead98 --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 +#include + +const int FUZZ_DATA_LEN = 3; +const int FUZZ_FST_DATA = 0; +const int FUZZ_SND_DATA = 1; +const int FUZZ_TRD_DATA = 2; +const int FUZZ_FTH_DATA = 3; + +namespace OHOS { + bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + { + if (data == nullptr) { + return false; + } + + bool result = false; + if (size >= FUZZ_DATA_LEN) { + result = data[FUZZ_FST_DATA] == 'F' && + data[FUZZ_SND_DATA] == 'U' && + data[FUZZ_TRD_DATA] == 'Z' && + data[FUZZ_FTH_DATA] == 'Z'; + } + return result; + } +} // namespace.OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DoSomethingInterestingWithMyAPI(data, size); + return 0; +} + diff --git a/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.h b/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.h new file mode 100644 index 0000000..83d6f55 --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/calculator_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef EXAMPLE_CALCULATOR_FUZZER_H_ +#define EXAMPLE_CALCULATOR_FUZZER_H_ + +#define FUZZ_PROJECT_NAME "calculator_fuzzer" + +#endif // EXAMPLE_CALCULATOR_FUZZER_H_ \ No newline at end of file diff --git a/examples/calculator/test/fuzztest/common/calculator_fuzzer/corpus/init b/examples/calculator/test/fuzztest/common/calculator_fuzzer/corpus/init new file mode 100644 index 0000000..59aefb5 --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. +# 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. +FUZZ \ No newline at end of file diff --git a/examples/calculator/test/fuzztest/common/calculator_fuzzer/project.xml b/examples/calculator/test/fuzztest/common/calculator_fuzzer/project.xml new file mode 100644 index 0000000..85e7ef2 --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 3799caa0745349e9c032642bdea32c5a41c2b76e Mon Sep 17 00:00:00 2001 From: stivn Date: Thu, 14 Apr 2022 15:07:58 +0800 Subject: [PATCH 11/15] modified the js driver Signed-off-by: stivn --- src/core/driver/drivers.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index 14a32f3..271d249 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -54,6 +54,8 @@ LOG = platform_logger("Drivers") DEFAULT_TEST_PATH = "/%s/%s/" % ("data", "test") TIME_OUT = 900 * 1000 +JS_TIMEOUT = 10 +CYCLE_TIMES = 30 ############################################################################## @@ -670,20 +672,29 @@ class JSUnitTestDriver(IDriver): if main_result: self._execute_hapfile_jsunittest() try: + status = False + actiontime = JS_TIMEOUT + times = CYCLE_TIMES if timeout: - time.sleep(float(timeout)) + actiontime = timeout + times = 1 device_log_file_open = os.open(device_log_file, os.O_RDONLY, stat.S_IWUSR | stat.S_IRUSR) with os.fdopen(device_log_file_open, "r", encoding='utf-8') \ as file_read_pipe: + for i in range(0, times): + if status: + break + else: + time.sleep(float(actiontime)) start_time = int(time.time()) while True: data = file_read_pipe.readline() if data.find("JSApp:") != -1 and data.find("[end] run suites end") != -1: LOG.info("execute testcase successfully.") + status = True break - if int(time.time()) - start_time > 300: - LOG.info("execute testcase timeout.") + if int(time.time()) - start_time > 5: break finally: -- Gitee From f6c6e74b8d698e3dbdc4eb47eb3c9f7a88a7df56 Mon Sep 17 00:00:00 2001 From: stivn Date: Thu, 14 Apr 2022 20:59:43 +0800 Subject: [PATCH 12/15] Modified Signed-off-by: stivn --- src/core/driver/drivers.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index 271d249..dfc50af 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -687,19 +687,19 @@ class JSUnitTestDriver(IDriver): break else: time.sleep(float(actiontime)) - start_time = int(time.time()) - while True: - data = file_read_pipe.readline() - if data.find("JSApp:") != -1 and data.find("[end] run suites end") != -1: - LOG.info("execute testcase successfully.") - status = True - break + start_time = int(time.time()) + while True: + data = file_read_pipe.readline() + if data.find("JSApp:") != -1 and data.find("[end] run suites end") != -1: + LOG.info("execute testcase successfully.") + status = True + break if int(time.time()) - start_time > 5: - break + break finally: _lock_screen(self.config.device) - self._uninstall_hap(self.package_name) + self._uninstall_hap(self.package_name) else: self.result = result.get_test_results("Error: install hap failed") LOG.error("Error: install hap failed") -- Gitee From ff9ae596260d588170c387bf3baf6e076a2816f8 Mon Sep 17 00:00:00 2001 From: stivn Date: Thu, 28 Apr 2022 19:12:40 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9fuzz=20corpus=E8=AF=AD?= =?UTF-8?q?=E6=96=99=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: stivn --- src/core/driver/drivers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index dfc50af..5ca37ae 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -355,7 +355,7 @@ class ResultManager(object): result_josn_file_path) result_file_path = result_josn_file_path else: - LOG.error("%s not exist", remote_result_file) + LOG.info("%s not exist", remote_result_file) return result_file_path @@ -538,7 +538,7 @@ class CppTestDriver(IDriver): def _push_corpus_if_exist(self, suite_file): if "fuzztest" == self.config.testtype[0]: - corpus_path = os.path.join(get_fuzzer_path(suite_file), "corpus") + corpus_path = os.path.join(get_fuzzer_path(suite_file), os.path.join("corpus", "init")) self.config.device.push_file(corpus_path, os.path.join(self.config.target_test_path, "corpus")) -- Gitee From 9dac9bf122f6b0e006ddc8a16c8bc3a3c5d566ad Mon Sep 17 00:00:00 2001 From: stivn Date: Sat, 7 May 2022 15:07:56 +0800 Subject: [PATCH 14/15] modified the fuzz file Signed-off-by: stivn --- libs/fuzzlib/README_zh.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/fuzzlib/README_zh.md b/libs/fuzzlib/README_zh.md index 83424a1..97f9c1f 100644 --- a/libs/fuzzlib/README_zh.md +++ b/libs/fuzzlib/README_zh.md @@ -168,7 +168,9 @@ Fuzzing测试框架使用了LLVM编译器框架中的[libFuzzer](https://llvm.or ] } ``` - + **注意:** + - 测试套名称采用大驼峰风格,并且以FuzzTest结尾,测试套前缀与fuzzer目录名相对应。 + - module_out_path为测试套编译输出目录,内容为部件+模块名。 3. Fuzz配置编写 -- Gitee From b606df1c695224b18b58efc9fac69ef4320c22bf Mon Sep 17 00:00:00 2001 From: stivn Date: Thu, 12 May 2022 19:45:04 +0800 Subject: [PATCH 15/15] modified method of getting device log Signed-off-by: stivn --- src/core/driver/drivers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index 5ca37ae..9317784 100755 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -737,7 +737,7 @@ class JSUnitTestDriver(IDriver): as file_read_pipe: while True: data = file_read_pipe.readline() - if not data or not data.strip(): + if not data: break # only filter JSApp log if data.find("JSApp:") != -1: -- Gitee