summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Cherry2018-01-03 16:39:28 -0600
committerTom Cherry2018-01-04 16:00:11 -0600
commit919458c350563e578e2d55852fbae187756c130a (patch)
tree8a774f0884d5c7eeea328cbde9efd268d8983800 /property_service
parent2af1be4c167d8ef462418368fa1bcd3b77e5cfcd (diff)
downloadplatform-system-core-919458c350563e578e2d55852fbae187756c130a.tar.gz
platform-system-core-919458c350563e578e2d55852fbae187756c130a.tar.xz
platform-system-core-919458c350563e578e2d55852fbae187756c130a.zip
Create a host side checker for property info file correctness
Bug: 36001741 Test: verify a valid property info file and fail due to various failures Change-Id: Iadd38796aa619f87ec559fe5687bbe2009df8b2d
Diffstat (limited to 'property_service')
-rw-r--r--property_service/libpropertyinfoparser/Android.bp1
-rw-r--r--property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h1
-rw-r--r--property_service/libpropertyinfoserializer/Android.bp2
-rw-r--r--property_service/libpropertyinfoserializer/include/property_info_serializer/property_info_serializer.h4
-rw-r--r--property_service/libpropertyinfoserializer/property_info_file.cpp62
-rw-r--r--property_service/libpropertyinfoserializer/space_tokenizer.h50
-rw-r--r--property_service/property_info_checker/Android.bp14
-rw-r--r--property_service/property_info_checker/property_info_checker.cpp51
8 files changed, 185 insertions, 0 deletions
diff --git a/property_service/libpropertyinfoparser/Android.bp b/property_service/libpropertyinfoparser/Android.bp
index 3e732b51c..c046417dc 100644
--- a/property_service/libpropertyinfoparser/Android.bp
+++ b/property_service/libpropertyinfoparser/Android.bp
@@ -1,5 +1,6 @@
1cc_library_static { 1cc_library_static {
2 name: "libpropertyinfoparser", 2 name: "libpropertyinfoparser",
3 host_supported: true,
3 srcs: ["property_info_parser.cpp"], 4 srcs: ["property_info_parser.cpp"],
4 5
5 cpp_std: "experimental", 6 cpp_std: "experimental",
diff --git a/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h b/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h
index 8c3507e4f..2ee816124 100644
--- a/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h
+++ b/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h
@@ -18,6 +18,7 @@
18#define PROPERTY_INFO_PARSER_H 18#define PROPERTY_INFO_PARSER_H
19 19
20#include <stdint.h> 20#include <stdint.h>
21#include <stdlib.h>
21 22
22namespace android { 23namespace android {
23namespace properties { 24namespace properties {
diff --git a/property_service/libpropertyinfoserializer/Android.bp b/property_service/libpropertyinfoserializer/Android.bp
index 20e5e1366..bc28e8276 100644
--- a/property_service/libpropertyinfoserializer/Android.bp
+++ b/property_service/libpropertyinfoserializer/Android.bp
@@ -1,5 +1,6 @@
1cc_defaults { 1cc_defaults {
2 name: "propertyinfoserializer_defaults", 2 name: "propertyinfoserializer_defaults",
3 host_supported: true,
3 cpp_std: "experimental", 4 cpp_std: "experimental",
4 sanitize: { 5 sanitize: {
5 misc_undefined: ["signed-integer-overflow"], 6 misc_undefined: ["signed-integer-overflow"],
@@ -19,6 +20,7 @@ cc_library_static {
19 name: "libpropertyinfoserializer", 20 name: "libpropertyinfoserializer",
20 defaults: ["propertyinfoserializer_defaults"], 21 defaults: ["propertyinfoserializer_defaults"],
21 srcs: [ 22 srcs: [
23 "property_info_file.cpp",
22 "property_info_serializer.cpp", 24 "property_info_serializer.cpp",
23 "trie_builder.cpp", 25 "trie_builder.cpp",
24 "trie_serializer.cpp", 26 "trie_serializer.cpp",
diff --git a/property_service/libpropertyinfoserializer/include/property_info_serializer/property_info_serializer.h b/property_service/libpropertyinfoserializer/include/property_info_serializer/property_info_serializer.h
index f7e708ede..d2ec385d4 100644
--- a/property_service/libpropertyinfoserializer/include/property_info_serializer/property_info_serializer.h
+++ b/property_service/libpropertyinfoserializer/include/property_info_serializer/property_info_serializer.h
@@ -41,6 +41,10 @@ bool BuildTrie(const std::vector<PropertyInfoEntry>& property_info,
41 const std::string& default_context, const std::string& default_schema, 41 const std::string& default_context, const std::string& default_schema,
42 std::string* serialized_trie, std::string* error); 42 std::string* serialized_trie, std::string* error);
43 43
44void ParsePropertyInfoFile(const std::string& file_contents,
45 std::vector<PropertyInfoEntry>* property_infos,
46 std::vector<std::string>* errors);
47
44} // namespace properties 48} // namespace properties
45} // namespace android 49} // namespace android
46 50
diff --git a/property_service/libpropertyinfoserializer/property_info_file.cpp b/property_service/libpropertyinfoserializer/property_info_file.cpp
new file mode 100644
index 000000000..702f219d8
--- /dev/null
+++ b/property_service/libpropertyinfoserializer/property_info_file.cpp
@@ -0,0 +1,62 @@
1#include <property_info_serializer/property_info_serializer.h>
2
3#include <android-base/strings.h>
4
5#include "space_tokenizer.h"
6
7using android::base::Split;
8using android::base::StartsWith;
9using android::base::Trim;
10
11namespace android {
12namespace properties {
13
14bool ParsePropertyInfoLine(const std::string& line, PropertyInfoEntry* out, std::string* error) {
15 auto tokenizer = SpaceTokenizer(line);
16
17 auto property = tokenizer.GetNext();
18 if (property.empty()) {
19 *error = "Did not find a property entry in '" + line + "'";
20 return false;
21 }
22
23 auto context = tokenizer.GetNext();
24 if (context.empty()) {
25 *error = "Did not find a context entry in '" + line + "'";
26 return false;
27 }
28
29 // It is not an error to not find these, as older files will not contain them.
30 auto exact_match = tokenizer.GetNext();
31 auto schema = tokenizer.GetRemaining();
32
33 *out = {property, context, schema, exact_match == "exact"};
34 return true;
35}
36
37void ParsePropertyInfoFile(const std::string& file_contents,
38 std::vector<PropertyInfoEntry>* property_infos,
39 std::vector<std::string>* errors) {
40 // Do not clear property_infos to allow this function to be called on multiple files, with
41 // their results concatenated.
42 errors->clear();
43
44 for (const auto& line : Split(file_contents, "\n")) {
45 auto trimmed_line = Trim(line);
46 if (trimmed_line.empty() || StartsWith(trimmed_line, "#")) {
47 continue;
48 }
49
50 auto property_info_entry = PropertyInfoEntry{};
51 auto parse_error = std::string{};
52 if (!ParsePropertyInfoLine(trimmed_line, &property_info_entry, &parse_error)) {
53 errors->emplace_back(parse_error);
54 continue;
55 }
56
57 property_infos->emplace_back(property_info_entry);
58 }
59}
60
61} // namespace properties
62} // namespace android
diff --git a/property_service/libpropertyinfoserializer/space_tokenizer.h b/property_service/libpropertyinfoserializer/space_tokenizer.h
new file mode 100644
index 000000000..fba0c5841
--- /dev/null
+++ b/property_service/libpropertyinfoserializer/space_tokenizer.h
@@ -0,0 +1,50 @@
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#ifndef PROPERTY_INFO_SERIALIZER_SPACE_TOKENIZER_H
18#define PROPERTY_INFO_SERIALIZER_SPACE_TOKENIZER_H
19
20namespace android {
21namespace properties {
22
23class SpaceTokenizer {
24 public:
25 SpaceTokenizer(const std::string& string)
26 : string_(string), it_(string_.begin()), end_(string_.end()) {}
27
28 std::string GetNext() {
29 auto next = std::string();
30 while (it_ != end_ && !isspace(*it_)) {
31 next.push_back(*it_++);
32 }
33 while (it_ != end_ && isspace(*it_)) {
34 it_++;
35 }
36 return next;
37 }
38
39 std::string GetRemaining() { return std::string(it_, end_); }
40
41 private:
42 std::string string_;
43 std::string::const_iterator it_;
44 std::string::const_iterator end_;
45};
46
47} // namespace properties
48} // namespace android
49
50#endif
diff --git a/property_service/property_info_checker/Android.bp b/property_service/property_info_checker/Android.bp
new file mode 100644
index 000000000..2c9a2ba2a
--- /dev/null
+++ b/property_service/property_info_checker/Android.bp
@@ -0,0 +1,14 @@
1cc_binary {
2 name: "property_info_checker",
3 host_supported: true,
4 cpp_std: "experimental",
5 sanitize: {
6 misc_undefined: ["signed-integer-overflow"],
7 },
8 static_libs: [
9 "libpropertyinfoserializer",
10 "libpropertyinfoparser",
11 "libbase",
12 ],
13 srcs: ["property_info_checker.cpp"],
14} \ No newline at end of file
diff --git a/property_service/property_info_checker/property_info_checker.cpp b/property_service/property_info_checker/property_info_checker.cpp
new file mode 100644
index 000000000..e4f8264d4
--- /dev/null
+++ b/property_service/property_info_checker/property_info_checker.cpp
@@ -0,0 +1,51 @@
1#include <iostream>
2#include <string>
3#include <vector>
4
5#include <android-base/file.h>
6
7#include <property_info_serializer/property_info_serializer.h>
8
9using android::base::ReadFileToString;
10using android::properties::BuildTrie;
11using android::properties::ParsePropertyInfoFile;
12using android::properties::PropertyInfoEntry;
13
14int main(int argc, char** argv) {
15 if (argc < 2) {
16 std::cerr << "A list of property info files to be checked is expected on the command line"
17 << std::endl;
18 return -1;
19 }
20
21 auto property_info_entries = std::vector<PropertyInfoEntry>{};
22
23 for (int i = 1; i < argc; ++i) {
24 auto filename = argv[i];
25 auto file_contents = std::string{};
26 if (!ReadFileToString(filename, &file_contents)) {
27 std::cerr << "Could not read properties from '" << filename << "'" << std::endl;
28 return -1;
29 }
30
31 auto errors = std::vector<std::string>{};
32 ParsePropertyInfoFile(file_contents, &property_info_entries, &errors);
33 if (!errors.empty()) {
34 for (const auto& error : errors) {
35 std::cerr << "Could not read line from '" << filename << "': " << error << std::endl;
36 }
37 return -1;
38 }
39 }
40
41 auto serialized_contexts = std::string{};
42 auto build_trie_error = std::string{};
43
44 if (!BuildTrie(property_info_entries, "u:object_r:default_prop:s0", "\\s*", &serialized_contexts,
45 &build_trie_error)) {
46 std::cerr << "Unable to serialize property contexts: " << build_trie_error << std::endl;
47 return -1;
48 }
49
50 return 0;
51}