xref: /aosp_15_r20/system/netd/client/NetdClientRootTest.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
18*8542734aSAndroid Build Coastguard Worker #include <gtest/gtest.h>
19*8542734aSAndroid Build Coastguard Worker 
20*8542734aSAndroid Build Coastguard Worker #include "NetdClient.h"
21*8542734aSAndroid Build Coastguard Worker #include "android/net/INetd.h"
22*8542734aSAndroid Build Coastguard Worker #include "netid_client.h"
23*8542734aSAndroid Build Coastguard Worker #include "test_utils.h"
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker constexpr int TEST_UID1 = 99999;
26*8542734aSAndroid Build Coastguard Worker 
TEST(NetdClientTest,setSocketToInvalidNetwork)27*8542734aSAndroid Build Coastguard Worker TEST(NetdClientTest, setSocketToInvalidNetwork) {
28*8542734aSAndroid Build Coastguard Worker     const android::base::unique_fd s(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0));
29*8542734aSAndroid Build Coastguard Worker     ASSERT_LE(0, s);
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker     unsigned netId = NETID_UNSET;
32*8542734aSAndroid Build Coastguard Worker     const ScopedUidChange scopedUidChange(TEST_UID1);
33*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(-EACCES, setNetworkForSocket(android::net::INetd::LOCAL_NET_ID, s));
34*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(0, getNetworkForSocket(&netId, s));
35*8542734aSAndroid Build Coastguard Worker     EXPECT_EQ(static_cast<unsigned>(NETID_UNSET), netId);
36*8542734aSAndroid Build Coastguard Worker }
37