diff --git a/arkweb_utils/arkweb_utils.cpp b/arkweb_utils/arkweb_utils.cpp index 815da6aef7137360d21f4adf9337501c50579240..d8d037a31d1ff554e87ae124a2390968b5d068de 100644 --- a/arkweb_utils/arkweb_utils.cpp +++ b/arkweb_utils/arkweb_utils.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #if (defined(webview_arm64) && !defined(ASAN_DETECTOR)) #include "ace_forward_compatibility.h" @@ -92,6 +94,11 @@ const std::string SANDBOX_EVERGREEN_HAP_PATH = "/data/storage/el1/bundle/arkwebc const std::string JSON_CONFIG_PATH = "/data/service/el1/public/update/param_service/install/system/etc/ArkWebSafeBrowsing/generic/ArkWebCoreCfg.json"; const std::string WEB_PARAM_PREFIX = "web.engine."; +const std::string SYSTEM_PARAM_VERSION_PATH = + "/system/etc/ArkWebSafeBrowsing/generic/version.txt"; +const std::string UPDATE_PARAM_VERSION_PATH = + "/data/service/el1/public/update/param_service/install/system/etc/ArkWebSafeBrowsing/generic/version.txt"; +const int VERSION_TAG_LEN = 3; #if (defined(webview_arm64) && !defined(ASAN_DETECTOR)) const int MAX_DLCLOSE_COUNT = 10; @@ -205,6 +212,68 @@ static void ProcessJsonConfig(const Json::Value& root) } } +static bool GetVersionString(const std::string& versionFilePath, std::string& versionStr) +{ + std::ifstream file(versionFilePath); + if(!file.is_open()) { + WVLOG_E("can not open version file: %{public}s", versionFilePath.c_str()); + return false; + } + + if (std::getline(file, versionStr)) { + return true; + } + + WVLOG_E("%{public}s is empty.", versionFilePath.c_str()); + return false; +} + +static bool HandleVersionString(const std::string& versionStr, long long& versionNum) +{ + std::regex pattern(R"(version\s{0,1}=\s{0,1})(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}))"); + std::smatch matches; + if (std::regex_match(versionStr, matches, pattern)) { + std::ostringstream versionStream; + for (size_t i = 1; i < matches.size(); ++i) { + int num = std::stoi(matches[i].str()); + versionStream << std::setw(VERSION_TAG_LEN) << std::setfill('0') << num; + } + versionNum = std::stoll(versionStream.str()); + return true; + } + + WVLOG_E("incorrect version format, must be aa.bb.xx.yy: %{public}s", versionStr.c_str()); + return false; +} + +static bool CheckCloudCfgVersion(const std::string& systemParamVersionPath, + const std::string& updateParamVersionPath) +{ + std::string systemParamVersionStr; + std::string updateParamVersionStr; + if (!GetVersionString(systemParamVersionPath, systemParamVersionStr) || + !GetVersionString(updateParamVersionPath, updateParamVersionStr)) { + return false; + } + + long long systemParamVersionNum; + long long updateParamVersionNum; + if (!HandleVersionString(systemParamVersionStr, systemParamVersionNum) || + !HandleVersionString(updateParamVersionStr, updateParamVersionNum)) { + return false; + } + + if (updateParamVersionNum >= systemParamVersionNum) { + WVLOG_I("web param update version %{public}lld is more than system version %{public}lld, update valid.", + updateParamVersionNum, systemParamVersionNum); + return true; + } else { + WVLOG_I("web param update version %{public}lld is not more than system version %{public}lld, update invalid.", + updateParamVersionNum, systemParamVersionNum); + return false; + } +} + static void ParseCloudCfg() { std::ifstream jsonFile(JSON_CONFIG_PATH.c_str()); @@ -213,6 +282,10 @@ static void ParseCloudCfg() return; } + if (!CheckCloudCfgVersion(SYSTEM_PARAM_VERSION_PATH, UPDATE_PARAM_VERSION_PATH)) { + return; + } + Json::Value root; Json::CharReaderBuilder readerBuilder; std::string parseErrors; diff --git a/test/unittest/arkweb_utils_test/BUILD.gn b/test/unittest/arkweb_utils_test/BUILD.gn index c8764e4d876e970f244857f923e996cd95a434b6..9777fb41a5e553d13521106c18e01cba9ee40452 100644 --- a/test/unittest/arkweb_utils_test/BUILD.gn +++ b/test/unittest/arkweb_utils_test/BUILD.gn @@ -57,6 +57,7 @@ ohos_unittest("arkweb_utils_test") { "init:libbegetutil", "webview:libnweb", "window_manager:libwm", + "ace_engine:ace_forward_compatibility", ] } diff --git a/test/unittest/arkweb_utils_test/arkweb_utils_test.cpp b/test/unittest/arkweb_utils_test/arkweb_utils_test.cpp index 337505d2aae89e40d212ded5343d6ebe2b4d857a..eb151ea2346995101451df7dbffab71da15c16fa 100644 --- a/test/unittest/arkweb_utils_test/arkweb_utils_test.cpp +++ b/test/unittest/arkweb_utils_test/arkweb_utils_test.cpp @@ -37,12 +37,28 @@ const std::string ARK_WEB_CORE_ASAN_PATH_FOR_BUNDLE = "arkwebcore_asan/libs/arm" #endif #endif +class FileProcessor { +public: + static void WriteFile(const std::string& fileName, const std::string& content) + { + std::ofstream file(fileName); + if(!file.is_open()) { + return; + } + + file << content; + } +}; + class ArkWebUtilsTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); + + std::string systemTmpVersionFile_ = "system_version_tmp.txt"; + std::string updateTmpVersionFile_ = "update_version_tmp.txt"; }; void ArkWebUtilsTest::SetUpTestCase(void) @@ -378,4 +394,34 @@ HWTEST_F(ArkWebUtilsTest, ArkWebUtilsTest_ProcessParamItem_005, TestSize.Level1) EXPECT_NE(g_legacyApp, nullptr); EXPECT_EQ(g_legacyApp->find(appBundleName) != g_legacyApp->end(), true); } + +HWTEST_F(ArkWebUtilsTest, ArkWebUtilsTest_GetVersionString_001, TestSize.Level1) +{ + std::string systemParamVersionPath = "system_version_tmp_not_exist.txt"; + std::string systemParamVersionStr; + EXPECT_EQ(GetVersionString(systemParamVersionPath, systemParamVersionStr), false); +} + +HWTEST_F(ArkWebUtilsTest, ArkWebUtilsTest_CheckCloudCfgVersion_001, TestSize.Level1) +{ + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), false); + + std::string wrongVersionTxt = "version=1000.00.0.0"; + FileProcessor::WriteFile(systemTmpVersionFile_, wrongVersionTxt); + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), false); + + FileProcessor::WriteFile(updateTmpVersionFile_, wrongVersionTxt); + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), false); + + std::string versionTxt = "version=1.10.25.100"; + FileProcessor::WriteFile(systemTmpVersionFile_, versionTxt); + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), false); + + FileProcessor::WriteFile(updateTmpVersionFile_, versionTxt); + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), true); + + std::string higherVersionTxt = "version=1.10.25.101"; + FileProcessor::WriteFile(systemTmpVersionFile_, higherVersionTxt); + EXPECT_EQ(CheckCloudCfgVersion(systemTmpVersionFile_, updateTmpVersionFile_), false); +} } // namespace OHOS::NWeb \ No newline at end of file