blob: cb8bb0eab6c188a7b7453cde2a46e7e65f24a44e [file] [log] [blame]
ludi5c81c762017-05-19 13:47:53 -07001/*
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
57using android::base::unique_fd;
58using android::netdutils::Fd;
59using android::netdutils::MockSyscalls;
60using android::netdutils::Slice;
61using android::netdutils::Status;
62using android::netdutils::StatusOr;
63using android::netdutils::Syscalls;
64
Jonathan Basseri20a17c32017-09-14 17:42:40 -070065using ::testing::_;
ludi5c81c762017-05-19 13:47:53 -070066using ::testing::DoAll;
Jonathan Basserifc3b35f2017-09-14 15:58:16 -070067using ::testing::Invoke;
ludi5c81c762017-05-19 13:47:53 -070068using ::testing::Return;
69using ::testing::SaveArg;
70using ::testing::SetArgPointee;
Jonathan Basseri20a17c32017-09-14 17:42:40 -070071using ::testing::Values;
Jonathan Basserifc3b35f2017-09-14 15:58:16 -070072using ::testing::WithArg;
ludi5c81c762017-05-19 13:47:53 -070073
74/**
Jonathan Basseric6128662017-09-14 17:32:59 -070075 * 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.
ludi5c81c762017-05-19 13:47:53 -070077 */
Jonathan Basseric6128662017-09-14 17:32:59 -070078ACTION_TEMPLATE(SetArgSlice, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(value)) {
79 Slice orig = ::testing::get<N>(args);
ludi5c81c762017-05-19 13:47:53 -070080 android::netdutils::copy(orig, value);
81}
82
Jonathan Basseric6128662017-09-14 17:32:59 -070083/**
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 */
90ACTION_TEMPLATE(SaveFlattenedIovecs, HAS_1_TEMPLATE_PARAMS(int, N), AND_1_VALUE_PARAMS(resVec)) {
91 const std::vector<iovec>& iovs = ::testing::get<N>(args);
ludi5c81c762017-05-19 13:47:53 -070092
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
99namespace android {
100namespace net {
101
102static constexpr int DROID_SPI = 0xD1201D;
103static constexpr size_t KEY_LENGTH = 32;
104static constexpr int NLMSG_DEFAULTSIZE = 8192;
105
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700106static constexpr char LOCALHOST_V4[] = "127.0.0.1";
107static constexpr char LOCALHOST_V6[] = "::1";
108static constexpr char TEST_ADDR_V4[] = "8.8.8.8";
109static constexpr char TEST_ADDR_V6[] = "2001:4860:4860::8888";
110
ludi5c81c762017-05-19 13:47:53 -0700111struct Policy {
112 xfrm_userpolicy_info info;
113 xfrm_user_tmpl tmpl;
114};
115
116struct NetlinkResponse {
117 nlmsghdr hdr;
118 char buf[NLMSG_DEFAULTSIZE];
119};
120
Jonathan Basserid6c08582017-09-14 16:10:56 -0700121void 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
ludi5c81c762017-05-19 13:47:53 -0700129class XfrmControllerTest : public ::testing::Test {
130public:
131 MockSyscalls mockSyscalls;
132
133 void SetUp() override { netdutils::sSyscalls.swap(mockSyscalls); }
134};
135
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700136// Test class allowing IPv4/IPv6 parameterized tests.
137class XfrmControllerParameterizedTest : public XfrmControllerTest,
138 public ::testing::WithParamInterface<int> {};
ludi5c81c762017-05-19 13:47:53 -0700139
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700140// Helper to make generated test names readable.
141std::string FamilyName(::testing::TestParamInfo<int> info) {
142 return (info.param == AF_INET) ? "AF_INET" : "AF_INET6";
ludi5c81c762017-05-19 13:47:53 -0700143}
144
Benedict Wongb2daefb2017-12-06 22:05:46 -0800145/* Generate function to set value refered to by 3rd argument.
146 *
147 * This allows us to mock functions that pass in a pointer, expecting the result to be put into
148 * that location.
149 */
150ACTION_P(SetArg3IntValue, value) { *static_cast<int*>(arg3) = value; }
151
152TEST_F(XfrmControllerTest, TestFchown) {
153 XfrmController ctrl;
154 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
155
156 EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _))
157 .WillOnce(DoAll(SetArg3IntValue(UDP_ENCAP_ESPINUDP), Return(netdutils::status::ok)));
158
159 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid());
160 EXPECT_EQ(netdutils::status::ok, res);
161
162 struct stat info;
163 EXPECT_EQ(0, fstat(sockFd.get(), &info));
164 EXPECT_EQ(1001, (int)info.st_uid);
165}
166
167TEST_F(XfrmControllerTest, TestFchownInvalidFd) {
168 XfrmController ctrl;
169
170 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(unique_fd(), 1001, getuid());
171 EXPECT_EQ(netdutils::statusFromErrno(errno, "Failed to stat socket file descriptor"), res);
172}
173
174TEST_F(XfrmControllerTest, TestFchownIncorrectCallerUid) {
175 XfrmController ctrl;
176 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
177
178 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, 1001);
179 EXPECT_EQ(netdutils::statusFromErrno(EPERM, "fchown disabled for non-owner calls"), res);
180}
181
182TEST_F(XfrmControllerTest, TestFchownNonSocketFd) {
183 XfrmController ctrl;
184 unique_fd fd(open("/dev/null", 0));
185
186 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(fd, 1001, getuid());
187 EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "File descriptor was not a socket"), res);
188}
189
190TEST_F(XfrmControllerTest, TestFchownNonUdp) {
191 XfrmController ctrl;
192 unique_fd sockFd(socket(AF_INET, SOCK_STREAM, 0));
193
194 EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _))
195 .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok)));
196
197 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid());
198 EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket was not a UDP socket"), res);
199}
200
201TEST_F(XfrmControllerTest, TestFchownNonUdpEncap) {
202 XfrmController ctrl;
203 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
204
205 EXPECT_CALL(mockSyscalls, getsockopt(Fd(sockFd), IPPROTO_UDP, UDP_ENCAP, _, _))
206 .WillOnce(DoAll(SetArg3IntValue(0), Return(netdutils::status::ok)));
207
208 netdutils::Status res = ctrl.ipSecSetEncapSocketOwner(sockFd, 1001, getuid());
209 EXPECT_EQ(netdutils::statusFromErrno(EINVAL, "Socket did not have UDP-encap sockopt set"), res);
210}
211
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700212// The TEST_P cases below will run with each of the following value parameters.
213INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(AF_INET, AF_INET6),
214 FamilyName);
215
216TEST_P(XfrmControllerParameterizedTest, TestIpSecAllocateSpi) {
217 const int family = GetParam();
218 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
219 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
ludi5c81c762017-05-19 13:47:53 -0700220
Jonathan Basseri643d9632017-09-14 18:02:31 -0700221 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700222 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700223 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700224
Jonathan Basseri643d9632017-09-14 18:02:31 -0700225 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info));
ludi5c81c762017-05-19 13:47:53 -0700226
227 // A vector to hold the flattened netlink message for nlMsgSlice
228 std::vector<uint8_t> nlMsgBuf;
229 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700230 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700231 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700232 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700233
Jonathan Basseri643d9632017-09-14 18:02:31 -0700234 XfrmController ctrl;
235 int outSpi = 0;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700236 Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, static_cast<int>(XfrmDirection::OUT),
237 localAddr, remoteAddr, DROID_SPI, &outSpi);
ludi5c81c762017-05-19 13:47:53 -0700238
Jonathan Basserid6c08582017-09-14 16:10:56 -0700239 EXPECT_TRUE(isOk(res)) << res;
ludi5c81c762017-05-19 13:47:53 -0700240 EXPECT_EQ(DROID_SPI, outSpi);
Jonathan Basseri643d9632017-09-14 18:02:31 -0700241 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700242
243 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
244 nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN);
245
246 xfrm_userspi_info userspi{};
247 netdutils::extract(nlMsgSlice, userspi);
248
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700249 EXPECT_EQ(family, userspi.info.sel.family);
250 expectAddressEquals(family, localAddr, userspi.info.saddr);
251 expectAddressEquals(family, remoteAddr, userspi.info.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700252
253 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min));
254 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max));
255}
256
manojboopathi4d2d6f12017-12-06 11:11:31 -0800257void testIpSecAddSecurityAssociation(int family, const MockSyscalls& mockSyscalls,
258 const XfrmMode& mode) {
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700259 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
260 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
ludi5c81c762017-05-19 13:47:53 -0700261
Jonathan Basseri643d9632017-09-14 18:02:31 -0700262 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700263 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700264 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700265
266 std::vector<uint8_t> authKey(KEY_LENGTH, 0);
267 std::vector<uint8_t> cryptKey(KEY_LENGTH, 1);
268
269 // Calculate the length of the expected netlink message.
Jonathan Basseri643d9632017-09-14 18:02:31 -0700270 size_t expectedMsgLength =
Jonathan Basserid6c08582017-09-14 16:10:56 -0700271 NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) +
272 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) +
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700273 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH);
ludi5c81c762017-05-19 13:47:53 -0700274
275 std::vector<uint8_t> nlMsgBuf;
276 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700277 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700278 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700279 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700280
Jonathan Basseri643d9632017-09-14 18:02:31 -0700281 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700282 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800283 1 /* resourceId */, static_cast<int>(mode),
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700284 static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, 0 /* underlying network */,
285 DROID_SPI, "hmac(sha256)" /* auth algo */, authKey, 128 /* auth trunc length */,
286 "cbc(aes)" /* encryption algo */, cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */,
287 {}, 0, static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */);
ludi5c81c762017-05-19 13:47:53 -0700288
Jonathan Basserid6c08582017-09-14 16:10:56 -0700289 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700290 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700291
292 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
293 nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN);
294
295 xfrm_usersa_info usersa{};
296 netdutils::extract(nlMsgSlice, usersa);
297
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700298 EXPECT_EQ(family, usersa.family);
ludi5c81c762017-05-19 13:47:53 -0700299 EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid));
manojboopathi4d2d6f12017-12-06 11:11:31 -0800300 EXPECT_EQ(static_cast<int>(mode), usersa.mode);
301
302 if (mode == XfrmMode::TUNNEL) {
303 EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags);
304 } else {
305 EXPECT_EQ(0, usersa.flags);
306 }
307
ludi5c81c762017-05-19 13:47:53 -0700308 EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi);
309 EXPECT_EQ(IPPROTO_ESP, usersa.id.proto);
310
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700311 expectAddressEquals(family, localAddr, usersa.saddr);
312 expectAddressEquals(family, remoteAddr, usersa.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700313
Jonathan Basseri643d9632017-09-14 18:02:31 -0700314 // Extract and check the encryption/authentication algorithms.
ludi5c81c762017-05-19 13:47:53 -0700315 Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info)));
316
317 // Extract and check the encryption/authentication algorithm
318 XfrmController::nlattr_algo_crypt encryptAlgo{};
319 XfrmController::nlattr_algo_auth authAlgo{};
320 auto attrHandler = [&encryptAlgo, &authAlgo](const nlattr& attr, const Slice& attr_payload) {
321 Slice buf = attr_payload;
322 if (attr.nla_type == XFRMA_ALG_CRYPT) {
323 encryptAlgo.hdr = attr;
324 netdutils::extract(buf, encryptAlgo.crypt);
325 buf = drop(buf, sizeof(xfrm_algo));
326 netdutils::extract(buf, encryptAlgo.key);
327 } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) {
328 authAlgo.hdr = attr;
329 netdutils::extract(buf, authAlgo.auth);
330 buf = drop(buf, sizeof(xfrm_algo_auth));
331 netdutils::extract(buf, authAlgo.key);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700332 } else {
333 FAIL() << "Unexpected nlattr type: " << attr.nla_type;
ludi5c81c762017-05-19 13:47:53 -0700334 }
335 };
336 forEachNetlinkAttribute(attr_buf, attrHandler);
337
Jonathan Basserid6c08582017-09-14 16:10:56 -0700338 // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages.
ludi5c81c762017-05-19 13:47:53 -0700339 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()),
340 reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH));
341 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()),
342 reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH));
343}
344
manojboopathi4d2d6f12017-12-06 11:11:31 -0800345TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) {
346 const int family = GetParam();
347 testIpSecAddSecurityAssociation(family, mockSyscalls, XfrmMode::TRANSPORT);
348}
349
350TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) {
351 const int family = GetParam();
352 testIpSecAddSecurityAssociation(family, mockSyscalls, XfrmMode::TUNNEL);
353}
354
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700355TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) {
356 // TODO: Implement this test, which is nearly identical to
357 // TestIpSecAddSecurityAssociation.
358}
ludi5c81c762017-05-19 13:47:53 -0700359
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700360// Test that input validation rejects IPv6 UDP encap.
361TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) {
362 EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0);
363
364 XfrmController ctrl;
365 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800366 1, static_cast<int>(XfrmMode::TRANSPORT), static_cast<int>(XfrmDirection::OUT),
367 LOCALHOST_V6, TEST_ADDR_V6, 0, DROID_SPI, "hmac(sha256)", {}, 128, "cbc(aes)",
368 {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700369
370 EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected";
371}
372
373TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) {
374 const int family = GetParam();
375 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
376 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700377
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700378 size_t optlen = 0;
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700379 Policy policy{};
380 // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a
381 // lambda than to write a gMock action.
382 auto SavePolicy = [&policy](const void* value) {
383 policy = *reinterpret_cast<const Policy*>(value);
384 };
385
ludi5c81c762017-05-19 13:47:53 -0700386 struct sockaddr socketaddr;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700387 socketaddr.sa_family = family;
ludi5c81c762017-05-19 13:47:53 -0700388
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700389 unique_fd sock(socket(family, SOCK_STREAM, 0));
ludi5c81c762017-05-19 13:47:53 -0700390
391 EXPECT_CALL(mockSyscalls, getsockname(_, _, _))
392 .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok)));
393
394 EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _))
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700395 .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen),
396 Return(netdutils::status::ok)));
ludi5c81c762017-05-19 13:47:53 -0700397
Jonathan Basseri643d9632017-09-14 18:02:31 -0700398 XfrmController ctrl;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700399 Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */,
400 static_cast<int>(XfrmDirection::OUT),
401 localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700402
Jonathan Basserid6c08582017-09-14 16:10:56 -0700403 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700404 EXPECT_EQ(sizeof(Policy), optlen);
ludi5c81c762017-05-19 13:47:53 -0700405
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700406 EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid));
407 EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi);
ludi5c81c762017-05-19 13:47:53 -0700408
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700409 expectAddressEquals(family, localAddr, policy.tmpl.saddr);
410 expectAddressEquals(family, remoteAddr, policy.tmpl.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700411}
412
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700413TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) {
414 const int family = GetParam();
415 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
416 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700417
418 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700419 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700420 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700421
Jonathan Basseri643d9632017-09-14 18:02:31 -0700422 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id));
ludi5c81c762017-05-19 13:47:53 -0700423
424 std::vector<uint8_t> nlMsgBuf;
425 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700426 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700427 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700428 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700429
Jonathan Basseri643d9632017-09-14 18:02:31 -0700430 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700431 Status res = ctrl.ipSecDeleteSecurityAssociation(
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700432 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700433
Jonathan Basserid6c08582017-09-14 16:10:56 -0700434 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700435 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700436
437 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
438 nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN);
439
440 xfrm_usersa_id said{};
441 netdutils::extract(nlMsgSlice, said);
442
443 EXPECT_EQ(htonl(DROID_SPI), said.spi);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700444 expectAddressEquals(family, remoteAddr, said.daddr);
ludi5c81c762017-05-19 13:47:53 -0700445}
446
447} // namespace net
448} // namespace android