ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | * xfrm_ctrl_test.cpp - unit tests for xfrm controllers. |
| 17 | */ |
| 18 | |
| 19 | #include <cerrno> |
| 20 | #include <cinttypes> |
| 21 | #include <cstdint> |
| 22 | #include <cstdio> |
| 23 | #include <cstdlib> |
| 24 | #include <set> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <arpa/inet.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <gmock/gmock.h> |
| 30 | #include <ifaddrs.h> |
| 31 | #include <linux/if.h> |
| 32 | #include <linux/if_tun.h> |
| 33 | #include <linux/netfilter/nfnetlink.h> |
| 34 | #include <linux/netlink.h> |
| 35 | #include <netdb.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <android-base/macros.h> |
| 41 | #include <android-base/stringprintf.h> |
| 42 | #include <android-base/strings.h> |
| 43 | #include <android-base/unique_fd.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | |
| 46 | #include "NetdConstants.h" |
| 47 | #include "NetlinkCommands.h" |
| 48 | #include "Stopwatch.h" |
| 49 | #include "XfrmController.h" |
| 50 | #include "android/net/INetd.h" |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 51 | #include "binder/IServiceManager.h" |
| 52 | #include "netdutils/MockSyscalls.h" |
| 53 | #include "netdutils/Netlink.h" |
| 54 | #include "tun_interface.h" |
| 55 | |
| 56 | using android::base::unique_fd; |
| 57 | using android::netdutils::Fd; |
| 58 | using android::netdutils::MockSyscalls; |
| 59 | using android::netdutils::Slice; |
| 60 | using android::netdutils::Status; |
| 61 | using android::netdutils::StatusOr; |
| 62 | using android::netdutils::Syscalls; |
| 63 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 64 | using ::testing::_; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 65 | using ::testing::DoAll; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 66 | using ::testing::Invoke; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 67 | using ::testing::Return; |
| 68 | using ::testing::SaveArg; |
| 69 | using ::testing::SetArgPointee; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 70 | using ::testing::Values; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 71 | using ::testing::WithArg; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 74 | * This gMock action works like SetArgPointee, but for netdutils::Slice. |
| 75 | * It sets the memory which is pointed to by the N-th argument with the supplied value. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 76 | */ |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 77 | ACTION_TEMPLATE(SetArgSlice, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(value)) { |
| 78 | Slice orig = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 79 | android::netdutils::copy(orig, value); |
| 80 | } |
| 81 | |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 82 | /** |
| 83 | * This gMock action works like SaveArg, but is specialized for vector<iovec>. |
| 84 | * It copies the memory pointed to by each of the iovecs into a single vector<uint8_t>. |
| 85 | * |
| 86 | * Flattening the iovec objects cannot be done later, since there is no guarantee that the memory |
| 87 | * they point to will still be valid after the mock method returns. |
| 88 | */ |
| 89 | ACTION_TEMPLATE(SaveFlattenedIovecs, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(resVec)) { |
| 90 | const std::vector<iovec>& iovs = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 91 | |
| 92 | for (const iovec& iov : iovs) { |
| 93 | resVec->insert(resVec->end(), reinterpret_cast<uint8_t*>(iov.iov_base), |
| 94 | reinterpret_cast<uint8_t*>(iov.iov_base) + iov.iov_len); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | namespace android { |
| 99 | namespace net { |
| 100 | |
| 101 | static constexpr int DROID_SPI = 0xD1201D; |
| 102 | static constexpr size_t KEY_LENGTH = 32; |
| 103 | static constexpr int NLMSG_DEFAULTSIZE = 8192; |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 104 | static constexpr uint32_t TEST_XFRM_OUTPUT_MARK = 0x512; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 105 | static constexpr uint32_t TEST_XFRM_MARK = 0x123; |
| 106 | static constexpr uint32_t TEST_XFRM_MASK = 0xFFFFFFFF; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 107 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 108 | static constexpr char LOCALHOST_V4[] = "127.0.0.1"; |
| 109 | static constexpr char LOCALHOST_V6[] = "::1"; |
| 110 | static constexpr char TEST_ADDR_V4[] = "8.8.8.8"; |
| 111 | static constexpr char TEST_ADDR_V6[] = "2001:4860:4860::8888"; |
| 112 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 113 | struct Policy { |
| 114 | xfrm_userpolicy_info info; |
| 115 | xfrm_user_tmpl tmpl; |
| 116 | }; |
| 117 | |
| 118 | struct NetlinkResponse { |
| 119 | nlmsghdr hdr; |
| 120 | char buf[NLMSG_DEFAULTSIZE]; |
| 121 | }; |
| 122 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 123 | void expectAddressEquals(int family, const std::string& expected, const xfrm_address_t& actual) { |
| 124 | char actualStr[INET6_ADDRSTRLEN]; |
| 125 | const char* ret = |
| 126 | inet_ntop(family, reinterpret_cast<const void*>(&actual), actualStr, INET6_ADDRSTRLEN); |
| 127 | EXPECT_NE(nullptr, ret) << "Unable to convert xfrm_address_t to string"; |
| 128 | EXPECT_EQ(expected, actualStr); |
| 129 | } |
| 130 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 131 | class XfrmControllerTest : public ::testing::Test { |
| 132 | public: |
| 133 | MockSyscalls mockSyscalls; |
| 134 | |
| 135 | void SetUp() override { netdutils::sSyscalls.swap(mockSyscalls); } |
| 136 | }; |
| 137 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 138 | // Test class allowing IPv4/IPv6 parameterized tests. |
| 139 | class XfrmControllerParameterizedTest : public XfrmControllerTest, |
| 140 | public ::testing::WithParamInterface<int> {}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 141 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 142 | // Helper to make generated test names readable. |
| 143 | std::string FamilyName(::testing::TestParamInfo<int> info) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 144 | switch(info.param) { |
| 145 | case 4: return "IPv4"; |
| 146 | case 5: return "IPv64DualStack"; |
| 147 | case 6: return "IPv6"; |
| 148 | } |
| 149 | return android::base::StringPrintf("UNKNOWN family type: %d", info.param); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 152 | /* Generate function to set value refered to by 3rd argument. |
| 153 | * |
| 154 | * This allows us to mock functions that pass in a pointer, expecting the result to be put into |
| 155 | * that location. |
| 156 | */ |
| 157 | ACTION_P(SetArg3IntValue, value) { *static_cast<int*>(arg3) = value; } |
| 158 | |
| 159 | TEST_F(XfrmControllerTest, TestFchown) { |
| 160 | XfrmController ctrl; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 161 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 162 | |
| 163 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 164 | .WillOnce(DoAll(SetArg3IntValue(UDP_ENCAP_ESPINUDP), Return(netdutils::status::ok))); |
| 165 | |
| 166 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 167 | EXPECT_EQ(netdutils::status::ok, res); |
| 168 | |
| 169 | struct stat info; |
| 170 | EXPECT_EQ(0, fstat(sockFd.get(), &info)); |
| 171 | EXPECT_EQ(1001, (int)info.st_uid); |
| 172 | } |
| 173 | |
| 174 | TEST_F(XfrmControllerTest, TestFchownInvalidFd) { |
| 175 | XfrmController ctrl; |
| 176 | |
| 177 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(unique_fd(), 1001, getuid()); |
| 178 | EXPECT_EQ(netdutils::statusFromErrno(errno, "Failed to stat socket file descriptor"), res); |
| 179 | } |
| 180 | |
| 181 | TEST_F(XfrmControllerTest, TestFchownIncorrectCallerUid) { |
| 182 | XfrmController ctrl; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 183 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 184 | |
| 185 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, 1001); |
| 186 | EXPECT_EQ(netdutils::statusFromErrno(EPERM, "fchown disabled for non-owner calls"), res); |
| 187 | } |
| 188 | |
| 189 | TEST_F(XfrmControllerTest, TestFchownNonSocketFd) { |
| 190 | XfrmController ctrl; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 191 | unique_fd fd(open("/dev/null", O_CLOEXEC)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 192 | |
| 193 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(fd, 1001, getuid()); |
| 194 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "File descriptor was not a socket"), res); |
| 195 | } |
| 196 | |
| 197 | TEST_F(XfrmControllerTest, TestFchownNonUdp) { |
| 198 | XfrmController ctrl; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 199 | unique_fd sockFd(socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 200 | |
| 201 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 202 | .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok))); |
| 203 | |
| 204 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 205 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket was not a UDP socket"), res); |
| 206 | } |
| 207 | |
| 208 | TEST_F(XfrmControllerTest, TestFchownNonUdpEncap) { |
| 209 | XfrmController ctrl; |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 210 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 211 | |
| 212 | EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _)) |
| 213 | .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok))); |
| 214 | |
| 215 | netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid()); |
| 216 | EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket did not have UDP-encap sockopt set"), res); |
| 217 | } |
| 218 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 219 | // The TEST_P cases below will run with each of the following value parameters. |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 220 | INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(4, 5, 6), |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 221 | FamilyName); |
| 222 | |
| 223 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAllocateSpi) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 224 | const int version = GetParam(); |
| 225 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 226 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 227 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 228 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 229 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 230 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 231 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 232 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 233 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 234 | |
| 235 | // A vector to hold the flattened netlink message for nlMsgSlice |
| 236 | std::vector<uint8_t> nlMsgBuf; |
| 237 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 238 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 239 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 240 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 241 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 242 | XfrmController ctrl; |
| 243 | int outSpi = 0; |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 244 | Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, localAddr, |
| 245 | remoteAddr, DROID_SPI, &outSpi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 246 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 247 | EXPECT_TRUE(isOk(res)) << res; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 248 | EXPECT_EQ(DROID_SPI, outSpi); |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 249 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 250 | |
| 251 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 252 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 253 | |
| 254 | xfrm_userspi_info userspi{}; |
| 255 | netdutils::extract(nlMsgSlice, userspi); |
| 256 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 257 | EXPECT_EQ(AF_UNSPEC, userspi.info.sel.family); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 258 | expectAddressEquals(family, localAddr, userspi.info.saddr); |
| 259 | expectAddressEquals(family, remoteAddr, userspi.info.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 260 | |
| 261 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min)); |
| 262 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max)); |
| 263 | } |
| 264 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 265 | void testIpSecAddSecurityAssociation(int version, const MockSyscalls& mockSyscalls, |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 266 | const XfrmMode& mode, __u32 underlying_netid) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 267 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 268 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 269 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 270 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 271 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 272 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 273 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 274 | |
| 275 | std::vector<uint8_t> authKey(KEY_LENGTH, 0); |
| 276 | std::vector<uint8_t> cryptKey(KEY_LENGTH, 1); |
| 277 | |
| 278 | // Calculate the length of the expected netlink message. |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 279 | size_t expectedMsgLength = |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 280 | NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) + |
| 281 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) + |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 282 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH) + |
| 283 | NLA_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 284 | |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 285 | if (underlying_netid) { |
| 286 | expectedMsgLength += NLA_ALIGN(sizeof(XfrmController::nlattr_xfrm_output_mark)); |
| 287 | } |
| 288 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 289 | std::vector<uint8_t> nlMsgBuf; |
| 290 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 291 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 292 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 293 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 294 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 295 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 296 | Status res = ctrl.ipSecAddSecurityAssociation( |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 297 | 1 /* resourceId */, static_cast<int>(mode), localAddr, remoteAddr, |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 298 | underlying_netid /* underlying netid */, DROID_SPI, TEST_XFRM_MARK /* mark */, |
| 299 | TEST_XFRM_MASK /* mask */, "hmac(sha256)" /* auth algo */, |
| 300 | authKey, 128 /* auth trunc length */, "cbc(aes)" /* encryption algo */, |
| 301 | cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */, {}, 0, |
| 302 | static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 303 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 304 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 305 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 306 | |
| 307 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 308 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 309 | |
| 310 | xfrm_usersa_info usersa{}; |
| 311 | netdutils::extract(nlMsgSlice, usersa); |
| 312 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 313 | EXPECT_EQ(family, usersa.family); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 314 | EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid)); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 315 | EXPECT_EQ(static_cast<int>(mode), usersa.mode); |
| 316 | |
| 317 | if (mode == XfrmMode::TUNNEL) { |
| 318 | EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags); |
| 319 | } else { |
| 320 | EXPECT_EQ(0, usersa.flags); |
| 321 | } |
| 322 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 323 | EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi); |
| 324 | EXPECT_EQ(IPPROTO_ESP, usersa.id.proto); |
| 325 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 326 | expectAddressEquals(family, localAddr, usersa.saddr); |
| 327 | expectAddressEquals(family, remoteAddr, usersa.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 328 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 329 | // Extract and check the encryption/authentication algorithms. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 330 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info))); |
| 331 | |
| 332 | // Extract and check the encryption/authentication algorithm |
| 333 | XfrmController::nlattr_algo_crypt encryptAlgo{}; |
| 334 | XfrmController::nlattr_algo_auth authAlgo{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 335 | XfrmController::nlattr_xfrm_mark mark{}; |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 336 | XfrmController::nlattr_xfrm_output_mark outputmark{}; |
| 337 | auto attrHandler = [&encryptAlgo, &authAlgo, &mark, &outputmark](const nlattr& attr, |
| 338 | const Slice& attr_payload) { |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 339 | Slice buf = attr_payload; |
| 340 | if (attr.nla_type == XFRMA_ALG_CRYPT) { |
| 341 | encryptAlgo.hdr = attr; |
| 342 | netdutils::extract(buf, encryptAlgo.crypt); |
| 343 | buf = drop(buf, sizeof(xfrm_algo)); |
| 344 | netdutils::extract(buf, encryptAlgo.key); |
| 345 | } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) { |
| 346 | authAlgo.hdr = attr; |
| 347 | netdutils::extract(buf, authAlgo.auth); |
| 348 | buf = drop(buf, sizeof(xfrm_algo_auth)); |
| 349 | netdutils::extract(buf, authAlgo.key); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 350 | } else if (attr.nla_type == XFRMA_MARK) { |
| 351 | mark.hdr = attr; |
| 352 | netdutils::extract(buf, mark.mark); |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 353 | } else if (attr.nla_type == XFRMA_OUTPUT_MARK) { |
| 354 | mark.hdr = attr; |
| 355 | netdutils::extract(buf, outputmark.outputMark); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 356 | } else { |
| 357 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 358 | } |
| 359 | }; |
| 360 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 361 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 362 | // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 363 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()), |
| 364 | reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH)); |
| 365 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()), |
| 366 | reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH)); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 367 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 368 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 369 | if (underlying_netid) { |
| 370 | EXPECT_EQ(TEST_XFRM_OUTPUT_MARK, outputmark.outputMark); |
| 371 | } |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 372 | } |
| 373 | |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 374 | TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 375 | const int version = GetParam(); |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 376 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TRANSPORT, 0); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 380 | const int version = GetParam(); |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 381 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TUNNEL, 0); |
| 382 | } |
| 383 | |
| 384 | TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociationWithOutputMark) { |
| 385 | const int version = GetParam(); |
| 386 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TUNNEL, |
| 387 | TEST_XFRM_OUTPUT_MARK); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 390 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) { |
| 391 | // TODO: Implement this test, which is nearly identical to |
| 392 | // TestIpSecAddSecurityAssociation. |
| 393 | } |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 394 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 395 | // Test that input validation rejects IPv6 UDP encap. |
| 396 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) { |
| 397 | EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0); |
| 398 | |
| 399 | XfrmController ctrl; |
| 400 | Status res = ctrl.ipSecAddSecurityAssociation( |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 401 | 1, static_cast<int>(XfrmMode::TRANSPORT), |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 402 | LOCALHOST_V6, TEST_ADDR_V6, 0, DROID_SPI, 0, 0, "hmac(sha256)", {}, 128, "cbc(aes)", |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 403 | {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 404 | |
| 405 | EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected"; |
| 406 | } |
| 407 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 408 | TEST_F(XfrmControllerTest, TestIpSecApplyTransportModeTransformChecksFamily) { |
| 409 | struct sockaddr socketaddr; |
| 410 | socketaddr.sa_family = AF_INET; |
| 411 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 412 | unique_fd sock(socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 413 | |
| 414 | EXPECT_CALL(mockSyscalls, getsockname(Fd(sock), _, _)) |
| 415 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 416 | |
| 417 | XfrmController ctrl; |
| 418 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 419 | static_cast<int>(XfrmDirection::OUT), |
| 420 | LOCALHOST_V6, TEST_ADDR_V6, DROID_SPI); |
| 421 | |
| 422 | EXPECT_EQ(res.code(), EINVAL); |
| 423 | } |
| 424 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 425 | TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 426 | const int version = GetParam(); |
| 427 | const int sockFamily = (version == 4) ? AF_INET : AF_INET6; |
| 428 | const int xfrmFamily = (version == 6) ? AF_INET6: AF_INET; |
| 429 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 430 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 431 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 432 | size_t optlen = 0; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 433 | Policy policy{}; |
| 434 | // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a |
| 435 | // lambda than to write a gMock action. |
| 436 | auto SavePolicy = [&policy](const void* value) { |
| 437 | policy = *reinterpret_cast<const Policy*>(value); |
| 438 | }; |
| 439 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 440 | struct sockaddr socketaddr; |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 441 | socketaddr.sa_family = sockFamily; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 442 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 443 | unique_fd sock(socket(sockFamily, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 444 | |
| 445 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 446 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 447 | |
| 448 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 449 | .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen), |
| 450 | Return(netdutils::status::ok))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 451 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 452 | XfrmController ctrl; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 453 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 454 | static_cast<int>(XfrmDirection::OUT), |
| 455 | localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 456 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 457 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 458 | EXPECT_EQ(sizeof(Policy), optlen); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 459 | |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 460 | EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid)); |
| 461 | EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 462 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 463 | expectAddressEquals(xfrmFamily, localAddr, policy.tmpl.saddr); |
| 464 | expectAddressEquals(xfrmFamily, remoteAddr, policy.tmpl.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 465 | } |
| 466 | |
ludi | 8799163 | 2017-10-30 15:28:11 -0700 | [diff] [blame] | 467 | TEST_P(XfrmControllerParameterizedTest, TestIpSecRemoveTransportModeTransform) { |
| 468 | const int version = GetParam(); |
| 469 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 470 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 471 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 472 | |
| 473 | socklen_t optlen; |
| 474 | const void* optval; |
| 475 | |
| 476 | struct sockaddr socketaddr; |
| 477 | socketaddr.sa_family = family; |
| 478 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 479 | unique_fd sock(socket(family, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
ludi | 8799163 | 2017-10-30 15:28:11 -0700 | [diff] [blame] | 480 | |
| 481 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 482 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 483 | |
| 484 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
| 485 | .WillOnce(DoAll(SaveArg<3>(&optval), SaveArg<4>(&optlen), |
| 486 | Return(netdutils::status::ok))); |
| 487 | XfrmController ctrl; |
| 488 | Status res = ctrl.ipSecRemoveTransportModeTransform(sock); |
| 489 | |
| 490 | EXPECT_TRUE(isOk(res)) << res; |
| 491 | EXPECT_EQ(nullptr, optval); |
| 492 | EXPECT_EQ(static_cast<socklen_t>(0), optlen); |
| 493 | } |
| 494 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 495 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) { |
Benedict Wong | 6e83b39 | 2018-01-11 18:22:33 -0800 | [diff] [blame] | 496 | const int version = GetParam(); |
| 497 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 498 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 499 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 500 | |
| 501 | NetlinkResponse response{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 502 | response.hdr.nlmsg_type = XFRM_MSG_DELSA; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 503 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 504 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 505 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id)) + |
| 506 | NLA_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 507 | |
| 508 | std::vector<uint8_t> nlMsgBuf; |
| 509 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 510 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 511 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 512 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 513 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 514 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 515 | Status res = ctrl.ipSecDeleteSecurityAssociation(1 /* resourceId */, localAddr, remoteAddr, |
| 516 | DROID_SPI, TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 517 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 518 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 519 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 520 | |
| 521 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 522 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 523 | |
| 524 | xfrm_usersa_id said{}; |
| 525 | netdutils::extract(nlMsgSlice, said); |
| 526 | |
| 527 | EXPECT_EQ(htonl(DROID_SPI), said.spi); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 528 | expectAddressEquals(family, remoteAddr, said.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 529 | } |
| 530 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 531 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAddSecurityPolicy) { |
| 532 | const int version = GetParam(); |
| 533 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 534 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 535 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 536 | |
| 537 | NetlinkResponse response{}; |
| 538 | response.hdr.nlmsg_type = XFRM_MSG_NEWPOLICY; |
| 539 | Slice responseSlice = netdutils::makeSlice(response); |
| 540 | |
| 541 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_info)) + |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 542 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_user_tmpl)) + |
| 543 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 544 | |
| 545 | std::vector<uint8_t> nlMsgBuf; |
| 546 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 547 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 548 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 549 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 550 | |
| 551 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 552 | Status res = ctrl.ipSecAddSecurityPolicy( |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 553 | 1 /* resourceId */, family, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, |
| 554 | 0 /* SPI */, TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 555 | |
| 556 | EXPECT_TRUE(isOk(res)) << res; |
| 557 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 558 | |
| 559 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 560 | nlmsghdr hdr; |
| 561 | netdutils::extract(nlMsgSlice, hdr); |
| 562 | EXPECT_EQ(XFRM_MSG_NEWPOLICY, hdr.nlmsg_type); |
| 563 | |
| 564 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 565 | xfrm_userpolicy_info userpolicy{}; |
| 566 | netdutils::extract(nlMsgSlice, userpolicy); |
| 567 | |
| 568 | EXPECT_EQ(static_cast<uint8_t>(XfrmDirection::OUT), userpolicy.dir); |
| 569 | |
| 570 | // Drop the user policy info. |
| 571 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_userpolicy_info))); |
| 572 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 573 | // Extract and check the user tmpl and mark. |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 574 | XfrmController::nlattr_user_tmpl usertmpl{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 575 | XfrmController::nlattr_xfrm_mark mark{}; |
| 576 | auto attrHandler = [&usertmpl, &mark](const nlattr& attr, const Slice& attr_payload) { |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 577 | Slice buf = attr_payload; |
| 578 | if (attr.nla_type == XFRMA_TMPL) { |
| 579 | usertmpl.hdr = attr; |
| 580 | netdutils::extract(buf, usertmpl.tmpl); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 581 | } else if (attr.nla_type == XFRMA_MARK) { |
| 582 | mark.hdr = attr; |
| 583 | netdutils::extract(buf, mark.mark); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 584 | } else { |
| 585 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
| 586 | } |
| 587 | }; |
| 588 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 589 | |
| 590 | expectAddressEquals(family, remoteAddr, usertmpl.tmpl.id.daddr); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 591 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 592 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
| 593 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | TEST_P(XfrmControllerParameterizedTest, TestIpSecUpdateSecurityPolicy) { |
| 597 | const int version = GetParam(); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 598 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 599 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 600 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 601 | |
| 602 | NetlinkResponse response{}; |
| 603 | response.hdr.nlmsg_type = XFRM_MSG_UPDPOLICY; |
| 604 | Slice responseSlice = netdutils::makeSlice(response); |
| 605 | |
| 606 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_info)) + |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 607 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_user_tmpl)) + |
| 608 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 609 | |
| 610 | std::vector<uint8_t> nlMsgBuf; |
| 611 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 612 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 613 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 614 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 615 | |
| 616 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 617 | Status res = ctrl.ipSecUpdateSecurityPolicy( |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 618 | 1 /* resourceId */, family, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, |
| 619 | 0 /* SPI */, 0 /* Mark */, 0 /* Mask */); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 620 | |
| 621 | EXPECT_TRUE(isOk(res)) << res; |
| 622 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 623 | |
| 624 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 625 | nlmsghdr hdr; |
| 626 | netdutils::extract(nlMsgSlice, hdr); |
| 627 | EXPECT_EQ(XFRM_MSG_UPDPOLICY, hdr.nlmsg_type); |
| 628 | } |
| 629 | |
| 630 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityPolicy) { |
| 631 | const int version = GetParam(); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 632 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 633 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 634 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 635 | |
| 636 | NetlinkResponse response{}; |
| 637 | response.hdr.nlmsg_type = XFRM_MSG_DELPOLICY; |
| 638 | Slice responseSlice = netdutils::makeSlice(response); |
| 639 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 640 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_id)) + |
| 641 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 642 | |
| 643 | std::vector<uint8_t> nlMsgBuf; |
| 644 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 645 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 646 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 647 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 648 | |
| 649 | XfrmController ctrl; |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 650 | Status res = ctrl.ipSecDeleteSecurityPolicy(1 /* resourceId */, family, |
| 651 | static_cast<int>(XfrmDirection::OUT), |
| 652 | TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 653 | |
| 654 | EXPECT_TRUE(isOk(res)) << res; |
| 655 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 656 | |
| 657 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 658 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 659 | |
| 660 | xfrm_userpolicy_id policyid{}; |
| 661 | netdutils::extract(nlMsgSlice, policyid); |
| 662 | |
| 663 | EXPECT_EQ(static_cast<uint8_t>(XfrmDirection::OUT), policyid.dir); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 664 | |
| 665 | // Drop the user policy id. |
| 666 | nlMsgSlice = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_userpolicy_id))); |
| 667 | // Extract and check the mark. |
| 668 | XfrmController::nlattr_xfrm_mark mark{}; |
| 669 | netdutils::extract(nlMsgSlice, mark); |
| 670 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 671 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
| 672 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 673 | } |
| 674 | |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 675 | // TODO: Add tests for VTIs, ensuring that we are sending the correct data over netlink. |
| 676 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 677 | } // namespace net |
| 678 | } // namespace android |