xref: /aosp_15_r20/test/vts/vts_hal_hidl_target/VtsHalHidlTargetTestEnvBase.cpp (revision 9a74111979c139a065a9a7e4d45972320c5732c7)
1*9a741119SAndroid Build Coastguard Worker /*
2*9a741119SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*9a741119SAndroid Build Coastguard Worker  *
4*9a741119SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*9a741119SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*9a741119SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*9a741119SAndroid Build Coastguard Worker  *
8*9a741119SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*9a741119SAndroid Build Coastguard Worker  *
10*9a741119SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*9a741119SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*9a741119SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9a741119SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*9a741119SAndroid Build Coastguard Worker  * limitations under the License.
15*9a741119SAndroid Build Coastguard Worker  */
16*9a741119SAndroid Build Coastguard Worker 
17*9a741119SAndroid Build Coastguard Worker #define LOG_TAG "VtsHalHidlTargetTestEnvBase"
18*9a741119SAndroid Build Coastguard Worker 
19*9a741119SAndroid Build Coastguard Worker #include "VtsHalHidlTargetTestEnvBase.h"
20*9a741119SAndroid Build Coastguard Worker 
21*9a741119SAndroid Build Coastguard Worker #include <iostream>
22*9a741119SAndroid Build Coastguard Worker #include <string>
23*9a741119SAndroid Build Coastguard Worker 
24*9a741119SAndroid Build Coastguard Worker #include <hidl-util/FqInstance.h>
25*9a741119SAndroid Build Coastguard Worker 
26*9a741119SAndroid Build Coastguard Worker static const std::string kListFlag = "--list_registered_services";
27*9a741119SAndroid Build Coastguard Worker static const std::string kServiceInstanceFlag = "--hal_service_instance=";
28*9a741119SAndroid Build Coastguard Worker 
29*9a741119SAndroid Build Coastguard Worker using namespace std;
30*9a741119SAndroid Build Coastguard Worker 
31*9a741119SAndroid Build Coastguard Worker namespace testing {
32*9a741119SAndroid Build Coastguard Worker 
SetUp()33*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::SetUp() {
34*9a741119SAndroid Build Coastguard Worker   if (!inited_) {
35*9a741119SAndroid Build Coastguard Worker     cerr << "Environment not inited, did you forget to call init()?" << endl;
36*9a741119SAndroid Build Coastguard Worker     exit(-1);
37*9a741119SAndroid Build Coastguard Worker   }
38*9a741119SAndroid Build Coastguard Worker   // Register services used in the test.
39*9a741119SAndroid Build Coastguard Worker   registerTestServices();
40*9a741119SAndroid Build Coastguard Worker   // For a no-op run which just print the registered hal services.
41*9a741119SAndroid Build Coastguard Worker   if (listService_) {
42*9a741119SAndroid Build Coastguard Worker     listRegisteredServices();
43*9a741119SAndroid Build Coastguard Worker     exit(0);
44*9a741119SAndroid Build Coastguard Worker   }
45*9a741119SAndroid Build Coastguard Worker   // Call the customized setup process.
46*9a741119SAndroid Build Coastguard Worker   HidlSetUp();
47*9a741119SAndroid Build Coastguard Worker }
48*9a741119SAndroid Build Coastguard Worker 
TearDown()49*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::TearDown() {
50*9a741119SAndroid Build Coastguard Worker   // Call the customized teardown process.
51*9a741119SAndroid Build Coastguard Worker   HidlTearDown();
52*9a741119SAndroid Build Coastguard Worker }
53*9a741119SAndroid Build Coastguard Worker 
init(int * argc,char ** argv)54*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::init(int* argc, char** argv) {
55*9a741119SAndroid Build Coastguard Worker   if (inited_) return;
56*9a741119SAndroid Build Coastguard Worker   for (int i = 1; i < *argc; i++) {
57*9a741119SAndroid Build Coastguard Worker     if (parseVtsTestOption(argv[i])) {
58*9a741119SAndroid Build Coastguard Worker       // Shift the remainder of the argv list left by one.
59*9a741119SAndroid Build Coastguard Worker       for (int j = i; j != *argc; j++) {
60*9a741119SAndroid Build Coastguard Worker         argv[j] = argv[j + 1];
61*9a741119SAndroid Build Coastguard Worker       }
62*9a741119SAndroid Build Coastguard Worker 
63*9a741119SAndroid Build Coastguard Worker       // Decrements the argument count.
64*9a741119SAndroid Build Coastguard Worker       (*argc)--;
65*9a741119SAndroid Build Coastguard Worker 
66*9a741119SAndroid Build Coastguard Worker       // We also need to decrement the iterator as we just removed an element.
67*9a741119SAndroid Build Coastguard Worker       i--;
68*9a741119SAndroid Build Coastguard Worker     }
69*9a741119SAndroid Build Coastguard Worker   }
70*9a741119SAndroid Build Coastguard Worker   inited_ = true;
71*9a741119SAndroid Build Coastguard Worker }
72*9a741119SAndroid Build Coastguard Worker 
parseVtsTestOption(const char * arg)73*9a741119SAndroid Build Coastguard Worker bool VtsHalHidlTargetTestEnvBase::parseVtsTestOption(const char* arg) {
74*9a741119SAndroid Build Coastguard Worker   // arg must not be NULL.
75*9a741119SAndroid Build Coastguard Worker   if (arg == NULL) return false;
76*9a741119SAndroid Build Coastguard Worker 
77*9a741119SAndroid Build Coastguard Worker   if (arg == kListFlag) {
78*9a741119SAndroid Build Coastguard Worker     listService_ = true;
79*9a741119SAndroid Build Coastguard Worker     return true;
80*9a741119SAndroid Build Coastguard Worker   }
81*9a741119SAndroid Build Coastguard Worker 
82*9a741119SAndroid Build Coastguard Worker   if (string(arg).find(kServiceInstanceFlag) == 0) {
83*9a741119SAndroid Build Coastguard Worker     // value is the part after "--hal_service_instance="
84*9a741119SAndroid Build Coastguard Worker     string value = string(arg).substr(kServiceInstanceFlag.length());
85*9a741119SAndroid Build Coastguard Worker     addHalServiceInstance(value);
86*9a741119SAndroid Build Coastguard Worker     return true;
87*9a741119SAndroid Build Coastguard Worker   }
88*9a741119SAndroid Build Coastguard Worker   return false;
89*9a741119SAndroid Build Coastguard Worker }
90*9a741119SAndroid Build Coastguard Worker 
addHalServiceInstance(const string & halServiceInstance)91*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::addHalServiceInstance(
92*9a741119SAndroid Build Coastguard Worker     const string& halServiceInstance) {
93*9a741119SAndroid Build Coastguard Worker   // hal_service_instance follows the format:
94*9a741119SAndroid Build Coastguard Worker   // package@version::interface/instance e.g.:
95*9a741119SAndroid Build Coastguard Worker   // [email protected]::IVibrator/default
96*9a741119SAndroid Build Coastguard Worker   if (!isValidInstance(halServiceInstance)) {
97*9a741119SAndroid Build Coastguard Worker     cerr << "Input instance " << halServiceInstance
98*9a741119SAndroid Build Coastguard Worker          << "does not confirm to the HAL instance format. "
99*9a741119SAndroid Build Coastguard Worker          << "Expect format: package@version::interface/instance." << endl;
100*9a741119SAndroid Build Coastguard Worker     exit(-1);
101*9a741119SAndroid Build Coastguard Worker   }
102*9a741119SAndroid Build Coastguard Worker   string halName = halServiceInstance.substr(0, halServiceInstance.find('/'));
103*9a741119SAndroid Build Coastguard Worker   string instanceName =
104*9a741119SAndroid Build Coastguard Worker       halServiceInstance.substr(halServiceInstance.find('/') + 1);
105*9a741119SAndroid Build Coastguard Worker   // Fail the process if trying to pass multiple service names for the same
106*9a741119SAndroid Build Coastguard Worker   // service instance.
107*9a741119SAndroid Build Coastguard Worker   if (halServiceInstances_.find(halName) != halServiceInstances_.end()) {
108*9a741119SAndroid Build Coastguard Worker     cerr << "Existing instance " << halName << "with name "
109*9a741119SAndroid Build Coastguard Worker          << halServiceInstances_[halName] << endl;
110*9a741119SAndroid Build Coastguard Worker     exit(-1);
111*9a741119SAndroid Build Coastguard Worker   }
112*9a741119SAndroid Build Coastguard Worker   halServiceInstances_[halName] = instanceName;
113*9a741119SAndroid Build Coastguard Worker }
114*9a741119SAndroid Build Coastguard Worker 
getServiceName(const string & instanceName,const string & defaultName)115*9a741119SAndroid Build Coastguard Worker string VtsHalHidlTargetTestEnvBase::getServiceName(const string& instanceName,
116*9a741119SAndroid Build Coastguard Worker                                                    const string& defaultName) {
117*9a741119SAndroid Build Coastguard Worker   if (halServiceInstances_.find(instanceName) != halServiceInstances_.end()) {
118*9a741119SAndroid Build Coastguard Worker     return halServiceInstances_[instanceName];
119*9a741119SAndroid Build Coastguard Worker   }
120*9a741119SAndroid Build Coastguard Worker   // Could not find the instance.
121*9a741119SAndroid Build Coastguard Worker   cerr << "Does not find service name for " << instanceName
122*9a741119SAndroid Build Coastguard Worker        << " using default name: " << defaultName << endl;
123*9a741119SAndroid Build Coastguard Worker   return defaultName;
124*9a741119SAndroid Build Coastguard Worker }
125*9a741119SAndroid Build Coastguard Worker 
registerTestService(const string & FQName)126*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::registerTestService(const string& FQName) {
127*9a741119SAndroid Build Coastguard Worker   registeredHalServices_.insert(FQName);
128*9a741119SAndroid Build Coastguard Worker }
129*9a741119SAndroid Build Coastguard Worker 
listRegisteredServices()130*9a741119SAndroid Build Coastguard Worker void VtsHalHidlTargetTestEnvBase::listRegisteredServices() {
131*9a741119SAndroid Build Coastguard Worker   for (const string& service : registeredHalServices_) {
132*9a741119SAndroid Build Coastguard Worker     cout << "hal_service: " << service << endl;
133*9a741119SAndroid Build Coastguard Worker   }
134*9a741119SAndroid Build Coastguard Worker   cout << "service_comb_mode: " << mode_ << endl;
135*9a741119SAndroid Build Coastguard Worker }
136*9a741119SAndroid Build Coastguard Worker 
isValidInstance(const string & halServiceInstance)137*9a741119SAndroid Build Coastguard Worker bool VtsHalHidlTargetTestEnvBase::isValidInstance(
138*9a741119SAndroid Build Coastguard Worker     const string& halServiceInstance) {
139*9a741119SAndroid Build Coastguard Worker   ::android::FqInstance fqInstance;
140*9a741119SAndroid Build Coastguard Worker   return (fqInstance.setTo(halServiceInstance) && fqInstance.hasPackage() &&
141*9a741119SAndroid Build Coastguard Worker           fqInstance.hasVersion() && fqInstance.hasInterface() &&
142*9a741119SAndroid Build Coastguard Worker           fqInstance.hasInstance());
143*9a741119SAndroid Build Coastguard Worker }
144*9a741119SAndroid Build Coastguard Worker 
145*9a741119SAndroid Build Coastguard Worker }  // namespace testing
146