1 /*
2 * Copyright (c) 2021 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 #include <cstddef>
18 #include <cstdint>
19 #include <iostream>
20 #include <cstdio>
21 #include <ctime>
22 #include <memory>
23 #include <securec.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include "tee_client_type.h"
28 #include "tee_log.h"
29 #include "tc_ns_client.h"
30 #include "tui_event.h"
31 #include <dlfcn.h>
32 #include "stubMock.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36 using namespace std;
37 using namespace OHOS::PowerMgr;
38 using namespace OHOS::Rosen;
39 using namespace OHOS::Telephony;
40 extern "C" void TeeTuiThreadWork(void);
41 extern "C" int32_t GetEvent(char *eventParam, int *paramLen);
42 extern "C" void HandleEvent(const char *eventParam, int32_t paramLen);
43
44 class TUIDaemonTest : public testing::Test {
45 public:
46 static void SetUpTestCase(void);
47 static void TearDownTestCase(void);
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase(void)52 void TUIDaemonTest::SetUpTestCase(void)
53 {
54 printf("SetUp\n");
55 }
56
TearDownTestCase(void)57 void TUIDaemonTest::TearDownTestCase(void)
58 {
59 printf("SetUpTestCase\n");
60 }
61
SetUp(void)62 void TUIDaemonTest::SetUp(void)
63 {
64 printf("TearDownTestCase\n");
65 }
66
TearDown(void)67 void TUIDaemonTest::TearDown(void)
68 {
69 printf("TearDown\n");
70 }
71 namespace {
72 /**
73 * @tc.name: TUIDaemonInit_001
74 * @tc.desc: Check Tui Daemon init.
75 * @tc.type: FUNC
76 * @tc.require: issueNumber
77 */
78 HWTEST_F(TUIDaemonTest, TUIDaemonInit_001, TestSize.Level1)
79 {
80 auto tuiDaemon = new TUIDaemon();
81 tuiDaemon->TuiDaemonInit(true);
82 EXPECT_TRUE(tuiDaemon->mTUIDisplayListener_ == nullptr);
83 }
84
85 /**
86 * @tc.name: GetTUIEventInstance_001
87 * @tc.desc: Check Tui Event status.
88 * @tc.type: FUNC
89 * @tc.require: issueNumber
90 */
91 HWTEST_F(TUIDaemonTest, GetTUIEventInstance_001, TestSize.Level1)
92 {
93 /* check get instance */
94 auto TmpInstance = TUIEvent::GetInstance();
95 EXPECT_TRUE(TmpInstance != nullptr);
96
97 /* get & set status */
98 auto status = TmpInstance->TUIGetStatus();
99 EXPECT_TRUE(status == false);
100 TmpInstance->TUISetStatus(true);
101 status = TmpInstance->TUIGetStatus();
102 EXPECT_TRUE(status);
103 TmpInstance->TUISetStatus(false);
104 status = TmpInstance->TUIGetStatus();
105 EXPECT_TRUE(status == false);
106 }
107
108 /**
109 * @tc.name: GetTUIEventInstance_002
110 * @tc.desc: Check Tui event runninglock status.
111 * @tc.type: FUNC
112 * @tc.require: issueNumber
113 */
114 HWTEST_F(TUIDaemonTest, GetTUIEventInstance_002, TestSize.Level1)
115 {
116 /* check get instance */
117 auto TmpInstance = TUIEvent::GetInstance();
118 EXPECT_TRUE(TmpInstance != nullptr);
119
120 /* get & release lock */
121 TmpInstance->TUIGetRunningLock();
122 TmpInstance->TUIReleaseRunningLock();
123 }
124
125 /**
126 * @tc.name: DealWithState_001
127 * @tc.desc: Test Tui event DealWithState.
128 * @tc.type: FUNC
129 * @tc.require: issueNumber
130 */
131 HWTEST_F(TUIDaemonTest, DealWithState_001, TestSize.Level1)
132 {
133 auto TmpInstance = TUIEvent::GetInstance();
134 EXPECT_TRUE(TmpInstance != nullptr);
135
136 auto status = TmpInstance->TUIGetStatus();
137 EXPECT_TRUE(status == false);
138 TmpInstance->TUIDealWithEvent(true);
139 status = TmpInstance->TUIGetStatus();
140 EXPECT_TRUE(status == true);
141 TmpInstance->TUIDealWithEvent(false);
142 status = TmpInstance->TUIGetStatus();
143 EXPECT_TRUE(status == false);
144 }
145
146 /**
147 * @tc.name: DealWithState_002
148 * @tc.desc: Test Tui event DealWithState.
149 * @tc.type: FUNC
150 * @tc.require: issueNumber
151 */
152 HWTEST_F(TUIDaemonTest, DealWithState_002, TestSize.Level1)
153 {
154 auto TmpInstance = TUIEvent::GetInstance();
155 EXPECT_TRUE(TmpInstance != nullptr);
156
157 auto status = TmpInstance->TUIGetStatus();
158 EXPECT_TRUE(status == false);
159 TmpInstance->TUIDealWithEvent(false);
160 status = TmpInstance->TUIGetStatus();
161 EXPECT_TRUE(status == false);
162 TmpInstance->TUIDealWithEvent(true);
163 status = TmpInstance->TUIGetStatus();
164 EXPECT_TRUE(status == true);
165 }
166
167 HWTEST_F(TUIDaemonTest, TeeTuiThreadWork_001, TestSize.Level1)
168 {
169 char tmp;
170 int ret = GetEvent(nullptr, nullptr);
171 EXPECT_TRUE(ret == -1);
172 ret = GetEvent(&tmp, nullptr);
173 EXPECT_TRUE(ret == -1);
174 AddMockStub obj;
175 SetGlobalStubMock(&obj);
176 EXPECT_CALL(obj, GetEventParamFromTui).WillRepeatedly(Return(-1));
177 char param1[] = "unused";
178 int paramLen = 0;
179 ret = GetEvent(param1, ¶mLen);
180 EXPECT_TRUE(ret == -1);
181 ret = GetEvent(param1, ¶mLen);
182 EXPECT_TRUE(ret == -1);
183 char param2[] = "config";
184 ret = GetEvent(param2, ¶mLen);
185 EXPECT_TRUE(ret == -1);
186 ret = GetEvent(param2, ¶mLen);
187 EXPECT_TRUE(ret == -1);
188 HandleEvent(nullptr, 0);
189 HandleEvent(&tmp, 0);
190 HandleEvent(param1, 7);
191 HandleEvent(param2, 7);
192 char param3[] = "unknow";
193 HandleEvent(param3, 7);
194 SetGlobalStubMock(nullptr);
195 }
196
197 HWTEST_F(TUIDaemonTest, TeeTuiThreadWork_002, TestSize.Level1)
198 {
199 auto tmp1 = new TUIDisplayListener();
200 tmp1->OnFoldStatusChanged(OHOS::Rosen::FoldStatus::EXPAND);
201 tmp1->OnFoldStatusChanged(OHOS::Rosen::FoldStatus::FOLDED);
202 tmp1->OnFoldStatusChanged(OHOS::Rosen::FoldStatus::UNKNOWN);
203
204 auto tmp2 = new TUICallManagerCallback();
205 OHOS::Telephony::CallAttributeInfo info;
206 info.callState = OHOS::Telephony::TelCallState::CALL_STATUS_INCOMING;
207 tmp2->OnCallDetailsChange(info);
208 AddMockStub obj;
209 SetGlobalStubMock(&obj);
210 EXPECT_CALL(obj, GetEventParamFromTui).WillRepeatedly(Return(-1));
211 TeeTuiThreadWork();
212 EXPECT_CALL(obj, GetEventParamFromTui).WillOnce(Return(1)).WillRepeatedly(Return(-1));
213 TeeTuiThreadWork();
214 SetGlobalStubMock(nullptr);
215 }
216
217 HWTEST_F(TUIDaemonTest, TeeTuiThreadWork_003, TestSize.Level1)
218 {
219 int ret = tee_open(nullptr, 0, 0);
220 EXPECT_TRUE(ret != 0);
221
222 ret = tee_open("/data/TeeTuiThreadWork_003", 0, 1);
223 EXPECT_TRUE(ret != 0);
224
225 int fd = -1;
226 tee_close(nullptr);
227 tee_close(&fd);
228 fd = tee_open("./TeeTuiThreadWork_003", O_WRONLY | O_TRUNC | O_CREAT, 0);
229 tee_close(&fd);
230 EXPECT_TRUE(ret != 0);
231 }
232 }