Atneya Nair | 9f91a5e | 2024-05-09 16:25:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2024 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 <media/NativePermissionController.h> |
| 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include <android-base/expected.h> |
| 23 | |
| 24 | using ::android::base::unexpected; |
| 25 | using ::android::binder::Status; |
| 26 | using com::android::media::permission::NativePermissionController; |
| 27 | using com::android::media::permission::UidPackageState; |
| 28 | |
| 29 | class NativePermissionControllerTest : public ::testing::Test { |
| 30 | protected: |
| 31 | android::sp<NativePermissionController> holder_ = |
| 32 | android::sp<NativePermissionController>::make(); |
| 33 | NativePermissionController& controller_ = *holder_; |
| 34 | }; |
| 35 | static UidPackageState createState(uid_t uid, std::vector<std::string> packagesNames) { |
| 36 | UidPackageState out{}; |
| 37 | out.uid = uid; |
| 38 | out.packageNames = std::move(packagesNames); |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | static std::vector<std::string> makeVector(const char* one) { |
| 43 | return {one}; |
| 44 | } |
| 45 | |
| 46 | static std::vector<std::string> makeVector(const char* one, const char* two) { |
| 47 | return {one, two}; |
| 48 | } |
| 49 | |
| 50 | #define UNWRAP_EQ(expr, desired_expr) \ |
| 51 | do { \ |
| 52 | auto tmp_ = (expr); \ |
| 53 | EXPECT_TRUE(tmp_.has_value()); \ |
| 54 | if (tmp_.has_value()) EXPECT_EQ(*tmp_, desired_expr); \ |
| 55 | } while (0) |
| 56 | |
| 57 | // --- Tests for non-populated ---- |
| 58 | TEST_F(NativePermissionControllerTest, getPackagesForUid_NotPopulated) { |
| 59 | // Verify errors are returned |
| 60 | EXPECT_EQ(controller_.getPackagesForUid(10000), unexpected{android::NO_INIT}); |
| 61 | EXPECT_EQ(controller_.getPackagesForUid(10001), unexpected{android::NO_INIT}); |
| 62 | |
| 63 | // fixed uids should work |
| 64 | UNWRAP_EQ(controller_.getPackagesForUid(1000), makeVector("system")); |
| 65 | } |
| 66 | |
| 67 | TEST_F(NativePermissionControllerTest, validateUidPackagePair_NotPopulated) { |
| 68 | // Verify errors are returned |
| 69 | EXPECT_EQ(controller_.validateUidPackagePair(10000, "com.package"), |
| 70 | unexpected{android::NO_INIT}); |
| 71 | |
| 72 | // fixed uids should work |
| 73 | UNWRAP_EQ(controller_.validateUidPackagePair(1000, "system"), true); |
| 74 | } |
| 75 | // --- Tests for populatePackagesForUids ---- |
| 76 | TEST_F(NativePermissionControllerTest, populatePackages_EmptyInput) { |
| 77 | std::vector<UidPackageState> input; |
| 78 | |
| 79 | // succeeds |
| 80 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 81 | |
| 82 | // Verify unknown uid behavior |
| 83 | const auto res1 = controller_.getPackagesForUid(10000); |
| 84 | ASSERT_FALSE(res1.has_value()); |
| 85 | EXPECT_EQ(res1.error(), ::android::BAD_VALUE); |
| 86 | } |
| 87 | |
| 88 | TEST_F(NativePermissionControllerTest, populatePackages_ValidInput) { |
| 89 | std::vector<UidPackageState> input{ |
| 90 | createState(10000, {"com.example.app1", "com.example.app2"}), |
| 91 | createState(10001, {"com.example2.app1"}), |
| 92 | }; |
| 93 | |
| 94 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 95 | |
| 96 | UNWRAP_EQ(controller_.getPackagesForUid(10000), |
| 97 | makeVector("com.example.app1", "com.example.app2")); |
| 98 | UNWRAP_EQ(controller_.getPackagesForUid(10001), makeVector("com.example2.app1")); |
| 99 | } |
| 100 | |
| 101 | // --- Tests for updatePackagesForUid --- |
| 102 | TEST_F(NativePermissionControllerTest, updatePackages_NewUid) { |
| 103 | std::vector<UidPackageState> input{ |
| 104 | createState(10000, {"com.example.app1", "com.example.app2"}), |
| 105 | createState(10001, {"com.example2.app1"}), |
| 106 | }; |
| 107 | UidPackageState newState = createState(12000, {"com.example.other"}); |
| 108 | |
| 109 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 110 | EXPECT_TRUE(controller_.updatePackagesForUid(newState).isOk()); |
| 111 | |
| 112 | // Verify the results: only the updated package should be changed |
| 113 | UNWRAP_EQ(controller_.getPackagesForUid(10000), |
| 114 | makeVector("com.example.app1", "com.example.app2")); |
| 115 | UNWRAP_EQ(controller_.getPackagesForUid(10001), makeVector("com.example2.app1")); |
| 116 | UNWRAP_EQ(controller_.getPackagesForUid(12000), makeVector("com.example.other")); |
| 117 | } |
| 118 | |
| 119 | TEST_F(NativePermissionControllerTest, updatePackages_ExistingUid) { |
| 120 | std::vector<UidPackageState> input{ |
| 121 | createState(10000, {"com.example.app1", "com.example.app2", "com.example.app3"}), |
| 122 | createState(10001, {"com.example2.app1"}), |
| 123 | }; |
| 124 | |
| 125 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 126 | // Update packages for existing uid |
| 127 | UidPackageState newState = createState(10000, {"com.example.other", "com.example.new"}); |
| 128 | EXPECT_TRUE(controller_.updatePackagesForUid(newState).isOk()); |
| 129 | |
| 130 | // Verify update |
| 131 | UNWRAP_EQ(controller_.getPackagesForUid(10000), |
| 132 | makeVector("com.example.other", "com.example.new")); |
| 133 | } |
| 134 | |
| 135 | TEST_F(NativePermissionControllerTest, updatePackages_EmptyRemovesEntry) { |
| 136 | std::vector<UidPackageState> input{ |
| 137 | createState(10000, {"com.example.app1"}), |
| 138 | }; |
| 139 | |
| 140 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 141 | |
| 142 | UidPackageState newState{}; // Empty package list |
| 143 | newState.uid = 10000; |
| 144 | EXPECT_TRUE(controller_.updatePackagesForUid(newState).isOk()); |
| 145 | // getPackages for unknown UID should error out |
| 146 | const auto res = controller_.getPackagesForUid(10000); |
| 147 | ASSERT_FALSE(res.has_value()); |
| 148 | EXPECT_EQ(res.error(), ::android::BAD_VALUE); |
| 149 | } |
| 150 | |
| 151 | TEST_F(NativePermissionControllerTest, validateUidPackagePair_ValidPair) { |
| 152 | std::vector<UidPackageState> input{ |
| 153 | createState(10000, {"com.example.app1", "com.example.app2"}), |
| 154 | }; |
| 155 | |
| 156 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 157 | |
| 158 | UNWRAP_EQ(controller_.validateUidPackagePair(10000, "com.example.app1"), true); |
| 159 | } |
| 160 | |
| 161 | TEST_F(NativePermissionControllerTest, validateUidPackagePair_InvalidPackage) { |
| 162 | std::vector<UidPackageState> input{ |
| 163 | createState(10000, {"com.example.app1", "com.example.app2"}), |
| 164 | }; |
| 165 | |
| 166 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 167 | |
| 168 | UNWRAP_EQ(controller_.validateUidPackagePair(10000, "com.example.other"), false); |
| 169 | } |
| 170 | |
| 171 | TEST_F(NativePermissionControllerTest, validateUidPackagePair_UnknownUid) { |
| 172 | std::vector<UidPackageState> input{ |
| 173 | createState(10000, {"com.example.app1", "com.example.app2"}), |
| 174 | }; |
| 175 | |
| 176 | EXPECT_TRUE(controller_.populatePackagesForUids(input).isOk()); |
| 177 | |
| 178 | UNWRAP_EQ(controller_.validateUidPackagePair(12000, "any.package"), false); |
| 179 | } |