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; |
| 105 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 106 | static constexpr char LOCALHOST_V4[] = "127.0.0.1"; |
| 107 | static constexpr char LOCALHOST_V6[] = "::1"; |
| 108 | static constexpr char TEST_ADDR_V4[] = "8.8.8.8"; |
| 109 | static constexpr char TEST_ADDR_V6[] = "2001:4860:4860::8888"; |
| 110 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 111 | struct Policy { |
| 112 | xfrm_userpolicy_info info; |
| 113 | xfrm_user_tmpl tmpl; |
| 114 | }; |
| 115 | |
| 116 | struct NetlinkResponse { |
| 117 | nlmsghdr hdr; |
| 118 | char buf[NLMSG_DEFAULTSIZE]; |
| 119 | }; |
| 120 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 121 | void expectAddressEquals(int family, const std::string& expected, const xfrm_address_t& actual) { |
| 122 | char actualStr[INET6_ADDRSTRLEN]; |
| 123 | const char* ret = |
| 124 | inet_ntop(family, reinterpret_cast<const void*>(&actual), actualStr, INET6_ADDRSTRLEN); |
| 125 | EXPECT_NE(nullptr, ret) << "Unable to convert xfrm_address_t to string"; |
| 126 | EXPECT_EQ(expected, actualStr); |
| 127 | } |
| 128 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 129 | class XfrmControllerTest : public ::testing::Test { |
| 130 | public: |
| 131 | MockSyscalls mockSyscalls; |
| 132 | |
| 133 | void SetUp() override { netdutils::sSyscalls.swap(mockSyscalls); } |
| 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) { |
| 142 | return (info.param == AF_INET) ? "AF_INET" : "AF_INET6"; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 145 | // The TEST_P cases below will run with each of the following value parameters. |
| 146 | INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(AF_INET, AF_INET6), |
| 147 | FamilyName); |
| 148 | |
| 149 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAllocateSpi) { |
| 150 | const int family = GetParam(); |
| 151 | const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 152 | const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 153 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 154 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 155 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 156 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 157 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 158 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 159 | |
| 160 | // A vector to hold the flattened netlink message for nlMsgSlice |
| 161 | std::vector<uint8_t> nlMsgBuf; |
| 162 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 163 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 164 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 165 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 166 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 167 | XfrmController ctrl; |
| 168 | int outSpi = 0; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 169 | Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), |
| 170 | localAddr, remoteAddr, DROID_SPI, &outSpi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 171 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 172 | EXPECT_TRUE(isOk(res)) << res; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 173 | EXPECT_EQ(DROID_SPI, outSpi); |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 174 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 175 | |
| 176 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 177 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 178 | |
| 179 | xfrm_userspi_info userspi{}; |
| 180 | netdutils::extract(nlMsgSlice, userspi); |
| 181 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 182 | EXPECT_EQ(family, userspi.info.sel.family); |
| 183 | expectAddressEquals(family, localAddr, userspi.info.saddr); |
| 184 | expectAddressEquals(family, remoteAddr, userspi.info.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 185 | |
| 186 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min)); |
| 187 | EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max)); |
| 188 | } |
| 189 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 190 | TEST_P(XfrmControllerParameterizedTest, TestIpSecAddSecurityAssociation) { |
| 191 | const int family = GetParam(); |
| 192 | const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 193 | const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 194 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 195 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 196 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 197 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 198 | |
| 199 | std::vector<uint8_t> authKey(KEY_LENGTH, 0); |
| 200 | std::vector<uint8_t> cryptKey(KEY_LENGTH, 1); |
| 201 | |
| 202 | // Calculate the length of the expected netlink message. |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 203 | size_t expectedMsgLength = |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 204 | NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) + |
| 205 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) + |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 206 | NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 207 | |
| 208 | std::vector<uint8_t> nlMsgBuf; |
| 209 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 210 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 211 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 212 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 213 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 214 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 215 | Status res = ctrl.ipSecAddSecurityAssociation( |
| 216 | 1 /* resourceId */, static_cast<int>(XfrmMode::TUNNEL), |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 217 | static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, 0 /* underlying network */, |
| 218 | DROID_SPI, "hmac(sha256)" /* auth algo */, authKey, 128 /* auth trunc length */, |
| 219 | "cbc(aes)" /* encryption algo */, cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */, |
| 220 | {}, 0, static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 221 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 222 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 223 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 224 | |
| 225 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 226 | nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN); |
| 227 | |
| 228 | xfrm_usersa_info usersa{}; |
| 229 | netdutils::extract(nlMsgSlice, usersa); |
| 230 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 231 | EXPECT_EQ(family, usersa.family); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 232 | EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid)); |
| 233 | EXPECT_EQ(XFRM_MODE_TUNNEL, usersa.mode); |
| 234 | EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi); |
| 235 | EXPECT_EQ(IPPROTO_ESP, usersa.id.proto); |
| 236 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 237 | expectAddressEquals(family, localAddr, usersa.saddr); |
| 238 | expectAddressEquals(family, remoteAddr, usersa.id.daddr); |
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 | // Extract and check the encryption/authentication algorithms. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 241 | Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info))); |
| 242 | |
| 243 | // Extract and check the encryption/authentication algorithm |
| 244 | XfrmController::nlattr_algo_crypt encryptAlgo{}; |
| 245 | XfrmController::nlattr_algo_auth authAlgo{}; |
| 246 | auto attrHandler = [&encryptAlgo, &authAlgo](const nlattr& attr, const Slice& attr_payload) { |
| 247 | Slice buf = attr_payload; |
| 248 | if (attr.nla_type == XFRMA_ALG_CRYPT) { |
| 249 | encryptAlgo.hdr = attr; |
| 250 | netdutils::extract(buf, encryptAlgo.crypt); |
| 251 | buf = drop(buf, sizeof(xfrm_algo)); |
| 252 | netdutils::extract(buf, encryptAlgo.key); |
| 253 | } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) { |
| 254 | authAlgo.hdr = attr; |
| 255 | netdutils::extract(buf, authAlgo.auth); |
| 256 | buf = drop(buf, sizeof(xfrm_algo_auth)); |
| 257 | netdutils::extract(buf, authAlgo.key); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 258 | } else { |
| 259 | FAIL() << "Unexpected nlattr type: " << attr.nla_type; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 260 | } |
| 261 | }; |
| 262 | forEachNetlinkAttribute(attr_buf, attrHandler); |
| 263 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 264 | // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages. |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 265 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()), |
| 266 | reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH)); |
| 267 | EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()), |
| 268 | reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH)); |
| 269 | } |
| 270 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 271 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) { |
| 272 | // TODO: Implement this test, which is nearly identical to |
| 273 | // TestIpSecAddSecurityAssociation. |
| 274 | } |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 275 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 276 | // Test that input validation rejects IPv6 UDP encap. |
| 277 | TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) { |
| 278 | EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0); |
| 279 | |
| 280 | XfrmController ctrl; |
| 281 | Status res = ctrl.ipSecAddSecurityAssociation( |
| 282 | 1, static_cast<int>(XfrmMode::TUNNEL), static_cast<int>(XfrmDirection::OUT), LOCALHOST_V6, |
| 283 | TEST_ADDR_V6, 0, DROID_SPI, "hmac(sha256)", {}, 128, "cbc(aes)", {}, 0, "", {}, 0, |
| 284 | static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0); |
| 285 | |
| 286 | EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected"; |
| 287 | } |
| 288 | |
| 289 | TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) { |
| 290 | const int family = GetParam(); |
| 291 | const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 292 | const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 293 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 294 | size_t optlen = 0; |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 295 | Policy policy{}; |
| 296 | // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a |
| 297 | // lambda than to write a gMock action. |
| 298 | auto SavePolicy = [&policy](const void* value) { |
| 299 | policy = *reinterpret_cast<const Policy*>(value); |
| 300 | }; |
| 301 | |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 302 | struct sockaddr socketaddr; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 303 | socketaddr.sa_family = family; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 304 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 305 | unique_fd sock(socket(family, SOCK_STREAM, 0)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 306 | |
| 307 | EXPECT_CALL(mockSyscalls, getsockname(_, _, _)) |
| 308 | .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok))); |
| 309 | |
| 310 | EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _)) |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 311 | .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen), |
| 312 | Return(netdutils::status::ok))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 313 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 314 | XfrmController ctrl; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 315 | Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */, |
| 316 | static_cast<int>(XfrmDirection::OUT), |
| 317 | localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 318 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 319 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 320 | EXPECT_EQ(sizeof(Policy), optlen); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 321 | |
Jonathan Basseri | fc3b35f | 2017-09-14 15:58:16 -0700 | [diff] [blame] | 322 | EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid)); |
| 323 | EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 324 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 325 | expectAddressEquals(family, localAddr, policy.tmpl.saddr); |
| 326 | expectAddressEquals(family, remoteAddr, policy.tmpl.id.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 329 | TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) { |
| 330 | const int family = GetParam(); |
| 331 | const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4; |
| 332 | const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 333 | |
| 334 | NetlinkResponse response{}; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 335 | response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 336 | Slice responseSlice = netdutils::makeSlice(response); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 337 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 338 | size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id)); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 339 | |
| 340 | std::vector<uint8_t> nlMsgBuf; |
| 341 | EXPECT_CALL(mockSyscalls, writev(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 342 | .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 343 | EXPECT_CALL(mockSyscalls, read(_, _)) |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 344 | .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice))); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 345 | |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 346 | XfrmController ctrl; |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 347 | Status res = ctrl.ipSecDeleteSecurityAssociation( |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 348 | 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, DROID_SPI); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 349 | |
Jonathan Basseri | d6c0858 | 2017-09-14 16:10:56 -0700 | [diff] [blame] | 350 | EXPECT_TRUE(isOk(res)) << res; |
Jonathan Basseri | 643d963 | 2017-09-14 18:02:31 -0700 | [diff] [blame] | 351 | EXPECT_EQ(expectedMsgLength, nlMsgBuf.size()); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 352 | |
| 353 | Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf); |
| 354 | nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN); |
| 355 | |
| 356 | xfrm_usersa_id said{}; |
| 357 | netdutils::extract(nlMsgSlice, said); |
| 358 | |
| 359 | EXPECT_EQ(htonl(DROID_SPI), said.spi); |
Jonathan Basseri | 20a17c3 | 2017-09-14 17:42:40 -0700 | [diff] [blame] | 360 | expectAddressEquals(family, remoteAddr, said.daddr); |
ludi | 5c81c76 | 2017-05-19 13:47:53 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | } // namespace net |
| 364 | } // namespace android |