1 /*
2 * Copyright (c) 2022-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 /*
17 * Copyright (c) 2022 Huawei Device Co., Ltd.
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31 #include <gtest/gtest.h>
32 #include "hilog_tag_wrapper.h"
33 #include "iremote_proxy.h"
34 #include "test_observer_proxy.h"
35
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::AAFwk;
39
40 namespace {
41 const std::string CMD = "ls -l";
42 const std::string WRONGCMD = "kk -l";
43 } // namespace
44
45 class TestObserverProxyTest : public ::testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp() override;
50 void TearDown() override;
51 };
52
SetUpTestCase()53 void TestObserverProxyTest::SetUpTestCase()
54 {}
55
TearDownTestCase()56 void TestObserverProxyTest::TearDownTestCase()
57 {}
58
SetUp()59 void TestObserverProxyTest::SetUp()
60 {}
61
TearDown()62 void TestObserverProxyTest::TearDown()
63 {}
64
65 namespace OHOS {
66 class MockIRemoteObject : public IRemoteObject {
67 public:
MockIRemoteObject()68 MockIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
69
~MockIRemoteObject()70 ~MockIRemoteObject() {}
71
GetObjectRefCount()72 int32_t GetObjectRefCount() override
73 {
74 return 0;
75 }
76
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
78 {
79 return 0;
80 }
81
IsProxyObject() const82 bool IsProxyObject() const override
83 {
84 return true;
85 }
86
CheckObjectLegality() const87 bool CheckObjectLegality() const override
88 {
89 return true;
90 }
91
AddDeathRecipient(const sptr<DeathRecipient> & recipient)92 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
93 {
94 return true;
95 }
96
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)97 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
98 {
99 return true;
100 }
101
Marshalling(Parcel & parcel) const102 bool Marshalling(Parcel &parcel) const override
103 {
104 return true;
105 }
106
AsInterface()107 sptr<IRemoteBroker> AsInterface() override
108 {
109 return nullptr;
110 }
111
Dump(int fd,const std::vector<std::u16string> & args)112 int Dump(int fd, const std::vector<std::u16string> &args) override
113 {
114 return 0;
115 }
116
GetObjectDescriptor() const117 std::u16string GetObjectDescriptor() const
118 {
119 if (bExchange) {
120 std::u16string descriptor = std::u16string();
121 return descriptor;
122 } else {
123 std::u16string descriptor = std::u16string(u"testDescriptor");
124 return descriptor;
125 }
126 }
127
SetExchange(bool bEx)128 static void SetExchange(bool bEx) {
129 bExchange = bEx;
130 }
131 private:
132 static bool bExchange;
133 };
134 bool MockIRemoteObject::bExchange = true;
135 }
136
137 /**
138 * @tc.number: Test_Observer_Proxy_Test_0100
139 * @tc.name: Test ExecuteShellCommand
140 * @tc.desc: Verify the ExecuteShellCommand fail because failed to read result.
141 */
142 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0100, TestSize.Level1)
143 {
144 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0100 start");
145 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
146 OHOS::MockIRemoteObject::SetExchange(true);
147 TestObserverProxy testObserverProxy(object);
148 EXPECT_EQ(testObserverProxy.ExecuteShellCommand(CMD.c_str(), 0).stdResult.size(), 0);
149 OHOS::MockIRemoteObject::SetExchange(false);
150 testObserverProxy.ExecuteShellCommand(CMD.c_str(), 0);
151 testObserverProxy.ExecuteShellCommand("", 0);
152 std::string cmdstr;
153 testObserverProxy.ExecuteShellCommand(cmdstr, 0);
154 testObserverProxy.ExecuteShellCommand(cmdstr, false);
155 testObserverProxy.ExecuteShellCommand(WRONGCMD.c_str(), 0);
156 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0100 end");
157 }
158
159 /**
160 * @tc.number: Test_Observer_Proxy_Test_0200
161 * @tc.name: Test TestStatus
162 * @tc.desc: Verify the TestStatus process not crush.
163 */
164 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0200, TestSize.Level1)
165 {
166 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0200 start");
167 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
168 ASSERT_NE(object, nullptr);
169 OHOS::MockIRemoteObject::SetExchange(true);
170 TestObserverProxy testObserverProxy(object);
171 testObserverProxy.TestStatus(CMD.c_str(), 0);
172 OHOS::MockIRemoteObject::SetExchange(false);
173 testObserverProxy.TestStatus(CMD.c_str(), 0);
174 testObserverProxy.TestStatus(CMD.c_str(), false);
175 testObserverProxy.TestStatus(WRONGCMD.c_str(), 0);
176 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0200 end");
177 }
178
179 /**
180 * @tc.number: Test_Observer_Proxy_Test_0300
181 * @tc.name: Test TestFinished
182 * @tc.desc: Verify the TestFinished process not crush.
183 */
184 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0300, TestSize.Level1)
185 {
186 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0300 start");
187 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
188 ASSERT_NE(object, nullptr);
189 OHOS::MockIRemoteObject::SetExchange(true);
190 TestObserverProxy testObserverProxy(object);
191 testObserverProxy.TestFinished(CMD.c_str(), 0);
192 OHOS::MockIRemoteObject::SetExchange(false);
193 testObserverProxy.TestFinished(CMD.c_str(), 0);
194 testObserverProxy.TestFinished(CMD.c_str(), false);
195 testObserverProxy.TestFinished(WRONGCMD.c_str(), 0);
196 TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Proxy_Test_0300 end");
197 }
198