libdm: improve ParseStatusText() and test it

Simplify the argument parsing of DmTargetSnapshot::ParseStatusText() and
improve its robustness when dealing with wrong imputs.

Add test for DmTargetSnapshot::ParseStatusText().

Change-Id: I7f078c9ecacb402e71db49e3e7072e37cffbc234
Test: dm_test
Signed-off-by: Alessio Balsini <balsini@google.com>
This commit is contained in:
Alessio Balsini 2019-07-31 18:05:11 +01:00
parent 690c8c8f25
commit 4560856e33
2 changed files with 46 additions and 24 deletions

View file

@ -16,6 +16,7 @@
#include "libdm/dm_target.h"
#include <inttypes.h>
#include <stdio.h>
#include <sys/types.h>
@ -165,35 +166,29 @@ bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) {
}
bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) {
// Try to parse the line as it should be
int args = sscanf(text.c_str(), "%" PRIu64 "/%" PRIu64 " %" PRIu64, &status->sectors_allocated,
&status->total_sectors, &status->metadata_sectors);
if (args == 3) {
return true;
}
auto sections = android::base::Split(text, " ");
if (sections.size() == 0) {
LOG(ERROR) << "could not parse empty status";
return false;
}
// Error codes are: "Invalid", "Overflow" and "Merge failed"
if (sections.size() == 1) {
// This is probably an error code, "Invalid" is possible as is "Overflow"
// on 4.4+.
if (text == "Invalid" || text == "Overflow") {
status->error = text;
return true;
}
} else if (sections.size() == 2 && text == "Merge failed") {
status->error = text;
return true;
}
if (sections.size() != 2) {
LOG(ERROR) << "snapshot status should have two components";
return false;
}
auto sector_info = android::base::Split(sections[0], "/");
if (sector_info.size() != 2) {
LOG(ERROR) << "snapshot sector info should have two components";
return false;
}
if (!android::base::ParseUint(sections[1], &status->metadata_sectors)) {
LOG(ERROR) << "could not parse metadata sectors";
return false;
}
if (!android::base::ParseUint(sector_info[0], &status->sectors_allocated)) {
LOG(ERROR) << "could not parse sectors allocated";
return false;
}
if (!android::base::ParseUint(sector_info[1], &status->total_sectors)) {
LOG(ERROR) << "could not parse total sectors";
return false;
}
return true;
LOG(ERROR) << "could not parse snapshot status: wrong format";
return false;
}
std::string DmTargetCrypt::GetParameterString() const {

View file

@ -456,6 +456,33 @@ TEST(libdm, DmSnapshotOverflow) {
}
}
TEST(libdm, ParseStatusText) {
DmTargetSnapshot::Status status;
// Bad inputs
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("X", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456 789", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456/789", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456/789", &status));
EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 / 456 789", &status));
// Good input
EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("123/456 789", &status));
EXPECT_EQ(status.sectors_allocated, 123);
EXPECT_EQ(status.total_sectors, 456);
EXPECT_EQ(status.metadata_sectors, 789);
// Known error codes
EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Invalid", &status));
EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Merge failed", &status));
EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Overflow", &status));
}
TEST(libdm, CryptArgs) {
DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
ASSERT_EQ(target1.name(), "crypt");