1 /*
2  * Copyright (C) 2018 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 <string>
18 
19 #include "TestHelpers.h"
20 #include "android-base/stringprintf.h"
21 #include "gtest/gtest.h"
22 #include "idmap2/FileUtils.h"
23 #include "private/android_filesystem_config.h"
24 
25 namespace android::idmap2::utils {
26 
27 #ifdef __ANDROID__
TEST(FileUtilsTests,UidHasWriteAccessToPath)28 TEST(FileUtilsTests, UidHasWriteAccessToPath) {
29   constexpr const char* tmp_path = "/data/local/tmp/test@idmap";
30   const std::string cache_path(base::StringPrintf("%s/test@idmap", kIdmapCacheDir.data()));
31   const std::string sneaky_cache_path(
32       base::StringPrintf("/data/../%s/test@idmap", kIdmapCacheDir.data()));
33 
34   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, tmp_path));
35   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, cache_path));
36   ASSERT_TRUE(UidHasWriteAccessToPath(AID_ROOT, sneaky_cache_path));
37 
38   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, tmp_path));
39   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, cache_path));
40   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SYSTEM, sneaky_cache_path));
41 
42   constexpr const uid_t AID_SOME_APP = AID_SYSTEM + 1;
43   ASSERT_TRUE(UidHasWriteAccessToPath(AID_SOME_APP, tmp_path));
44   ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, cache_path));
45   ASSERT_FALSE(UidHasWriteAccessToPath(AID_SOME_APP, sneaky_cache_path));
46 }
47 #endif
48 
49 }  // namespace android::idmap2::utils
50