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 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_TEST, "DbinderTestAgent" };
32 
33 class DbinderTestAgent : public DistributedAgent {
34 public:
35     DbinderTestAgent();
36     ~DbinderTestAgent();
37     virtual bool SetUp();
38     virtual bool TearDown();
39     virtual int OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnValueLen);
40     virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
41         const std::string &strExpectValue, int expectValueLen);
42 
43 private:
44     std::string localUdid;
45     void KillService() const;
46     void RestartService() const;
47 };
48 
DbinderTestAgent()49 DbinderTestAgent::DbinderTestAgent()
50 {
51     std::string pkgName = "dbinderService";
52     NodeBasicInfo nodeBasicInfo;
53     if (GetLocalNodeDeviceInfo(pkgName.c_str(), &nodeBasicInfo) != 0) {
54         DBINDER_LOGE(LOG_LABEL, "Get local node device info failed");
55         return;
56     }
57     std::string networkId(nodeBasicInfo.networkId);
58     localUdid = networkId;
59 }
60 
~DbinderTestAgent()61 DbinderTestAgent::~DbinderTestAgent() {}
62 
SetUp()63 bool DbinderTestAgent::SetUp()
64 {
65     DBINDER_LOGI(LOG_LABEL, "enter SetUp");
66     StartDBinderServiceTestService();
67     return true;
68 }
69 
TearDown()70 bool DbinderTestAgent::TearDown()
71 {
72     DBINDER_LOGI(LOG_LABEL, "enter TearDown");
73     KillService();
74     return true;
75 }
76 
77 // from test framework
OnProcessMsg(const std::string & strMsg,int len,std::string & strReturnValue,int returnValueLen)78 int DbinderTestAgent::OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue,
79     int returnValueLen)
80 {
81     std::string msg = "Ask Device ID";
82     if (strncmp(msg.c_str(), strMsg.c_str(), len) == 0) {
83         strReturnValue = localUdid;
84         returnValueLen = strlen(localUdid.c_str());
85         return returnValueLen;
86     } else {
87         return DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnValueLen);
88     }
89 }
90 
91 // from test framework
OnProcessCmd(const std::string & strCommand,int cmdLen,const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)92 int DbinderTestAgent::OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
93     const std::string &strExpectValue, int expectValueLen)
94 {
95     DBINDER_LOGI(LOG_LABEL, "enter OnProcessCmd");
96     if (strCommand == "KILL") {
97         DBINDER_LOGI(LOG_LABEL, "strCommand = %{public}s, strArgs = %{public}s", strCommand.c_str(), strArgs.c_str());
98         KillService();
99     } else if (strCommand == "RESTART") {
100         DBINDER_LOGI(LOG_LABEL, "strCommand = %{public}s, strArgs = %{public}s", strCommand.c_str(), strArgs.c_str());
101         RestartService();
102     } else {
103         return DistributedAgent::OnProcessCmd(strCommand, cmdLen, strArgs, argsLen, strExpectValue, expectValueLen);
104     }
105 
106     return 0;
107 }
108 
KillService() const109 void DbinderTestAgent::KillService() const
110 {
111     DBINDER_LOGI(LOG_LABEL, "enter KillService");
112     StopDBinderServiceTestService();
113 }
114 
RestartService() const115 void DbinderTestAgent::RestartService() const
116 {
117     DBINDER_LOGI(LOG_LABEL, "enter RestartService");
118     StartDBinderServiceTestService();
119 }
120 
main()121 int main()
122 {
123     // Test agent main function
124     DbinderTestAgent obj;
125     if (obj.SetUp()) {
126         obj.Start("agent.desc");
127         obj.Join();
128     } else {
129         DBINDER_LOGE(LOG_LABEL, "Init environment failed.");
130     }
131     if (obj.TearDown()) {
132         return 0;
133     } else {
134         DBINDER_LOGE(LOG_LABEL, "Clear environment failed.");
135         return -1;
136     }
137 }
138