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/ValidatedAttributionSourceState.h>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <android-base/expected.h>
23 #include <error/ExpectedMatchers.h>
24 #include <media/IPermissionProvider.h>
25 #include "error/BinderStatusMatcher.h"
26 
27 using ::android::base::unexpected;
28 using ::android::binder::Status;
29 using ::android::binder::Status::EX_ILLEGAL_ARGUMENT;
30 using ::android::binder::Status::EX_ILLEGAL_STATE;
31 using ::android::binder::Status::EX_SECURITY;
32 using ::android::content::AttributionSourceState;
33 using ::android::error::BinderResult;
34 using ::android::error::BinderStatusMatcher;
35 using ::android::error::IsErrorAnd;
36 using ::android::error::IsOkAnd;
37 using ::com::android::media::permission::IPermissionProvider;
38 using ::com::android::media::permission::PermissionEnum;
39 using ::com::android::media::permission::ValidatedAttributionSourceState;
40 
41 using ::testing::Eq;
42 using ::testing::Return;
43 
44 class MockPermissionProvider : public IPermissionProvider {
45   public:
46     MOCK_METHOD(BinderResult<std::vector<std::string>>, getPackagesForUid, (uid_t uid),
47                 (override, const));
48     MOCK_METHOD(BinderResult<bool>, validateUidPackagePair, (uid_t uid, const std::string&),
49                 (override, const));
50     MOCK_METHOD(BinderResult<bool>, checkPermission, (PermissionEnum perm, uid_t),
51                 (override, const));
52 };
53 
54 class ValidatedAttributionSourceStateTest : public ::testing::Test {
55   protected:
56     MockPermissionProvider mMockProvider;
57     const uid_t mUid = 10001;
58     const std::vector<std::string> mPackageList{"com.package1", "com.package2"};
59 };
60 
TEST_F(ValidatedAttributionSourceStateTest,providedPackageValid)61 TEST_F(ValidatedAttributionSourceStateTest, providedPackageValid) {
62     const std::string package = "com.package1";
63     EXPECT_CALL(mMockProvider, validateUidPackagePair(mUid, package)).WillOnce(Return(true));
64     AttributionSourceState attr;
65     attr.uid = mUid;
66     attr.packageName = package;
67     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
68                 IsOkAnd(Eq(attr)));
69 }
70 
TEST_F(ValidatedAttributionSourceStateTest,providedPackageInvalid)71 TEST_F(ValidatedAttributionSourceStateTest, providedPackageInvalid) {
72     const std::string package = "com.package.spoof";
73     EXPECT_CALL(mMockProvider, validateUidPackagePair(mUid, package)).WillOnce(Return(false));
74     AttributionSourceState attr;
75     attr.uid = mUid;
76     attr.packageName = package;
77     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
78                 IsErrorAnd(BinderStatusMatcher::hasException(EX_SECURITY)));
79 }
80 
TEST_F(ValidatedAttributionSourceStateTest,packageLookup_whenMissingPackage)81 TEST_F(ValidatedAttributionSourceStateTest, packageLookup_whenMissingPackage) {
82     EXPECT_CALL(mMockProvider, getPackagesForUid(mUid)).WillOnce(Return(mPackageList));
83     AttributionSourceState attr;
84     attr.uid = mUid;
85     AttributionSourceState expectedAttr;
86     expectedAttr.uid = mUid;
87     expectedAttr.packageName = "com.package1";
88     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
89                 IsOkAnd(Eq(expectedAttr)));
90 }
91 
TEST_F(ValidatedAttributionSourceStateTest,packageLookup_whenEmptyPackage)92 TEST_F(ValidatedAttributionSourceStateTest, packageLookup_whenEmptyPackage) {
93     EXPECT_CALL(mMockProvider, getPackagesForUid(mUid)).WillOnce(Return(mPackageList));
94     AttributionSourceState attr;
95     attr.uid = mUid;
96     attr.packageName = std::string{};
97     AttributionSourceState expectedAttr;
98     expectedAttr.uid = mUid;
99     expectedAttr.packageName = "com.package1";
100     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
101                 IsOkAnd(Eq(expectedAttr)));
102 }
103 
TEST_F(ValidatedAttributionSourceStateTest,controllerNotInitialized)104 TEST_F(ValidatedAttributionSourceStateTest, controllerNotInitialized) {
105     EXPECT_CALL(mMockProvider, getPackagesForUid(mUid))
106             .WillOnce(Return(unexpected{Status::fromExceptionCode(EX_ILLEGAL_STATE)}));
107     AttributionSourceState attr;
108     attr.uid = mUid;
109     attr.packageName = std::string{};
110     AttributionSourceState expectedAttr;
111     expectedAttr.uid = mUid;
112     expectedAttr.packageName = "com.package1";
113     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
114                 IsErrorAnd(BinderStatusMatcher::hasException(EX_ILLEGAL_STATE)));
115 }
116 
TEST_F(ValidatedAttributionSourceStateTest,uidNotFound)117 TEST_F(ValidatedAttributionSourceStateTest, uidNotFound) {
118     EXPECT_CALL(mMockProvider, getPackagesForUid(mUid))
119             .WillOnce(Return(unexpected{Status::fromExceptionCode(EX_ILLEGAL_ARGUMENT)}));
120     AttributionSourceState attr;
121     attr.uid = mUid;
122     attr.packageName = std::string{};
123     EXPECT_THAT(ValidatedAttributionSourceState::createFromTrustedUidNoPackage(attr, mMockProvider),
124                 IsErrorAnd(BinderStatusMatcher::hasException(EX_ILLEGAL_ARGUMENT)));
125 }
126