1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <gtest/gtest.h>
17 #include "socket_context.h"
18
19 using namespace testing::ext;
20
21 namespace {
22 class SocketContextTest : public testing::Test {
23 public:
SetUpTestCase()24 static void SetUpTestCase() {}
25
TearDownTestCase()26 static void TearDownTestCase() {}
SetUp()27 void SetUp() {}
TearDown()28 void TearDown() {}
29 };
30
31 /**
32 * @tc.name: Service
33 * @tc.desc: Socket send file descriptor test.
34 * @tc.type: FUNC
35 */
HWTEST_F(SocketContextTest, SendFileDescriptor, TestSize.Level1)36 HWTEST_F(SocketContextTest, SendFileDescriptor, TestSize.Level1)
37 {
38 SocketContext socketContext;
39 ASSERT_TRUE(!socketContext.SendFileDescriptor(-1));
40 ASSERT_TRUE(!socketContext.SendFileDescriptor(1));
41 }
42
43 /**
44 * @tc.name: Service
45 * @tc.desc: Abnormal socket detection.
46 * @tc.type: FUNC
47 */
HWTEST_F(SocketContextTest, RawProtocolProc, TestSize.Level1)48 HWTEST_F(SocketContextTest, RawProtocolProc, TestSize.Level1)
49 {
50 std::string s="abc";
51 SocketContext socketContext;
52 std::vector<unsigned char> buf(64); // 64: the buf size
53 ASSERT_EQ(socketContext.RawProtocolProc(2, (const int8_t *)s.c_str(), s.size()), -1);
54 EXPECT_FALSE(socketContext.ReceiveData(-1, buf.data(), buf.size()));
55 EXPECT_FALSE(socketContext.ReceiveData(1, nullptr, 0));
56 EXPECT_FALSE(socketContext.SendRaw(1, (const int8_t *)s.c_str(), s.size(), -1));
57 }
58
59 /**
60 * @tc.name: Service
61 * @tc.desc: Socket receiving test.
62 * @tc.type: FUNC
63 */
HWTEST_F(SocketContextTest, ReceiveFileDiscriptor, TestSize.Level1)64 HWTEST_F(SocketContextTest, ReceiveFileDiscriptor, TestSize.Level1)
65 {
66 SocketContext socketContext;
67 ASSERT_EQ(socketContext.ReceiveFileDiscriptor(), -1);
68 }
69 /**
70 * @tc.name: Service
71 * @tc.desc: Socket send heart beat test.
72 * @tc.type: FUNC
73 */
HWTEST_F(SocketContextTest, SendHeartBeat, TestSize.Level1)74 HWTEST_F(SocketContextTest, SendHeartBeat, TestSize.Level1)
75 {
76 SocketContext socketContext;
77 EXPECT_FALSE(socketContext.SendHeartBeat());
78 EXPECT_FALSE(socketContext.SendHookConfig(nullptr, 0));
79 }
80
81 } // namespace