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 #include <exception>
16 #include <iostream>
17 #include <string>
18 #include <thread>
19 #include <gtest/gtest.h>
20 #include <unistd.h>
21 #include <cstring>
22 #include <cstdint>
23 #include <cstdio>
24 #include <functional>
25 #include "sp_utils.h"
26 #include "sp_log.h"
27 #include "sp_task.h"
28 #include "parameters.h"
29 using namespace testing::ext;
30 using namespace std;
31 
32 namespace OHOS {
33 namespace SmartPerf {
34 class SPdaemonTaskTest : public testing::Test {
35 public:
SetUpTestCase()36     static void SetUpTestCase() {}
TearDownTestCase()37     static void TearDownTestCase() {}
SetUp()38     void SetUp() {}
TearDown()39     void TearDown() {}
40 };
41 
42 /**
43  * @tc.name: CheckTcpParamTestCase
44  * @tc.desc: Test CheckTcpParam
45  * @tc.type: FUNC
46  */
HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase001, TestSize.Level1)47 HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase001, TestSize.Level1)
48 {
49     std::string str = "-SESSIONID 12345678 -INTERVAL 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r -o";
50     std::string errorInfo = "";
51     SPTask &spTask = SPTask::GetInstance();
52     bool flag = spTask.CheckTcpParam(str, errorInfo);
53     EXPECT_TRUE(flag);
54 }
55 
HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase002, TestSize.Level1)56 HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase002, TestSize.Level1)
57 {
58     std::string str = "";
59     std::string errorInfo = "";
60     SPTask &spTask = SPTask::GetInstance();
61     bool flag = spTask.CheckTcpParam(str, errorInfo);
62     EXPECT_FALSE(flag);
63 }
64 
HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase003, TestSize.Level1)65 HWTEST_F(SPdaemonTaskTest, CheckTcpParamTestCase003, TestSize.Level1)
66 {
67     std::string str = "";
68     std::string errorInfo = "";
69     SPTask &spTask = SPTask::GetInstance();
70     bool flag = spTask.CheckTcpParam(str, errorInfo);
71     EXPECT_FALSE(flag);
72 }
73 
74 /**
75  * @tc.name: SPTask::DetectionAndGrab
76  * @tc.desc: Test DetectionAndGrab
77  * @tc.type: FUNC
78  */
HWTEST_F(SPdaemonTaskTest, DetectionAndGrabTestCase001, TestSize.Level1)79 HWTEST_F(SPdaemonTaskTest, DetectionAndGrabTestCase001, TestSize.Level1)
80 {
81     bool ret = false;
82     std::map<std::string, std::string> templateMap;
83     std::string recvStr = "-SESSIONID 12345678 -INTERVAL 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r -o";
84     SPTask::GetInstance().InitTask(recvStr);
85     templateMap = SPTask::GetInstance().DetectionAndGrab();
86 
87     if (templateMap.empty()) {
88         ret = true;
89     }
90 
91     EXPECT_EQ(ret, true);
92 }
93 }
94 }
95