power: fix TryDifferentGovernors test

Switch to std::string and libbase to simplify the test and avoid
string manipulation bugs

Bug: 154070740
Bug: 149044096
Test: atest VtsHalPowerV1_0TargetTest
Change-Id: Ic80ca462cfeb650edcdc6420bf1b3190aa14dbe4
Signed-off-by: Connor O'Brien <connoro@google.com>
This commit is contained in:
Connor O'Brien 2020-04-21 18:09:02 -07:00
parent 08092046fb
commit 957857f19d

View file

@ -19,6 +19,8 @@
#include <cutils/properties.h>
#include <android-base/file.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <android/hardware/power/1.0/IPower.h>
#include <gtest/gtest.h>
@ -73,26 +75,16 @@ TEST_P(PowerHidlTest, SetInteractive) {
TEST_P(PowerHidlTest, TryDifferentGovernors) {
Return<void> ret;
unique_fd fd1(open(CPU_GOVERNOR_PATH, O_RDWR));
unique_fd fd2(open(AVAILABLE_GOVERNORS_PATH, O_RDONLY));
if (fd1 < 0 || fd2 < 0) {
std::string old_governor, governors;
if (!android::base::ReadFileToString(CPU_GOVERNOR_PATH, &old_governor) ||
!android::base::ReadFileToString(AVAILABLE_GOVERNORS_PATH, &governors)) {
// Files don't exist, so skip the rest of the test case
SUCCEED();
return;
}
char old_governor[80];
ASSERT_LE(0, read(fd1, old_governor, 80));
char governors[1024];
unsigned len = read(fd2, governors, 1024);
ASSERT_LE(0u, len);
governors[len] = '\0';
char *saveptr;
char *name = strtok_r(governors, " \n", &saveptr);
while (name) {
ASSERT_LE(0, write(fd1, name, strlen(name)));
auto all_governors = android::base::Split(governors, " \n");
for (const auto &governor : all_governors) {
ASSERT_TRUE(android::base::WriteStringToFile(governor, CPU_GOVERNOR_PATH));
ret = power->setInteractive(true);
ASSERT_TRUE(ret.isOk());
@ -104,11 +96,9 @@ TEST_P(PowerHidlTest, TryDifferentGovernors) {
power->powerHint(PowerHint::LAUNCH, 1);
power->powerHint(PowerHint::LAUNCH, 0);
name = strtok_r(NULL, " \n", &saveptr);
}
ASSERT_LE(0, write(fd1, old_governor, strlen(old_governor)));
ASSERT_TRUE(android::base::WriteStringToFile(old_governor, CPU_GOVERNOR_PATH));
}
// Sanity check Power::powerHint on good and bad inputs.