From a2c13b58134fd77d1b3e1751d36b06e51e18a6c1 Mon Sep 17 00:00:00 2001 From: stivn Date: Wed, 13 Apr 2022 10:51:36 +0800 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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