Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "persistent_properties.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Mark Salyzyn | 9f1cf25 | 2018-11-12 12:45:59 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "util.h" |
| 27 | |
| 28 | using namespace std::string_literals; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace init { |
| 32 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 33 | PersistentProperties VectorToPersistentProperties( |
| 34 | const std::vector<std::pair<std::string, std::string>>& input_properties) { |
| 35 | PersistentProperties persistent_properties; |
| 36 | |
| 37 | for (const auto& [name, value] : input_properties) { |
| 38 | auto persistent_property_record = persistent_properties.add_properties(); |
| 39 | persistent_property_record->set_name(name); |
| 40 | persistent_property_record->set_value(value); |
| 41 | } |
| 42 | |
| 43 | return persistent_properties; |
| 44 | } |
| 45 | |
| 46 | void CheckPropertiesEqual(std::vector<std::pair<std::string, std::string>> expected, |
| 47 | const PersistentProperties& persistent_properties) { |
| 48 | for (const auto& persistent_property_record : persistent_properties.properties()) { |
| 49 | auto it = std::find_if(expected.begin(), expected.end(), |
| 50 | [persistent_property_record](const auto& entry) { |
| 51 | return entry.first == persistent_property_record.name() && |
| 52 | entry.second == persistent_property_record.value(); |
| 53 | }); |
| 54 | ASSERT_TRUE(it != expected.end()) |
Tom Cherry | a141907 | 2017-09-19 13:12:57 -0700 | [diff] [blame] | 55 | << "Found unexpected property (" << persistent_property_record.name() << ", " |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 56 | << persistent_property_record.value() << ")"; |
| 57 | expected.erase(it); |
| 58 | } |
| 59 | auto joiner = [](const std::vector<std::pair<std::string, std::string>>& vector) { |
| 60 | std::string result; |
| 61 | for (const auto& [name, value] : vector) { |
| 62 | result += " (" + name + ", " + value + ")"; |
| 63 | } |
| 64 | return result; |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 65 | }; |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 66 | EXPECT_TRUE(expected.empty()) << "Did not find expected properties:" << joiner(expected); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST(persistent_properties, EndToEnd) { |
| 70 | TemporaryFile tf; |
| 71 | ASSERT_TRUE(tf.fd != -1); |
| 72 | persistent_property_filename = tf.path; |
| 73 | |
| 74 | std::vector<std::pair<std::string, std::string>> persistent_properties = { |
| 75 | {"persist.sys.locale", "en-US"}, |
| 76 | {"persist.sys.timezone", "America/Los_Angeles"}, |
| 77 | {"persist.test.empty.value", ""}, |
| 78 | {"persist.test.new.line", "abc\n\n\nabc"}, |
| 79 | {"persist.test.numbers", "1234567890"}, |
| 80 | {"persist.test.non.ascii", "\x00\x01\x02\xFF\xFE\xFD\x7F\x8F\x9F"}, |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 81 | // We don't currently allow for non-ascii names for system properties, but this is a policy |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 82 | // decision, not a technical limitation. |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 83 | {"persist.\x00\x01\x02\xFF\xFE\xFD\x7F\x8F\x9F", "non-ascii-name"}, |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 86 | ASSERT_RESULT_OK( |
| 87 | WritePersistentPropertyFile(VectorToPersistentProperties(persistent_properties))); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 88 | |
| 89 | auto read_back_properties = LoadPersistentProperties(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 90 | CheckPropertiesEqual(persistent_properties, read_back_properties); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST(persistent_properties, AddProperty) { |
| 94 | TemporaryFile tf; |
| 95 | ASSERT_TRUE(tf.fd != -1); |
| 96 | persistent_property_filename = tf.path; |
| 97 | |
| 98 | std::vector<std::pair<std::string, std::string>> persistent_properties = { |
| 99 | {"persist.sys.timezone", "America/Los_Angeles"}, |
| 100 | }; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 101 | ASSERT_RESULT_OK( |
| 102 | WritePersistentPropertyFile(VectorToPersistentProperties(persistent_properties))); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 103 | |
| 104 | WritePersistentProperty("persist.sys.locale", "pt-BR"); |
| 105 | |
| 106 | std::vector<std::pair<std::string, std::string>> persistent_properties_expected = { |
| 107 | {"persist.sys.timezone", "America/Los_Angeles"}, |
| 108 | {"persist.sys.locale", "pt-BR"}, |
| 109 | }; |
| 110 | |
| 111 | auto read_back_properties = LoadPersistentProperties(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 112 | CheckPropertiesEqual(persistent_properties_expected, read_back_properties); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(persistent_properties, UpdateProperty) { |
| 116 | TemporaryFile tf; |
| 117 | ASSERT_TRUE(tf.fd != -1); |
| 118 | persistent_property_filename = tf.path; |
| 119 | |
| 120 | std::vector<std::pair<std::string, std::string>> persistent_properties = { |
| 121 | {"persist.sys.locale", "en-US"}, |
| 122 | {"persist.sys.timezone", "America/Los_Angeles"}, |
| 123 | }; |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 124 | ASSERT_RESULT_OK( |
| 125 | WritePersistentPropertyFile(VectorToPersistentProperties(persistent_properties))); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 126 | |
| 127 | WritePersistentProperty("persist.sys.locale", "pt-BR"); |
| 128 | |
| 129 | std::vector<std::pair<std::string, std::string>> persistent_properties_expected = { |
| 130 | {"persist.sys.locale", "pt-BR"}, |
| 131 | {"persist.sys.timezone", "America/Los_Angeles"}, |
| 132 | }; |
| 133 | |
| 134 | auto read_back_properties = LoadPersistentProperties(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 135 | CheckPropertiesEqual(persistent_properties_expected, read_back_properties); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(persistent_properties, UpdatePropertyBadParse) { |
| 139 | TemporaryFile tf; |
| 140 | ASSERT_TRUE(tf.fd != -1); |
| 141 | persistent_property_filename = tf.path; |
| 142 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 143 | ASSERT_RESULT_OK(WriteFile(tf.path, "ab")); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 144 | |
| 145 | WritePersistentProperty("persist.sys.locale", "pt-BR"); |
| 146 | |
| 147 | auto read_back_properties = LoadPersistentProperties(); |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 148 | EXPECT_GT(read_back_properties.properties().size(), 0); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 149 | |
Tom Cherry | a97faba | 2017-09-15 15:44:04 -0700 | [diff] [blame] | 150 | auto it = |
| 151 | std::find_if(read_back_properties.properties().begin(), |
| 152 | read_back_properties.properties().end(), [](const auto& entry) { |
| 153 | return entry.name() == "persist.sys.locale" && entry.value() == "pt-BR"; |
| 154 | }); |
| 155 | EXPECT_FALSE(it == read_back_properties.properties().end()); |
Tom Cherry | 16fad42 | 2017-08-04 15:59:03 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace init |
| 159 | } // namespace android |