aboutsummaryrefslogtreecommitdiff
path: root/tests/src/com/android/providers/contacts/ContactsMockPackageManager.java
blob: 62f17eab5723a8b39eff3e060dd9dda0cb167a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.providers.contacts;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Binder;
import android.os.UserHandle;
import android.test.mock.MockPackageManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Mock {@link PackageManager} that knows about a specific set of packages
 * to help test security models. Because {@link Binder#getCallingUid()}
 * can't be mocked, you'll have to find your mock-UID manually using your
 * {@link Context#getPackageName()}.
 */
public class ContactsMockPackageManager extends MockPackageManager {

    private final Context mRealContext;

    private final HashMap<Integer, String> mForward = new HashMap<Integer, String>();
    private final HashMap<String, Integer> mReverse = new HashMap<String, Integer>();
    private List<PackageInfo> mPackages;

    public ContactsMockPackageManager(Context realContext) {
        mRealContext = realContext;
    }

    /**
     * Add a UID-to-package mapping, which is then stored internally.
     */
    public void addPackage(int packageUid, String packageName) {
        mForward.put(packageUid, packageName);
        mReverse.put(packageName, packageUid);
    }

    public void removePackage(int packageUid) {
        final String packageName = mForward.remove(packageUid);
        if (packageName != null) {
            mReverse.remove(packageName);
        }
    }

    @Override
    public String getNameForUid(int uid) {
        return "name-for-uid";
    }

    @Override
    public String[] getPackagesForUid(int uid) {
        final String packageName = mForward.get(uid);
        if (packageName != null) {
            return new String[] {packageName};
        } else if (mPackages != null) {
            return new String[] { mPackages.get(0).packageName };
        } else {
            return new String[] {};
        }
    }

    @Override
    public ApplicationInfo getApplicationInfo(String packageName, int flags)
            throws NameNotFoundException {
        ApplicationInfo info = new ApplicationInfo();
        Integer uid = mReverse.get(packageName);
        if (uid == null) {
            throw new NameNotFoundException();
        }
        info.uid = (uid != null) ? uid : -1;
        info.flags = ApplicationInfo.FLAG_INSTALLED;
        return info;
    }

    public void setInstalledPackages(List<PackageInfo> packages) {
        this.mPackages = packages;
    }

    @Override
    public List<PackageInfo> getInstalledPackages(int flags) {
        return mPackages;
    }

    @Override
    public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
        for (PackageInfo info : mPackages) {
            if (info.packageName.equals(packageName)) {
                return info;
            }
        }
        throw new NameNotFoundException();
    }

    @Override
    public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
        return new ArrayList<ResolveInfo>();
    }

    @Override
    public Resources getResourcesForApplication(String appPackageName) {
        if (mRealContext.getPackageName().equals(appPackageName)) {
            return mRealContext.getResources();
        }
        return new ContactsMockResources();
    }

    @Override
    public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags,
            String metaDataKey) {
        final List<ProviderInfo> ret = new ArrayList<>();
        final List<PackageInfo> packages = getInstalledPackages(flags);
        if (packages == null) {
            return ret;
        }
        for (PackageInfo pkg : packages) {
            if (pkg.providers == null) {
                continue;
            }
            for (ProviderInfo proi : pkg.providers) {
                if (metaDataKey == null) {
                    ret.add(proi);
                } else {
                    if (proi.metaData != null && proi.metaData.containsKey(metaDataKey)) {
                        ret.add(proi);
                    }
                }
            }
        }
        return ret;
    }

    @Override
    public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, ResolveInfoFlags flags,
            UserHandle user) {
        return new ArrayList<>();
    }

    @Override
    public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
        return 123;
    }
}