diff --git a/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp index 1d9aeb7d..8b98a2b1 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp @@ -1404,4 +1404,145 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_41, TestSize.Level0) GTEST_LOG_(INFO) << "App_Spawn_Sandbox_41 end"; } + +/** +* @tc.name: App_Spawn_Sandbox_42 +* @tc.desc: parse config file for fstype . +* @tc.type: FUNC +* @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K +* @tc.author: +*/ +HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_42, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_42 start"; + std::string mJsconfig = "{ \ + \"mount-paths\": [{ \ + \"src-path\": \"/data/app/el1//base\", \ + \"sandbox-path\": \"/storage/Users//appdata/el1\", \ + \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \ + \"dac-override-sensitive\": \"true\", \ + \"fs-type\": \"sharefs\", \ + \"options\": \"support_overwrite=1\" \ + }] \ + }"; + nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str()); + const char *mountPath = "mount-paths"; + nlohmann::json j_secondConfig = j_config[mountPath][0]; + OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config); + + std::string fsType = OHOS::AppSpawn::SandboxUtils::GetSandboxFsType(j_secondConfig); + int ret = strcmp(fsType.c_str(), "sharefs"); + if (SandboxUtils::deviceTypeEnable_ == true) { + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } + + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_42 end"; +} + +/** +* @tc.name: App_Spawn_Sandbox_43 +* @tc.desc: get sandbox mount config when section is common. +* @tc.type: FUNC +* @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K +* @tc.author: +*/ +HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_43, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_43 start"; + std::string mJsconfig = "{ \ + \"mount-paths\": [{ \ + \"src-path\": \"/data/app/el1//base\", \ + \"sandbox-path\": \"/storage/Users//appdata/el1\", \ + \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \ + \"dac-override-sensitive\": \"true\", \ + \"fs-type\": \"sharefs\", \ + \"options\": \"support_overwrite=1\" \ + }] \ + }"; + nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str()); + const char *mountPath = "mount-paths"; + nlohmann::json j_secondConfig = j_config[mountPath][0]; + + OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config); + OHOS::AppSpawn::SandboxUtils::SandboxMountConfig mountConfig; + std::string section = "common"; + OHOS::AppSpawn::SandboxUtils::GetSandboxMountConfig(section, j_secondConfig, mountConfig); + int ret = strcmp(mountConfig.fsType.c_str(), "sharefs"); + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_43 end"; +} + +/** +* @tc.name: App_Spawn_Sandbox_44 +* @tc.desc: get sandbox mount config when section is permission. +* @tc.type: FUNC +* @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K +* @tc.author: +*/ +HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_44, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_44 start"; + std::string mJsconfig = "{ \ + \"mount-paths\": [{ \ + \"src-path\": \"/data/app/el1//base\", \ + \"sandbox-path\": \"/storage/Users//appdata/el1\", \ + \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \ + \"dac-override-sensitive\": \"true\", \ + \"fs-type\": \"sharefs\", \ + \"options\": \"support_overwrite=1\" \ + }] \ + }"; + nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str()); + const char *mountPath = "mount-paths"; + nlohmann::json j_secondConfig = j_config[mountPath][0]; + + OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config); + OHOS::AppSpawn::SandboxUtils::SandboxMountConfig mountConfig; + std::string section = "permission"; + OHOS::AppSpawn::SandboxUtils::GetSandboxMountConfig(section, j_secondConfig, mountConfig); + int ret = strcmp(mountConfig.fsType.c_str(), "sharefs"); + if (SandboxUtils::deviceTypeEnable_ == true) { + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_44 end"; +} + +/** +* @tc.name: App_Spawn_Sandbox_45 +* @tc.desc: parse config file for options. +* @tc.type: FUNC +* @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K +* @tc.author: +*/ +HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_45, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_45 start"; + std::string mJsconfig = "{ \ + \"mount-paths\": [{ \ + \"src-path\": \"/data/app/el1//base\", \ + \"sandbox-path\": \"/storage/Users//appdata/el1\", \ + \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \ + \"dac-override-sensitive\": \"true\", \ + \"fs-type\": \"sharefs\", \ + \"options\": \"support_overwrite=1\" \ + }] \ + }"; + nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str()); + const char *mountPath = "mount-paths"; + nlohmann::json j_secondConfig = j_config[mountPath][0]; + + OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config); + std::string options = OHOS::AppSpawn::SandboxUtils::GetSandboxOptions(j_secondConfig); + int ret = strcmp(options.c_str(), "support_overwrite=1"); + if (SandboxUtils::deviceTypeEnable_ == true) { + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } + GTEST_LOG_(INFO) << "App_Spawn_Sandbox_45 end"; +} } // namespace OHOS diff --git a/util/include/sandbox_utils.h b/util/include/sandbox_utils.h index 37f60418..540248a0 100644 --- a/util/include/sandbox_utils.h +++ b/util/include/sandbox_utils.h @@ -38,8 +38,7 @@ public: static std::set GetMountPermissionNames(); typedef struct { unsigned long mountFlags; - const char *optionsPoint; - const char *fsTypePoint; + std::string optionsPoint; std::string fsType; std::string sandboxPath; } SandboxMountConfig; @@ -107,8 +106,8 @@ private: static int32_t GetMountPermissionFlags(const std::string permissionName); static bool GetSandboxDacOverrideEnable(nlohmann::json &config); static unsigned long GetSandboxMountFlags(nlohmann::json &config); - static const char *GetSandboxFsType(nlohmann::json &config); - static const char *GetSandboxOptions(nlohmann::json &config); + static std::string GetSandboxFsType(nlohmann::json &config); + static std::string GetSandboxOptions(nlohmann::json &config); static std::string GetSandboxPath(const ClientSocket::AppProperty *appProperty, nlohmann::json &mntPoint, const std::string §ion, std::string sandboxRoot); static void GetSandboxMountConfig(const std::string §ion, nlohmann::json &mntPoint, diff --git a/util/src/sandbox_utils.cpp b/util/src/sandbox_utils.cpp index 93c1f44c..efa912ab 100644 --- a/util/src/sandbox_utils.cpp +++ b/util/src/sandbox_utils.cpp @@ -443,7 +443,7 @@ unsigned long SandboxUtils::GetSandboxMountFlags(nlohmann::json &config) return mountFlags; } -const char *SandboxUtils::GetSandboxFsType(nlohmann::json &config) +std::string SandboxUtils::GetSandboxFsType(nlohmann::json &config) { std::string fsType; if (GetSandboxDacOverrideEnable(config) && (deviceTypeEnable_ == true) @@ -452,11 +452,10 @@ const char *SandboxUtils::GetSandboxFsType(nlohmann::json &config) } else { fsType = ""; } - const char *fsTypePoint = fsType.empty() ? nullptr : fsType.c_str(); - return fsTypePoint; + return fsType; } -const char *SandboxUtils::GetSandboxOptions(nlohmann::json &config) +std::string SandboxUtils::GetSandboxOptions(nlohmann::json &config) { std::string options; if (GetSandboxDacOverrideEnable(config) && (deviceTypeEnable_ == true) && @@ -465,8 +464,7 @@ const char *SandboxUtils::GetSandboxOptions(nlohmann::json &config) } else { options = ""; } - const char *optionsPoint = options.empty() ? nullptr : options.c_str(); - return optionsPoint; + return options; } void SandboxUtils::GetSandboxMountConfig(const std::string §ion, nlohmann::json &mntPoint, @@ -474,12 +472,10 @@ void SandboxUtils::GetSandboxMountConfig(const std::string §ion, nlohmann::j { if (section.compare(g_permissionPrefix) == 0) { mountConfig.optionsPoint = GetSandboxOptions(mntPoint); - mountConfig.fsTypePoint = GetSandboxFsType(mntPoint); - mountConfig.fsType = (mountConfig.fsTypePoint != nullptr) ? mountConfig.fsTypePoint : ""; + mountConfig.fsType = GetSandboxFsType(mntPoint); } else { mountConfig.fsType = (mntPoint.find(g_fsType) != mntPoint.end()) ? mntPoint[g_fsType].get() : ""; - mountConfig.fsTypePoint = mountConfig.fsType.empty() ? nullptr : mountConfig.fsType.c_str(); - mountConfig.optionsPoint = nullptr; + mountConfig.optionsPoint = ""; } return; } @@ -536,8 +532,8 @@ int SandboxUtils::DoAllMntPointsMount(const ClientSocket::AppProperty *appProper /* if app mount failed for special strategy, we need deal with common mount config */ int ret = HandleSpecialAppMount(appProperty, srcPath, sandboxPath, mountConfig.fsType, mountFlags); if (ret < 0) { - ret = DoAppSandboxMountOnce(srcPath.c_str(), sandboxPath.c_str(), mountConfig.fsTypePoint, - mountFlags, mountConfig.optionsPoint, mountSharedFlag); + ret = DoAppSandboxMountOnce(srcPath.c_str(), sandboxPath.c_str(), mountConfig.fsType.c_str(), + mountFlags, mountConfig.optionsPoint.c_str(), mountSharedFlag); } if (ret) { std::string actionStatus = g_statusCheck;