1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <atomic>
18 #include <deque>
19 #include <iostream>
20 #include <mutex>
21
22 #include <arpa/inet.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <linux/netfilter/nfnetlink_log.h>
26
27 #include <netdutils/MockSyscalls.h>
28 #include "NFLogListener.h"
29
30 using ::testing::_;
31 using ::testing::DoAll;
32 using ::testing::Exactly;
33 using ::testing::Invoke;
34 using ::testing::Return;
35 using ::testing::SaveArg;
36 using ::testing::StrictMock;
37
38 namespace android {
39 namespace net {
40
41 using netdutils::makeSlice;
42 using netdutils::NetlinkListenerInterface;
43 using netdutils::Slice;
44 using netdutils::StatusOr;
45 using netdutils::status::ok;
46
47 constexpr int kNFLogPacketMsgType = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_PACKET;
48 constexpr int kNetlinkMsgDoneType = (NFNL_SUBSYS_NONE << 8) | NLMSG_DONE;
49
50 class MockNetlinkListener : public NetlinkListenerInterface {
51 public:
52 ~MockNetlinkListener() override = default;
53
54 MOCK_METHOD1(send, netdutils::Status(const Slice msg));
55 MOCK_METHOD2(subscribe, netdutils::Status(uint16_t type, const DispatchFn& fn));
56 MOCK_METHOD1(unsubscribe, netdutils::Status(uint16_t type));
57 MOCK_METHOD0(join, void());
58 MOCK_METHOD1(registerSkErrorHandler, void(const SkErrorHandler& handler));
59 };
60
61 class NFLogListenerTest : public testing::Test {
62 protected:
NFLogListenerTest()63 NFLogListenerTest() {
64 EXPECT_CALL(*mNLListener, subscribe(kNFLogPacketMsgType, _))
65 .WillOnce(DoAll(SaveArg<1>(&mPacketFn), Return(ok)));
66 EXPECT_CALL(*mNLListener, subscribe(kNetlinkMsgDoneType, _))
67 .WillOnce(DoAll(SaveArg<1>(&mDoneFn), Return(ok)));
68 mListener.reset(new NFLogListener(mNLListener));
69 }
70
~NFLogListenerTest()71 ~NFLogListenerTest() {
72 EXPECT_CALL(*mNLListener, unsubscribe(kNFLogPacketMsgType)).WillOnce(Return(ok));
73 EXPECT_CALL(*mNLListener, unsubscribe(kNetlinkMsgDoneType)).WillOnce(Return(ok));
74 }
75
sendOk(const Slice buf)76 static StatusOr<size_t> sendOk(const Slice buf) { return buf.size(); }
77
subscribe(uint16_t type,const NFLogListenerInterface::DispatchFn & fn)78 void subscribe(uint16_t type, const NFLogListenerInterface::DispatchFn& fn) {
79 // Two sends for cfgCmdBind() & cfgMode(), one send at destruction time for cfgCmdUnbind()
80 EXPECT_CALL(*mNLListener, send(_)).Times(Exactly(3)).WillRepeatedly(Invoke(sendOk));
81 EXPECT_OK(mListener->subscribe(type, fn));
82 }
83
sendEmptyMsg(uint16_t type)84 void sendEmptyMsg(uint16_t type) {
85 struct {
86 nlmsghdr nlmsg;
87 nfgenmsg nfmsg;
88 } msg = {};
89
90 msg.nlmsg.nlmsg_type = kNFLogPacketMsgType;
91 msg.nlmsg.nlmsg_len = sizeof(msg);
92 msg.nfmsg.res_id = htons(type);
93 mPacketFn(msg.nlmsg, drop(makeSlice(msg), sizeof(msg.nlmsg)));
94 }
95
96 NetlinkListenerInterface::DispatchFn mPacketFn;
97 NetlinkListenerInterface::DispatchFn mDoneFn;
98 std::shared_ptr<StrictMock<MockNetlinkListener>> mNLListener{
99 new StrictMock<MockNetlinkListener>()};
100 std::unique_ptr<NFLogListener> mListener;
101 };
102
TEST_F(NFLogListenerTest,subscribe)103 TEST_F(NFLogListenerTest, subscribe) {
104 constexpr uint16_t kType = 38;
105 const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {};
106 subscribe(kType, dispatchFn);
107 }
108
TEST_F(NFLogListenerTest,nlmsgDone)109 TEST_F(NFLogListenerTest, nlmsgDone) {
110 constexpr uint16_t kType = 38;
111 const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {};
112 subscribe(kType, dispatchFn);
113 mDoneFn({}, {});
114 }
115
TEST_F(NFLogListenerTest,dispatchOk)116 TEST_F(NFLogListenerTest, dispatchOk) {
117 int invocations = 0;
118 constexpr uint16_t kType = 38;
119 const auto dispatchFn = [&invocations, kType](const nlmsghdr&, const nfgenmsg& nfmsg,
120 const Slice) {
121 EXPECT_EQ(kType, ntohs(nfmsg.res_id));
122 ++invocations;
123 };
124 subscribe(kType, dispatchFn);
125 sendEmptyMsg(kType);
126 EXPECT_EQ(1, invocations);
127 }
128
TEST_F(NFLogListenerTest,dispatchUnknownType)129 TEST_F(NFLogListenerTest, dispatchUnknownType) {
130 constexpr uint16_t kType = 38;
131 constexpr uint16_t kBadType = kType + 1;
132 const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {
133 // Expect no invocations
134 ASSERT_TRUE(false);
135 };
136 subscribe(kType, dispatchFn);
137 sendEmptyMsg(kBadType);
138 }
139
140 } // namespace net
141 } // namespace android
142