Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1553cf82 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "libdm: improve ParseStatusText() and test it"

parents 873d867e 4560856e
Loading
Loading
Loading
Loading
+19 −24
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "libdm/dm_target.h"

#include <inttypes.h>
#include <stdio.h>
#include <sys/types.h>

@@ -165,36 +166,30 @@ bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) {
}

bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) {
    auto sections = android::base::Split(text, " ");
    if (sections.size() == 1) {
        // This is probably an error code, "Invalid" is possible as is "Overflow"
        // on 4.4+.
        status->error = text;
    // 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;
    }
    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";
    auto sections = android::base::Split(text, " ");
    if (sections.size() == 0) {
        LOG(ERROR) << "could not parse empty status";
        return false;
    }
    if (!android::base::ParseUint(sections[1], &status->metadata_sectors)) {
        LOG(ERROR) << "could not parse metadata sectors";
        return false;
    // Error codes are: "Invalid", "Overflow" and "Merge failed"
    if (sections.size() == 1) {
        if (text == "Invalid" || text == "Overflow") {
            status->error = text;
            return true;
        }
    if (!android::base::ParseUint(sector_info[0], &status->sectors_allocated)) {
        LOG(ERROR) << "could not parse sectors allocated";
        return false;
    } else if (sections.size() == 2 && text == "Merge failed") {
        status->error = text;
        return true;
    }
    if (!android::base::ParseUint(sector_info[1], &status->total_sectors)) {
        LOG(ERROR) << "could not parse total sectors";
    LOG(ERROR) << "could not parse snapshot status: wrong format";
    return false;
}
    return true;
}

std::string DmTargetCrypt::GetParameterString() const {
    std::vector<std::string> argv = {
+27 −0
Original line number Diff line number Diff line
@@ -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");