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