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