1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 
17 #include "mock_native_module_manager.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace {
22 bool g_mockCheckModuleLoadable = false;
23 LIBHANDLE g_mockLoadModuleLibrary = nullptr;
24 NativeModule *g_mockFindNativeModuleByDisk = nullptr;
25 NativeModule *g_mockFindNativeModuleByCache = nullptr;
26 }
27 
MockFindNativeModuleByDisk(NativeModule * module)28 void MockFindNativeModuleByDisk(NativeModule *module)
29 {
30     g_mockFindNativeModuleByDisk = module;
31 }
32 
MockFindNativeModuleByCache(NativeModule * module)33 void MockFindNativeModuleByCache(NativeModule *module)
34 {
35     g_mockFindNativeModuleByCache = module;
36 }
37 
MockLoadModuleLibrary(LIBHANDLE handle)38 void MockLoadModuleLibrary(LIBHANDLE handle)
39 {
40     g_mockLoadModuleLibrary = handle;
41     GTEST_LOG_(INFO) << g_mockLoadModuleLibrary;
42 }
43 
MockCheckModuleLoadable(bool loadable)44 void MockCheckModuleLoadable(bool loadable)
45 {
46     g_mockCheckModuleLoadable = loadable;
47 }
48 
MockResetModuleManagerState()49 void MockResetModuleManagerState()
50 {
51     g_mockFindNativeModuleByDisk = nullptr;
52     g_mockFindNativeModuleByCache = nullptr;
53     g_mockCheckModuleLoadable = false;
54     g_mockLoadModuleLibrary = nullptr;
55 }
56 
FindNativeModuleByDisk(const char * moduleName,const char * path,const char * relativePath,bool internal,const bool isAppModule,std::string & errInfo,char nativeModulePath[][NAPI_PATH_MAX],NativeModule * cacheNativeModule)57 NativeModule* NativeModuleManager::FindNativeModuleByDisk(const char* moduleName, const char* path,
58     const char* relativePath, bool internal, const bool isAppModule, std::string& errInfo,
59     char nativeModulePath[][NAPI_PATH_MAX], NativeModule* cacheNativeModule)
60 {
61     GTEST_LOG_(INFO) << g_mockFindNativeModuleByDisk;
62     return g_mockFindNativeModuleByDisk;
63 }
64 
LoadModuleLibrary(std::string & moduleKey,const char * path,const char * pathKey,const bool isAppModule,std::string & errInfo,uint32_t & errReason)65 LIBHANDLE NativeModuleManager::LoadModuleLibrary(std::string &moduleKey, const char* path,
66     const char* pathKey, const bool isAppModule, std::string& errInfo, uint32_t& errReason)
67 {
68     GTEST_LOG_(INFO) << g_mockLoadModuleLibrary;
69     return g_mockLoadModuleLibrary;
70 }
71 
CheckModuleLoadable(const char * moduleName,std::unique_ptr<ApiAllowListChecker> & apiAllowListChecker)72 bool ModuleLoadChecker::CheckModuleLoadable(const char* moduleName,
73     std::unique_ptr<ApiAllowListChecker>& apiAllowListChecker)
74 {
75     apiAllowListChecker = nullptr;
76     GTEST_LOG_(INFO) << g_mockCheckModuleLoadable;
77     return g_mockCheckModuleLoadable;
78 }
79 
FindNativeModuleByCache(const char * moduleName,char nativeModulePath[][NAPI_PATH_MAX],NativeModule * & cacheNativeModule,NativeModuleHeadTailStruct & cacheHeadTailStruct)80 NativeModule* NativeModuleManager::FindNativeModuleByCache(const char* moduleName,
81                                                            char nativeModulePath[][NAPI_PATH_MAX],
82                                                            NativeModule*& cacheNativeModule,
83                                                            NativeModuleHeadTailStruct& cacheHeadTailStruct)
84 {
85     GTEST_LOG_(INFO) << g_mockFindNativeModuleByCache;
86     return g_mockFindNativeModuleByCache;
87 }
88