diff --git a/nfc_core/bundle.json b/nfc_core/bundle.json index 7667ce54bb551e6c6b9898c37332272b2c89d427..baac7e88f7d7bf720c003740ae54f5b975becefa 100644 --- a/nfc_core/bundle.json +++ b/nfc_core/bundle.json @@ -75,7 +75,7 @@ } ], "test": [ - "//foundation/communication/nfc/nfc_core/test/services:unittest" + "//foundation/communication/nfc/nfc_core/test:test_nfc_service" ] }, "hisysevent_config": [ diff --git a/nfc_core/interfaces/innerkits/include/tags/taginfo.h b/nfc_core/interfaces/innerkits/include/tags/taginfo.h index 52d6b6ac113222449669f4b22d3b10646e5e2a1d..ba2c066b62c82ee9481e4fabae31466744249f4e 100644 --- a/nfc_core/interfaces/innerkits/include/tags/taginfo.h +++ b/nfc_core/interfaces/innerkits/include/tags/taginfo.h @@ -66,7 +66,7 @@ public: static std::shared_ptr Unmarshalling(Parcel& parcel); std::string GetStringExtrasData(AppExecFwk::PacMap& extraData, const std::string& extrasName); - int GetIntExtrasData(AppExecFwk::PacMap extrasData, const std::string& extrasName); + int GetIntExtrasData(AppExecFwk::PacMap& extrasData, const std::string& extrasName); std::weak_ptr GetTagExtrasData() const; AppExecFwk::PacMap GetTechExtrasData(KITS::TagTechnology tech); bool IsTechSupported(KITS::TagTechnology tech); diff --git a/nfc_core/interfaces/innerkits/src/tags/taginfo.cpp b/nfc_core/interfaces/innerkits/src/tags/taginfo.cpp index 70084ca79761f4e3f154bad30f1bdcad6c881572..1f5e062cba79d5e30ca64678be97d72184cda9e8 100644 --- a/nfc_core/interfaces/innerkits/src/tags/taginfo.cpp +++ b/nfc_core/interfaces/innerkits/src/tags/taginfo.cpp @@ -95,9 +95,9 @@ std::string TagInfo::GetStringExtrasData(AppExecFwk::PacMap& extrasData, const s return extrasData.GetStringValue(extrasName); } -int TagInfo::GetIntExtrasData(AppExecFwk::PacMap extrasData, const std::string& extrasName) +int TagInfo::GetIntExtrasData(AppExecFwk::PacMap& extrasData, const std::string& extrasName) { - if (extrasData.IsEmpty()) { + if (extrasData.IsEmpty() || extrasName.empty()) { return NfcErrorCode::NFC_SDK_ERROR_INVALID_PARAM; } diff --git a/nfc_core/test/BUILD.gn b/nfc_core/test/BUILD.gn index e68c93964eeaf963b0c32c6d684e28c5462af916..64ec054bb230388cc11aabb5eb2d8596efee3e71 100644 --- a/nfc_core/test/BUILD.gn +++ b/nfc_core/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -13,7 +13,10 @@ import("//build/test.gni") -group("unittest") { +group("test_nfc_service") { testonly = true - deps += [ "unittest:nfc_service_test" ] + deps = [ + "fuzztest:fuzztest", + "unittest:unittest", + ] } diff --git a/nfc_core/test/fuzztest/BUILD.gn b/nfc_core/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..54eee4186b86826e2388f7aa897be277dfd17b93 --- /dev/null +++ b/nfc_core/test/fuzztest/BUILD.gn @@ -0,0 +1,19 @@ +# Copyright (c) 2022 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/test.gni") + +group("fuzztest") { + testonly = true + deps = [ "tags_test/taginfo:taginfo_fuzztest" ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/BUILD.gn b/nfc_core/test/fuzztest/tags_test/taginfo/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..26b4db731f2c2c52875a8a23abe15b2c09102da8 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2022 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/test.gni") + +group("taginfo_fuzztest") { + testonly = true + deps = [ + "getintextrasdata_fuzzer:fuzztest", + "getstringextrasdata_fuzzer:fuzztest", + "gettechextrasdata_fuzzer:fuzztest", + "istechsupported_fuzzer:fuzztest", + ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/BUILD.gn b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8b34bc818024513dc8e54db28878320141517056 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 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") + +ohos_fuzztest("GetIntExtrasDataFuzzTest") { + module_out_path = "nfc/services" + + fuzz_config_file = "//foundation/communication/nfc/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer" + + include_dirs = [ + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include/tags", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "getintextrasdata_fuzzer.cpp" ] + + deps = [ "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits" ] + + external_deps = [ + "ability_base:want", + "ipc:ipc_core", + "utils_base:utils", + ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetIntExtrasDataFuzzTest", + ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/corpus/init b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..6d6bd4a361a3ab1279a47eccd61eedfda91e73c8 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.cpp b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d732acfd9ab4af6ffb74b44ccd568814eeb68b9a --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 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 "getintextrasdata_fuzzer.h" + +#include +#include + +#include "taginfo.h" +#include "tag_session_proxy.h" + +namespace OHOS { + using namespace OHOS::NFC::KITS; + using namespace OHOS::NFC::TAG; + + constexpr const auto FUZZER_TEST_UID = "0102"; + constexpr const auto FUZZER_TEST_DISC_ID = 1; + + std::shared_ptr FuzzGetTagInfo() + { + std::vector tagTechList; + tagTechList.push_back(static_cast(TagTechnology::NFC_A_TECH)); + tagTechList.push_back(static_cast(TagTechnology::NFC_ISODEP_TECH)); + std::shared_ptr tagTechExtrasData = std::make_shared(); + std::string tagUid = FUZZER_TEST_UID; + int tagRfDiscId = FUZZER_TEST_DISC_ID; + sptr tagSession = new TagSessionProxy(nullptr); + return std::make_shared(tagTechList, tagTechExtrasData, tagUid, tagRfDiscId, tagSession); + } + + void FuzzGetIntExtrasData(const uint8_t* data, size_t size) + { + std::shared_ptr tagInfo = FuzzGetTagInfo(); + if (tagInfo == nullptr) { + std::cout << "tagInfo is nullptr." << std::endl; + return; + } + AppExecFwk::PacMap extrasData = tagInfo->GetTechExtrasData(TagTechnology::NFC_A_TECH); + std::string extrasName = NfcSdkCommon::UnsignedCharArrayToString(data, size); + + tagInfo->GetIntExtrasData(extrasData, extrasName); + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzGetIntExtrasData(data, size); + return 0; +} + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.h b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..e151ee95acabf66be9164a2d0782f83c11054ddc --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/getintextrasdata_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 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 GETINTEXTRASDATA_FUZZER_H +#define GETINTEXTRASDATA_FUZZER_H + +#define FUZZ_PROJECT_NAME "getintextrasdata_fuzzer" + +#endif // GETINTEXTRASDATA_FUZZER_H diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/project.xml b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getintextrasdata_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/BUILD.gn b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b00766bfc1bd7ca67357b6ca6e53e085da2bb2b5 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 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") + +ohos_fuzztest("GetStringExtrasDataFuzzTest") { + module_out_path = "nfc/services" + + fuzz_config_file = "//foundation/communication/nfc/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer" + + include_dirs = [ + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include/tags", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "getstringextrasdata_fuzzer.cpp" ] + + deps = [ "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits" ] + + external_deps = [ + "ability_base:want", + "ipc:ipc_core", + "utils_base:utils", + ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetStringExtrasDataFuzzTest", + ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/corpus/init b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..6d6bd4a361a3ab1279a47eccd61eedfda91e73c8 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.cpp b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2aad14ff961afe42cbc9573ee9e603672f44bc1c --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 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 "getstringextrasdata_fuzzer.h" + +#include +#include + +#include "taginfo.h" +#include "tag_session_proxy.h" + +namespace OHOS { + using namespace OHOS::NFC::KITS; + using namespace OHOS::NFC::TAG; + + constexpr const auto FUZZER_TEST_UID = "0102"; + constexpr const auto FUZZER_TEST_DISC_ID = 1; + + std::shared_ptr FuzzGetTagInfo() + { + std::vector tagTechList; + tagTechList.push_back(static_cast(TagTechnology::NFC_A_TECH)); + tagTechList.push_back(static_cast(TagTechnology::NFC_ISODEP_TECH)); + std::shared_ptr tagTechExtrasData = std::make_shared(); + std::string tagUid = FUZZER_TEST_UID; + int tagRfDiscId = FUZZER_TEST_DISC_ID; + sptr tagSession = new TagSessionProxy(nullptr); + return std::make_shared(tagTechList, tagTechExtrasData, tagUid, tagRfDiscId, tagSession); + } + + void FuzzGetStringExtrasData(const uint8_t* data, size_t size) + { + std::shared_ptr tagInfo = FuzzGetTagInfo(); + if (tagInfo == nullptr) { + std::cout << "tagInfo is nullptr." << std::endl; + return; + } + AppExecFwk::PacMap extrasData = tagInfo->GetTechExtrasData(TagTechnology::NFC_A_TECH); + std::string extrasName = NfcSdkCommon::UnsignedCharArrayToString(data, size); + + tagInfo->GetStringExtrasData(extrasData, extrasName); + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzGetStringExtrasData(data, size); + return 0; +} + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.h b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..7cc02c528ec4bc61df4977ce187e7f6a444525bc --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/getstringextrasdata_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 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 GETSTRINGEXTRASDATA_FUZZER_H +#define GETSTRINGEXTRASDATA_FUZZER_H + +#define FUZZ_PROJECT_NAME "getstringextrasdata_fuzzer" + +#endif // GETSTRINGEXTRASDATA_FUZZER_H diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/project.xml b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/getstringextrasdata_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/BUILD.gn b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..05a1c0cca2c2c8fc34ba1711d9d23ebcdddf6e5c --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 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") + +ohos_fuzztest("GetTechExtrasDataFuzzTest") { + module_out_path = "nfc/services" + + fuzz_config_file = "//foundation/communication/nfc/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer" + + include_dirs = [ + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include/tags", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "gettechextrasdata_fuzzer.cpp" ] + + deps = [ "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits" ] + + external_deps = [ + "ability_base:want", + "ipc:ipc_core", + "utils_base:utils", + ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetTechExtrasDataFuzzTest", + ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/corpus/init b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..6d6bd4a361a3ab1279a47eccd61eedfda91e73c8 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.cpp b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..033d2f088831ed103f1e88c9d3d83faec9ed6617 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 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 "gettechextrasdata_fuzzer.h" + +#include +#include + +#include "taginfo.h" +#include "tag_session_proxy.h" + +namespace OHOS { + using namespace OHOS::NFC::KITS; + using namespace OHOS::NFC::TAG; + + constexpr const auto FUZZER_THRESHOLD = 4; + constexpr const auto FUZZER_TEST_UID = "0102"; + constexpr const auto FUZZER_TEST_DISC_ID = 1; + + uint32_t ConvertToUint32(const uint8_t* ptr) + { + if (ptr == nullptr) { + return 0; + } + + // Shift the 0th number to the left by 24 bits, shift the 1st number to the left by 16 bits, + // shift the 2nd number to the left by 8 bits, and not shift the 3rd number to the left + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); + } + + std::shared_ptr FuzzGetTagInfo() + { + std::vector tagTechList; + tagTechList.push_back(static_cast(TagTechnology::NFC_A_TECH)); + tagTechList.push_back(static_cast(TagTechnology::NFC_ISODEP_TECH)); + std::shared_ptr tagTechExtrasData = std::make_shared(); + std::string tagUid = FUZZER_TEST_UID; + int tagRfDiscId = FUZZER_TEST_DISC_ID; + sptr tagSession = new TagSessionProxy(nullptr); + return std::make_shared(tagTechList, tagTechExtrasData, tagUid, tagRfDiscId, tagSession); + } + + void FuzzGetTechExtrasData(const uint8_t* data, size_t size) + { + TagTechnology tech = static_cast(ConvertToUint32(data)); + std::shared_ptr tagInfo = FuzzGetTagInfo(); + if (tagInfo == nullptr) { + std::cout << "tagInfo is nullptr." << std::endl; + return; + } + + tagInfo->GetTechExtrasData(tech); + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (size < OHOS::FUZZER_THRESHOLD) { + return 0; + } + + /* Run your code on data */ + OHOS::FuzzGetTechExtrasData(data, size); + return 0; +} + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.h b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..8527c949158dbfcf2252d6b2fb1ecf329b8f8217 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/gettechextrasdata_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 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 GETTECHEXTRASDATA_FUZZER_H +#define GETTECHEXTRASDATA_FUZZER_H + +#define FUZZ_PROJECT_NAME "gettechextrasdata_fuzzer" + +#endif // GETTECHEXTRASDATA_FUZZER_H diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/project.xml b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/gettechextrasdata_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/BUILD.gn b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1a21d95f6cd60a6971ee28f650f658e9be4c68d4 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 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") + +ohos_fuzztest("IsTechSupportedFuzzTest") { + module_out_path = "nfc/services" + + fuzz_config_file = "//foundation/communication/nfc/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer" + + include_dirs = [ + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include", + "//foundation/communication/nfc/nfc_core/interfaces/innerkits/include/tags", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "istechsupported_fuzzer.cpp" ] + + deps = [ "//foundation/communication/nfc/nfc_core/interfaces/innerkits:nfc_inner_kits" ] + + external_deps = [ + "ability_base:want", + "ipc:ipc_core", + "utils_base:utils", + ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":IsTechSupportedFuzzTest", + ] +} diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/corpus/init b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..6d6bd4a361a3ab1279a47eccd61eedfda91e73c8 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.cpp b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..564856f7c121a46c1b831e98e8506389aff1eda2 --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 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 "istechsupported_fuzzer.h" + +#include +#include + +#include "taginfo.h" +#include "tag_session_proxy.h" + +namespace OHOS { + using namespace OHOS::NFC::KITS; + using namespace OHOS::NFC::TAG; + + constexpr const auto FUZZER_THRESHOLD = 4; + constexpr const auto FUZZER_TEST_UID = "0102"; + constexpr const auto FUZZER_TEST_DISC_ID = 1; + + uint32_t ConvertToUint32(const uint8_t* ptr) + { + if (ptr == nullptr) { + return 0; + } + + // Shift the 0th number to the left by 24 bits, shift the 1st number to the left by 16 bits, + // shift the 2nd number to the left by 8 bits, and not shift the 3rd number to the left + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); + } + + std::shared_ptr FuzzGetTagInfo() + { + std::vector tagTechList; + tagTechList.push_back(static_cast(TagTechnology::NFC_A_TECH)); + tagTechList.push_back(static_cast(TagTechnology::NFC_ISODEP_TECH)); + std::shared_ptr tagTechExtrasData = std::make_shared(); + std::string tagUid = FUZZER_TEST_UID; + int tagRfDiscId = FUZZER_TEST_DISC_ID; + sptr tagSession = new TagSessionProxy(nullptr); + return std::make_shared(tagTechList, tagTechExtrasData, tagUid, tagRfDiscId, tagSession); + } + + void FuzzIsTechSupported(const uint8_t* data, size_t size) + { + TagTechnology tech = static_cast(ConvertToUint32(data)); + std::shared_ptr tagInfo = FuzzGetTagInfo(); + if (tagInfo == nullptr) { + std::cout << "tagInfo is nullptr." << std::endl; + return; + } + + tagInfo->IsTechSupported(tech); + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (size < OHOS::FUZZER_THRESHOLD) { + return 0; + } + + /* Run your code on data */ + OHOS::FuzzIsTechSupported(data, size); + return 0; +} + diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.h b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..0d3219fcc82aedd5e8487c6ca245f9077f62db4d --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/istechsupported_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 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 ISTECHSUPPORTED_FUZZER_H +#define ISTECHSUPPORTED_FUZZER_H + +#define FUZZ_PROJECT_NAME "istechsupported_fuzzer" + +#endif // ISTECHSUPPORTED_FUZZER_H diff --git a/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/project.xml b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/nfc_core/test/fuzztest/tags_test/taginfo/istechsupported_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/nfc_core/test/services/BUILD.gn b/nfc_core/test/unittest/BUILD.gn similarity index 87% rename from nfc_core/test/services/BUILD.gn rename to nfc_core/test/unittest/BUILD.gn index 6468bf5946d63b8537a3772bcadb7990ae689b78..0cf180ebb2ec43e8f808dc8ee285f33c2ff1c554 100644 --- a/nfc_core/test/services/BUILD.gn +++ b/nfc_core/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -15,5 +15,5 @@ import("//build/test.gni") group("unittest") { testonly = true - deps = [ "unittest:nfc_service_unittest" ] + deps = [ "services:nfc_service_unittest" ] } diff --git a/nfc_core/test/services/unittest/BUILD.gn b/nfc_core/test/unittest/services/BUILD.gn similarity index 97% rename from nfc_core/test/services/unittest/BUILD.gn rename to nfc_core/test/unittest/services/BUILD.gn index e29824ab9c063e4b547cb3536355f227cbddbead..028867fab051da2aaee607666b389363683f9e17 100644 --- a/nfc_core/test/services/unittest/BUILD.gn +++ b/nfc_core/test/unittest/services/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 diff --git a/nfc_core/test/services/unittest/controller_test/nfc_controller_test.cpp b/nfc_core/test/unittest/services/controller_test/nfc_controller_test.cpp similarity index 100% rename from nfc_core/test/services/unittest/controller_test/nfc_controller_test.cpp rename to nfc_core/test/unittest/services/controller_test/nfc_controller_test.cpp diff --git a/nfc_core/test/services/unittest/tags_test/nfca_tag_test.cpp b/nfc_core/test/unittest/services/tags_test/nfca_tag_test.cpp similarity index 100% rename from nfc_core/test/services/unittest/tags_test/nfca_tag_test.cpp rename to nfc_core/test/unittest/services/tags_test/nfca_tag_test.cpp diff --git a/nfc_core/test/services/unittest/tags_test/tag_info_test.cpp b/nfc_core/test/unittest/services/tags_test/tag_info_test.cpp similarity index 100% rename from nfc_core/test/services/unittest/tags_test/tag_info_test.cpp rename to nfc_core/test/unittest/services/tags_test/tag_info_test.cpp