• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 <cstring>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include "appspawn_kickdog.h"
24 #include "securec.h"
25 
26 #include "app_spawn_stub.h"
27 #include "app_spawn_test_helper.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 class AppSpawnKickDogTest : public testing::Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase() {}
TearDownTestCase()36     static void TearDownTestCase() {}
SetUp()37     void SetUp() {}
TearDown()38     void TearDown() {}
39 };
40 
CheckFileContent(const char * filePath,const char * targetStr)41 static int CheckFileContent(const char *filePath, const char *targetStr)
42 {
43     char buf[100];
44 
45     if (filePath == nullptr) {
46         printf("para invalid\n");
47         return -1;
48     }
49 
50     int fileFd = open(filePath, O_RDWR);
51     if (fileFd == -1) {
52         printf("open %s fail,errno:%d\n", filePath, errno);
53         return fileFd;
54     }
55 
56     int readResult = read(fileFd, buf, sizeof(buf) - 1);
57     if (readResult <= 0) {
58         printf("read %s fail,result:%d\n", filePath, readResult);
59         close(fileFd);
60         return -1;
61     }
62 
63     if (strcmp(buf, targetStr) != 0) {
64         printf("read buf %s is not euqal target str:%s\n", buf, targetStr);
65         close(fileFd);
66         return -1;
67     } else {
68         printf("read buf %s is euqal to target str:%s\n", buf, targetStr);
69         ftruncate(fileFd, 0);
70     }
71 
72     close(fileFd);
73     return 0;
74 }
75 
CheckDeviceInLinux()76 static bool CheckDeviceInLinux()
77 {
78     struct utsname uts;
79     if (uname(&uts) == -1) {
80         printf("uname get failed");
81         return false;
82     }
83     if (strcmp(uts.sysname, "Linux") == 0) {
84         printf("uname sysname is Linux");
85         return true;
86     }
87     return false;
88 }
89 
90 /**
91  * @brief watchdog开启及定时kickdog
92  *
93  */
94 HWTEST_F(AppSpawnKickDogTest, App_Spawn_AppSpawnKickDog_001, TestSize.Level0)
95 {
96     int fileFd = open(HM_APPSPAWN_WATCHDOG_FILE, O_CREAT);
97     EXPECT_EQ(fileFd != -1, 1);
98     close(fileFd);
99 
100     std::unique_ptr<OHOS::AppSpawnTestServer> testServer =
101         std::make_unique<OHOS::AppSpawnTestServer>("appspawn -mode appspawn");
102     AddPreloadHook(HOOK_PRIO_COMMON, SpawnKickDogStart);
103     testServer->Start(nullptr);
104     if (CheckDeviceInLinux()) {
105         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), 0);
106         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), -1);
107     } else {
108         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_ON), 0);
109         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), -1);
110     }
111     sleep(12); // wait for kick dog(kick every 10 seconds)
112     if (CheckDeviceInLinux()) {
113         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), -1);
114         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
115     } else {
116         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_ON), -1);
117         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), 0);
118     }
119     sleep(10);
120     if (CheckDeviceInLinux()) {
121         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
122     } else {
123         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), 0);
124     }
125     testServer->Stop();
126     EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, nullptr), -1);
127 
128     remove(HM_APPSPAWN_WATCHDOG_FILE);
129 }
130 
131 /**
132  * @brief nwebspawn接入hungtask定时kickdog
133  *
134  */
135 HWTEST_F(AppSpawnKickDogTest, App_Spawn_AppSpawnKickDog_002, TestSize.Level0)
136 {
137     int fileFd = open(HM_APPSPAWN_WATCHDOG_FILE, O_CREAT);
138     EXPECT_EQ(fileFd != -1, 1);
139     close(fileFd);
140 
141     std::unique_ptr<OHOS::AppSpawnTestServer> testServer =
142         std::make_unique<OHOS::AppSpawnTestServer>("appspawn -mode nwebspawn");
143     AddPreloadHook(HOOK_PRIO_COMMON, SpawnKickDogStart);
144     testServer->Start(nullptr);
145     if (CheckDeviceInLinux()) {
146         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), 0);
147         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), -1);
148     } else {
149         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_NWEBSPAWN_WATCHDOG_ON), 0);
150         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_NWEBSPAWN_WATCHDOG_KICK), -1);
151     }
152     sleep(12); // wait for kick dog(kick every 10 seconds)
153     if (CheckDeviceInLinux()) {
154         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), -1);
155         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
156     } else {
157         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_NWEBSPAWN_WATCHDOG_ON), -1);
158         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_NWEBSPAWN_WATCHDOG_KICK), 0);
159     }
160     sleep(10);
161     if (CheckDeviceInLinux()) {
162         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
163     } else {
164         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_NWEBSPAWN_WATCHDOG_KICK), 0);
165     }
166     testServer->Stop();
167     EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, nullptr), -1);
168 
169     remove(HM_APPSPAWN_WATCHDOG_FILE);
170 }
171 }  // namespace OHOS
172