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 #define LOG_TAG "PackageManagerProxyTest"
18
19 #include <aidl/google/sdv/packagemanagerproxy/IPackageManagerProxy.h>
20 #include <android/binder_manager.h>
21 #include <log/log.h>
22
23 #include <iostream>
24
25 namespace {
26
27 using ::aidl::google::sdv::packagemanagerproxy::IPackageManagerProxy;
28
29 } // namespace
30
main(int argc,char ** argv)31 int main(int argc, char** argv) {
32 ALOGI("PackageManagerProxy Test Client started.");
33
34 std::string packageName;
35
36 if (argc == 2) {
37 packageName = argv[1];
38 } else {
39 std::cerr << "Usage: proxymanagerproxyd_testclient <package name>" << std::endl;
40 return 1;
41 }
42
43 auto myService =
44 IPackageManagerProxy::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(
45 "google.sdv.packagemanagerproxy.IPackageManagerProxy/default")));
46
47 std::cout << "Fetching package info for \"" << packageName << "\"" << std::endl;
48
49 // Uid
50 int32_t uid = 0;
51 auto result = myService->getPackageUid(packageName, /*flags=*/0, /*userId=*/0, &uid);
52 if (!result.isOk()) {
53 ALOGE("getPackageUid failed: (%d, %d), %s", result.getExceptionCode(),
54 result.getServiceSpecificError(), result.getMessage());
55 return 1;
56 }
57
58 std::cout << "Uid: " << uid << std::endl;
59
60 // Version code
61 int64_t versionCode = 0;
62 result = myService->getVersionCodeForPackage(packageName, &versionCode);
63
64 if (!result.isOk()) {
65 ALOGE("getVersionCodeForPackage failed: (%d, %d), %s", result.getExceptionCode(),
66 result.getServiceSpecificError(), result.getMessage());
67 return 1;
68 }
69
70 std::cout << "Version Code: " << versionCode << std::endl;
71
72 // Validate getting package name by uid
73 std::vector<int32_t> uids = {uid};
74 std::vector<std::string> fetchedPackageNames;
75 result = myService->getNamesForUids(uids, &fetchedPackageNames);
76
77 if (!result.isOk()) {
78 ALOGE("getNamesForUids failed: (%d, %d), %s", result.getExceptionCode(),
79 result.getServiceSpecificError(), result.getMessage());
80 return 1;
81 }
82
83 if (fetchedPackageNames.size() != 1) {
84 ALOGE("Expected 1 returned package name, actually received %zu",
85 fetchedPackageNames.size());
86 return 1;
87 }
88
89 if (fetchedPackageNames[0] != packageName) {
90 ALOGE("Package names do not match. Received: \"%s\", expected: \"%s\"",
91 fetchedPackageNames[0].c_str(), packageName.c_str());
92 return 1;
93 }
94
95 std::cout << "Fetched package name from Uid: " << fetchedPackageNames[0] << std::endl;
96
97 return 0;
98 }
99