diff --git a/packing_tool/frameworks/test/unittest/json/distro_filter_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/distro_filter_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..96a54f01794cbd2278988e367ec444359111f9a9 --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/distro_filter_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("distro_filter_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("distro_filter_test") { + module_out_path = module_output_path + public_configs = [ ":distro_filter_test_config" ] + sources = [ + "../../../../src/json/distro_filter.cpp", + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "../../../../src/utils.cpp", + "distro_filter_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} diff --git a/packing_tool/frameworks/test/unittest/json/distro_filter_test/distro_filter_test.cpp b/packing_tool/frameworks/test/unittest/json/distro_filter_test/distro_filter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dde2617745754e4b08af921c540ebe29b0b8c90c --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/distro_filter_test/distro_filter_test.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2024 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 +#define private public +#define protected public +#include "distro_filter.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +std::string policyValueJsonString = "{" + "\"policy\": \"\"," + "\"value\": [" + "\"xxx\"" + "]" +"}"; + +std::string policyValueString = "{" + "\\\"policy\\\": \\\"\\\"," + "\\\"value\\\": [" + "\\\"xxx\\\"" + "]" +"}"; + +std::string distroFilterJsonString = "{" + "\"apiVersion\": \"" + policyValueString + "\"," + "\"screenShape\": \"" + policyValueString + "\"," + "\"screenDensity\": \"" + policyValueString + "\"," + "\"screenWindow\": \"" + policyValueString + "\"," + "\"countryCode\": \"" + policyValueString + "\"" +"}"; + +std::string policyValueJsonString2 = "{" + "\"policy\": \"xxx\"," + "\"value\": [" + "\"xxx\"" + "]" +"}"; + +std::string policyValueString2 = "{" + "\\\"policy\\\": \\\"xxx\\\"," + "\\\"value\\\": [" + "\\\"xxx\\\"" + "]" +"}"; + +std::string distroFilterJsonString2 = "{" + "\"apiVersion\": \"" + policyValueString2 + "\"," + "\"screenShape\": \"" + policyValueString2 + "\"," + "\"screenDensity\": \"" + policyValueString2 + "\"," + "\"screenWindow\": \"" + policyValueString2 + "\"," + "\"countryCode\": \"" + policyValueString2 + "\"" +"}"; + +class DistroFilterTest : public testing::Test { +public: + DistroFilterTest() {} + virtual ~DistroFilterTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void DistroFilterTest::SetUpTestCase() {} + +void DistroFilterTest::TearDownTestCase() {} + +void DistroFilterTest::SetUp() {} + +void DistroFilterTest::TearDown() {} + +/* + * @tc.name: PolicyValue_ParseFromString_0100 + * @tc.desc: PolicyValue Function Test. + * ParseFromString/IsEmpty + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistroFilterTest, PolicyValue_ParseFromString_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PolicyValue policyValue; + EXPECT_TRUE(policyValue.ParseFromString(policyValueJsonString)); + EXPECT_TRUE(policyValue.IsEmpty()); +} + +/* + * @tc.name: PolicyValue_ParseFromString_0200 + * @tc.desc: PolicyValue Function Test. + * ParseFromString/IsEmpty + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistroFilterTest, PolicyValue_ParseFromString_0200, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PolicyValue policyValue; + EXPECT_TRUE(policyValue.ParseFromString(policyValueJsonString2)); + EXPECT_FALSE(policyValue.IsEmpty()); +} + +/* + * @tc.name: DistroFilter_ParseFromString_0100 + * @tc.desc: DistroFilter Function Test. + * ParseFromString/ParseApiVersion/ParseScreenShape/ParseScreenDensity/ParseScreenWindow/ParseCountryCode + * /IsEmpty/Dump + * PolicyValue Function Test. + * ParseFromString/IsEmpty + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistroFilterTest, DistroFilter_ParseFromString_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::DistroFilter distroFilter; + EXPECT_TRUE(distroFilter.ParseFromString(distroFilterJsonString)); + EXPECT_TRUE(distroFilter.IsEmpty()); + EXPECT_EQ(distroFilter.Dump(), ""); +} + +/* + * @tc.name: DistroFilter_ParseFromString_0200 + * @tc.desc: DistroFilter Function Test. + * ParseFromString/ParseApiVersion/ParseScreenShape/ParseScreenDensity/ParseScreenWindow/ParseCountryCode + * /IsEmpty/Dump + * PolicyValue Function Test. + * ParseFromString/IsEmpty + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistroFilterTest, DistroFilter_ParseFromString_0200, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::DistroFilter distroFilter; + EXPECT_TRUE(distroFilter.ParseFromString(distroFilterJsonString2)); + EXPECT_FALSE(distroFilter.IsEmpty()); + EXPECT_EQ(distroFilter.Dump(), + "distroFilter: apiVersion: policy is xxx, " + "value is xxx screenShape: policy is xxx, " + "value is xxx screenDensity: policy is xxx, " + "value is xxx screenWindow: policy is xxx, " + "value is xxx countryCode: policy is xxx, " + "value is xxx"); +} +} diff --git a/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..53b8a174e1e9563e3315a8655c8c236df1cf43d5 --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("hap_verify_utils_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("hap_verify_utils_test") { + module_out_path = module_output_path + public_configs = [ ":hap_verify_utils_test_config" ] + sources = [ + "../../../../src/json/distro_filter.cpp", + "../../../../src/json/hap_verify_info.cpp", + "../../../../src/json/hap_verify_utils.cpp", + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "../../../../src/utils.cpp", + "hap_verify_utils_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} diff --git a/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa6db906783d45aae3ae74047a3bcf594836a26f --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/hap_verify_utils_test/hap_verify_utils_test.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2024 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 +#define private public +#define protected public +#include "hap_verify_utils.h" +#include "hap_verify_info.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +class HapVerifyUtilsTest : public testing::Test { +public: + HapVerifyUtilsTest() {} + virtual ~HapVerifyUtilsTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void HapVerifyUtilsTest::SetUpTestCase() {} + +void HapVerifyUtilsTest::TearDownTestCase() {} + +void HapVerifyUtilsTest::SetUp() {} + +void HapVerifyUtilsTest::TearDown() {} + +namespace { + OHOS::AppPackingTool::HapVerifyInfo hapVerifyInfo1; + OHOS::AppPackingTool::HapVerifyInfo hapVerifyInfo2; +} + +void InitHapVerifyInfo1() +{ + // hapVerifyInfo1 + hapVerifyInfo1.SetBundleName("test"); + hapVerifyInfo1.SetBundleType("hap"); + hapVerifyInfo1.SetVendor("vendor"); + + OHOS::AppPackingTool::Version version1; + version1.versionCode = 1; + version1.versionName = "1.0.0"; + version1.minCompatibleVersionCode = 1; + hapVerifyInfo1.SetVersion(version1); + + OHOS::AppPackingTool::ModuleApiVersion moduleApiVersion1; + moduleApiVersion1.compatibleApiVersion = 1; + moduleApiVersion1.targetApiVersion = 1; + moduleApiVersion1.releaseType = "release"; + hapVerifyInfo1.SetApiVersion(moduleApiVersion1); + + hapVerifyInfo1.SetTargetBundleName("test"); + hapVerifyInfo1.SetTargetPriority(1); + hapVerifyInfo1.SetDebug(false); + hapVerifyInfo1.SetModuleName("test"); + hapVerifyInfo1.SetModuleType("entry"); + + OHOS::AppPackingTool::MultiAppMode multiAppMode1; + multiAppMode1.multiAppModeType = "standard"; + multiAppMode1.maxCount = 1; + hapVerifyInfo1.SetMultiAppMode(multiAppMode1); + + const std::list deviceTypes1 = {"phone", "watch"}; + hapVerifyInfo1.SetDeviceTypes(deviceTypes1); + + OHOS::AppPackingTool::ApiVersion apiVersion1; + apiVersion1.policy = "include"; + + OHOS::AppPackingTool::ScreenShape screenShape1; + screenShape1.policy = "include"; + + OHOS::AppPackingTool::ScreenDensity screenDensity1; + screenDensity1.policy = "include"; + + OHOS::AppPackingTool::ScreenWindow screenWindow1; + screenWindow1.policy = "include"; + + OHOS::AppPackingTool::CountryCode countryCode1; + countryCode1.policy = "include"; + + OHOS::AppPackingTool::DistroFilter distroFilter1; + distroFilter1.apiVersion = apiVersion1; + distroFilter1.screenShape = screenShape1; + distroFilter1.screenDensity = screenDensity1; + distroFilter1.screenWindow = screenWindow1; + distroFilter1.countryCode = countryCode1; + + hapVerifyInfo1.SetDistroFilter(distroFilter1); +} + +void InitHapVerifyInfo2() +{ + // hapVerifyInfo2 + hapVerifyInfo2.SetBundleName("test"); + hapVerifyInfo2.SetBundleType("hap"); + hapVerifyInfo2.SetVendor("vendor"); + + OHOS::AppPackingTool::Version version2; + version2.versionCode = 1; + version2.versionName = "1.0.0"; + version2.minCompatibleVersionCode = 1; + hapVerifyInfo2.SetVersion(version2); + + OHOS::AppPackingTool::ModuleApiVersion moduleApiVersion2; + moduleApiVersion2.compatibleApiVersion = 1; + moduleApiVersion2.targetApiVersion = 1; + moduleApiVersion2.releaseType = "release"; + hapVerifyInfo2.SetApiVersion(moduleApiVersion2); + + hapVerifyInfo2.SetTargetBundleName("test"); + hapVerifyInfo2.SetTargetPriority(1); + hapVerifyInfo2.SetDebug(false); + hapVerifyInfo2.SetModuleName("test2"); + hapVerifyInfo2.SetModuleType("entry"); + + OHOS::AppPackingTool::MultiAppMode multiAppMode2; + multiAppMode2.multiAppModeType = "standard"; + multiAppMode2.maxCount = 1; + hapVerifyInfo2.SetMultiAppMode(multiAppMode2); + + const std::list deviceTypes2 = {"computer", "ipad"}; + hapVerifyInfo2.SetDeviceTypes(deviceTypes2); + + OHOS::AppPackingTool::ApiVersion apiVersion2; + apiVersion2.policy = "exclude"; + apiVersion2.value = {"c", "d"}; + + OHOS::AppPackingTool::ScreenShape screenShape2; + screenShape2.policy = "exclude"; + screenShape2.value = {"c", "d"}; + + OHOS::AppPackingTool::ScreenDensity screenDensity2; + screenDensity2.policy = "exclude"; + screenDensity2.value = {"c", "d"}; + + OHOS::AppPackingTool::ScreenWindow screenWindow2; + screenWindow2.policy = "exclude"; + screenWindow2.value = {"c", "d"}; + + OHOS::AppPackingTool::CountryCode countryCode2; + countryCode2.policy = "exclude"; + countryCode2.value = {"c", "d"}; + + OHOS::AppPackingTool::DistroFilter distroFilter2; + distroFilter2.apiVersion = apiVersion2; + distroFilter2.screenShape = screenShape2; + distroFilter2.screenDensity = screenDensity2; + distroFilter2.screenWindow = screenWindow2; + distroFilter2.countryCode = countryCode2; + + hapVerifyInfo2.SetDistroFilter(distroFilter2); +} + +/* + * @tc.name: CheckHapIsValid_0100 + * @tc.desc: CheckHapIsValid + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HapVerifyUtilsTest, CheckHapIsValid_0100, Function | MediumTest | Level1) +{ + InitHapVerifyInfo1(); + InitHapVerifyInfo2(); + + // hapVerifyInfos + std::list hapVerifyInfos; + hapVerifyInfos.push_back(hapVerifyInfo1); + hapVerifyInfos.push_back(hapVerifyInfo2); + + OHOS::AppPackingTool::HapVerifyUtils utils; + EXPECT_TRUE(utils.CheckHapIsValid(hapVerifyInfos)); +} +} diff --git a/packing_tool/frameworks/test/unittest/json/json_utils_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/json_utils_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0d8657e0df0e3fd91fcbbab540c47084b3e81cb6 --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/json_utils_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("json_utils_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("json_utils_test") { + module_out_path = module_output_path + public_configs = [ ":json_utils_test_config" ] + sources = [ + "../../../../src/json/json_utils.cpp", + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "../../../../src/utils.cpp", + "json_utils_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} diff --git a/packing_tool/frameworks/test/unittest/json/json_utils_test/json_utils_test.cpp b/packing_tool/frameworks/test/unittest/json/json_utils_test/json_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..835ce42ecab1499f09c05a12805272fa035c4f8f --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/json_utils_test/json_utils_test.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2024 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 +#define private public +#define protected public +#include "json_utils.h" +#include "pt_json.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +const std::string MODULE_JSON_PATH = "/data/module.json"; +const std::string CONFIG_JSON_PATH = "/data/config.json"; +const std::string PATCH_JSON_PATH = "/data/patch.json"; + +const std::string jsonString = "{" + "\"name\": \"Json.CN\"," + "\"app\": \"apptest\"," + "\"module\": {" + "\"name\": \"nametest\"," + "\"type\": \"typetest\"," + "\"deviceTypes\": [" + "\"aaaaaaaaaaa\"," + "\"bbbbbbbbbbb\"," + "\"ccccccccccc\"" + "]" + "}" +"}"; + +class JsonUtilsTest : public testing::Test { +public: + JsonUtilsTest() {} + virtual ~JsonUtilsTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void JsonUtilsTest::SetUpTestCase() {} + +void JsonUtilsTest::TearDownTestCase() {} + +void JsonUtilsTest::SetUp() {} + +void JsonUtilsTest::TearDown() {} + +/* + * @tc.name: IsModuleJson_0100 + * @tc.desc: IsModuleJson/CheckFileName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(JsonUtilsTest, IsModuleJson_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::JsonUtils jsonUtils; + system("touch data/module.json"); + EXPECT_TRUE(jsonUtils.IsModuleJson(MODULE_JSON_PATH)); + system("rm -f data/module.json"); +} + +/* + * @tc.name: IsConfigJson_0100 + * @tc.desc: IsConfigJson/CheckFileName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(JsonUtilsTest, IsConfigJson_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::JsonUtils jsonUtils; + system("touch data/config.json"); + EXPECT_TRUE(jsonUtils.IsConfigJson(CONFIG_JSON_PATH)); + system("rm -f data/config.json"); +} + +/* + * @tc.name: IsPatchJson_0100 + * @tc.desc: IsPatchJson/CheckFileName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(JsonUtilsTest, IsPatchJson_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::JsonUtils jsonUtils; + system("touch data/patch.json"); + EXPECT_TRUE(jsonUtils.IsPatchJson(PATCH_JSON_PATH)); + system("rm -f data/patch.json"); +} + +/* + * @tc.name: JsonFromFile_0100 + * @tc.desc: JsonFromFile. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(JsonUtilsTest, JsonFromFile_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::JsonUtils jsonUtils; + std::string test("data/test.json"); + FILE *fp = fopen(test.c_str(), "w"); + EXPECT_TRUE(fp != nullptr); + if (fp != nullptr) { + fwrite(jsonString.c_str(), jsonString.size(), 1, fp); + fclose(fp); + EXPECT_NE(jsonUtils.JsonFromFile(test), nullptr); + } + system("rm -f data/test.json"); +} + +/* + * @tc.name: JsonToFile_0100 + * @tc.desc: JsonToFile/StrToFile + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(JsonUtilsTest, JsonToFile_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::JsonUtils jsonUtils; + system("touch data/test.json"); + EXPECT_TRUE(jsonUtils.JsonToFile(OHOS::AppPackingTool::PtJson::Parse(jsonString), "data/test.json")); + system("rm -f data/test.json"); +} +} diff --git a/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4df3f8a884a37dc43174999fdde7af7c88ab492d --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("normalize_version_utils_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("normalize_version_utils_test") { + module_out_path = module_output_path + public_configs = [ ":normalize_version_utils_test_config" ] + sources = [ + "../../../../src/json/normalize_version_utils.cpp", + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "../../../../src/utils.cpp", + "normalize_version_utils_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} diff --git a/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/normalize_version_utils_test.cpp b/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/normalize_version_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb964cadc81cfcb423938ae0e83173978eed53eb --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/normalize_version_utils_test/normalize_version_utils_test.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024 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 +#include +#define private public +#define protected public +#include "normalize_version_utils.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +class NormalizeVersionUtilsTest : public testing::Test { +public: + NormalizeVersionUtilsTest() {} + virtual ~NormalizeVersionUtilsTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void NormalizeVersionUtilsTest::SetUpTestCase() {} + +void NormalizeVersionUtilsTest::TearDownTestCase() {} + +void NormalizeVersionUtilsTest::SetUp() {} + +void NormalizeVersionUtilsTest::TearDown() {} + +/* + * @tc.name: ToString_0100 + * @tc.desc: ToString. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NormalizeVersionUtilsTest, ToString_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::NormalizeVersion normalizeVersion; + normalizeVersion.originVersionCode = 100; + normalizeVersion.originVersionName = "versionNameTest"; + normalizeVersion.moduleName = "nameTest"; + + OHOS::AppPackingTool::NormalizeVersionUtils utils; + EXPECT_EQ(utils.ToString(normalizeVersion), "{\"moduleName\":\"nameTest\",\"originVersionCode\":100," + "\"originVersionName\":\"versionNameTest\"}"); +} + +/* + * @tc.name: ArrayToString_0100 + * @tc.desc: ArrayToString. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(NormalizeVersionUtilsTest, ArrayToString_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::NormalizeVersion normalizeVersion1; + normalizeVersion1.originVersionCode = 100; + normalizeVersion1.originVersionName = "versionNameTest"; + normalizeVersion1.moduleName = "nameTest"; + + OHOS::AppPackingTool::NormalizeVersion normalizeVersion2; + normalizeVersion2.originVersionCode = 200; + normalizeVersion2.originVersionName = "versionNameTest2"; + normalizeVersion2.moduleName = "nameTest2"; + + std::list normalizeVersions; + normalizeVersions.push_back(normalizeVersion1); + normalizeVersions.push_back(normalizeVersion2); + + OHOS::AppPackingTool::NormalizeVersionUtils utils; + EXPECT_EQ(utils.ArrayToString(normalizeVersions), "[{\"moduleName\":\"nameTest\",\"originVersionCode\":100," + "\"originVersionName\":\"versionNameTest\"},{\"moduleName\":\"nameTest2\",\"originVersionCode\":200," + "\"originVersionName\":\"versionNameTest2\"}]"); +} +} diff --git a/packing_tool/frameworks/test/unittest/json/pack_info_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/pack_info_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9d69b18689da2e2555d8b0367cccdbcf8e8f82de --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/pack_info_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("pack_info_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("pack_info_test") { + module_out_path = module_output_path + public_configs = [ ":pack_info_test_config" ] + sources = [ + "../../../../src/json/pack_info.cpp", + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "../../../../src/utils.cpp", + "pack_info_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} diff --git a/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp b/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cba5ab273ddfceac8b69a979ca1af68ea0e2bfe --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp @@ -0,0 +1,351 @@ +/* + * Copyright (c) 2024 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 +#define private public +#define protected public +#include "pack_info.h" +#include "pt_json.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; +using namespace std; + +namespace OHOS { + +const std::string jsonString = "{" + "\"summary\": {" + "\"app\": {" + "\"bundleName\": \"com.example.myapplication\"," + "\"bundleType\": \"bundleApp\"," + "\"version\": {" + "\"code\": 1000000," + "\"name\": \"1.0.0\"" + "}" + "}," + "\"modules\": [" + "{" + "\"mainAbility\": \"EntryAbility\"," + "\"deviceType\": [" + "\"default\"," + "\"tablet\"" + "]," + "\"abilities\": [" + "{" + "\"name\": \"EntryAbility\"," + "\"label\": \"$string:EntryAbility_label\"" + "}" + "]," + "\"distro\": {" + "\"moduleType\": \"entry\"," + "\"installationFree\": false," + "\"deliveryWithInstall\": true," + "\"moduleName\": \"entry\"" + "}," + "\"extensionAbilities\": [" + "]," + "\"apiVersion\": {" + "\"compatible\": 12," + "\"releaseType\": \"Canary2\"," + "\"target\": 12" + "}" + "}" + "]" + "}," + "\"packages\":[" + "{" + "\"deviceType\": [" + "\"default\"," + "\"tablet\"" + "]," + "\"moduleType\": \"entry\"," + "\"deliveryWithInstall\": true," + "\"name\": \"entry-default\"" + "}" + "]" +"}"; + +class PackInfoTest : public testing::Test { +public: + PackInfoTest() {} + virtual ~PackInfoTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void PackInfoTest::SetUpTestCase() {} + +void PackInfoTest::TearDownTestCase() {} + +void PackInfoTest::SetUp() {} + +void PackInfoTest::TearDown() {} + +/* + * @tc.name: ParseFromString_0100 + * @tc.desc: ParseFromString. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, ParseFromString_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + EXPECT_TRUE(packInfo.ParseFromString(jsonString)); +} + +/* + * @tc.name: GetSummaryObject_0100 + * @tc.desc: GetSummaryObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetSummaryObject_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::unique_ptr summaryObj; + EXPECT_TRUE(packInfo.GetSummaryObject(summaryObj)); + EXPECT_NE(summaryObj, nullptr); +} + +/* + * @tc.name: GetAppObject_0100 + * @tc.desc: GetAppObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetAppObject_0100, Function | MediumTest | Level1) +{ + std::unique_ptr appObj; + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + EXPECT_TRUE(packInfo.GetAppObject(appObj)); + EXPECT_NE(appObj, nullptr); +} + +/* + * @tc.name: GetBundleName_0100 + * @tc.desc: GetBundleName. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetBundleName_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::string bundleName = ""; + EXPECT_TRUE(packInfo.GetBundleName(bundleName)); + EXPECT_NE(bundleName, ""); +} + +/* + * @tc.name: SetBundleName_0100 + * @tc.desc: SetBundleName. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, SetBundleName_0100, Function | MediumTest | Level1) +{ + std::string bundleName = "com.example.myapplication"; + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + EXPECT_TRUE(packInfo.SetBundleName(bundleName)); +} + +/* + * @tc.name: GetBundleType_0100 + * @tc.desc: GetBundleType. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetBundleType_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::string bundleType = ""; + std::string defaultBundleType = "app"; + EXPECT_TRUE(packInfo.GetBundleType(bundleType, defaultBundleType)); + EXPECT_EQ(bundleType, "bundleApp"); +} + +/* + * @tc.name: GetVersionObject_0100 + * @tc.desc: GetVersionObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetVersionObject_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::unique_ptr versionObj; + EXPECT_TRUE(packInfo.GetVersionObject(versionObj)); + EXPECT_NE(versionObj, nullptr); +} + +/* + * @tc.name: GetDistroObject_0100 + * @tc.desc: GetDistroObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetDistroObject_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + int32_t moduleIndex = 0; + std::unique_ptr distroObj; + EXPECT_TRUE(packInfo.GetDistroObject(moduleIndex, distroObj)); + EXPECT_NE(distroObj, nullptr); +} + +/* + * @tc.name: GetExtensionAbilitiesObj_0100 + * @tc.desc: GetExtensionAbilitiesObj. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetExtensionAbilitiesObj_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + int32_t moduleIndex = 0; + std::unique_ptr extensionAbilitiesObj; + EXPECT_TRUE(packInfo.GetExtensionAbilitiesObj(moduleIndex, extensionAbilitiesObj)); + EXPECT_NE(extensionAbilitiesObj, nullptr); +} + +/* + * @tc.name: GetExtensionAbilitiesObjByModulesObj_0100 + * @tc.desc: GetExtensionAbilitiesObjByModulesObj. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetExtensionAbilitiesObjByModulesObj_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::unique_ptr modulesObj; + packInfo.GetModulesObject(modulesObj); + int32_t moduleIndex = 0; + std::unique_ptr extensionAbilitiesObj; + EXPECT_TRUE(packInfo.GetExtensionAbilitiesObjByModulesObj(modulesObj, moduleIndex, extensionAbilitiesObj)); + EXPECT_NE(extensionAbilitiesObj, nullptr); +} + +/* + * @tc.name: GetPackageObject_0100 + * @tc.desc: GetPackageObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetPackageObject_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + int32_t packageIndex = 0; + std::unique_ptr packageObj; + EXPECT_TRUE(packInfo.GetPackageObject(packageIndex, packageObj)); + EXPECT_NE(packageObj, nullptr); +} + +/* + * @tc.name: GetVersion_0100 + * @tc.desc: GetVersion. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetVersion_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfoVersion version; + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + EXPECT_TRUE(packInfo.GetVersion(version)); + EXPECT_EQ(version.code, 1000000); + EXPECT_EQ(version.name, "1.0.0"); +} + +/* + * @tc.name: SetVersionCode_0100 + * @tc.desc: SetVersionCode. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, SetVersionCode_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + int versionCode = 1; + EXPECT_TRUE(packInfo.SetVersionCode(versionCode)); +} + +/* + * @tc.name: SetVersionName_0100 + * @tc.desc: SetVersionName. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, SetVersionName_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::string versionName = "2.0.0"; + EXPECT_TRUE(packInfo.SetVersionName(versionName)); +} + +/* + * @tc.name: GetNameByPackageObj_0100 + * @tc.desc: GetNameByPackageObj. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetNameByPackageObj_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + int32_t packageIndex = 0; + std::unique_ptr packageObj; + packInfo.GetPackageObject(packageIndex, packageObj); + EXPECT_NE(packageObj, nullptr); + std::string name = ""; + EXPECT_TRUE(packInfo.GetNameByPackageObj(packageObj, name)); + EXPECT_EQ(name, "entry-default"); +} + +/* + * @tc.name: GetFormNames_0100 + * @tc.desc: GetFormNames. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PackInfoTest, GetFormNames_0100, Function | MediumTest | Level1) +{ + OHOS::AppPackingTool::PackInfo packInfo; + packInfo.ParseFromString(jsonString); + std::list formNames; + std::list formFullNames; + EXPECT_TRUE(packInfo.GetFormNames(formNames, formFullNames)); +} +} diff --git a/packing_tool/frameworks/test/unittest/json/pt_json_test/BUILD.gn b/packing_tool/frameworks/test/unittest/json/pt_json_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a8ae5e43077280b58d5a90428afa63b4472e35cb --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/pt_json_test/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2024 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/ohos.gni") +import("//build/test.gni") + +config("pt_json_test_config") { + include_dirs = [ + "../../../../include", + "../../../../include/json", + ] + + cflags_cc = [ "-fexceptions" ] + cflags_objcc = cflags_cc +} + +module_output_path = "developtools/packing_tool" + +ohos_unittest("pt_json_test") { + module_out_path = module_output_path + public_configs = [ ":pt_json_test_config" ] + sources = [ + "../../../../src/json/pt_json.cpp", + "../../../../src/log.cpp", + "pt_json_test.cpp", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson_static", + "hilog:libhilog", + "openssl:libcrypto_shared", + "zlib:libz", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":pt_json_test" ] +} diff --git a/packing_tool/frameworks/test/unittest/json/pt_json_test/pt_json_test.cpp b/packing_tool/frameworks/test/unittest/json/pt_json_test/pt_json_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b8793ec5160d8919b5017aab32e02910ecdc93b --- /dev/null +++ b/packing_tool/frameworks/test/unittest/json/pt_json_test/pt_json_test.cpp @@ -0,0 +1,498 @@ +/* + * Copyright (c) 2024 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 +#define private public +#define protected public +#include "pt_json.h" +#undef private +#undef protected + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +std::string content = "{" + "\"name\": \"Json.CN\"," + "\"app\": \"apptest\"," + "\"module\": {" + "\"name\": \"nametest\"," + "\"type\": \"typetest\"," + "\"deviceTypes\": [" + "\"aaaaaaaaaaa\"," + "\"bbbbbbbbbbb\"," + "\"ccccccccccc\"" + "]" + "}," + "\"bundleName\": \"bundleNametest\"," + "\"versionCode\": 100," + "\"versionName\": \"versionNametest\"," + "\"patchVersionCode\": 200," + "\"patchVersionName\": \"patchVersionNametest\"," + "\"originalModuleHash\": \"originalModuleHashtest\"" +"}"; + +class PtJsonTest : public testing::Test { +public: + PtJsonTest() {} + virtual ~PtJsonTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void PtJsonTest::SetUpTestCase() {} + +void PtJsonTest::TearDownTestCase() {} + +void PtJsonTest::SetUp() {} + +void PtJsonTest::TearDown() {} + +/* + * @tc.name: CreateObject_0100 + * @tc.desc: CreateObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, CreateObject_0100, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonObject = ptJson.CreateObject(); + EXPECT_TRUE(ptjsonObject != NULL); +} + +/* + * @tc.name: CreateArray_0200 + * @tc.desc: CreateArray. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, CreateArray_0200, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonArray = ptJson.CreateArray(); + EXPECT_TRUE(ptjsonArray != NULL); +} + +/* + * @tc.name: ReleaseRoot_0300 + * @tc.desc: ReleaseRoot. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, ReleaseRoot_0300, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + ptJson.ReleaseRoot(); + EXPECT_TRUE(ptJson.object_ == NULL); +} + +/* + * @tc.name: Parse_0400 + * @tc.desc: Parse. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Parse_0400, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjson = ptJson.Parse(content); + EXPECT_TRUE(ptjson != NULL); +} + +/* + * @tc.name: Stringify_0500 + * @tc.desc: Stringify. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Stringify_0500, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjson = ptJson.Parse(content); + EXPECT_TRUE(ptjson != NULL); + EXPECT_TRUE(!ptjson->Stringify().empty()); +} + +/* + * @tc.name: Add_0600 + * @tc.desc: Add. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Add_0600, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonObject = ptJson.CreateObject(); + EXPECT_TRUE(ptjsonObject != NULL); + + EXPECT_TRUE(ptJson.Add("AAA", true)); + EXPECT_TRUE(ptJson.Add("BBB", 123)); + EXPECT_TRUE(ptJson.Add("CCC", 123.5)); + EXPECT_TRUE(ptJson.Add("DDD", "ABC")); + EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject)); +} + +/* + * @tc.name: Push_0700 + * @tc.desc: Push. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Push_0700, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonObject = ptJson.CreateObject(); + EXPECT_TRUE(ptjsonObject != NULL); + + std::unique_ptr ptjsonArray = ptJson.CreateArray(); + EXPECT_TRUE(ptjsonArray != NULL); + + EXPECT_TRUE(ptJson.Add("AAA", true)); + EXPECT_TRUE(ptJson.Add("BBB", 123)); + EXPECT_TRUE(ptJson.Add("CCC", 123.5)); + EXPECT_TRUE(ptJson.Add("DDD", "ABC")); + EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject)); + + EXPECT_TRUE(ptJson.Push(true)); + EXPECT_TRUE(ptJson.Push(123)); + EXPECT_TRUE(ptJson.Push(123.5)); + EXPECT_TRUE(ptJson.Push("ABC")); + EXPECT_TRUE(ptJson.Push(ptjsonArray)); +} + +/* + * @tc.name: Contains_0800 + * @tc.desc: Contains. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Contains_0800, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + EXPECT_TRUE(ptJson.Add("AAA", true)); + EXPECT_TRUE(ptJson.Remove("AAA")); +} + +/* + * @tc.name: Contains_0900 + * @tc.desc: Contains. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, Contains_0900, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + EXPECT_TRUE(ptJson.Add("AAA", true)); + EXPECT_TRUE(ptJson.Contains("AAA")); +} + +/* + * @tc.name: GetKey_1000 + * @tc.desc: GetKey. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, GetKey_1000, Function | MediumTest | Level1) +{ + char a[] = "{\"firstName\":\"Brett\"}"; + cJSON*root = cJSON_Parse(a); + cJSON*item = cJSON_GetObjectItem(root, "firstName"); + OHOS::AppPackingTool::PtJson ptJson(item); + EXPECT_TRUE(!ptJson.GetKey().empty()); + EXPECT_TRUE(!ptJson.Stringify().empty()); +} + +/* + * @tc.name: GetJson_1100 + * @tc.desc: GetJson. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, GetJson_1100, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + EXPECT_TRUE(ptJson.GetJson() != nullptr); +} + +/* + * @tc.name: IsBool_1200 + * @tc.desc: IsBool. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsBool_1200, Function | MediumTest | Level1) +{ + cJSON *node = cJSON_CreateBool(true); + OHOS::AppPackingTool::PtJson ptJsonBool(node); + + EXPECT_TRUE(ptJsonBool.IsBool()); + EXPECT_TRUE(ptJsonBool.GetBool(true)); +} + +/* + * @tc.name: IsNumber_1300 + * @tc.desc: IsNumber. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsNumber_1300, Function | MediumTest | Level1) +{ + cJSON *node = cJSON_CreateNumber(12345); + OHOS::AppPackingTool::PtJson ptJsonNumber(node); + + EXPECT_TRUE(ptJsonNumber.IsNumber()); + EXPECT_EQ(ptJsonNumber.GetInt(12345), 12345); + EXPECT_EQ(ptJsonNumber.GetInt64(12345), 12345); + EXPECT_EQ(ptJsonNumber.GetUInt(12345), 12345); + EXPECT_EQ(ptJsonNumber.GetUInt64(12345), 12345); + EXPECT_EQ(ptJsonNumber.GetDouble(12345.5), 12345); +} + +/* + * @tc.name: IsString_1400 + * @tc.desc: IsString. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsString_1400, Function | MediumTest | Level1) +{ + cJSON *node = cJSON_CreateString("abcd"); + OHOS::AppPackingTool::PtJson ptJsonString(node); + + EXPECT_TRUE(ptJsonString.IsString()); + EXPECT_STREQ(ptJsonString.GetString().c_str(), "abcd"); +} + +/* + * @tc.name: IsObject_1500 + * @tc.desc: IsObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsObject_1500, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonObject = ptJson.CreateObject(); + EXPECT_TRUE(ptjsonObject != NULL); + EXPECT_TRUE(ptjsonObject->IsObject()); +} + +/* + * @tc.name: IsArray_1600 + * @tc.desc: IsArray. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsArray_1600, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonArray = ptJson.CreateArray(); + EXPECT_TRUE(ptjsonArray != NULL); + EXPECT_TRUE(ptjsonArray->IsArray()); + ptjsonArray->Push("11111"); + ptjsonArray->Push("22222"); + EXPECT_STREQ(ptjsonArray->Get(1)->GetString().c_str(), "22222"); + EXPECT_TRUE(ptjsonArray->GetSize() > 0); +} + +/* + * @tc.name: IsNull_1700 + * @tc.desc: IsNull. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, IsNull_1700, Function | MediumTest | Level1) +{ + cJSON *cjson = cJSON_CreateNull(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + EXPECT_TRUE(ptJson.IsNull()); +} + +/* + * @tc.name: SetBool_1800 + * @tc.desc: SetBool. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, SetBool_1800, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + bool flag = false; + ptJson.Add("AAA", true); + EXPECT_EQ(ptJson.SetBool("AAA", flag), OHOS::AppPackingTool::Result::SUCCESS); + ptJson.GetBool("AAA", &flag); + EXPECT_FALSE(flag); +} + +/* + * @tc.name: SetInt_1900 + * @tc.desc: SetInt. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, SetInt_1900, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + EXPECT_TRUE(ptJson.Add("BBB", 123)); + int number = 0; + EXPECT_EQ(ptJson.SetInt("BBB", 321), OHOS::AppPackingTool::Result::SUCCESS); + ptJson.GetInt("BBB", &number); + EXPECT_EQ(number, 321); +} + +/* + * @tc.name: SetInt64_2000 + * @tc.desc: SetInt64. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, SetInt64_2000, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + EXPECT_TRUE(ptJson.Add("BBB", 123)); + EXPECT_TRUE(ptJson.Add("CCC", 123.5)); + + int64_t value64 = 11111; + EXPECT_EQ(ptJson.SetInt64("BBB", value64), OHOS::AppPackingTool::Result::SUCCESS); + int64_t value642; + EXPECT_EQ(ptJson.GetInt64("BBB", &value642), OHOS::AppPackingTool::Result::SUCCESS); + EXPECT_EQ(value642, 11111); + + uint32_t value32 = 2222; + EXPECT_EQ(ptJson.SetUInt("BBB", value32), OHOS::AppPackingTool::Result::SUCCESS); + uint32_t value322; + EXPECT_EQ(ptJson.GetUInt("BBB", &value322), OHOS::AppPackingTool::Result::SUCCESS); + EXPECT_EQ(value322, 2222); + + uint64_t valueInt64 = 3333; + EXPECT_EQ(ptJson.SetUInt64("BBB", valueInt64), OHOS::AppPackingTool::Result::SUCCESS); + uint64_t valueInt642; + EXPECT_EQ(ptJson.GetUInt64("BBB", &valueInt642), OHOS::AppPackingTool::Result::SUCCESS); + EXPECT_EQ(valueInt642, 3333); + + double valueDouble = 4444; + EXPECT_EQ(ptJson.SetDouble("CCC", valueDouble), OHOS::AppPackingTool::Result::SUCCESS); + double valueDouble2; + EXPECT_EQ(ptJson.GetDouble("CCC", &valueDouble2), OHOS::AppPackingTool::Result::SUCCESS); + EXPECT_EQ(valueDouble2, 4444); +} + +/* + * @tc.name: SetString_2100 + * @tc.desc: SetString. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, SetString_2100, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + EXPECT_TRUE(ptJson.Add("DDD", "ABC")); + std::string str("1234567890"); + EXPECT_EQ(ptJson.SetString("DDD", str), OHOS::AppPackingTool::Result::SUCCESS); + std::string str2; + EXPECT_EQ(ptJson.GetString("DDD", &str2), OHOS::AppPackingTool::Result::SUCCESS); + EXPECT_STREQ(str2.c_str(), str.c_str()); +} + +/* + * @tc.name: GetObject_2200 + * @tc.desc: GetObject. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, GetObject_2200, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + std::unique_ptr ptjsonObject = ptJson.CreateObject(); + EXPECT_TRUE(ptjsonObject != NULL); + EXPECT_TRUE(ptJson.Add("EEE", ptjsonObject)); + std::unique_ptr Object; + EXPECT_EQ(ptJson.GetObject("EEE", &Object), OHOS::AppPackingTool::Result::SUCCESS); +} + +/* + * @tc.name: GetArray_2300 + * @tc.desc: GetArray. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, GetArray_2300, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + ptJson.Add("FFF", ptJson.CreateArray()); + std::unique_ptr Array; + EXPECT_EQ(ptJson.GetArray("FFF", &Array), OHOS::AppPackingTool::Result::SUCCESS); +} + +/* + * @tc.name: GetAny_2400 + * @tc.desc: GetAny. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(PtJsonTest, GetAny_2400, Function | MediumTest | Level1) +{ + cJSON *cjson = new cJSON(); + OHOS::AppPackingTool::PtJson ptJson(cjson); + + ptJson.Add("FFF", ptJson.CreateArray()); + std::unique_ptr Object; + EXPECT_EQ(ptJson.GetAny("FFF", &Object), OHOS::AppPackingTool::Result::SUCCESS); +} +} // namespace OHOS \ No newline at end of file