1 /*
2  * Copyright (C) 2017 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 "VtsHalHidlTargetTestEnvBase"
18 
19 #include "VtsHalHidlTargetTestEnvBase.h"
20 
21 #include <iostream>
22 #include <string>
23 
24 #include <hidl-util/FqInstance.h>
25 
26 static const std::string kListFlag = "--list_registered_services";
27 static const std::string kServiceInstanceFlag = "--hal_service_instance=";
28 
29 using namespace std;
30 
31 namespace testing {
32 
SetUp()33 void VtsHalHidlTargetTestEnvBase::SetUp() {
34   if (!inited_) {
35     cerr << "Environment not inited, did you forget to call init()?" << endl;
36     exit(-1);
37   }
38   // Register services used in the test.
39   registerTestServices();
40   // For a no-op run which just print the registered hal services.
41   if (listService_) {
42     listRegisteredServices();
43     exit(0);
44   }
45   // Call the customized setup process.
46   HidlSetUp();
47 }
48 
TearDown()49 void VtsHalHidlTargetTestEnvBase::TearDown() {
50   // Call the customized teardown process.
51   HidlTearDown();
52 }
53 
init(int * argc,char ** argv)54 void VtsHalHidlTargetTestEnvBase::init(int* argc, char** argv) {
55   if (inited_) return;
56   for (int i = 1; i < *argc; i++) {
57     if (parseVtsTestOption(argv[i])) {
58       // Shift the remainder of the argv list left by one.
59       for (int j = i; j != *argc; j++) {
60         argv[j] = argv[j + 1];
61       }
62 
63       // Decrements the argument count.
64       (*argc)--;
65 
66       // We also need to decrement the iterator as we just removed an element.
67       i--;
68     }
69   }
70   inited_ = true;
71 }
72 
parseVtsTestOption(const char * arg)73 bool VtsHalHidlTargetTestEnvBase::parseVtsTestOption(const char* arg) {
74   // arg must not be NULL.
75   if (arg == NULL) return false;
76 
77   if (arg == kListFlag) {
78     listService_ = true;
79     return true;
80   }
81 
82   if (string(arg).find(kServiceInstanceFlag) == 0) {
83     // value is the part after "--hal_service_instance="
84     string value = string(arg).substr(kServiceInstanceFlag.length());
85     addHalServiceInstance(value);
86     return true;
87   }
88   return false;
89 }
90 
addHalServiceInstance(const string & halServiceInstance)91 void VtsHalHidlTargetTestEnvBase::addHalServiceInstance(
92     const string& halServiceInstance) {
93   // hal_service_instance follows the format:
94   // package@version::interface/instance e.g.:
95   // android.hardware.vibrator@1.0::IVibrator/default
96   if (!isValidInstance(halServiceInstance)) {
97     cerr << "Input instance " << halServiceInstance
98          << "does not confirm to the HAL instance format. "
99          << "Expect format: package@version::interface/instance." << endl;
100     exit(-1);
101   }
102   string halName = halServiceInstance.substr(0, halServiceInstance.find('/'));
103   string instanceName =
104       halServiceInstance.substr(halServiceInstance.find('/') + 1);
105   // Fail the process if trying to pass multiple service names for the same
106   // service instance.
107   if (halServiceInstances_.find(halName) != halServiceInstances_.end()) {
108     cerr << "Existing instance " << halName << "with name "
109          << halServiceInstances_[halName] << endl;
110     exit(-1);
111   }
112   halServiceInstances_[halName] = instanceName;
113 }
114 
getServiceName(const string & instanceName,const string & defaultName)115 string VtsHalHidlTargetTestEnvBase::getServiceName(const string& instanceName,
116                                                    const string& defaultName) {
117   if (halServiceInstances_.find(instanceName) != halServiceInstances_.end()) {
118     return halServiceInstances_[instanceName];
119   }
120   // Could not find the instance.
121   cerr << "Does not find service name for " << instanceName
122        << " using default name: " << defaultName << endl;
123   return defaultName;
124 }
125 
registerTestService(const string & FQName)126 void VtsHalHidlTargetTestEnvBase::registerTestService(const string& FQName) {
127   registeredHalServices_.insert(FQName);
128 }
129 
listRegisteredServices()130 void VtsHalHidlTargetTestEnvBase::listRegisteredServices() {
131   for (const string& service : registeredHalServices_) {
132     cout << "hal_service: " << service << endl;
133   }
134   cout << "service_comb_mode: " << mode_ << endl;
135 }
136 
isValidInstance(const string & halServiceInstance)137 bool VtsHalHidlTargetTestEnvBase::isValidInstance(
138     const string& halServiceInstance) {
139   ::android::FqInstance fqInstance;
140   return (fqInstance.setTo(halServiceInstance) && fqInstance.hasPackage() &&
141           fqInstance.hasVersion() && fqInstance.hasInterface() &&
142           fqInstance.hasInstance());
143 }
144 
145 }  // namespace testing
146