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 <sys/socket.h> 27 #include <arpa/inet.h> 28 #include "sdk_data_recv.h" 29 #include "sp_task.h" 30 #include "GetLog.h" 31 const int TCP_PORT = 20888; 32 using namespace testing::ext; 33 using namespace std; 34 35 namespace OHOS { 36 namespace SmartPerf { 37 class SPdaemonGetLogTest : public testing::Test { 38 public: SetUpTestCase()39 static void SetUpTestCase() {} TearDownTestCase()40 static void TearDownTestCase() {} SetUp()41 void SetUp() {} TearDown()42 void TearDown() {} 43 CreateAndBindSocket()44 int CreateAndBindSocket() 45 { 46 int tcpServerFd = socket(AF_INET, SOCK_STREAM, 0); 47 if (tcpServerFd < 0) { 48 perror("TCP socket creation failed"); 49 return -1; 50 } 51 52 struct sockaddr_in tcpServerAddr = {}; 53 tcpServerAddr.sin_family = AF_INET; 54 tcpServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 55 tcpServerAddr.sin_port = htons(TCP_PORT); 56 57 if (::bind(tcpServerFd, reinterpret_cast<struct sockaddr*>(&tcpServerAddr), sizeof(tcpServerAddr)) < 0) { 58 perror("TCP bind failed"); 59 close(tcpServerFd); 60 return -1; 61 } 62 63 return tcpServerFd; 64 } 65 AcceptClientConnection(int tcpServerFd)66 int AcceptClientConnection(int tcpServerFd) 67 { 68 if (listen(tcpServerFd, 1) < 0) { 69 perror("Listen failed"); 70 return -1; 71 } 72 73 int clientFd = accept(tcpServerFd, nullptr, nullptr); 74 if (clientFd < 0) { 75 perror("Accept failed"); 76 } 77 return clientFd; 78 } 79 GetLogCommand()80 void GetLogCommand() 81 { 82 int tcpServerFd = CreateAndBindSocket(); 83 if (tcpServerFd < 0) { 84 return; 85 } 86 87 int clientFd = AcceptClientConnection(tcpServerFd); 88 if (clientFd < 0) { 89 close(tcpServerFd); 90 return; 91 } 92 93 FILE* fp = fopen("/data/local/tmp/getlogtest.tar.gz", "wb"); 94 if (!fp) { 95 perror("File open failed"); 96 close(clientFd); 97 close(tcpServerFd); 98 return; 99 } 100 char buffer[4096]; 101 ssize_t bytesRead; 102 while ((bytesRead = recv(clientFd, buffer, sizeof(buffer), 0)) > 0) { 103 if (fwrite(buffer, 1, bytesRead, fp) != bytesRead) { 104 perror("fwrite failed"); 105 close(clientFd); 106 close(tcpServerFd); 107 return; 108 } 109 } 110 int fcloseResult = fclose(fp); 111 if (fcloseResult == EOF) { 112 perror("fclose failed"); 113 close(tcpServerFd); 114 return; 115 } 116 117 close(clientFd); 118 close(tcpServerFd); 119 } 120 }; 121 122 /** 123 * @tc.name: GetLog::SendFileTestCase 124 * @tc.desc: Test SendFileTestCase 125 * @tc.type: FUNC 126 */ 127 HWTEST_F(SPdaemonGetLogTest, SendFileTestCase, TestSize.Level1) 128 { 129 std::map<std::string, std::string> result = GetLog::GetInstance().ItemData(); __anon2893be860102() 130 std::thread tcpServer = std::thread([this]() { 131 GetLogCommand(); 132 }); 133 GetLog::GetInstance().SetLogFileSocketPort(TCP_PORT); 134 GetLog::GetInstance().LogFileSocketConnect(); 135 GetLog::GetInstance().SendLogFile(); 136 tcpServer.join(); 137 } 138 } // namespace SmartPerf 139 }