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

Commit b62f9c5d authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Validate persistent properties file" am: 85d69af3 am: 0db7f482

parents 201cad7f 0db7f482
Loading
Loading
Loading
Loading
+21 −7
Original line number Diff line number Diff line
@@ -155,19 +155,33 @@ Result<std::string> ReadPersistentPropertyFile() {
    return *file_contents;
}

Result<PersistentProperties> ParsePersistentPropertyFile(const std::string& file_contents) {
    PersistentProperties persistent_properties;
    if (!persistent_properties.ParseFromString(file_contents)) {
        return Error() << "Unable to parse persistent property file: Could not parse protobuf";
    }
    for (auto& prop : persistent_properties.properties()) {
        if (!StartsWith(prop.name(), "persist.")) {
            return Error() << "Unable to load persistent property file: property '" << prop.name()
                           << "' doesn't start with 'persist.'";
        }
    }
    return persistent_properties;
}

}  // namespace

Result<PersistentProperties> LoadPersistentPropertyFile() {
    auto file_contents = ReadPersistentPropertyFile();
    if (!file_contents.ok()) return file_contents.error();

    PersistentProperties persistent_properties;
    if (persistent_properties.ParseFromString(*file_contents)) return persistent_properties;

    auto persistent_properties = ParsePersistentPropertyFile(*file_contents);
    if (!persistent_properties.ok()) {
        // If the file cannot be parsed in either format, then we don't have any recovery
        // mechanisms, so we delete it to allow for future writes to take place successfully.
        unlink(persistent_property_filename.c_str());
    return Error() << "Unable to parse persistent property file: Could not parse protobuf";
    }
    return persistent_properties;
}

Result<void> WritePersistentPropertyFile(const PersistentProperties& persistent_properties) {
+23 −0
Original line number Diff line number Diff line
@@ -155,5 +155,28 @@ TEST(persistent_properties, UpdatePropertyBadParse) {
    EXPECT_FALSE(it == read_back_properties.properties().end());
}

TEST(persistent_properties, RejectNonPersistProperty) {
    TemporaryFile tf;
    ASSERT_TRUE(tf.fd != -1);
    persistent_property_filename = tf.path;

    WritePersistentProperty("notpersist.sys.locale", "pt-BR");

    auto read_back_properties = LoadPersistentProperties();
    EXPECT_EQ(read_back_properties.properties().size(), 0);

    WritePersistentProperty("persist.sys.locale", "pt-BR");

    read_back_properties = LoadPersistentProperties();
    EXPECT_GT(read_back_properties.properties().size(), 0);

    auto it = std::find_if(read_back_properties.properties().begin(),
                           read_back_properties.properties().end(), [](const auto& entry) {
                               return entry.name() == "persist.sys.locale" &&
                                      entry.value() == "pt-BR";
                           });
    EXPECT_FALSE(it == read_back_properties.properties().end());
}

}  // namespace init
}  // namespace android