diff --git a/cj_environment/frameworks/cj_environment/src/cj_environment.cpp b/cj_environment/frameworks/cj_environment/src/cj_environment.cpp index d55b1151499fa4888d229866c52308ec2cf804d0..9c724d48cb385f3481b72cb7aa3145a60f45504c 100644 --- a/cj_environment/frameworks/cj_environment/src/cj_environment.cpp +++ b/cj_environment/frameworks/cj_environment/src/cj_environment.cpp @@ -43,9 +43,8 @@ namespace { const std::string SANDBOX_LIB_PATH = "/data/storage/el1/bundle/libs/" APP_LIB_NAME; -const std::string CJ_RT_PATH = SANDBOX_LIB_PATH + "/runtime"; -const std::string CJ_LIB_PATH = SANDBOX_LIB_PATH + "/ohos"; -const std::string CJ_SYSLIB_PATH = "/system/lib64:/system/lib64/platformsdk"; +const std::string CJ_COMPATIBILITY_PATH = SANDBOX_LIB_PATH + "/runtime"; +const std::string CJ_MOCK_PATH = SANDBOX_LIB_PATH + "/ohos"; const std::string CJ_CHIPSDK_PATH = "/system/lib64/chipset-pub-sdk:/system/lib64/chipset-sdk"; const std::string CJ_SDK_PATH = "/system/lib64/platformsdk/cjsdk"; const std::string CJ_RUNTIME_PATH = "/system/lib64/platformsdk/cjsdk/runtime"; @@ -237,6 +236,7 @@ const char *CJEnvironment::cjRomSDKNSName = "cj_rom_sdk"; const char *CJEnvironment::cjSysNSName = "default"; const char *CJEnvironment::cjCompatibilitySDKNSName = "cj_compatibility_sdk"; const char *CJEnvironment::cjRuntimeNSName = "cj_runtime"; +const char *CJEnvironment::cjMockNSName = "cj_mock_sdk"; std::string CJEnvironment::appVersion = "5.1.0.0"; const uint32_t CJEnvironment::majorVersion = 5; const uint32_t CJEnvironment::minorVersion = 1; @@ -338,14 +338,8 @@ bool CJEnvironment::RegisterCangjieCallback() void* CJEnvironment::LoadRuntimeLib(const char* runtimeLibName) { Dl_namespace compatibilitySDK; - Dl_namespace romSDK; dlns_get(cjCompatibilitySDKNSName, &compatibilitySDK); - dlns_get(cjRomSDKNSName, &romSDK); auto dso = DynamicLoadLibrary(&compatibilitySDK, runtimeLibName, 1); - if (!dso) { - LOGI("Try to load %{public}s in rom.", runtimeLibName); - dso = DynamicLoadLibrary(&romSDK, runtimeLibName, 1); - } return dso; } @@ -544,6 +538,7 @@ void CJEnvironment::InitCJAppNS(const std::string& path) Dl_namespace ns; DynamicInitNamespace(&ns, path.c_str(), CJEnvironment::cjAppNSName); DynamicInherit(&ns, CJEnvironment::cjCompatibilitySDKNSName, "allow_all_shared_libs"); + DynamicInherit(&ns, CJEnvironment::cjMockNSName, "allow_all_shared_libs"); DynamicInherit(&ns, CJEnvironment::cjRomSDKNSName, "allow_all_shared_libs"); DynamicInherit(&ns, CJEnvironment::cjRuntimeNSName, "allow_all_shared_libs"); if (nsMode_ == NSMode::APP) { @@ -568,8 +563,11 @@ void CJEnvironment::InitCJCompatibilitySDKNS(const std::string& path) LOGI("InitCJCompatibilitySDKNS: %{public}s", path.c_str()); Dl_namespace ns; DynamicInitNamespace(&ns, path.c_str(), CJEnvironment::cjCompatibilitySDKNSName); + DynamicInherit(&ns, CJEnvironment::cjMockNSName, "allow_all_shared_libs"); DynamicInherit(&ns, CJEnvironment::cjRomSDKNSName, "allow_all_shared_libs"); DynamicInherit(&ns, CJEnvironment::cjRuntimeNSName, "allow_all_shared_libs"); + DynamicInheritByName(CJEnvironment::cjMockNSName, + CJEnvironment::cjCompatibilitySDKNSName, "allow_all_shared_libs"); #endif } @@ -582,6 +580,15 @@ void CJEnvironment::InitCJRuntimeNS(const std::string& path) #endif } +void CJEnvironment::InitCJMockNS(const std::string& path) +{ +#ifdef __OHOS__ + LOGI("InitCJMockNS: %{public}s", path.c_str()); + Dl_namespace ns; + DynamicInitNamespace(&ns, path.c_str(), CJEnvironment::cjMockNSName); +#endif +} + void CJEnvironment::UnLoadRuntimeApis() { if (lazyApis_ != nullptr) { @@ -677,7 +684,6 @@ void* CJEnvironment::LoadCJLibrary(OHOS::CJEnvironment::LibraryKind kind, const { #ifdef __OHOS__ Dl_namespace ns; - Dl_namespace sdk; switch (kind) { case APP: dlns_get(CJEnvironment::cjAppNSName, &ns); @@ -687,14 +693,9 @@ void* CJEnvironment::LoadCJLibrary(OHOS::CJEnvironment::LibraryKind kind, const break; case SDK: dlns_get(CJEnvironment::cjCompatibilitySDKNSName, &ns); - dlns_get(CJEnvironment::cjRomSDKNSName, &sdk); break; } auto handle = DynamicLoadLibrary(&ns, dlName, 0); - if (!handle && kind == SDK) { - LOGI("Try to load %{public}s in rom.", dlName); - handle = DynamicLoadLibrary(&sdk, dlName, 0); - } #else auto handle = DynamicLoadLibrary(dlName, 1); #endif @@ -792,8 +793,9 @@ void CJEnvironment::InitRuntimeNS() if (nsMode_ == NSMode::APP) { InitCJChipSDKNS(CJ_CHIPSDK_PATH); } + InitCJMockNS(CJ_MOCK_PATH); InitCJRomSDKNS(CJ_SDK_PATH); - InitCJCompatibilitySDKNS(CJ_RT_PATH + ":" + CJ_LIB_PATH); + InitCJCompatibilitySDKNS(CJ_COMPATIBILITY_PATH); #endif } diff --git a/cj_environment/frameworks/cj_environment/src/dynamic_loader_ohos.cpp b/cj_environment/frameworks/cj_environment/src/dynamic_loader_ohos.cpp index 9d48c38afc2cf8acc60a9a20731803e1333541eb..300c695dab47b9e671f9bffe96ee64f3cfdf14ff 100644 --- a/cj_environment/frameworks/cj_environment/src/dynamic_loader_ohos.cpp +++ b/cj_environment/frameworks/cj_environment/src/dynamic_loader_ohos.cpp @@ -215,7 +215,7 @@ void DynamicInherit(Dl_namespace* child, const char* parent, const char* shared) Dl_namespace parentNamespace; int res = dlns_get(parent, &parentNamespace); if (res != 0) { - LOGE("Fail to get ns %{public}s'.", parent); + LOGE("Fail to get ns %{public}s.", parent); return; } res = dlns_inherit(child, &parentNamespace, shared); @@ -224,4 +224,37 @@ void DynamicInherit(Dl_namespace* child, const char* parent, const char* shared) return; } } + +void DynamicInheritByName(const char* child, const char* parent, const char* shared) +{ + if (child == nullptr) { + LOGE("child namespace is null."); + return; + } + if (parent == nullptr) { + LOGE("parent namespace name is null."); + return; + } + if (shared == nullptr) { + LOGE("shared libs is null."); + return; + } + Dl_namespace childNamespace; + Dl_namespace parentNamespace; + int res = dlns_get(child, &childNamespace); + if (res != 0) { + LOGE("Fail to get ns %{public}s.", child); + return; + } + res = dlns_get(parent, &parentNamespace); + if (res != 0) { + LOGE("Fail to get ns %{public}s.", parent); + return; + } + res = dlns_inherit(&childNamespace, &parentNamespace, shared); + if (res != 0) { + LOGE("ns %{public}s failed to inherit ns %{public}s.", child, parent); + return; + } +} } diff --git a/cj_environment/interfaces/inner_api/cj_environment.h b/cj_environment/interfaces/inner_api/cj_environment.h index 205fe5634740ba86b2211f2ea75f8b16cac819b7..eb3def14f4edf23c23eaa664008265a6eb3469d8 100644 --- a/cj_environment/interfaces/inner_api/cj_environment.h +++ b/cj_environment/interfaces/inner_api/cj_environment.h @@ -96,6 +96,7 @@ public: void InitCJCompatibilitySDKNS(const std::string& path); void InitCJChipSDKNS(const std::string& path); void InitCJRuntimeNS(const std::string& path); + void InitCJMockNS(const std::string& path); void InitRuntimeNS(); void* InitUIScheduler(); int FiniCJRuntime(); @@ -114,6 +115,7 @@ public: static const char *cjSysNSName; static const char *cjCompatibilitySDKNSName; static const char *cjRuntimeNSName; + static const char *cjMockNSName; static std::string appVersion; static const uint32_t majorVersion; static const uint32_t minorVersion; diff --git a/cj_environment/interfaces/inner_api/dynamic_loader.h b/cj_environment/interfaces/inner_api/dynamic_loader.h index c815d6179008f13c4ec071ccc4e3bd59b34abc22..6466a6e8d11f73038f26fb17028e5d6e5883f4b5 100644 --- a/cj_environment/interfaces/inner_api/dynamic_loader.h +++ b/cj_environment/interfaces/inner_api/dynamic_loader.h @@ -32,6 +32,7 @@ void DynamicFreeLibrary(void* so); #ifdef __OHOS__ void DynamicInitNamespace(Dl_namespace* ns, const char* entries, const char* name); void DynamicInherit(Dl_namespace* child, const char* parent, const char* shared); +void DynamicInheritByName(const char* child, const char* parent, const char* shared); #endif }; diff --git a/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp b/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp index ccc2b1914a572a79a2074d658286212960ba517d..0892ba0a82661913faa9c18d7a8843db2695858c 100644 --- a/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp +++ b/cj_environment/test/unittest/cj_environment_test/cj_environment_test.cpp @@ -322,4 +322,17 @@ HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJNS_0100, TestSize.Level2) cJEnvironment.InitCJNS(appPath); EXPECT_EQ(CJEnvironment::GetInstance(), nullptr); } + +/** + * @tc.name: InitCJMockNS_0100 + * @tc.desc: Test InitCJMockNS. + * @tc.type: FUNC + */ +HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJMockNS_0100, TestSize.Level2) +{ + CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP); + std::string appPath = "com/ohos/unittest/test/"; + cJEnvironment.InitCJMockNS(appPath); + EXPECT_EQ(CJEnvironment::GetInstance(), nullptr); +} } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/cj_environment_test/cj_environment_test.cpp b/test/unittest/cj_environment_test/cj_environment_test.cpp index 9e08816dc5f121703faf5cccd7d733176ea91470..fb3dad689f2efeec77b09426bd7e4736f36f4fb1 100644 --- a/test/unittest/cj_environment_test/cj_environment_test.cpp +++ b/test/unittest/cj_environment_test/cj_environment_test.cpp @@ -296,6 +296,7 @@ HWTEST_F(CjEnvironmentTest, CjEnvironmentTestDynamicInherit_001, TestSize.Level2 Dl_namespace ns; dlns_get("cj_test_namespace", &ns); DynamicInherit(&ns, "cj_test_namespace_2", "allow_all_shared_libs"); + DynamicInheritByName("cj_test_namespace", "cj_test_namespace_2", "allow_all_shared_libs"); EXPECT_EQ(CJEnvironment::GetInstance(), nullptr); } @@ -309,6 +310,8 @@ HWTEST_F(CjEnvironmentTest, CjEnvironmentTestDynamicInherit_002, TestSize.Level2 Dl_namespace ns; dlns_get("cj_test_namespace", &ns); DynamicInherit(&ns, "default", "allow_all_shared_libs"); + DynamicInheritByName("cj_test_namespace", "default", "allow_all_shared_libs"); + DynamicInheritByName("default", "cj_test_namespace", "allow_all_shared_libs"); EXPECT_EQ(CJEnvironment::GetInstance(), nullptr); } @@ -322,6 +325,9 @@ HWTEST_F(CjEnvironmentTest, CjEnvironmentTestDynamicInherit_003, TestSize.Level2 Dl_namespace ns; dlns_get("cj_test_namespace", &ns); DynamicInherit(&ns, nullptr, nullptr); + DynamicInheritByName(nullptr, nullptr, nullptr); + DynamicInheritByName("cj_test_namespace", nullptr, nullptr); + DynamicInheritByName("cj_test_namespace", "cj_test_namespace", nullptr); EXPECT_EQ(CJEnvironment::GetInstance(), nullptr); }