blob: b160e8e08618b3e41126261302ad786dd461ea57 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Kongstad02751232018-04-27 13:16:32 +020017#include <string>
18
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070019#include "TestHelpers.h"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010020#include "android-base/stringprintf.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070021#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020022#include "idmap2/FileUtils.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070023#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010025namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020026
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010027#ifdef __ANDROID__
28TEST(FileUtilsTests, UidHasWriteAccessToPath) {
29 constexpr const char* tmp_path = "/data/local/tmp/test@idmap";
Yurii Zubrytskyia29f3c02023-05-12 16:05:13 -070030 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 Kongstad1da49dc2019-01-14 10:03:53 +010033
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 Kongstad0eba72a2018-11-29 08:23:14 +010049} // namespace android::idmap2::utils