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