• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
22 
23 #include <utils/Log.h>
24 
25 static constexpr const char* kListFlag = "--list_registered_services";
26 static constexpr const char* kServceInstanceFlag = "--hal_service_instance";
27 
28 using namespace std;
29 
30 namespace testing {
31 
SetUp()32 void VtsHalHidlTargetTestEnvBase::SetUp() {
33   if (!inited_) {
34     ALOGE("Environment not inited, did you forget to call init()?");
35     abort();
36   }
37   // Register services used in the test.
38   registerTestServices();
39   // For a dummy run which just print the registered hal services.
40   if (listService_) {
41     listRegisteredServices();
42     exit(0);
43   }
44   // Call the customized setup process.
45   HidlSetUp();
46 }
47 
TearDown()48 void VtsHalHidlTargetTestEnvBase::TearDown() {
49   // Call the customized teardown process.
50   HidlTearDown();
51 }
52 
init(int * argc,char ** argv)53 void VtsHalHidlTargetTestEnvBase::init(int* argc, char** argv) {
54   if (inited_) return;
55   for (int i = 1; i < *argc; i++) {
56     if (parseVtsTestOption(argv[i])) {
57       // Shift the remainder of the argv list left by one.
58       for (int j = i; j != *argc; j++) {
59         argv[j] = argv[j + 1];
60       }
61 
62       // Decrements the argument count.
63       (*argc)--;
64 
65       // We also need to decrement the iterator as we just removed an element.
66       i--;
67     }
68   }
69   inited_ = true;
70 }
71 
parseVtsTestOption(const char * arg)72 bool VtsHalHidlTargetTestEnvBase::parseVtsTestOption(const char* arg) {
73   // str and flag must not be NULL.
74   if (arg == NULL) return false;
75 
76   if (strncmp(arg, kListFlag, strlen(kListFlag)) == 0) {
77     listService_ = true;
78     return true;
79   }
80 
81   if (strncmp(arg, kServceInstanceFlag, strlen(kServceInstanceFlag)) == 0) {
82     // value is the past after "--hal_service_instance="
83     const char* value = arg + strlen(kServceInstanceFlag) + 1;
84     addHalServiceInstance(string(value));
85     return true;
86   }
87   return false;
88 }
89 
addHalServiceInstance(string halServiceInstance)90 void VtsHalHidlTargetTestEnvBase::addHalServiceInstance(
91     string halServiceInstance) {
92   // hal_service_instance follows the format:
93   // package@version::interface/service_name e.g.:
94   // android.hardware.vibrator@1.0::IVibrator/default
95   string instance_name =
96       halServiceInstance.substr(0, halServiceInstance.find('/'));
97   string service_name =
98       halServiceInstance.substr(halServiceInstance.find('/') + 1);
99   // Fail the process if trying to pass multiple service names for the same
100   // service instance.
101   if (halServiceInstances_.find(instance_name) != halServiceInstances_.end()) {
102     ALOGE("Exisitng instance %s with name %s", instance_name.c_str(),
103           halServiceInstances_[instance_name].c_str());
104     abort();
105   }
106   halServiceInstances_[instance_name] = service_name;
107 }
108 
getServiceName(string instanceName)109 string VtsHalHidlTargetTestEnvBase::getServiceName(string instanceName) {
110   if (halServiceInstances_.find(instanceName) != halServiceInstances_.end()) {
111     return halServiceInstances_[instanceName];
112   }
113   // Could not find the instance.
114   return "";
115 }
116 
registerTestService(string FQName)117 void VtsHalHidlTargetTestEnvBase::registerTestService(string FQName) {
118   registeredHalServices_.insert(FQName);
119 }
120 
listRegisteredServices()121 void VtsHalHidlTargetTestEnvBase::listRegisteredServices() {
122   for (string service : registeredHalServices_) {
123     printf("hal_service: %s\n", service.c_str());
124   }
125 }
126 
127 }  // namespace testing
128