1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker *
4*d57664e9SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker *
8*d57664e9SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker *
10*d57664e9SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker */
16*d57664e9SAndroid Build Coastguard Worker
17*d57664e9SAndroid Build Coastguard Worker #include <string>
18*d57664e9SAndroid Build Coastguard Worker
19*d57664e9SAndroid Build Coastguard Worker #include "TestHelpers.h"
20*d57664e9SAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
21*d57664e9SAndroid Build Coastguard Worker #include "gtest/gtest.h"
22*d57664e9SAndroid Build Coastguard Worker #include "idmap2/FileUtils.h"
23*d57664e9SAndroid Build Coastguard Worker #include "private/android_filesystem_config.h"
24*d57664e9SAndroid Build Coastguard Worker
25*d57664e9SAndroid Build Coastguard Worker namespace android::idmap2::utils {
26*d57664e9SAndroid Build Coastguard Worker
27*d57664e9SAndroid Build Coastguard Worker #ifdef __ANDROID__
TEST(FileUtilsTests,UidHasWriteAccessToPath)28*d57664e9SAndroid Build Coastguard Worker TEST(FileUtilsTests, UidHasWriteAccessToPath) {
29*d57664e9SAndroid Build Coastguard Worker constexpr const char* tmp_path = "/data/local/tmp/test@idmap";
30*d57664e9SAndroid Build Coastguard Worker const std::string cache_path(base::StringPrintf("%s/test@idmap", kIdmapCacheDir.data()));
31*d57664e9SAndroid Build Coastguard Worker const std::string sneaky_cache_path(
32*d57664e9SAndroid Build Coastguard Worker base::StringPrintf("/data/../%s/test@idmap", kIdmapCacheDir.data()));
33*d57664e9SAndroid Build Coastguard Worker
34*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, tmp_path));
35*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, cache_path));
36*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, sneaky_cache_path));
37*d57664e9SAndroid Build Coastguard Worker
38*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, tmp_path));
39*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, cache_path));
40*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, sneaky_cache_path));
41*d57664e9SAndroid Build Coastguard Worker
42*d57664e9SAndroid Build Coastguard Worker constexpr const uid_t AID_SOME_APP = AID_SYSTEM + 1;
43*d57664e9SAndroid Build Coastguard Worker ASSERT_TRUE(UidHasWriteAccessToPath(AID_SOME_APP, tmp_path));
44*d57664e9SAndroid Build Coastguard Worker ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, cache_path));
45*d57664e9SAndroid Build Coastguard Worker ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, sneaky_cache_path));
46*d57664e9SAndroid Build Coastguard Worker }
47*d57664e9SAndroid Build Coastguard Worker #endif
48*d57664e9SAndroid Build Coastguard Worker
49*d57664e9SAndroid Build Coastguard Worker } // namespace android::idmap2::utils
50