Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 19 | #include "TestHelpers.h" |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 22 | #include "idmap2/FileUtils.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 23 | #include "private/android_filesystem_config.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 24 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 25 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 26 | |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 27 | #ifdef __ANDROID__ |
| 28 | TEST(FileUtilsTests, UidHasWriteAccessToPath) { |
| 29 | constexpr const char* tmp_path = "/data/local/tmp/test@idmap"; |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame] | 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())); |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 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 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 49 | } // namespace android::idmap2::utils |