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 0000000000000000000000000000000000000000..45b78fd75ebeec0f4eb2b00a397a73d4c24f017e --- /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 0000000000000000000000000000000000000000..beead983a4a1e81b5ad1837869849d7d71797503 --- /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 0000000000000000000000000000000000000000..83d6f5592719ea49d5867d32b0bfd4c9b7130c29 --- /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 0000000000000000000000000000000000000000..59aefb51c6e5a670d20c9bda3030fcdf8db29e5a --- /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 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/examples/calculator/test/fuzztest/common/calculator_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/src/core/config/resource_manager.py b/src/core/config/resource_manager.py index 538b813dce5cf23e7c04717af23899d6f0d276ee..69c646367eb03a36d08713f46d96515003baf5fa 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 = "300" ############################################################################## @@ -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"] diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index ca08bc4f774843d54b33753fcd8660cbad363fe6..271d249ed68fe56a2f1255bdb3b674dc58cfe674 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 ############################################################################## @@ -639,7 +641,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 +656,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 +671,34 @@ class JSUnitTestDriver(IDriver): if main_result: self._execute_hapfile_jsunittest() + try: + status = False + actiontime = JS_TIMEOUT + times = CYCLE_TIMES + if 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 > 5: + 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 +755,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 +783,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))