• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <chrono>
26 #include "sdk_data_recv.h"
27 #include "sp_task.h"
28 const int TCP_PORT = 12567;
29 using namespace testing::ext;
30 using namespace std;
31 
32 namespace OHOS {
33 namespace SmartPerf {
34 class SPdaemonSdkDataTest : public testing::Test {
35 public:
SetUpTestCase()36     static void SetUpTestCase() {}
TearDownTestCase()37     static void TearDownTestCase() {}
SetUp()38     void SetUp() {}
TearDown()39     void TearDown() {}
ConnectSdkServer()40     int ConnectSdkServer()
41     {
42         struct sockaddr_in servaddr;
43         int sockfd = socket(AF_INET, SOCK_STREAM, 0);
44         if (sockfd < 0) {
45             perror("socket creation failed");
46             return -1;
47         }
48 
49         std::fill_n(reinterpret_cast<char*>(&servaddr), sizeof(servaddr), 0);
50         servaddr.sin_family = AF_INET;
51         servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
52         servaddr.sin_port = htons(TCP_PORT);
53 
54         if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
55             printf("connect failed: %s", strerror(errno));
56             close(sockfd);
57             return -1;
58         }
59         return sockfd;
60     }
61 };
62 
63 /**
64  * @tc.name: SdkDataRecv::ItemData
65  * @tc.desc: Test ItemData
66  * @tc.type: FUNC
67  */
68 HWTEST_F(SPdaemonSdkDataTest, ItemDataTestCase, TestSize.Level1)
69 {
70     std::map<std::string, std::string> result = SdkDataRecv::GetInstance().ItemData();
71     EXPECT_EQ(result.size(), 0);
72 }
73 /**
74  * @tc.name: SdkDataRecv::ServerThread
75  * @tc.desc: Test ServerThread
76  * @tc.type: FUNC
77  */
78 HWTEST_F(SPdaemonSdkDataTest, ServerThreadTestCase, TestSize.Level1)
79 {
80     std::vector<std::string> dataVec;
81     SdkDataRecv::GetInstance().SetRunningState(true);
82     SPTask::GetInstance().GetRecordState();
__anon18bb6eec0102() 83     std::thread sdk = std::thread([&dataVec, this]() {
84         SdkDataRecv::GetInstance().ServerThread(dataVec);
85     });
86     std::this_thread::sleep_for(std::chrono::milliseconds(2000));
87     int sockfd = ConnectSdkServer();
88     EXPECT_NE(sockfd, -1);
89     std::string sendData = "{src:test,para0:1,time:1000,enable:1,value:1}";
90     int ret = send(sockfd, sendData.c_str(), sendData.size(), 0);
91     if (ret != sendData.size()) {
92         printf("Send message error: %d, %s\n", sockfd, strerror(errno));
93     }
94     std::this_thread::sleep_for(std::chrono::milliseconds(2000));
95     close(sockfd);
96     SdkDataRecv::GetInstance().SetRunningState(false);
97     close(SdkDataRecv::GetInstance().GetListenFd());
98     sdk.join();
99     SPTask::GetInstance().GetRecordState();
100     EXPECT_EQ(dataVec.size(), 0);
101 }
102 }
103 }