1 /**
2  * Copyright (c) 2020, 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 #include "PolicyManager.h"
18 
19 #include <aidl/android/hardware/automotive/vehicle/VehicleApPowerStateReport.h>
20 #include <android-base/file.h>
21 #include <gmock/gmock.h>
22 
23 #include <unordered_set>
24 
25 namespace android {
26 namespace frameworks {
27 namespace automotive {
28 namespace powerpolicy {
29 
30 using ::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy;
31 using ::aidl::android::frameworks::automotive::powerpolicy::PowerComponent;
32 using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport;
33 using ::testing::UnorderedElementsAre;
34 using ::tinyxml2::XML_SUCCESS;
35 using ::tinyxml2::XMLDocument;
36 
37 namespace test {
38 
39 constexpr const char* kDirPrefix = "/tests/data/";
40 
41 constexpr const char* kValidPowerPolicyXmlFile = "valid_power_policy.xml";
42 constexpr const char* kValidPowerPolicyCustomComponentsXmlFile =
43         "valid_power_policy_custom_components.xml";
44 constexpr const char* kInvalidPowerPolicyCustomComponentsXmlFile =
45         "invalid_power_policy_custom_components.xml";
46 constexpr const char* kValidPowerPolicyNoPowerPolicyGroupsXmlFile =
47         "valid_power_policy_no_power_policy_groups.xml";
48 constexpr const char* kValidPowerPolicyNoSystemPowerPolicyXmlFile =
49         "valid_power_policy_no_system_power_policy.xml";
50 constexpr const char* kValidPowerPolicyPowerPoliciesOnlyXmlFile =
51         "valid_power_policy_policies_only.xml";
52 constexpr const char* kValidPowerPolicySystemPowerPolicyOnlyXmlFile =
53         "valid_power_policy_system_power_policy_only.xml";
54 constexpr const char* kValidPowerPolicyWithDefaultPolicyGroup =
55         "valid_power_policy_default_policy_group.xml";
56 constexpr const char* kValidPowerPolicyWithInvalidDefaultPolicyGroup =
57         "invalid_system_power_policy_incorrect_default_power_policy_group_id.xml";
58 const std::vector<const char*> kInvalidPowerPolicyXmlFiles =
59         {"invalid_power_policy_incorrect_id.xml",
60          "invalid_power_policy_incorrect_othercomponent.xml",
61          "invalid_power_policy_incorrect_value.xml", "invalid_power_policy_unknown_component.xml",
62          "invalid_system_power_policy_incorrect_default_power_policy_group_id.xml"};
63 const std::vector<const char*> kInvalidPowerPolicyGroupXmlFiles =
64         {"invalid_power_policy_group_incorrect_state.xml",
65          "invalid_power_policy_group_missing_policy.xml"};
66 const std::vector<const char*> kInvalidSystemPowerPolicyXmlFiles =
67         {"invalid_system_power_policy_incorrect_component.xml",
68          "invalid_system_power_policy_incorrect_id.xml"};
69 
70 constexpr const char* kExistingPowerPolicyId = "expected_to_be_registered";
71 constexpr const char* kExistingPowerPolicyId_OtherOff = "policy_id_other_off";
72 constexpr const char* kExistingPowerPolicyId_OtherOn = "policy_id_other_on";
73 constexpr const char* kExistingPowerPolicyId_OtherUntouched = "policy_id_other_untouched";
74 constexpr const char* kExistingPowerPolicyId_OtherNone = "policy_id_other_none";
75 constexpr const char* kNonExistingPowerPolicyId = "non_existing_power_poicy_id";
76 constexpr const char* kValidPowerPolicyGroupId = "mixed_policy_group";
77 constexpr const char* kInvalidPowerPolicyGroupId = "invalid_policy_group";
78 constexpr const char* kSystemPolicyIdNoUserInteraction = "system_power_policy_no_user_interaction";
79 constexpr const char* kSystemPolicyIdinitialOn = "system_power_policy_initial_on";
80 constexpr const char* kSystemPolicyIdinitialAllOn = "system_power_policy_all_on";
81 constexpr const char* kSystemPolicyIdSuspendPrep = "system_power_policy_suspend_prep";
82 constexpr const char* kMixedPolicyGroupName = "mixed_policy_group";
83 
84 constexpr const int CUSTOM_COMPONENT_ID_1000 = 1000;
85 constexpr const int CUSTOM_COMPONENT_AUX_INPUT = 1002;
86 constexpr const int CUSTOM_COMPONENT_SPECIAL_SENSOR = 1003;
87 
88 const VehicleApPowerStateReport kExistingTransition = VehicleApPowerStateReport::WAIT_FOR_VHAL;
89 const VehicleApPowerStateReport kNonExistingTransition = static_cast<VehicleApPowerStateReport>(-1);
90 
createCarPowerPolicy(const std::string & id,const std::vector<PowerComponent> & enabledComponents,const std::vector<PowerComponent> & disabledComponents)91 CarPowerPolicy createCarPowerPolicy(const std::string& id,
92                                     const std::vector<PowerComponent>& enabledComponents,
93                                     const std::vector<PowerComponent>& disabledComponents) {
94     CarPowerPolicy policy;
95     policy.policyId = id;
96     policy.enabledComponents = enabledComponents;
97     policy.disabledComponents = disabledComponents;
98     return policy;
99 }
100 
createCarPowerPolicyWithCustomComponents(const std::string & id,const std::vector<PowerComponent> & enabledComponents,const std::vector<PowerComponent> & disabledComponents,const std::vector<int> & enabledCustomComponents,const std::vector<int> & disabledCustomComponents)101 CarPowerPolicy createCarPowerPolicyWithCustomComponents(
102         const std::string& id, const std::vector<PowerComponent>& enabledComponents,
103         const std::vector<PowerComponent>& disabledComponents,
104         const std::vector<int>& enabledCustomComponents,
105         const std::vector<int>& disabledCustomComponents) {
106     CarPowerPolicy policy;
107     policy.policyId = id;
108     policy.enabledComponents = enabledComponents;
109     policy.disabledComponents = disabledComponents;
110     policy.enabledCustomComponents = enabledCustomComponents;
111     policy.disabledCustomComponents = disabledCustomComponents;
112     return policy;
113 }
114 
115 const CarPowerPolicy kExistingPowerPolicyWithCustomComponents_OtherOff =
116         createCarPowerPolicyWithCustomComponents("policy_id_custom_other_off",
117                                                  {PowerComponent::WIFI},
118                                                  {PowerComponent::AUDIO, PowerComponent::MEDIA,
119                                                   PowerComponent::DISPLAY,
120                                                   PowerComponent::BLUETOOTH,
121                                                   PowerComponent::CELLULAR,
122                                                   PowerComponent::ETHERNET,
123                                                   PowerComponent::PROJECTION, PowerComponent::NFC,
124                                                   PowerComponent::INPUT,
125                                                   PowerComponent::VOICE_INTERACTION,
126                                                   PowerComponent::VISUAL_INTERACTION,
127                                                   PowerComponent::TRUSTED_DEVICE_DETECTION,
128                                                   PowerComponent::LOCATION,
129                                                   PowerComponent::MICROPHONE, PowerComponent::CPU},
130                                                  {CUSTOM_COMPONENT_AUX_INPUT},
131                                                  {CUSTOM_COMPONENT_ID_1000,
132                                                   CUSTOM_COMPONENT_SPECIAL_SENSOR});
133 
134 const CarPowerPolicy kExistingPowerPolicy_OtherOff_With_Custom_Components =
135         createCarPowerPolicyWithCustomComponents("policy_id_other_off", {PowerComponent::WIFI},
136                                                  {PowerComponent::AUDIO, PowerComponent::MEDIA,
137                                                   PowerComponent::DISPLAY,
138                                                   PowerComponent::BLUETOOTH,
139                                                   PowerComponent::CELLULAR,
140                                                   PowerComponent::ETHERNET,
141                                                   PowerComponent::PROJECTION, PowerComponent::NFC,
142                                                   PowerComponent::INPUT,
143                                                   PowerComponent::VOICE_INTERACTION,
144                                                   PowerComponent::VISUAL_INTERACTION,
145                                                   PowerComponent::TRUSTED_DEVICE_DETECTION,
146                                                   PowerComponent::LOCATION,
147                                                   PowerComponent::MICROPHONE, PowerComponent::CPU},
148                                                  {CUSTOM_COMPONENT_AUX_INPUT},
149                                                  {CUSTOM_COMPONENT_ID_1000,
150                                                   CUSTOM_COMPONENT_SPECIAL_SENSOR});
151 const CarPowerPolicy kExistingPowerPolicy_OtherOff =
152         createCarPowerPolicy("policy_id_other_off", {PowerComponent::WIFI},
153                              {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
154                               PowerComponent::BLUETOOTH, PowerComponent::CELLULAR,
155                               PowerComponent::ETHERNET, PowerComponent::PROJECTION,
156                               PowerComponent::NFC, PowerComponent::INPUT,
157                               PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
158                               PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
159                               PowerComponent::MICROPHONE, PowerComponent::CPU});
160 const CarPowerPolicy kExistingPowerPolicyWithCustomComponents_OtherOn =
161         createCarPowerPolicyWithCustomComponents("policy_id_other_on",
162                                                  {PowerComponent::WIFI, PowerComponent::MEDIA,
163                                                   PowerComponent::DISPLAY,
164                                                   PowerComponent::BLUETOOTH,
165                                                   PowerComponent::CELLULAR,
166                                                   PowerComponent::ETHERNET,
167                                                   PowerComponent::PROJECTION, PowerComponent::NFC,
168                                                   PowerComponent::INPUT, PowerComponent::LOCATION,
169                                                   PowerComponent::MICROPHONE, PowerComponent::CPU},
170                                                  {PowerComponent::AUDIO,
171                                                   PowerComponent::VOICE_INTERACTION,
172                                                   PowerComponent::VISUAL_INTERACTION,
173                                                   PowerComponent::TRUSTED_DEVICE_DETECTION},
174                                                  {CUSTOM_COMPONENT_ID_1000,
175                                                   CUSTOM_COMPONENT_SPECIAL_SENSOR},
176                                                  {CUSTOM_COMPONENT_AUX_INPUT});
177 const CarPowerPolicy kExistingPowerPolicy_ToBeRegistered =
178         createCarPowerPolicy("expected_to_be_registered",
179                              {PowerComponent::WIFI, PowerComponent::AUDIO, PowerComponent::MEDIA,
180                               PowerComponent::DISPLAY, PowerComponent::BLUETOOTH,
181                               PowerComponent::CELLULAR, PowerComponent::ETHERNET,
182                               PowerComponent::PROJECTION, PowerComponent::NFC,
183                               PowerComponent::INPUT, PowerComponent::VOICE_INTERACTION,
184                               PowerComponent::VISUAL_INTERACTION,
185                               PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
186                               PowerComponent::MICROPHONE, PowerComponent::CPU},
187                              {});
188 const CarPowerPolicy kExistingPowerPolicy_OtherOn =
189         createCarPowerPolicy("policy_id_other_on",
190                              {PowerComponent::MEDIA, PowerComponent::DISPLAY,
191                               PowerComponent::BLUETOOTH, PowerComponent::WIFI,
192                               PowerComponent::CELLULAR, PowerComponent::ETHERNET,
193                               PowerComponent::PROJECTION, PowerComponent::NFC,
194                               PowerComponent::INPUT, PowerComponent::LOCATION,
195                               PowerComponent::MICROPHONE, PowerComponent::CPU},
196                              {PowerComponent::AUDIO, PowerComponent::VOICE_INTERACTION,
197                               PowerComponent::VISUAL_INTERACTION,
198                               PowerComponent::TRUSTED_DEVICE_DETECTION});
199 const CarPowerPolicy kExistingPowerPolicy_OtherOn_WithOEMComponents =
200         createCarPowerPolicyWithCustomComponents("policy_id_other_on",
201                                                  {PowerComponent::MEDIA, PowerComponent::DISPLAY,
202                                                   PowerComponent::BLUETOOTH, PowerComponent::WIFI,
203                                                   PowerComponent::CELLULAR,
204                                                   PowerComponent::ETHERNET,
205                                                   PowerComponent::PROJECTION, PowerComponent::NFC,
206                                                   PowerComponent::INPUT, PowerComponent::LOCATION,
207                                                   PowerComponent::MICROPHONE, PowerComponent::CPU},
208                                                  {PowerComponent::AUDIO,
209                                                   PowerComponent::VOICE_INTERACTION,
210                                                   PowerComponent::VISUAL_INTERACTION,
211                                                   PowerComponent::TRUSTED_DEVICE_DETECTION},
212                                                  {CUSTOM_COMPONENT_ID_1000,
213                                                   CUSTOM_COMPONENT_AUX_INPUT,
214                                                   CUSTOM_COMPONENT_SPECIAL_SENSOR},
215                                                  {});
216 const CarPowerPolicy kExistingPowerPolicy_OtherUntouched =
217         createCarPowerPolicy("policy_id_other_untouched",
218                              {PowerComponent::AUDIO, PowerComponent::DISPLAY,
219                               PowerComponent::BLUETOOTH, PowerComponent::WIFI,
220                               PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
221                               PowerComponent::TRUSTED_DEVICE_DETECTION},
222                              {});
223 const CarPowerPolicy kExistingPowerPolicy_OtherUntouchedCustom =
224         createCarPowerPolicyWithCustomComponents("policy_id_other_untouched",
225                                                  {PowerComponent::AUDIO, PowerComponent::DISPLAY,
226                                                   PowerComponent::BLUETOOTH, PowerComponent::WIFI,
227                                                   PowerComponent::VOICE_INTERACTION,
228                                                   PowerComponent::VISUAL_INTERACTION,
229                                                   PowerComponent::TRUSTED_DEVICE_DETECTION},
230                                                  {}, {CUSTOM_COMPONENT_AUX_INPUT}, {});
231 const CarPowerPolicy kExistingPowerPolicy_OtherNone =
232         createCarPowerPolicy("policy_id_other_none", {PowerComponent::WIFI},
233                              {PowerComponent::AUDIO, PowerComponent::VOICE_INTERACTION,
234                               PowerComponent::VISUAL_INTERACTION,
235                               PowerComponent::TRUSTED_DEVICE_DETECTION});
236 const CarPowerPolicy& kExistingTransitionPolicy = kExistingPowerPolicy_OtherOn;
237 const CarPowerPolicy kSystemPowerPolicyAllOn =
238         createCarPowerPolicy("system_power_policy_all_on",
239                              {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
240                               PowerComponent::BLUETOOTH, PowerComponent::WIFI,
241                               PowerComponent::CELLULAR, PowerComponent::ETHERNET,
242                               PowerComponent::PROJECTION, PowerComponent::NFC,
243                               PowerComponent::INPUT, PowerComponent::VOICE_INTERACTION,
244                               PowerComponent::VISUAL_INTERACTION,
245                               PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
246                               PowerComponent::MICROPHONE, PowerComponent::CPU},
247                              {});
248 const CarPowerPolicy kSystemPowerPolicyInitialOn =
249         createCarPowerPolicy("system_power_policy_initial_on",
250                              {PowerComponent::AUDIO, PowerComponent::DISPLAY, PowerComponent::CPU},
251                              {PowerComponent::MEDIA, PowerComponent::BLUETOOTH,
252                               PowerComponent::WIFI, PowerComponent::CELLULAR,
253                               PowerComponent::ETHERNET, PowerComponent::PROJECTION,
254                               PowerComponent::NFC, PowerComponent::INPUT,
255                               PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
256                               PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
257                               PowerComponent::MICROPHONE});
258 const CarPowerPolicy kSystemPowerPolicyNoUserInteraction =
259         createCarPowerPolicy("system_power_policy_no_user_interaction",
260                              {PowerComponent::WIFI, PowerComponent::CELLULAR,
261                               PowerComponent::ETHERNET, PowerComponent::TRUSTED_DEVICE_DETECTION,
262                               PowerComponent::CPU},
263                              {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
264                               PowerComponent::BLUETOOTH, PowerComponent::PROJECTION,
265                               PowerComponent::NFC, PowerComponent::INPUT,
266                               PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
267                               PowerComponent::LOCATION, PowerComponent::MICROPHONE});
268 const CarPowerPolicy kSystemPowerPolicySuspendPrep =
269         createCarPowerPolicy("system_power_policy_suspend_prep", {},
270                              {PowerComponent::AUDIO, PowerComponent::BLUETOOTH,
271                               PowerComponent::WIFI, PowerComponent::LOCATION,
272                               PowerComponent::MICROPHONE, PowerComponent::CPU});
273 const CarPowerPolicy kModifiedSystemPowerPolicy =
274         createCarPowerPolicy("system_power_policy_no_user_interaction",
275                              {PowerComponent::BLUETOOTH, PowerComponent::WIFI,
276                               PowerComponent::CELLULAR, PowerComponent::ETHERNET,
277                               PowerComponent::NFC, PowerComponent::CPU},
278                              {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
279                               PowerComponent::PROJECTION, PowerComponent::INPUT,
280                               PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
281                               PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
282                               PowerComponent::MICROPHONE});
283 
getTestDataPath(const char * filename)284 std::string getTestDataPath(const char* filename) {
285     static std::string baseDir = android::base::GetExecutableDirectory();
286     return baseDir + kDirPrefix + filename;
287 }
288 
289 template <typename T>
printVectors(const std::vector<T> & a,const std::vector<T> & b,std::string (* toStringFn)(T))290 void printVectors(const std::vector<T>& a, const std::vector<T>& b, std::string (*toStringFn)(T)) {
291     auto vectorToString = [&toStringFn](const std::vector<T>& v) -> std::string {
292         std::ostringstream stringStream;
293         std::for_each(v.begin(), v.end(), [&stringStream, &toStringFn](const auto& element) {
294             stringStream << toStringFn(element) << " ";
295         });
296         return stringStream.str();
297     };
298     ALOGE("Vector a: %s", vectorToString(a).c_str());
299     ALOGE("Vector b: %s", vectorToString(b).c_str());
300 }
301 
302 template <typename T>
compareComponentVectors(const std::vector<T> & a,const std::vector<T> & b,std::string (* toStringFn)(T))303 bool compareComponentVectors(const std::vector<T>& a, const std::vector<T>& b,
304                              std::string (*toStringFn)(T)) {
305     int lenA = a.size();
306     int lenB = b.size();
307     if (lenA != lenB) {
308         ALOGE("Component vectors mismatch");
309         printVectors(a, b, toStringFn);
310         return false;
311     }
312     std::unordered_set<T> componentSet;
313     for (const auto component : a) {
314         componentSet.insert(component);
315     }
316     for (const auto component : b) {
317         if (componentSet.count(component) == 0) {
318             return false;
319         }
320         componentSet.erase(component);
321     }
322     return componentSet.size() == 0;
323 }
324 
compareComponents(const std::vector<PowerComponent> & a,const std::vector<PowerComponent> & b)325 bool compareComponents(const std::vector<PowerComponent>& a, const std::vector<PowerComponent>& b) {
326     return compareComponentVectors(a, b,
327                                    aidl::android::frameworks::automotive::powerpolicy::toString);
328 }
329 
intToString(int component)330 std::string intToString(int component) {
331     return std::to_string(component);
332 }
333 
compareCustomComponents(const std::optional<std::vector<int>> & optionalA,const std::optional<std::vector<int>> & optionalB)334 bool compareCustomComponents(const std::optional<std::vector<int>>& optionalA,
335                              const std::optional<std::vector<int>>& optionalB) {
336     if (!optionalA.has_value() && !optionalB.has_value()) {
337         return true;
338     }
339 
340     if (!(optionalA.has_value() && optionalB.has_value())) {  // one of the arrays is empty
341         return false;
342     }
343 
344     const auto& a = *optionalA;
345     const auto& b = *optionalB;
346 
347     return compareComponentVectors(a, b, intToString);
348 }
349 
isEqual(const CarPowerPolicy & a,const CarPowerPolicy & b)350 bool isEqual(const CarPowerPolicy& a, const CarPowerPolicy& b) {
351     if (a.policyId != b.policyId) {
352         ALOGE("isEqual  a.polictID != b.policyId %s, %s", a.policyId.c_str(), b.policyId.c_str());
353     }
354 
355     return a.policyId == b.policyId &&
356             compareComponents(a.enabledComponents, b.enabledComponents) &&
357             compareComponents(a.disabledComponents, b.disabledComponents) &&
358             compareCustomComponents(a.enabledCustomComponents, b.enabledCustomComponents) &&
359             compareCustomComponents(a.disabledCustomComponents, b.disabledCustomComponents);
360 }
361 
comparePolicies(const std::vector<CarPowerPolicy> & actualPolicies,std::unordered_map<std::string,CarPowerPolicy> expectedPolicies)362 bool comparePolicies(const std::vector<CarPowerPolicy>& actualPolicies,
363                      std::unordered_map<std::string, CarPowerPolicy> expectedPolicies) {
364     if (actualPolicies.size() != expectedPolicies.size()) return false;
365     for (const auto& policy : actualPolicies) {
366         if (expectedPolicies.count(policy.policyId) == 0) {
367             return false;
368         }
369         if (!isEqual(policy, expectedPolicies[policy.policyId])) return false;
370         expectedPolicies.erase(policy.policyId);
371     }
372     return expectedPolicies.size() == 0;
373 }
374 
checkPolicies(const PolicyManager & policyManager)375 void checkPolicies(const PolicyManager& policyManager) {
376     ASSERT_FALSE(policyManager.getPowerPolicy(kNonExistingPowerPolicyId).ok());
377 
378     // otherComponents behavior = off
379     auto policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOff);
380     ASSERT_TRUE(policyMeta.ok());
381     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherOff));
382     // otherComponents behavior = on
383     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOn);
384     ASSERT_TRUE(policyMeta.ok());
385     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherOn));
386     // otherComponents behavior = untouched
387     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherUntouched);
388     ASSERT_TRUE(policyMeta.ok());
389     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherUntouched));
390     // otherComponents behavior = none
391     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherNone);
392     ASSERT_TRUE(policyMeta.ok());
393     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherNone));
394 }
395 
checkPoliciesWithCustomComponents(const PolicyManager & policyManager)396 void checkPoliciesWithCustomComponents(const PolicyManager& policyManager) {
397     ASSERT_FALSE(policyManager.getPowerPolicy(kNonExistingPowerPolicyId).ok());
398 
399     // otherComponents behavior = off
400     auto policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOff);
401     ASSERT_TRUE(policyMeta.ok());
402     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy,
403                         kExistingPowerPolicy_OtherOff_With_Custom_Components));
404     // policy with custom components
405     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOff);
406     ASSERT_TRUE(policyMeta.ok());
407     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy,
408                         kExistingPowerPolicy_OtherOff_With_Custom_Components));
409     // otherComponents behavior = on
410     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOn);
411     ASSERT_TRUE(policyMeta.ok());
412     ASSERT_TRUE(
413             isEqual(*policyMeta->powerPolicy, kExistingPowerPolicyWithCustomComponents_OtherOn));
414     // otherComponents behavior = untouched
415     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherUntouched);
416     ASSERT_TRUE(policyMeta.ok());
417     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherUntouchedCustom));
418     // otherComponents behavior = none
419     policyMeta = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherNone);
420     ASSERT_TRUE(policyMeta.ok());
421     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kExistingPowerPolicy_OtherNone));
422 }
423 
checkPowerPolicyGroups(const PolicyManager & policyManager)424 void checkPowerPolicyGroups(const PolicyManager& policyManager) {
425     auto policy = policyManager.getDefaultPowerPolicyForState(kValidPowerPolicyGroupId,
426                                                               kExistingTransition);
427     ASSERT_TRUE(policy.ok());
428     ASSERT_TRUE(isEqual(**policy, kExistingTransitionPolicy));
429     ASSERT_FALSE(
430             policyManager
431                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kNonExistingTransition)
432                     .ok());
433 }
434 
checkSystemPowerPolicy(const PolicyManager & policyManager,const CarPowerPolicy & expectedPolicy)435 void checkSystemPowerPolicy(const PolicyManager& policyManager,
436                             const CarPowerPolicy& expectedPolicy) {
437     auto policyMeta = policyManager.getPowerPolicy(kSystemPolicyIdNoUserInteraction);
438     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, expectedPolicy));
439 }
440 
checkInvalidPolicies(const PolicyManager & policyManager)441 void checkInvalidPolicies(const PolicyManager& policyManager) {
442     ASSERT_FALSE(policyManager.getPowerPolicy(kExistingPowerPolicyId).ok());
443     ASSERT_FALSE(policyManager.getPowerPolicy(kNonExistingPowerPolicyId).ok());
444     ASSERT_FALSE(
445             policyManager
446                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kExistingTransition)
447                     .ok());
448     ASSERT_FALSE(
449             policyManager
450                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kNonExistingTransition)
451                     .ok());
452     auto policyMeta = policyManager.getPowerPolicy(kSystemPolicyIdNoUserInteraction);
453     ASSERT_TRUE(isEqual(*policyMeta->powerPolicy, kSystemPowerPolicyNoUserInteraction));
454 }
455 
assertDefaultPolicies(const PolicyManager & policyManager)456 void assertDefaultPolicies(const PolicyManager& policyManager) {
457     ASSERT_TRUE(policyManager.getPowerPolicy(kSystemPolicyIdSuspendPrep).ok());
458     ASSERT_TRUE(policyManager.getPowerPolicy(kSystemPolicyIdNoUserInteraction).ok());
459     ASSERT_TRUE(policyManager.getPowerPolicy(kSystemPolicyIdinitialOn).ok());
460     ASSERT_TRUE(policyManager.getPowerPolicy(kSystemPolicyIdinitialAllOn).ok());
461 }
462 
463 }  // namespace test
464 
465 namespace internal {
466 
467 class PolicyManagerPeer {
468 public:
PolicyManagerPeer(PolicyManager * manager)469     explicit PolicyManagerPeer(PolicyManager* manager) : mManager(manager) {
470         manager->initRegularPowerPolicy(/*override=*/true);
471         manager->initPreemptivePowerPolicy();
472     }
473 
expectValidPowerPolicyXML(const char * filename)474     void expectValidPowerPolicyXML(const char* filename) { readXmlFile(filename); }
expectInvalidPowerPolicyXML(const char * filename)475     void expectInvalidPowerPolicyXML(const char* filename) { readXmlFile(filename); }
476 
477 private:
readXmlFile(const char * filename)478     void readXmlFile(const char* filename) {
479         XMLDocument xmlDoc;
480         std::string path = test::getTestDataPath(filename);
481         xmlDoc.LoadFile(path.c_str());
482         ASSERT_TRUE(xmlDoc.ErrorID() == XML_SUCCESS);
483         mManager->readPowerPolicyFromXml(xmlDoc);
484     }
485 
486 private:
487     PolicyManager* mManager;
488 };
489 
490 }  // namespace internal
491 
492 namespace test {
493 
494 class PolicyManagerTest : public ::testing::Test {};
495 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicy)496 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicy) {
497     PolicyManager policyManager;
498     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
499     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
500 
501     checkPolicies(policyManager);
502 }
503 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicyGroup)504 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicyGroup) {
505     PolicyManager policyManager;
506     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
507     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
508 
509     checkPowerPolicyGroups(policyManager);
510 }
511 
TEST_F(PolicyManagerTest,TestValidXml_SystemPowerPolicy)512 TEST_F(PolicyManagerTest, TestValidXml_SystemPowerPolicy) {
513     PolicyManager policyManager;
514     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
515     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
516 
517     checkSystemPowerPolicy(policyManager, kModifiedSystemPowerPolicy);
518 }
519 
TEST_F(PolicyManagerTest,TestValidXml_NoPowerPolicyGroups)520 TEST_F(PolicyManagerTest, TestValidXml_NoPowerPolicyGroups) {
521     PolicyManager policyManager;
522     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
523     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyNoPowerPolicyGroupsXmlFile);
524 
525     checkPolicies(policyManager);
526     ASSERT_FALSE(
527             policyManager
528                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kExistingTransition)
529                     .ok());
530     ASSERT_FALSE(
531             policyManager
532                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kNonExistingTransition)
533                     .ok());
534     checkSystemPowerPolicy(policyManager, kModifiedSystemPowerPolicy);
535 }
536 
TEST_F(PolicyManagerTest,TestValidXml_NoSystemPowerPolicy)537 TEST_F(PolicyManagerTest, TestValidXml_NoSystemPowerPolicy) {
538     PolicyManager policyManager;
539     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
540     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyNoSystemPowerPolicyXmlFile);
541 
542     checkPolicies(policyManager);
543     checkPowerPolicyGroups(policyManager);
544     checkSystemPowerPolicy(policyManager, kSystemPowerPolicyNoUserInteraction);
545 }
546 
TEST_F(PolicyManagerTest,TestValidXml_PoliciesOnly)547 TEST_F(PolicyManagerTest, TestValidXml_PoliciesOnly) {
548     PolicyManager policyManager;
549     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
550     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyPowerPoliciesOnlyXmlFile);
551 
552     checkPolicies(policyManager);
553     ASSERT_FALSE(
554             policyManager
555                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kExistingTransition)
556                     .ok());
557     ASSERT_FALSE(
558             policyManager
559                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kNonExistingTransition)
560                     .ok());
561     checkSystemPowerPolicy(policyManager, kSystemPowerPolicyNoUserInteraction);
562 }
563 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicyCustomComponents)564 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicyCustomComponents) {
565     PolicyManager policyManager;
566     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
567     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyCustomComponentsXmlFile);
568     checkPoliciesWithCustomComponents(policyManager);
569 }
570 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicyCustomComponents_Valid)571 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicyCustomComponents_Valid) {
572     PolicyManager policyManager;
573     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
574     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyCustomComponentsXmlFile);
575     auto policy = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOff);
576     ASSERT_TRUE(policy.ok());
577 }
578 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicyCustomComponents_invalid_xml)579 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicyCustomComponents_invalid_xml) {
580     PolicyManager policyManager;
581     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
582     policyManagerPeer.expectValidPowerPolicyXML(kInvalidPowerPolicyCustomComponentsXmlFile);
583     auto policy = policyManager.getPowerPolicy(kExistingPowerPolicyId_OtherOff);
584     ASSERT_FALSE(policy.ok());
585 }
586 
TEST_F(PolicyManagerTest,TestValidXml_SystemPowerPolicyOnly)587 TEST_F(PolicyManagerTest, TestValidXml_SystemPowerPolicyOnly) {
588     PolicyManager policyManager;
589     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
590     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicySystemPowerPolicyOnlyXmlFile);
591 
592     ASSERT_FALSE(policyManager.getPowerPolicy(kExistingPowerPolicyId).ok());
593     ASSERT_FALSE(policyManager.getPowerPolicy(kNonExistingPowerPolicyId).ok());
594     ASSERT_FALSE(
595             policyManager
596                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kExistingTransition)
597                     .ok());
598     ASSERT_FALSE(
599             policyManager
600                     .getDefaultPowerPolicyForState(kValidPowerPolicyGroupId, kNonExistingTransition)
601                     .ok());
602     checkSystemPowerPolicy(policyManager, kModifiedSystemPowerPolicy);
603 }
604 
TEST_F(PolicyManagerTest,TestValidXml_TestDefaultPowerPolicyGroupId)605 TEST_F(PolicyManagerTest, TestValidXml_TestDefaultPowerPolicyGroupId) {
606     PolicyManager policyManager;
607     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
608     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyWithDefaultPolicyGroup);
609 
610     ASSERT_TRUE(policyManager.getDefaultPolicyGroup() == kMixedPolicyGroupName);
611 }
612 
TEST_F(PolicyManagerTest,TestValidXml_TestInvalidDefaultPowerPolicyGroupId)613 TEST_F(PolicyManagerTest, TestValidXml_TestInvalidDefaultPowerPolicyGroupId) {
614     PolicyManager policyManager;
615     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
616     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyWithInvalidDefaultPolicyGroup);
617 
618     ASSERT_EQ(policyManager.getDefaultPolicyGroup(), "");
619 
620     ASSERT_FALSE(
621             policyManager
622                     .getDefaultPowerPolicyForState(kInvalidPowerPolicyGroupId, kExistingTransition)
623                     .ok());
624 }
625 
TEST_F(PolicyManagerTest,TestDefaultPowerPolicies)626 TEST_F(PolicyManagerTest, TestDefaultPowerPolicies) {
627     PolicyManager policyManager;
628     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
629 
630     assertDefaultPolicies(policyManager);
631 }
632 
TEST_F(PolicyManagerTest,TestValidXml_DefaultPowerPolicies)633 TEST_F(PolicyManagerTest, TestValidXml_DefaultPowerPolicies) {
634     PolicyManager policyManager;
635     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
636     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
637 
638     assertDefaultPolicies(policyManager);
639 }
640 
TEST_F(PolicyManagerTest,TestInvalidPowerPolicyXml)641 TEST_F(PolicyManagerTest, TestInvalidPowerPolicyXml) {
642     for (const auto& filename : kInvalidPowerPolicyXmlFiles) {
643         PolicyManager policyManager;
644         internal::PolicyManagerPeer policyManagerPeer(&policyManager);
645         policyManagerPeer.expectInvalidPowerPolicyXML(filename);
646 
647         checkInvalidPolicies(policyManager);
648     }
649 }
650 
TEST_F(PolicyManagerTest,TestInvalidPowerPolicyGroupXml)651 TEST_F(PolicyManagerTest, TestInvalidPowerPolicyGroupXml) {
652     for (const auto& filename : kInvalidPowerPolicyGroupXmlFiles) {
653         PolicyManager policyManager;
654         internal::PolicyManagerPeer policyManagerPeer(&policyManager);
655         policyManagerPeer.expectInvalidPowerPolicyXML(filename);
656 
657         checkInvalidPolicies(policyManager);
658     }
659 }
660 
TEST_F(PolicyManagerTest,TestInvalidSystemPowerPolicyXml)661 TEST_F(PolicyManagerTest, TestInvalidSystemPowerPolicyXml) {
662     for (const auto& filename : kInvalidSystemPowerPolicyXmlFiles) {
663         PolicyManager policyManager;
664         internal::PolicyManagerPeer policyManagerPeer(&policyManager);
665         policyManagerPeer.expectInvalidPowerPolicyXML(filename);
666 
667         checkInvalidPolicies(policyManager);
668     }
669 }
670 
TEST_F(PolicyManagerTest,TestValidXml_PowerPolicyGroupAvailable)671 TEST_F(PolicyManagerTest, TestValidXml_PowerPolicyGroupAvailable) {
672     PolicyManager policyManager;
673     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
674     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
675 
676     ASSERT_TRUE(policyManager.isPowerPolicyGroupAvailable(kValidPowerPolicyGroupId));
677     ASSERT_FALSE(policyManager.isPowerPolicyGroupAvailable(kInvalidPowerPolicyGroupId));
678 }
679 
TEST_F(PolicyManagerTest,TestSystemPowerPolicyAllOn)680 TEST_F(PolicyManagerTest, TestSystemPowerPolicyAllOn) {
681     PolicyManager policyManager;
682     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
683     std::unordered_set<PowerComponent> enabledComponentSet;
684     auto policyMeta = policyManager.getPowerPolicy("system_power_policy_all_on");
685 
686     ASSERT_TRUE(policyMeta.ok());
687 
688     CarPowerPolicyPtr systemPolicyDefault = policyMeta->powerPolicy;
689     for (const auto& component : systemPolicyDefault->enabledComponents) {
690         enabledComponentSet.insert(component);
691     }
692     for (const auto component : ::ndk::enum_range<PowerComponent>()) {
693         if (component >= PowerComponent::MINIMUM_CUSTOM_COMPONENT_VALUE) {
694             continue;  // skip custom components
695         }
696         ASSERT_GT(enabledComponentSet.count(component), static_cast<size_t>(0));
697         enabledComponentSet.erase(component);
698     }
699 
700     ASSERT_TRUE(enabledComponentSet.empty());
701     ASSERT_TRUE(systemPolicyDefault->disabledComponents.empty());
702 }
703 
TEST_F(PolicyManagerTest,TestGetCustomComponents)704 TEST_F(PolicyManagerTest, TestGetCustomComponents) {
705     PolicyManager policyManager;
706     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
707     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyCustomComponentsXmlFile);
708 
709     const auto customComponents = policyManager.getCustomComponents();
710 
711     // Custom components defined in the XML are 1000, 1002, and 1003.
712     ASSERT_THAT(customComponents, UnorderedElementsAre(1000, 1002, 1003));
713 }
714 
TEST_F(PolicyManagerTest,TestGetRegisteredPolicies)715 TEST_F(PolicyManagerTest, TestGetRegisteredPolicies) {
716     PolicyManager policyManager;
717     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
718     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyPowerPoliciesOnlyXmlFile);
719     std::unordered_map<std::string, CarPowerPolicy>
720             expectedPolicies{{"expected_to_be_registered", kExistingPowerPolicy_ToBeRegistered},
721                              {"policy_id_other_on", kExistingPowerPolicy_OtherOn},
722                              {"policy_id_other_off", kExistingPowerPolicy_OtherOff},
723                              {"policy_id_other_untouched", kExistingPowerPolicy_OtherUntouched},
724                              {"policy_id_other_none", kExistingPowerPolicy_OtherNone},
725                              {"system_power_policy_no_user_interaction",
726                               kSystemPowerPolicyNoUserInteraction},
727                              {"system_power_policy_suspend_prep", kSystemPowerPolicySuspendPrep},
728                              {"system_power_policy_all_on", kSystemPowerPolicyAllOn},
729                              {"system_power_policy_initial_on", kSystemPowerPolicyInitialOn}};
730 
731     const auto powerPolicies = policyManager.getRegisteredPolicies();
732 
733     ASSERT_TRUE(comparePolicies(powerPolicies, expectedPolicies));
734 }
735 
TEST_F(PolicyManagerTest,TestDefinePowerPolicyGroup)736 TEST_F(PolicyManagerTest, TestDefinePowerPolicyGroup) {
737     PolicyManager policyManager;
738     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
739     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
740 
741     const auto ret = policyManager.definePowerPolicyGroup("new_policy_group",
742                                                           {"policy_id_other_off",
743                                                            "policy_id_other_untouched"});
744 
745     ASSERT_TRUE(ret.ok()) << "New policy group should be defined";
746 }
747 
TEST_F(PolicyManagerTest,TestDefinePowerPolicyGroup_doubleRegistration)748 TEST_F(PolicyManagerTest, TestDefinePowerPolicyGroup_doubleRegistration) {
749     PolicyManager policyManager;
750     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
751     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
752 
753     const auto ret = policyManager.definePowerPolicyGroup("basic_policy_group",
754                                                           {"policy_id_other_off",
755                                                            "policy_id_other_untouched"});
756 
757     ASSERT_FALSE(ret.ok()) << "Policy group with the same ID cannot be defined";
758 }
759 
TEST_F(PolicyManagerTest,TestDefinePowerPolicyGroup_unregisteredPowerPolicy)760 TEST_F(PolicyManagerTest, TestDefinePowerPolicyGroup_unregisteredPowerPolicy) {
761     PolicyManager policyManager;
762     internal::PolicyManagerPeer policyManagerPeer(&policyManager);
763     policyManagerPeer.expectValidPowerPolicyXML(kValidPowerPolicyXmlFile);
764 
765     const auto ret = policyManager.definePowerPolicyGroup("new_policy_group",
766                                                           {"policy_id_other_off",
767                                                            "unregistered_power_policy"});
768 
769     ASSERT_FALSE(ret.ok()) << "Policy group having unregistered power policy cannot be defined";
770 }
771 
772 }  // namespace test
773 }  // namespace powerpolicy
774 }  // namespace automotive
775 }  // namespace frameworks
776 }  // namespace android
777