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

Commit 0de345ac authored by Alessio Balsini's avatar Alessio Balsini Committed by android-build-merger
Browse files

Merge "libdm: improve ParseStatusText() and test it" am: 1553cf82 am: 98417b72 am: 23bb9e78

am: c23c1fe1

Change-Id: Ie569af12b81e5a0625ecfadd1eb968300b5247d9
parents e1e4ac74 c23c1fe1
Loading
Loading
Loading
Loading
+19 −24
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


#include "libdm/dm_target.h"
#include "libdm/dm_target.h"


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


std::string DmTargetCrypt::GetParameterString() const {
std::string DmTargetCrypt::GetParameterString() const {
    std::vector<std::string> argv = {
    std::vector<std::string> argv = {
+27 −0
Original line number Original line 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) {
TEST(libdm, CryptArgs) {
    DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
    DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
    ASSERT_EQ(target1.name(), "crypt");
    ASSERT_EQ(target1.name(), "crypt");