1 /*
2 * Copyright (C) 2022 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
17 #include <android-base/unique_fd.h>
18 #include <gtest/gtest.h>
19
20 #include "NetdClient.h"
21 #include "android/net/INetd.h"
22 #include "netid_client.h"
23 #include "test_utils.h"
24
25 constexpr int TEST_UID1 = 99999;
26
TEST(NetdClientTest,setSocketToInvalidNetwork)27 TEST(NetdClientTest, setSocketToInvalidNetwork) {
28 const android::base::unique_fd s(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0));
29 ASSERT_LE(0, s);
30
31 unsigned netId = NETID_UNSET;
32 const ScopedUidChange scopedUidChange(TEST_UID1);
33 EXPECT_EQ(-EACCES, setNetworkForSocket(android::net::INetd::LOCAL_NET_ID, s));
34 EXPECT_EQ(0, getNetworkForSocket(&netId, s));
35 EXPECT_EQ(static_cast<unsigned>(NETID_UNSET), netId);
36 }
37