blob: ec44e6f32a5cd2778331e0534cca70c6d136d91f [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
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700145// The TEST_P cases below will run with each of the following value parameters.
146INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(AF_INET, AF_INET6),
147 FamilyName);
148
149TEST_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;
ludi5c81c762017-05-19 13:47:53 -0700153
Jonathan Basseri643d9632017-09-14 18:02:31 -0700154 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700155 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700156 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700157
Jonathan Basseri643d9632017-09-14 18:02:31 -0700158 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info));
ludi5c81c762017-05-19 13:47:53 -0700159
160 // A vector to hold the flattened netlink message for nlMsgSlice
161 std::vector<uint8_t> nlMsgBuf;
162 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700163 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700164 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700165 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700166
Jonathan Basseri643d9632017-09-14 18:02:31 -0700167 XfrmController ctrl;
168 int outSpi = 0;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700169 Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, static_cast<int>(XfrmDirection::OUT),
170 localAddr, remoteAddr, DROID_SPI, &outSpi);
ludi5c81c762017-05-19 13:47:53 -0700171
Jonathan Basserid6c08582017-09-14 16:10:56 -0700172 EXPECT_TRUE(isOk(res)) << res;
ludi5c81c762017-05-19 13:47:53 -0700173 EXPECT_EQ(DROID_SPI, outSpi);
Jonathan Basseri643d9632017-09-14 18:02:31 -0700174 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700175
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 Basseri20a17c32017-09-14 17:42:40 -0700182 EXPECT_EQ(family, userspi.info.sel.family);
183 expectAddressEquals(family, localAddr, userspi.info.saddr);
184 expectAddressEquals(family, remoteAddr, userspi.info.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700185
186 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min));
187 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max));
188}
189
manojboopathi4d2d6f12017-12-06 11:11:31 -0800190void testIpSecAddSecurityAssociation(int family, const MockSyscalls& mockSyscalls,
191 const XfrmMode& mode) {
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700192 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;
ludi5c81c762017-05-19 13:47:53 -0700194
Jonathan Basseri643d9632017-09-14 18:02:31 -0700195 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700196 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700197 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700198
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 Basseri643d9632017-09-14 18:02:31 -0700203 size_t expectedMsgLength =
Jonathan Basserid6c08582017-09-14 16:10:56 -0700204 NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) +
205 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) +
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700206 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH);
ludi5c81c762017-05-19 13:47:53 -0700207
208 std::vector<uint8_t> nlMsgBuf;
209 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700210 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700211 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700212 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700213
Jonathan Basseri643d9632017-09-14 18:02:31 -0700214 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700215 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800216 1 /* resourceId */, static_cast<int>(mode),
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700217 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 */);
ludi5c81c762017-05-19 13:47:53 -0700221
Jonathan Basserid6c08582017-09-14 16:10:56 -0700222 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700223 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700224
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 Basseri20a17c32017-09-14 17:42:40 -0700231 EXPECT_EQ(family, usersa.family);
ludi5c81c762017-05-19 13:47:53 -0700232 EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid));
manojboopathi4d2d6f12017-12-06 11:11:31 -0800233 EXPECT_EQ(static_cast<int>(mode), usersa.mode);
234
235 if (mode == XfrmMode::TUNNEL) {
236 EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags);
237 } else {
238 EXPECT_EQ(0, usersa.flags);
239 }
240
ludi5c81c762017-05-19 13:47:53 -0700241 EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi);
242 EXPECT_EQ(IPPROTO_ESP, usersa.id.proto);
243
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700244 expectAddressEquals(family, localAddr, usersa.saddr);
245 expectAddressEquals(family, remoteAddr, usersa.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700246
Jonathan Basseri643d9632017-09-14 18:02:31 -0700247 // Extract and check the encryption/authentication algorithms.
ludi5c81c762017-05-19 13:47:53 -0700248 Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info)));
249
250 // Extract and check the encryption/authentication algorithm
251 XfrmController::nlattr_algo_crypt encryptAlgo{};
252 XfrmController::nlattr_algo_auth authAlgo{};
253 auto attrHandler = [&encryptAlgo, &authAlgo](const nlattr& attr, const Slice& attr_payload) {
254 Slice buf = attr_payload;
255 if (attr.nla_type == XFRMA_ALG_CRYPT) {
256 encryptAlgo.hdr = attr;
257 netdutils::extract(buf, encryptAlgo.crypt);
258 buf = drop(buf, sizeof(xfrm_algo));
259 netdutils::extract(buf, encryptAlgo.key);
260 } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) {
261 authAlgo.hdr = attr;
262 netdutils::extract(buf, authAlgo.auth);
263 buf = drop(buf, sizeof(xfrm_algo_auth));
264 netdutils::extract(buf, authAlgo.key);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700265 } else {
266 FAIL() << "Unexpected nlattr type: " << attr.nla_type;
ludi5c81c762017-05-19 13:47:53 -0700267 }
268 };
269 forEachNetlinkAttribute(attr_buf, attrHandler);
270
Jonathan Basserid6c08582017-09-14 16:10:56 -0700271 // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages.
ludi5c81c762017-05-19 13:47:53 -0700272 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()),
273 reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH));
274 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()),
275 reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH));
276}
277
manojboopathi4d2d6f12017-12-06 11:11:31 -0800278TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) {
279 const int family = GetParam();
280 testIpSecAddSecurityAssociation(family, mockSyscalls, XfrmMode::TRANSPORT);
281}
282
283TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) {
284 const int family = GetParam();
285 testIpSecAddSecurityAssociation(family, mockSyscalls, XfrmMode::TUNNEL);
286}
287
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700288TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) {
289 // TODO: Implement this test, which is nearly identical to
290 // TestIpSecAddSecurityAssociation.
291}
ludi5c81c762017-05-19 13:47:53 -0700292
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700293// Test that input validation rejects IPv6 UDP encap.
294TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) {
295 EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0);
296
297 XfrmController ctrl;
298 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800299 1, static_cast<int>(XfrmMode::TRANSPORT), static_cast<int>(XfrmDirection::OUT),
300 LOCALHOST_V6, TEST_ADDR_V6, 0, DROID_SPI, "hmac(sha256)", {}, 128, "cbc(aes)",
301 {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700302
303 EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected";
304}
305
306TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) {
307 const int family = GetParam();
308 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
309 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700310
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700311 size_t optlen = 0;
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700312 Policy policy{};
313 // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a
314 // lambda than to write a gMock action.
315 auto SavePolicy = [&policy](const void* value) {
316 policy = *reinterpret_cast<const Policy*>(value);
317 };
318
ludi5c81c762017-05-19 13:47:53 -0700319 struct sockaddr socketaddr;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700320 socketaddr.sa_family = family;
ludi5c81c762017-05-19 13:47:53 -0700321
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700322 unique_fd sock(socket(family, SOCK_STREAM, 0));
ludi5c81c762017-05-19 13:47:53 -0700323
324 EXPECT_CALL(mockSyscalls, getsockname(_, _, _))
325 .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok)));
326
327 EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _))
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700328 .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen),
329 Return(netdutils::status::ok)));
ludi5c81c762017-05-19 13:47:53 -0700330
Jonathan Basseri643d9632017-09-14 18:02:31 -0700331 XfrmController ctrl;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700332 Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */,
333 static_cast<int>(XfrmDirection::OUT),
334 localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700335
Jonathan Basserid6c08582017-09-14 16:10:56 -0700336 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700337 EXPECT_EQ(sizeof(Policy), optlen);
ludi5c81c762017-05-19 13:47:53 -0700338
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700339 EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid));
340 EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi);
ludi5c81c762017-05-19 13:47:53 -0700341
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700342 expectAddressEquals(family, localAddr, policy.tmpl.saddr);
343 expectAddressEquals(family, remoteAddr, policy.tmpl.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700344}
345
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700346TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) {
347 const int family = GetParam();
348 const std::string localAddr = (family == AF_INET6) ? LOCALHOST_V6 : LOCALHOST_V4;
349 const std::string remoteAddr = (family == AF_INET6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700350
351 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700352 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700353 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700354
Jonathan Basseri643d9632017-09-14 18:02:31 -0700355 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id));
ludi5c81c762017-05-19 13:47:53 -0700356
357 std::vector<uint8_t> nlMsgBuf;
358 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700359 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700360 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700361 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700362
Jonathan Basseri643d9632017-09-14 18:02:31 -0700363 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700364 Status res = ctrl.ipSecDeleteSecurityAssociation(
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700365 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700366
Jonathan Basserid6c08582017-09-14 16:10:56 -0700367 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700368 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700369
370 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
371 nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN);
372
373 xfrm_usersa_id said{};
374 netdutils::extract(nlMsgSlice, said);
375
376 EXPECT_EQ(htonl(DROID_SPI), said.spi);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700377 expectAddressEquals(family, remoteAddr, said.daddr);
ludi5c81c762017-05-19 13:47:53 -0700378}
379
380} // namespace net
381} // namespace android