• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include <gtest/gtest.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 
20 #include "netlink_define.h"
21 #include "wrapper_listener.h"
22 
23 namespace OHOS {
24 namespace nmd {
25 namespace {
26 using namespace testing::ext;
27 constexpr int32_t TEST_SOCKET = 56;
__anon6adfc9a10202(int32_t socket) 28 WrapperListener::RecvFunc g_func = [](int32_t socket) { std::cout << "Socket :" << socket << std::endl; };
29 } // namespace
30 
31 class WrapperListenerTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37     std::shared_ptr<WrapperListener> instance_ = std::make_shared<WrapperListener>(TEST_SOCKET, g_func);
38 };
39 
SetUpTestCase()40 void WrapperListenerTest::SetUpTestCase() {}
41 
TearDownTestCase()42 void WrapperListenerTest::TearDownTestCase() {}
43 
SetUp()44 void WrapperListenerTest::SetUp() {}
45 
TearDown()46 void WrapperListenerTest::TearDown() {}
47 
48 HWTEST_F(WrapperListenerTest, StartTest001, TestSize.Level1)
49 {
50     int32_t testSocket = -2;
51     std::unique_ptr<WrapperListener> listener = std::make_unique<WrapperListener>(testSocket, g_func);
52     auto ret = listener->Start();
53     EXPECT_EQ(ret, NetlinkResult::ERROR);
54     ret = listener->Stop();
55     EXPECT_EQ(ret, NetlinkResult::ERROR);
56 }
57 
58 HWTEST_F(WrapperListenerTest, StartTest002, TestSize.Level1)
59 {
60     int32_t testSocket = socket(AF_INET, SOCK_STREAM, 0);
61     struct sockaddr_in address;
62     address.sin_family = AF_INET;
63     address.sin_addr.s_addr = INADDR_ANY;
64     bind(testSocket, reinterpret_cast<struct sockaddr*>(&address), sizeof(address));
65     std::unique_ptr<WrapperListener> listener = std::make_unique<WrapperListener>(testSocket, g_func);
66     auto ret = listener->Start();
67     EXPECT_EQ(ret, NetlinkResult::OK);
68     ret = listener->Stop();
69     EXPECT_EQ(ret, NetlinkResult::OK);
70 }
71 } // namespace nmd
72 } // namespace OHOS