• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
16 #include <memory>
17 #include <iostream>
18 #include <sstream>
19 #include <thread>
20 #include <sys/socket.h>
21 #include <arpa/inet.h>
22 #include "unistd.h"
23 #include "sp_utils.h"
24 #include "sp_server_socket.h"
25 #include "common.h"
26 
27 using namespace testing::ext;
28 using namespace std;
29 
30 namespace OHOS {
31 namespace SmartPerf {
32 class SpServerSocketTest : public testing::Test {
33 public:
34     int sock = 0;
35     struct sockaddr_in servAddr;
36 
SetUpTestCase()37     static void SetUpTestCase() {}
TearDownTestCase()38     static void TearDownTestCase() {}
39 
SetUp()40     void SetUp() {}
TearDown()41     void TearDown() {}
42 };
43 
44 HWTEST_F(SpServerSocketTest, InitTcpTest, TestSize.Level1)
45 {
46     std::string spDaemon = "SP_daemon";
47     std::string result;
48     bool tcpRet;
49     SPUtils::LoadCmd(spDaemon, result);
50     int tcpPort = 8284;
51     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
52         std::cerr << "Socket creation error";
53         return;
54     }
55     servAddr.sin_family = AF_INET;
56     servAddr.sin_port = htons(tcpPort);
57     if (inet_pton(AF_INET, "127.0.0.1", &servAddr.sin_addr) <= 0) {
58         std::cerr << "Invalid address/ Address not supported";
59         return;
60     }
61     if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
62         std::cerr << "Connection Failed";
63         return;
64     }
65     std::string message = "startRecord::";
66     char buffer[1024] = {0};
67     send(sock, message.c_str(), strlen(message.c_str()), 0);
68     read(sock, buffer, 1024);
69     std::string tcpBuffer(buffer);
70     if (tcpBuffer.find("startRecord") != std::string::npos) {
71         tcpRet = true;
72     }
73     EXPECT_TRUE(tcpRet);
74     close(sock);
75 }
76 
77 HWTEST_F(SpServerSocketTest, AcceptTest, TestSize.Level0)
78 {
79     SpServerSocket serverSocket;
80     int connFd = serverSocket.Accept();
81     EXPECT_EQ(connFd, -1);
82 }
83 
84 HWTEST_F(SpServerSocketTest, SendtoTest, TestSize.Level0)
85 {
86     SpServerSocket serverSocket;
87     std::string sendBuf = "test";
88     int ret = serverSocket.Sendto(sendBuf);
89     EXPECT_EQ(ret, 0);
90 }
91 
92 HWTEST_F(SpServerSocketTest, RecvFailureTest, TestSize.Level0)
93 {
94     SpServerSocket serverSocket;
95     int result = serverSocket.Recv();
96     ASSERT_LT(result, 0);
97 }
98 
99 HWTEST_F(SpServerSocketTest, RecvBufTest, TestSize.Level0)
100 {
101     SpServerSocket serverSocket;
102     std::string rbuf = serverSocket.RecvBuf();
103     EXPECT_EQ(rbuf, "");
104 }
105 }
106 }