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::Slice;
42 using netdutils::StatusOr;
43 using netdutils::makeSlice;
44 using netdutils::status::ok;
45 
46 constexpr int kNFLogPacketMsgType = (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_PACKET;
47 constexpr int kNetlinkMsgDoneType = (NFNL_SUBSYS_NONE << 8) | NLMSG_DONE;
48 
49 class MockNetlinkListener : public NetlinkListenerInterface {
50   public:
51     ~MockNetlinkListener() override = default;
52 
53     MOCK_METHOD1(send, netdutils::Status(const Slice msg));
54     MOCK_METHOD2(subscribe, netdutils::Status(uint16_t type, const DispatchFn& fn));
55     MOCK_METHOD1(unsubscribe, netdutils::Status(uint16_t type));
56     MOCK_METHOD0(join, void());
57     MOCK_METHOD1(registerSkErrorHandler, void(const SkErrorHandler& handler));
58 };
59 
60 class NFLogListenerTest : public testing::Test {
61   protected:
NFLogListenerTest()62     NFLogListenerTest() {
63         EXPECT_CALL(*mNLListener, subscribe(kNFLogPacketMsgType, _))
64             .WillOnce(DoAll(SaveArg<1>(&mPacketFn), Return(ok)));
65         EXPECT_CALL(*mNLListener, subscribe(kNetlinkMsgDoneType, _))
66             .WillOnce(DoAll(SaveArg<1>(&mDoneFn), Return(ok)));
67         mListener.reset(new NFLogListener(mNLListener));
68     }
69 
~NFLogListenerTest()70     ~NFLogListenerTest() {
71         EXPECT_CALL(*mNLListener, unsubscribe(kNFLogPacketMsgType)).WillOnce(Return(ok));
72         EXPECT_CALL(*mNLListener, unsubscribe(kNetlinkMsgDoneType)).WillOnce(Return(ok));
73     }
74 
sendOk(const Slice buf)75     static StatusOr<size_t> sendOk(const Slice buf) { return buf.size(); }
76 
subscribe(uint16_t type,const NFLogListenerInterface::DispatchFn & fn)77     void subscribe(uint16_t type, const NFLogListenerInterface::DispatchFn& fn) {
78         // Two sends for cfgCmdBind() & cfgMode(), one send at destruction time for cfgCmdUnbind()
79         EXPECT_CALL(*mNLListener, send(_)).Times(Exactly(3)).WillRepeatedly(Invoke(sendOk));
80         EXPECT_OK(mListener->subscribe(type, fn));
81     }
82 
sendEmptyMsg(uint16_t type)83     void sendEmptyMsg(uint16_t type) {
84         struct {
85             nlmsghdr nlmsg;
86             nfgenmsg nfmsg;
87         } msg = {};
88 
89         msg.nlmsg.nlmsg_type = kNFLogPacketMsgType;
90         msg.nlmsg.nlmsg_len = sizeof(msg);
91         msg.nfmsg.res_id = htons(type);
92         mPacketFn(msg.nlmsg, drop(makeSlice(msg), sizeof(msg.nlmsg)));
93     }
94 
95     NetlinkListenerInterface::DispatchFn mPacketFn;
96     NetlinkListenerInterface::DispatchFn mDoneFn;
97     std::shared_ptr<StrictMock<MockNetlinkListener>> mNLListener{
98         new StrictMock<MockNetlinkListener>()};
99     std::unique_ptr<NFLogListener> mListener;
100 };
101 
TEST_F(NFLogListenerTest,subscribe)102 TEST_F(NFLogListenerTest, subscribe) {
103     constexpr uint16_t kType = 38;
104     const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {};
105     subscribe(kType, dispatchFn);
106 }
107 
TEST_F(NFLogListenerTest,nlmsgDone)108 TEST_F(NFLogListenerTest, nlmsgDone) {
109     constexpr uint16_t kType = 38;
110     const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {};
111     subscribe(kType, dispatchFn);
112     mDoneFn({}, {});
113 }
114 
TEST_F(NFLogListenerTest,dispatchOk)115 TEST_F(NFLogListenerTest, dispatchOk) {
116     int invocations = 0;
117     constexpr uint16_t kType = 38;
118     const auto dispatchFn = [&invocations, kType](const nlmsghdr&, const nfgenmsg& nfmsg,
119                                                   const Slice) {
120         EXPECT_EQ(kType, ntohs(nfmsg.res_id));
121         ++invocations;
122     };
123     subscribe(kType, dispatchFn);
124     sendEmptyMsg(kType);
125     EXPECT_EQ(1, invocations);
126 }
127 
TEST_F(NFLogListenerTest,dispatchUnknownType)128 TEST_F(NFLogListenerTest, dispatchUnknownType) {
129     constexpr uint16_t kType = 38;
130     constexpr uint16_t kBadType = kType + 1;
131     const auto dispatchFn = [](const nlmsghdr&, const nfgenmsg&, const Slice) {
132         // Expect no invocations
133         ASSERT_TRUE(false);
134     };
135     subscribe(kType, dispatchFn);
136     sendEmptyMsg(kBadType);
137 }
138 
139 }  // namespace net
140 }  // namespace android
141