• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <gtest/gtest.h>
17 
18 #include "appspawn_utils.h"
19 #include "cJSON.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 #include "appspawn_silk.h"
25 
26 extern struct SilkConfig g_silkConfig;
27 APPSPAWN_STATIC bool ParseSilkConfig(const cJSON *root, struct SilkConfig *config);
28 #ifdef __cplusplus
29 }
30 #endif
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 class ParseSilkConfigTest : public ::testing::Test {
36 protected:
SetUp()37     void SetUp() override {}
TearDown()38     void TearDown() override {}
39 };
40 
41 /**
42  * @brief 模拟测试,正常解析json文件并且正常使能silk
43  *
44  */
45 HWTEST_F(ParseSilkConfigTest, Parse_Silk_Config_001, TestSize.Level0)
46 {
47     const char *silkJsonStr = "{\"enabled_app_list\":[\"com.taobao.taobao\",\"com.tencent.mm\"]}";
48     cJSON *root =  cJSON_Parse(silkJsonStr);
49     const char* TEST_ENABLE_SILK_PROCESS_NAME = "com.taobao.taobao";
50     bool ret = ParseSilkConfig(root, &g_silkConfig);
51     cJSON_Delete(root);
52     ASSERT_EQ(ret, true);
53     ASSERT_EQ(g_silkConfig.configCursor, 2);
54     ret = LoadSilkLibrary(TEST_ENABLE_SILK_PROCESS_NAME);
55     ASSERT_EQ(ret, true);
56     ASSERT_EQ(g_silkConfig.configItems, NULL);
57     ASSERT_EQ(g_silkConfig.configCursor, 0);
58 }
59 
60 /**
61  * @brief 模拟测试,processName为NULL的情况
62  *
63  */
64 HWTEST_F(ParseSilkConfigTest, Parse_Silk_Config_002, TestSize.Level0)
65 {
66     const char *silkJsonStr = "{\"enabled_app_list\":[\"com.taobao.taobao\",\"com.tencent.mm\"]}";
67     cJSON *root =  cJSON_Parse(silkJsonStr);
68     const char* TEST_ENABLE_SILK_PROCESS_NAME = NULL;
69     bool ret = ParseSilkConfig(root, &g_silkConfig);
70     cJSON_Delete(root);
71     ASSERT_EQ(ret, true);
72     ASSERT_EQ(g_silkConfig.configCursor, 2);
73     ret = LoadSilkLibrary(TEST_ENABLE_SILK_PROCESS_NAME);
74     ASSERT_EQ(ret, false);
75     ASSERT_EQ(g_silkConfig.configItems, NULL);
76     ASSERT_EQ(g_silkConfig.configCursor, 0);
77 }
78 
79 HWTEST_F(ParseSilkConfigTest, Parse_Silk_Config_003, TestSize.Level0)
80 {
81     const char *silkJsonStr = "{\"enabled_app_list0\":[\"com.taobao.taobao\",\"com.tencent.mm\"]}";
82     cJSON *root =  cJSON_Parse(silkJsonStr);
83     const char* testEnableSilkProcessName = NULL;
84     bool ret = ParseSilkConfig(root, &g_silkConfig);
85     cJSON_Delete(root);
86     ASSERT_EQ(ret, false);
87 }
88