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" |
| 51 | #include "android/net/UidRange.h" |
| 52 | #include "binder/IServiceManager.h" |
| 53 | #include "netdutils/MockSyscalls.h" |
| 54 | #include "netdutils/Netlink.h" |
| 55 | #include "tun_interface.h" |
| 56 | |
| 57 | using android::base::unique_fd; |
| 58 | using android::netdutils::Fd; |
| 59 | using android::netdutils::MockSyscalls; |
| 60 | using android::netdutils::Slice; |
| 61 | using android::netdutils::Status; |
| 62 | using android::netdutils::StatusOr; |
| 63 | using android::netdutils::Syscalls; |
| 64 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 65 | using ::testing::_; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 66 | using ::testing::DoAll; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 67 | using ::testing::Invoke; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 68 | using ::testing::Return; |
| 69 | using ::testing::SaveArg; |
| 70 | using ::testing::SetArgPointee; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 71 | using ::testing::Values; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 72 | using ::testing::WithArg; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 75 | * This gMock action works like SetArgPointee, but for netdutils::Slice. |
| 76 | * 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] | 77 | */ |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 78 | ACTION_TEMPLATE(SetArgSlice, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(value)) { |
| 79 | Slice orig = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 80 | android::netdutils::copy(orig, value); |
| 81 | } |
| 82 | |
Jonathan Basseri | c612866 | 2017-09-14 17:32:59 -0700 | [diff] [blame] | 83 | /** |
| 84 | * This gMock action works like SaveArg, but is specialized for vector<iovec>. |
| 85 | * It copies the memory pointed to by each of the iovecs into a single vector<uint8_t>. |
| 86 | * |
| 87 | * Flattening the iovec objects cannot be done later, since there is no guarantee that the memory |
| 88 | * they point to will still be valid after the mock method returns. |
| 89 | */ |
| 90 | ACTION_TEMPLATE(SaveFlattenedIovecs, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(resVec)) { |
| 91 | const std::vector<iovec>& iovs = ::testing::get<N>(args); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 92 | |
| 93 | for (const iovec& iov : iovs) { |
| 94 | resVec->insert(resVec->end(), reinterpret_cast<uint8_t*>(iov.iov_base), |
| 95 | reinterpret_cast<uint8_t*>(iov.iov_base) + iov.iov_len); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | namespace android { |
| 100 | namespace net { |
| 101 | |
| 102 | static constexpr int DROID_SPI = 0xD1201D; |
| 103 | static constexpr size_t KEY_LENGTH = 32; |
| 104 | static constexpr int NLMSG_DEFAULTSIZE = 8192; |
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; |
| 161 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 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; |
| 183 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 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; |
| 191 | unique_fd fd(open("/dev/null", 0)); |
| 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; |
| 199 | unique_fd sockFd(socket(AF_INET, SOCK_STREAM, 0)); |
| 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; |
| 210 | unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0)); |
| 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 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 257 | EXPECT_EQ(family, userspi.info.sel.family); |
| 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, |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 266 | const XfrmMode& mode) { |
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 | |
| 285 | std::vector<uint8_t> nlMsgBuf; |
| 286 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 287 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 288 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 289 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 290 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 291 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 292 | Status res = ctrl.ipSecAddSecurityAssociation( |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 293 | 1 /* resourceId */, static_cast<int>(mode), localAddr, remoteAddr, |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 294 | 0 /* underlying network */, DROID_SPI, TEST_XFRM_MARK /* mark */, TEST_XFRM_MASK /* mask */, |
| 295 | "hmac(sha256)" /* auth algo */, authKey, 128 /* auth trunc length */, |
| 296 | "cbc(aes)" /* encryption algo */, cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */, |
| 297 | {}, 0, static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 298 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 299 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 300 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 301 | |
| 302 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 303 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 304 | |
| 305 | xfrm_usersa_info usersa{}; |
| 306 | netdutils::extract(nlMsgSlice, usersa); |
| 307 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 308 | EXPECT_EQ(family, usersa.family); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 309 | EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid)); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 310 | EXPECT_EQ(static_cast<int>(mode), usersa.mode); |
| 311 | |
| 312 | if (mode == XfrmMode::TUNNEL) { |
| 313 | EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags); |
| 314 | } else { |
| 315 | EXPECT_EQ(0, usersa.flags); |
| 316 | } |
| 317 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 318 | EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi); |
| 319 | EXPECT_EQ(IPPROTO_ESP, usersa.id.proto); |
| 320 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 321 | expectAddressEquals(family, localAddr, usersa.saddr); |
| 322 | expectAddressEquals(family, remoteAddr, usersa.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 323 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 324 | // Extract and check the encryption/authentication algorithms. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 325 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info))); |
| 326 | |
| 327 | // Extract and check the encryption/authentication algorithm |
| 328 | XfrmController::nlattr_algo_crypt encryptAlgo{}; |
| 329 | XfrmController::nlattr_algo_auth authAlgo{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 330 | XfrmController::nlattr_xfrm_mark mark{}; |
| 331 | auto attrHandler = [&encryptAlgo, &authAlgo, &mark](const nlattr& attr, |
| 332 | const Slice& attr_payload) { |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 333 | Slice buf = attr_payload; |
| 334 | if (attr.nla_type == XFRMA_ALG_CRYPT) { |
| 335 | encryptAlgo.hdr = attr; |
| 336 | netdutils::extract(buf, encryptAlgo.crypt); |
| 337 | buf = drop(buf, sizeof(xfrm_algo)); |
| 338 | netdutils::extract(buf, encryptAlgo.key); |
| 339 | } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) { |
| 340 | authAlgo.hdr = attr; |
| 341 | netdutils::extract(buf, authAlgo.auth); |
| 342 | buf = drop(buf, sizeof(xfrm_algo_auth)); |
| 343 | netdutils::extract(buf, authAlgo.key); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 344 | } else if (attr.nla_type == XFRMA_MARK) { |
| 345 | mark.hdr = attr; |
| 346 | netdutils::extract(buf, mark.mark); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 347 | } else { |
| 348 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 349 | } |
| 350 | }; |
| 351 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 352 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 353 | // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 354 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()), |
| 355 | reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH)); |
| 356 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()), |
| 357 | reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH)); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 358 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 359 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 360 | } |
| 361 | |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 362 | TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 363 | const int version = GetParam(); |
| 364 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TRANSPORT); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 368 | const int version = GetParam(); |
| 369 | testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TUNNEL); |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 372 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) { |
| 373 | // TODO: Implement this test, which is nearly identical to |
| 374 | // TestIpSecAddSecurityAssociation. |
| 375 | } |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 376 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 377 | // Test that input validation rejects IPv6 UDP encap. |
| 378 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) { |
| 379 | EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0); |
| 380 | |
| 381 | XfrmController ctrl; |
| 382 | Status res = ctrl.ipSecAddSecurityAssociation( |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 383 | 1, static_cast<int>(XfrmMode::TRANSPORT), |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 384 | 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] | 385 | {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 386 | |
| 387 | EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected"; |
| 388 | } |
| 389 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 390 | TEST_F(XfrmControllerTest, TestIpSecApplyTransportModeTransformChecksFamily) { |
| 391 | struct sockaddr socketaddr; |
| 392 | socketaddr.sa_family = AF_INET; |
| 393 | |
| 394 | unique_fd sock(socket(AF_INET, SOCK_STREAM, 0)); |
| 395 | |
| 396 | EXPECT_CALL(mockSyscalls, getsockname(Fd(sock), _, _)) |
| 397 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 398 | |
| 399 | XfrmController ctrl; |
| 400 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 401 | static_cast<int>(XfrmDirection::OUT), |
| 402 | LOCALHOST_V6, TEST_ADDR_V6, DROID_SPI); |
| 403 | |
| 404 | EXPECT_EQ(res.code(), EINVAL); |
| 405 | } |
| 406 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 407 | TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) { |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 408 | const int version = GetParam(); |
| 409 | const int sockFamily = (version == 4) ? AF_INET : AF_INET6; |
| 410 | const int xfrmFamily = (version == 6) ? AF_INET6: AF_INET; |
| 411 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 412 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 413 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 414 | size_t optlen = 0; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 415 | Policy policy{}; |
| 416 | // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a |
| 417 | // lambda than to write a gMock action. |
| 418 | auto SavePolicy = [&policy](const void* value) { |
| 419 | policy = *reinterpret_cast<const Policy*>(value); |
| 420 | }; |
| 421 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 422 | struct sockaddr socketaddr; |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 423 | socketaddr.sa_family = sockFamily; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 424 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 425 | unique_fd sock(socket(sockFamily, SOCK_STREAM, 0)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 426 | |
| 427 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 428 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 429 | |
| 430 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 431 | .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen), |
| 432 | Return(netdutils::status::ok))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 433 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 434 | XfrmController ctrl; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 435 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 436 | static_cast<int>(XfrmDirection::OUT), |
| 437 | localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 438 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 439 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 440 | EXPECT_EQ(sizeof(Policy), optlen); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 441 | |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 442 | EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid)); |
| 443 | EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 444 | |
manojboopathi | 26f4292 | 2017-12-14 10:51:57 -0800 | [diff] [blame] | 445 | expectAddressEquals(xfrmFamily, localAddr, policy.tmpl.saddr); |
| 446 | expectAddressEquals(xfrmFamily, remoteAddr, policy.tmpl.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 447 | } |
| 448 | |
ludi | 8799163 | 2017-10-30 15:28:11 -0700 | [diff] [blame] | 449 | TEST_P(XfrmControllerParameterizedTest, TestIpSecRemoveTransportModeTransform) { |
| 450 | const int version = GetParam(); |
| 451 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 452 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 453 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 454 | |
| 455 | socklen_t optlen; |
| 456 | const void* optval; |
| 457 | |
| 458 | struct sockaddr socketaddr; |
| 459 | socketaddr.sa_family = family; |
| 460 | |
| 461 | unique_fd sock(socket(family, SOCK_STREAM, 0)); |
| 462 | |
| 463 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 464 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 465 | |
| 466 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
| 467 | .WillOnce(DoAll(SaveArg<3>(&optval), SaveArg<4>(&optlen), |
| 468 | Return(netdutils::status::ok))); |
| 469 | XfrmController ctrl; |
| 470 | Status res = ctrl.ipSecRemoveTransportModeTransform(sock); |
| 471 | |
| 472 | EXPECT_TRUE(isOk(res)) << res; |
| 473 | EXPECT_EQ(nullptr, optval); |
| 474 | EXPECT_EQ(static_cast<socklen_t>(0), optlen); |
| 475 | } |
| 476 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 477 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) { |
Benedict Wong | 6e83b39 | 2018-01-11 18:22:33 -0800 | [diff] [blame] | 478 | const int version = GetParam(); |
| 479 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 480 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 481 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 482 | |
| 483 | NetlinkResponse response{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 484 | response.hdr.nlmsg_type = XFRM_MSG_DELSA; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 485 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 486 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 487 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id)) + |
| 488 | NLA_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 489 | |
| 490 | std::vector<uint8_t> nlMsgBuf; |
| 491 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 492 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 493 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 494 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 495 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 496 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 497 | Status res = ctrl.ipSecDeleteSecurityAssociation(1 /* resourceId */, localAddr, remoteAddr, |
| 498 | DROID_SPI, TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 499 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 500 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 501 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 502 | |
| 503 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 504 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 505 | |
| 506 | xfrm_usersa_id said{}; |
| 507 | netdutils::extract(nlMsgSlice, said); |
| 508 | |
| 509 | EXPECT_EQ(htonl(DROID_SPI), said.spi); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 510 | expectAddressEquals(family, remoteAddr, said.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 511 | } |
| 512 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 513 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAddSecurityPolicy) { |
| 514 | const int version = GetParam(); |
| 515 | const int family = (version == 6) ? AF_INET6 : AF_INET; |
| 516 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 517 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 518 | |
| 519 | NetlinkResponse response{}; |
| 520 | response.hdr.nlmsg_type = XFRM_MSG_NEWPOLICY; |
| 521 | Slice responseSlice = netdutils::makeSlice(response); |
| 522 | |
| 523 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_info)) + |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 524 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_user_tmpl)) + |
| 525 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 526 | |
| 527 | std::vector<uint8_t> nlMsgBuf; |
| 528 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 529 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 530 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 531 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 532 | |
| 533 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 534 | Status res = ctrl.ipSecAddSecurityPolicy( |
| 535 | 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, |
| 536 | 0 /* SPI */, TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 537 | |
| 538 | EXPECT_TRUE(isOk(res)) << res; |
| 539 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 540 | |
| 541 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 542 | nlmsghdr hdr; |
| 543 | netdutils::extract(nlMsgSlice, hdr); |
| 544 | EXPECT_EQ(XFRM_MSG_NEWPOLICY, hdr.nlmsg_type); |
| 545 | |
| 546 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 547 | xfrm_userpolicy_info userpolicy{}; |
| 548 | netdutils::extract(nlMsgSlice, userpolicy); |
| 549 | |
| 550 | EXPECT_EQ(static_cast<uint8_t>(XfrmDirection::OUT), userpolicy.dir); |
| 551 | |
| 552 | // Drop the user policy info. |
| 553 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_userpolicy_info))); |
| 554 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 555 | // Extract and check the user tmpl and mark. |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 556 | XfrmController::nlattr_user_tmpl usertmpl{}; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 557 | XfrmController::nlattr_xfrm_mark mark{}; |
| 558 | auto attrHandler = [&usertmpl, &mark](const nlattr& attr, const Slice& attr_payload) { |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 559 | Slice buf = attr_payload; |
| 560 | if (attr.nla_type == XFRMA_TMPL) { |
| 561 | usertmpl.hdr = attr; |
| 562 | netdutils::extract(buf, usertmpl.tmpl); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 563 | } else if (attr.nla_type == XFRMA_MARK) { |
| 564 | mark.hdr = attr; |
| 565 | netdutils::extract(buf, mark.mark); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 566 | } else { |
| 567 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
| 568 | } |
| 569 | }; |
| 570 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 571 | |
| 572 | expectAddressEquals(family, remoteAddr, usertmpl.tmpl.id.daddr); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 573 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 574 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
| 575 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | TEST_P(XfrmControllerParameterizedTest, TestIpSecUpdateSecurityPolicy) { |
| 579 | const int version = GetParam(); |
| 580 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 581 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 582 | |
| 583 | NetlinkResponse response{}; |
| 584 | response.hdr.nlmsg_type = XFRM_MSG_UPDPOLICY; |
| 585 | Slice responseSlice = netdutils::makeSlice(response); |
| 586 | |
| 587 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_info)) + |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 588 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_user_tmpl)) + |
| 589 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 590 | |
| 591 | std::vector<uint8_t> nlMsgBuf; |
| 592 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 593 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 594 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 595 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 596 | |
| 597 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 598 | Status res = ctrl.ipSecUpdateSecurityPolicy( |
| 599 | 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, |
| 600 | 0 /* SPI */, 0 /* Mark */, 0 /* Mask */); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 601 | |
| 602 | EXPECT_TRUE(isOk(res)) << res; |
| 603 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 604 | |
| 605 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 606 | nlmsghdr hdr; |
| 607 | netdutils::extract(nlMsgSlice, hdr); |
| 608 | EXPECT_EQ(XFRM_MSG_UPDPOLICY, hdr.nlmsg_type); |
| 609 | } |
| 610 | |
| 611 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityPolicy) { |
| 612 | const int version = GetParam(); |
| 613 | const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 614 | const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
| 615 | |
| 616 | NetlinkResponse response{}; |
| 617 | response.hdr.nlmsg_type = XFRM_MSG_DELPOLICY; |
| 618 | Slice responseSlice = netdutils::makeSlice(response); |
| 619 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 620 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userpolicy_id)) + |
| 621 | NLMSG_ALIGN(sizeof(XfrmController::nlattr_xfrm_mark)); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 622 | |
| 623 | std::vector<uint8_t> nlMsgBuf; |
| 624 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
| 625 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
| 626 | EXPECT_CALL(mockSyscalls, read(_, _)) |
| 627 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
| 628 | |
| 629 | XfrmController ctrl; |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 630 | Status res = ctrl.ipSecDeleteSecurityPolicy( |
| 631 | 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, |
| 632 | TEST_XFRM_MARK, TEST_XFRM_MASK); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 633 | |
| 634 | EXPECT_TRUE(isOk(res)) << res; |
| 635 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
| 636 | |
| 637 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 638 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 639 | |
| 640 | xfrm_userpolicy_id policyid{}; |
| 641 | netdutils::extract(nlMsgSlice, policyid); |
| 642 | |
| 643 | EXPECT_EQ(static_cast<uint8_t>(XfrmDirection::OUT), policyid.dir); |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 644 | |
| 645 | // Drop the user policy id. |
| 646 | nlMsgSlice = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_userpolicy_id))); |
| 647 | // Extract and check the mark. |
| 648 | XfrmController::nlattr_xfrm_mark mark{}; |
| 649 | netdutils::extract(nlMsgSlice, mark); |
| 650 | EXPECT_EQ(TEST_XFRM_MARK, mark.mark.v); |
| 651 | EXPECT_EQ(TEST_XFRM_MASK, mark.mark.m); |
| 652 | |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 653 | } |
| 654 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 655 | } // namespace net |
| 656 | } // namespace android |