blob: a588cc5e5ed3930c9c25074203b0306c5a6c1569 [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) {
manojboopathi26f42922017-12-14 10:51:57 -0800142 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);
ludi5c81c762017-05-19 13:47:53 -0700148}
149
Benedict Wongb2daefb2017-12-06 22:05:46 -0800150/* 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 */
155ACTION_P(SetArg3IntValue, value) { *static_cast<int*>(arg3) = value; }
156
157TEST_F(XfrmControllerTest, TestFchown) {
158 XfrmController ctrl;
159 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
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
172TEST_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
179TEST_F(XfrmControllerTest, TestFchownIncorrectCallerUid) {
180 XfrmController ctrl;
181 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
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
187TEST_F(XfrmControllerTest, TestFchownNonSocketFd) {
188 XfrmController ctrl;
189 unique_fd fd(open("/dev/null", 0));
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
195TEST_F(XfrmControllerTest, TestFchownNonUdp) {
196 XfrmController ctrl;
197 unique_fd sockFd(socket(AF_INET, SOCK_STREAM, 0));
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
206TEST_F(XfrmControllerTest, TestFchownNonUdpEncap) {
207 XfrmController ctrl;
208 unique_fd sockFd(socket(AF_INET, SOCK_DGRAM, 0));
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 Basseri20a17c32017-09-14 17:42:40 -0700217// The TEST_P cases below will run with each of the following value parameters.
manojboopathi26f42922017-12-14 10:51:57 -0800218INSTANTIATE_TEST_CASE_P(ByFamily, XfrmControllerParameterizedTest, Values(4, 5, 6),
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700219 FamilyName);
220
221TEST_P(XfrmControllerParameterizedTest, TestIpSecAllocateSpi) {
manojboopathi26f42922017-12-14 10:51:57 -0800222 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;
ludi5c81c762017-05-19 13:47:53 -0700226
Jonathan Basseri643d9632017-09-14 18:02:31 -0700227 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700228 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700229 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700230
Jonathan Basseri643d9632017-09-14 18:02:31 -0700231 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_userspi_info));
ludi5c81c762017-05-19 13:47:53 -0700232
233 // A vector to hold the flattened netlink message for nlMsgSlice
234 std::vector<uint8_t> nlMsgBuf;
235 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700236 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700237 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700238 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700239
Jonathan Basseri643d9632017-09-14 18:02:31 -0700240 XfrmController ctrl;
241 int outSpi = 0;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700242 Status res = ctrl.ipSecAllocateSpi(1 /* resourceId */, static_cast<int>(XfrmDirection::OUT),
243 localAddr, remoteAddr, DROID_SPI, &outSpi);
ludi5c81c762017-05-19 13:47:53 -0700244
Jonathan Basserid6c08582017-09-14 16:10:56 -0700245 EXPECT_TRUE(isOk(res)) << res;
ludi5c81c762017-05-19 13:47:53 -0700246 EXPECT_EQ(DROID_SPI, outSpi);
Jonathan Basseri643d9632017-09-14 18:02:31 -0700247 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700248
249 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
250 nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN);
251
252 xfrm_userspi_info userspi{};
253 netdutils::extract(nlMsgSlice, userspi);
254
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700255 EXPECT_EQ(family, userspi.info.sel.family);
256 expectAddressEquals(family, localAddr, userspi.info.saddr);
257 expectAddressEquals(family, remoteAddr, userspi.info.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700258
259 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.min));
260 EXPECT_EQ(DROID_SPI, static_cast<int>(userspi.max));
261}
262
manojboopathi26f42922017-12-14 10:51:57 -0800263void testIpSecAddSecurityAssociation(int version, const MockSyscalls& mockSyscalls,
manojboopathi4d2d6f12017-12-06 11:11:31 -0800264 const XfrmMode& mode) {
manojboopathi26f42922017-12-14 10:51:57 -0800265 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;
ludi5c81c762017-05-19 13:47:53 -0700268
Jonathan Basseri643d9632017-09-14 18:02:31 -0700269 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700270 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700271 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700272
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 Basseri643d9632017-09-14 18:02:31 -0700277 size_t expectedMsgLength =
Jonathan Basserid6c08582017-09-14 16:10:56 -0700278 NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_info)) +
279 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_crypt, key) + KEY_LENGTH) +
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700280 NLA_ALIGN(offsetof(XfrmController::nlattr_algo_auth, key) + KEY_LENGTH);
ludi5c81c762017-05-19 13:47:53 -0700281
282 std::vector<uint8_t> nlMsgBuf;
283 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700284 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700285 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700286 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700287
Jonathan Basseri643d9632017-09-14 18:02:31 -0700288 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700289 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800290 1 /* resourceId */, static_cast<int>(mode),
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700291 static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, 0 /* underlying network */,
292 DROID_SPI, "hmac(sha256)" /* auth algo */, authKey, 128 /* auth trunc length */,
293 "cbc(aes)" /* encryption algo */, cryptKey, 0 /* crypt trunc length? */, "" /* AEAD algo */,
294 {}, 0, static_cast<int>(XfrmEncapType::NONE), 0 /* local port */, 0 /* remote port */);
ludi5c81c762017-05-19 13:47:53 -0700295
Jonathan Basserid6c08582017-09-14 16:10:56 -0700296 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700297 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700298
299 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
300 nlMsgSlice = drop(nlMsgSlice, NLMSG_HDRLEN);
301
302 xfrm_usersa_info usersa{};
303 netdutils::extract(nlMsgSlice, usersa);
304
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700305 EXPECT_EQ(family, usersa.family);
ludi5c81c762017-05-19 13:47:53 -0700306 EXPECT_EQ(1 /* Transform Id*/, static_cast<int>(usersa.reqid));
manojboopathi4d2d6f12017-12-06 11:11:31 -0800307 EXPECT_EQ(static_cast<int>(mode), usersa.mode);
308
309 if (mode == XfrmMode::TUNNEL) {
310 EXPECT_EQ(XFRM_STATE_AF_UNSPEC, usersa.flags);
311 } else {
312 EXPECT_EQ(0, usersa.flags);
313 }
314
ludi5c81c762017-05-19 13:47:53 -0700315 EXPECT_EQ(htonl(DROID_SPI), usersa.id.spi);
316 EXPECT_EQ(IPPROTO_ESP, usersa.id.proto);
317
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700318 expectAddressEquals(family, localAddr, usersa.saddr);
319 expectAddressEquals(family, remoteAddr, usersa.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700320
Jonathan Basseri643d9632017-09-14 18:02:31 -0700321 // Extract and check the encryption/authentication algorithms.
ludi5c81c762017-05-19 13:47:53 -0700322 Slice attr_buf = drop(nlMsgSlice, NLA_ALIGN(sizeof(xfrm_usersa_info)));
323
324 // Extract and check the encryption/authentication algorithm
325 XfrmController::nlattr_algo_crypt encryptAlgo{};
326 XfrmController::nlattr_algo_auth authAlgo{};
327 auto attrHandler = [&encryptAlgo, &authAlgo](const nlattr& attr, const Slice& attr_payload) {
328 Slice buf = attr_payload;
329 if (attr.nla_type == XFRMA_ALG_CRYPT) {
330 encryptAlgo.hdr = attr;
331 netdutils::extract(buf, encryptAlgo.crypt);
332 buf = drop(buf, sizeof(xfrm_algo));
333 netdutils::extract(buf, encryptAlgo.key);
334 } else if (attr.nla_type == XFRMA_ALG_AUTH_TRUNC) {
335 authAlgo.hdr = attr;
336 netdutils::extract(buf, authAlgo.auth);
337 buf = drop(buf, sizeof(xfrm_algo_auth));
338 netdutils::extract(buf, authAlgo.key);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700339 } else {
340 FAIL() << "Unexpected nlattr type: " << attr.nla_type;
ludi5c81c762017-05-19 13:47:53 -0700341 }
342 };
343 forEachNetlinkAttribute(attr_buf, attrHandler);
344
Jonathan Basserid6c08582017-09-14 16:10:56 -0700345 // TODO: Use ContainerEq or ElementsAreArray to get better test failure messages.
ludi5c81c762017-05-19 13:47:53 -0700346 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(cryptKey.data()),
347 reinterpret_cast<void*>(&encryptAlgo.key), KEY_LENGTH));
348 EXPECT_EQ(0, memcmp(reinterpret_cast<void*>(authKey.data()),
349 reinterpret_cast<void*>(&authAlgo.key), KEY_LENGTH));
350}
351
manojboopathi4d2d6f12017-12-06 11:11:31 -0800352TEST_P(XfrmControllerParameterizedTest, TestTransportModeIpSecAddSecurityAssociation) {
manojboopathi26f42922017-12-14 10:51:57 -0800353 const int version = GetParam();
354 testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TRANSPORT);
manojboopathi4d2d6f12017-12-06 11:11:31 -0800355}
356
357TEST_P(XfrmControllerParameterizedTest, TestTunnelModeIpSecAddSecurityAssociation) {
manojboopathi26f42922017-12-14 10:51:57 -0800358 const int version = GetParam();
359 testIpSecAddSecurityAssociation(version, mockSyscalls, XfrmMode::TUNNEL);
manojboopathi4d2d6f12017-12-06 11:11:31 -0800360}
361
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700362TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv4Encap) {
363 // TODO: Implement this test, which is nearly identical to
364 // TestIpSecAddSecurityAssociation.
365}
ludi5c81c762017-05-19 13:47:53 -0700366
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700367// Test that input validation rejects IPv6 UDP encap.
368TEST_F(XfrmControllerTest, TestIpSecAddSecurityAssociationIPv6Encap) {
369 EXPECT_CALL(mockSyscalls, writev(_, _)).Times(0);
370
371 XfrmController ctrl;
372 Status res = ctrl.ipSecAddSecurityAssociation(
manojboopathi4d2d6f12017-12-06 11:11:31 -0800373 1, static_cast<int>(XfrmMode::TRANSPORT), static_cast<int>(XfrmDirection::OUT),
374 LOCALHOST_V6, TEST_ADDR_V6, 0, DROID_SPI, "hmac(sha256)", {}, 128, "cbc(aes)",
375 {}, 0, "", {}, 0, static_cast<int>(XfrmEncapType::ESPINUDP_NON_IKE), 0, 0);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700376
377 EXPECT_FALSE(isOk(res)) << "IPv6 UDP encap not rejected";
378}
379
manojboopathi26f42922017-12-14 10:51:57 -0800380TEST_F(XfrmControllerTest, TestIpSecApplyTransportModeTransformChecksFamily) {
381 struct sockaddr socketaddr;
382 socketaddr.sa_family = AF_INET;
383
384 unique_fd sock(socket(AF_INET, SOCK_STREAM, 0));
385
386 EXPECT_CALL(mockSyscalls, getsockname(Fd(sock), _, _))
387 .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok)));
388
389 XfrmController ctrl;
390 Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */,
391 static_cast<int>(XfrmDirection::OUT),
392 LOCALHOST_V6, TEST_ADDR_V6, DROID_SPI);
393
394 EXPECT_EQ(res.code(), EINVAL);
395}
396
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700397TEST_P(XfrmControllerParameterizedTest, TestIpSecApplyTransportModeTransform) {
manojboopathi26f42922017-12-14 10:51:57 -0800398 const int version = GetParam();
399 const int sockFamily = (version == 4) ? AF_INET : AF_INET6;
400 const int xfrmFamily = (version == 6) ? AF_INET6: AF_INET;
401 const std::string localAddr = (version == 6) ? LOCALHOST_V6 : LOCALHOST_V4;
402 const std::string remoteAddr = (version == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700403
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700404 size_t optlen = 0;
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700405 Policy policy{};
406 // Need to cast from void* in order to "SaveArg" policy. Easier to invoke a
407 // lambda than to write a gMock action.
408 auto SavePolicy = [&policy](const void* value) {
409 policy = *reinterpret_cast<const Policy*>(value);
410 };
411
ludi5c81c762017-05-19 13:47:53 -0700412 struct sockaddr socketaddr;
manojboopathi26f42922017-12-14 10:51:57 -0800413 socketaddr.sa_family = sockFamily;
ludi5c81c762017-05-19 13:47:53 -0700414
manojboopathi26f42922017-12-14 10:51:57 -0800415 unique_fd sock(socket(sockFamily, SOCK_STREAM, 0));
ludi5c81c762017-05-19 13:47:53 -0700416
417 EXPECT_CALL(mockSyscalls, getsockname(_, _, _))
418 .WillOnce(DoAll(SetArgPointee<1>(socketaddr), Return(netdutils::status::ok)));
419
420 EXPECT_CALL(mockSyscalls, setsockopt(_, _, _, _, _))
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700421 .WillOnce(DoAll(WithArg<3>(Invoke(SavePolicy)), SaveArg<4>(&optlen),
422 Return(netdutils::status::ok)));
ludi5c81c762017-05-19 13:47:53 -0700423
Jonathan Basseri643d9632017-09-14 18:02:31 -0700424 XfrmController ctrl;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700425 Status res = ctrl.ipSecApplyTransportModeTransform(sock, 1 /* resourceId */,
426 static_cast<int>(XfrmDirection::OUT),
427 localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700428
Jonathan Basserid6c08582017-09-14 16:10:56 -0700429 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700430 EXPECT_EQ(sizeof(Policy), optlen);
ludi5c81c762017-05-19 13:47:53 -0700431
Jonathan Basserifc3b35f2017-09-14 15:58:16 -0700432 EXPECT_EQ(1 /* resourceId */, static_cast<int>(policy.tmpl.reqid));
433 EXPECT_EQ(htonl(DROID_SPI), policy.tmpl.id.spi);
ludi5c81c762017-05-19 13:47:53 -0700434
manojboopathi26f42922017-12-14 10:51:57 -0800435 expectAddressEquals(xfrmFamily, localAddr, policy.tmpl.saddr);
436 expectAddressEquals(xfrmFamily, remoteAddr, policy.tmpl.id.daddr);
ludi5c81c762017-05-19 13:47:53 -0700437}
438
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700439TEST_P(XfrmControllerParameterizedTest, TestIpSecDeleteSecurityAssociation) {
manojboopathi26f42922017-12-14 10:51:57 -0800440 const int family = (GetParam() == 6) ? AF_INET6 : AF_INET;
441 const std::string localAddr = (family == 6) ? LOCALHOST_V6 : LOCALHOST_V4;
442 const std::string remoteAddr = (family == 6) ? TEST_ADDR_V6 : TEST_ADDR_V4;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700443
444 NetlinkResponse response{};
ludi5c81c762017-05-19 13:47:53 -0700445 response.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700446 Slice responseSlice = netdutils::makeSlice(response);
ludi5c81c762017-05-19 13:47:53 -0700447
Jonathan Basseri643d9632017-09-14 18:02:31 -0700448 size_t expectedMsgLength = NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(xfrm_usersa_id));
ludi5c81c762017-05-19 13:47:53 -0700449
450 std::vector<uint8_t> nlMsgBuf;
451 EXPECT_CALL(mockSyscalls, writev(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700452 .WillOnce(DoAll(SaveFlattenedIovecs<1>(&nlMsgBuf), Return(expectedMsgLength)));
ludi5c81c762017-05-19 13:47:53 -0700453 EXPECT_CALL(mockSyscalls, read(_, _))
Jonathan Basseri643d9632017-09-14 18:02:31 -0700454 .WillOnce(DoAll(SetArgSlice<1>(responseSlice), Return(responseSlice)));
ludi5c81c762017-05-19 13:47:53 -0700455
Jonathan Basseri643d9632017-09-14 18:02:31 -0700456 XfrmController ctrl;
ludi5c81c762017-05-19 13:47:53 -0700457 Status res = ctrl.ipSecDeleteSecurityAssociation(
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700458 1 /* resourceId */, static_cast<int>(XfrmDirection::OUT), localAddr, remoteAddr, DROID_SPI);
ludi5c81c762017-05-19 13:47:53 -0700459
Jonathan Basserid6c08582017-09-14 16:10:56 -0700460 EXPECT_TRUE(isOk(res)) << res;
Jonathan Basseri643d9632017-09-14 18:02:31 -0700461 EXPECT_EQ(expectedMsgLength, nlMsgBuf.size());
ludi5c81c762017-05-19 13:47:53 -0700462
463 Slice nlMsgSlice = netdutils::makeSlice(nlMsgBuf);
464 nlMsgSlice = netdutils::drop(nlMsgSlice, NLMSG_HDRLEN);
465
466 xfrm_usersa_id said{};
467 netdutils::extract(nlMsgSlice, said);
468
469 EXPECT_EQ(htonl(DROID_SPI), said.spi);
Jonathan Basseri20a17c32017-09-14 17:42:40 -0700470 expectAddressEquals(family, remoteAddr, said.daddr);
ludi5c81c762017-05-19 13:47:53 -0700471}
472
473} // namespace net
474} // namespace android