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 <iostream>
17 #include <string>
18 #include "hilog/log.h"
19 #include "dbinder_test_service.h"
20 #include "ipc_skeleton.h"
21 #include "distributed_agent.h"
22 #include "dbinder_service_test_helper.h"
23 #include "log_tags.h"
24 #include "softbus_bus_center.h"
25
26 using namespace testing;
27 using namespace OHOS;
28 using namespace OHOS::DistributeSystemTest;
29 using namespace OHOS::HiviewDFX;
30
31 #ifndef TITLE
32 #define TITLE __PRETTY_FUNCTION__
33 #endif
34
35 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DbinderTestAgent" };
36 #define DBINDER_LOGE(fmt, args...) \
37 (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
38 #define DBINDER_LOGI(fmt, args...) \
39 (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
40
41 class DbinderTestAgent : public DistributedAgent {
42 public:
43 DbinderTestAgent();
44 ~DbinderTestAgent();
45 virtual bool SetUp();
46 virtual bool TearDown();
47 virtual int OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnBufLen);
48 virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
49 const std::string &strExpectValue, int expectValueLen);
50
51 private:
52 std::string localUdid;
53 void KillService() const;
54 void RestartService() const;
55 };
56
DbinderTestAgent()57 DbinderTestAgent::DbinderTestAgent()
58 {
59 std::string pkgName = "dbinderService";
60 NodeBasicInfo nodeBasicInfo;
61 if (GetLocalNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo) != 0) {
62 DBINDER_LOGE("Get local node device info failed");
63 return;
64 }
65 std::string networkId(nodeBasicInfo.networkId);
66 localUdid = networkId;
67 }
68
~DbinderTestAgent()69 DbinderTestAgent::~DbinderTestAgent() {}
70
SetUp()71 bool DbinderTestAgent::SetUp()
72 {
73 DBINDER_LOGI("enter SetUp");
74 StartDBinderServiceTestService();
75 return true;
76 }
77
TearDown()78 bool DbinderTestAgent::TearDown()
79 {
80 DBINDER_LOGI("enter TearDown");
81 KillService();
82 return true;
83 }
84
85 // from test framework
OnProcessMsg(const std::string & strMsg,int len,std::string & strReturnValue,int returnValueLen)86 int DbinderTestAgent::OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnValueLen)
87 {
88 std::string msg = "Ask Device ID";
89 if (strncmp(msg.c_str(), strMsg.c_str(), len) == 0) {
90 strReturnValue = localUdid;
91 returnValueLen = strlen(localUdid.c_str());
92 return returnValueLen;
93 } else {
94 return DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnValueLen);
95 }
96 }
97
98 // from test framework
OnProcessCmd(const std::string & strCommand,int cmdLen,const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)99 int DbinderTestAgent::OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
100 const std::string &strExpectValue, int expectValueLen)
101 {
102 DBINDER_LOGI("enter OnProcessCmd");
103 if (strCommand == "KILL") {
104 DBINDER_LOGI("strCommand = %{public}s, strArgs = %{public}s", strCommand.c_str(), strArgs.c_str());
105 KillService();
106 } else if (strCommand == "RESTART") {
107 DBINDER_LOGI("strCommand = %{public}s, strArgs = %{public}s", strCommand.c_str(), strArgs.c_str());
108 RestartService();
109 } else {
110 return DistributedAgent::OnProcessCmd(strCommand, cmdLen, strArgs, argsLen, strExpectValue, expectValueLen);
111 }
112
113 return 0;
114 }
115
KillService() const116 void DbinderTestAgent::KillService() const
117 {
118 DBINDER_LOGI("enter KillService");
119 StopDBinderServiceTestService();
120 }
121
RestartService() const122 void DbinderTestAgent::RestartService() const
123 {
124 DBINDER_LOGI("enter RestartService");
125 StartDBinderServiceTestService();
126 }
127
main()128 int main()
129 {
130 // Test agent main function
131 DbinderTestAgent obj;
132 if (obj.SetUp()) {
133 obj.Start("agent.desc");
134 obj.Join();
135 } else {
136 DBINDER_LOGE("Init environment failed.");
137 }
138 if (obj.TearDown()) {
139 return 0;
140 } else {
141 DBINDER_LOGE("Clear environment failed.");
142 return -1;
143 }
144 }
145