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 invaild\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 
76 /**
77  * @brief watchdog开启及定时kickdog
78  *
79  */
80 HWTEST_F(AppSpawnKickDogTest, App_Spawn_AppSpawnKickDog_001, TestSize.Level0)
81 {
82     AppSpawnContent content;
83     int fileFd = open(HM_APPSPAWN_WATCHDOG_FILE, O_CREAT);
84     EXPECT_EQ(fileFd != -1, 1);
85     close(fileFd);
86 
87     AppSpawnKickDogStart(&content);
88     std::unique_ptr<OHOS::AppSpawnTestServer> testServer =
89         std::make_unique<OHOS::AppSpawnTestServer>("appspawn -mode appspawn");
90     testServer->Start(nullptr);
91     if (access(LINUX_APPSPAWN_WATCHDOG_FILE, F_OK) == 0) {
92         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), 0);
93         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), -1);
94     } else {
95         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_ON), 0);
96         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), -1);
97     }
98     sleep(12); // wait for kick dog(kick every 10 seconds)
99     if (access(LINUX_APPSPAWN_WATCHDOG_FILE, F_OK) == 0) {
100         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_ON), -1);
101         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
102     } else {
103         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_ON), -1);
104         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), 0);
105     }
106     sleep(10);
107     if (access(LINUX_APPSPAWN_WATCHDOG_FILE, F_OK) == 0) {
108         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, LINUX_APPSPAWN_WATCHDOG_KICK), 0);
109     } else {
110         EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, HM_APPSPAWN_WATCHDOG_KICK), 0);
111     }
112     testServer->Stop();
113     EXPECT_EQ(CheckFileContent(HM_APPSPAWN_WATCHDOG_FILE, nullptr), -1);
114 
115     remove(HM_APPSPAWN_WATCHDOG_FILE);
116 }
117 }  // namespace OHOS
118