|
| 1 | +// Copyright 2025 The gVisor Authors. |
| 2 | +// |
| 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 <linux/netfilter/nfnetlink.h> |
| 16 | +#include <linux/netlink.h> |
| 17 | +#include <sys/socket.h> |
| 18 | + |
| 19 | +#include <functional> |
| 20 | +#include <string> |
| 21 | +#include <tuple> |
| 22 | + |
| 23 | +#include "gmock/gmock.h" |
| 24 | +#include "gtest/gtest.h" |
| 25 | +#include "absl/strings/str_format.h" |
| 26 | +#include "test/syscalls/linux/socket_netlink_util.h" |
| 27 | +#include "test/util/file_descriptor.h" |
| 28 | +#include "test/util/posix_error.h" |
| 29 | +#include "test/util/socket_util.h" |
| 30 | +#include "test/util/test_util.h" |
| 31 | + |
| 32 | +// Tests for NETLINK_NETFILTER sockets. |
| 33 | + |
| 34 | +namespace gvisor { |
| 35 | +namespace testing { |
| 36 | + |
| 37 | +namespace { |
| 38 | + |
| 39 | +using SockOptTest = ::testing::TestWithParam< |
| 40 | + std::tuple<int, std::function<bool(int)>, std::string>>; |
| 41 | + |
| 42 | +TEST_P(SockOptTest, GetSockOpt) { |
| 43 | + int sockopt = std::get<0>(GetParam()); |
| 44 | + auto verifier = std::get<1>(GetParam()); |
| 45 | + std::string verifier_description = std::get<2>(GetParam()); |
| 46 | + |
| 47 | + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE( |
| 48 | + Socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER)); |
| 49 | + |
| 50 | + int res; |
| 51 | + socklen_t len = sizeof(res); |
| 52 | + |
| 53 | + EXPECT_THAT(getsockopt(fd.get(), SOL_SOCKET, sockopt, &res, &len), |
| 54 | + SyscallSucceeds()); |
| 55 | + |
| 56 | + EXPECT_EQ(len, sizeof(res)); |
| 57 | + EXPECT_TRUE(verifier(res)) << absl::StrFormat( |
| 58 | + "getsockopt(%d, SOL_SOCKET, %d, &res, &len) => res=%d was unexpected, " |
| 59 | + "expected %s", |
| 60 | + fd.get(), sockopt, res, verifier_description); |
| 61 | +} |
| 62 | + |
| 63 | +std::function<bool(int)> IsPositive() { |
| 64 | + return [](int val) { return val > 0; }; |
| 65 | +} |
| 66 | + |
| 67 | +std::function<bool(int)> IsEqual(int target) { |
| 68 | + return [target](int val) { return val == target; }; |
| 69 | +} |
| 70 | + |
| 71 | +INSTANTIATE_TEST_SUITE_P( |
| 72 | + NetlinkNetfilterTest, SockOptTest, |
| 73 | + ::testing::Values( |
| 74 | + std::make_tuple(SO_SNDBUF, IsPositive(), "positive send buffer size"), |
| 75 | + std::make_tuple(SO_RCVBUF, IsPositive(), |
| 76 | + "positive receive buffer size"), |
| 77 | + std::make_tuple(SO_TYPE, IsEqual(SOCK_RAW), |
| 78 | + absl::StrFormat("SOCK_RAW (%d)", SOCK_RAW)), |
| 79 | + std::make_tuple(SO_DOMAIN, IsEqual(AF_NETLINK), |
| 80 | + absl::StrFormat("AF_NETLINK (%d)", AF_NETLINK)), |
| 81 | + std::make_tuple(SO_PROTOCOL, IsEqual(NETLINK_NETFILTER), |
| 82 | + absl::StrFormat("NETLINK_NETFILTER (%d)", |
| 83 | + NETLINK_NETFILTER)), |
| 84 | + std::make_tuple(SO_PASSCRED, IsEqual(0), "0"))); |
| 85 | + |
| 86 | +// Netlink sockets must be SOCK_DGRAM or SOCK_RAW. |
| 87 | +TEST(NetlinkNetfilterTest, CanCreateSocket) { |
| 88 | + FileDescriptor fd = |
| 89 | + ASSERT_NO_ERRNO_AND_VALUE(NetlinkBoundSocket(NETLINK_NETFILTER)); |
| 90 | + EXPECT_THAT(fd.get(), SyscallSucceeds()); |
| 91 | +} |
| 92 | +} // namespace |
| 93 | + |
| 94 | +} // namespace testing |
| 95 | +} // namespace gvisor |
0 commit comments