diff options
author | Tom Cherry <tomcherry@google.com> | 2017-12-13 00:29:30 -0800 |
---|---|---|
committer | Tom Cherry <tomcherry@google.com> | 2017-12-13 16:58:33 -0800 |
commit | 26eba2879d7d7c9f51d14eba2484e25df15b8dd3 (patch) | |
tree | 590321ce07df0b330a4dee1e673f8c21dae6c93c /property_service | |
parent | 212257dbb9c877736d53cc3e786810fa7684861a (diff) | |
download | core-26eba2879d7d7c9f51d14eba2484e25df15b8dd3.tar.gz |
Always check prefix matches array at each node
Currently, we only check if the name of a given node in the trie is a
prefix match and the prefix matches array of the final node. This is
incorrect however, as the prefix matches array of intermediate nodes
may contain the prefix for a given property. This change adds that
check and test cases for this case.
Bug: 36001741
Test: new unit tests
Change-Id: I9f58ebc559f2ac591aa44df9e71205704bf18f66
Diffstat (limited to 'property_service')
3 files changed, 105 insertions, 12 deletions
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 c2114ccff..8c3507e4f 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 @@ -172,6 +172,9 @@ class PropertyInfoArea : private SerializedData { TrieNode root_node() const { return trie(header()->root_offset); } private: + void CheckPrefixMatch(const char* remaining_name, const TrieNode& trie_node, + uint32_t* context_index, uint32_t* schema_index) const; + const PropertyInfoAreaHeader* header() const { return reinterpret_cast<const PropertyInfoAreaHeader*>(data_base()); } diff --git a/property_service/libpropertyinfoparser/property_info_parser.cpp b/property_service/libpropertyinfoparser/property_info_parser.cpp index 84f8c29f8..89864dd78 100644 --- a/property_service/libpropertyinfoparser/property_info_parser.cpp +++ b/property_service/libpropertyinfoparser/property_info_parser.cpp @@ -87,6 +87,21 @@ bool TrieNode::FindChildForString(const char* name, uint32_t namelen, TrieNode* return true; } +void PropertyInfoArea::CheckPrefixMatch(const char* remaining_name, const TrieNode& trie_node, + uint32_t* context_index, uint32_t* schema_index) const { + const uint32_t remaining_name_size = strlen(remaining_name); + for (uint32_t i = 0; i < trie_node.num_prefixes(); ++i) { + auto prefix_len = trie_node.prefix(i)->namelen; + if (prefix_len > remaining_name_size) continue; + + if (!strncmp(c_string(trie_node.prefix(i)->name_offset), remaining_name, prefix_len)) { + *context_index = trie_node.prefix(i)->context_index; + *schema_index = trie_node.prefix(i)->schema_index; + return; + } + } +} + void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* context_index, uint32_t* schema_index) const { uint32_t return_context_index = ~0u; @@ -104,6 +119,10 @@ void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* contex return_schema_index = trie_node.schema_index(); } + // Check prefixes at this node. This comes after the node check since these prefixes are by + // definition longer than the node itself. + CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_schema_index); + if (sep == nullptr) { break; } @@ -128,18 +147,8 @@ void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* contex } } // Check prefix matches for prefixes not deliminated with '.' - const uint32_t remaining_name_size = strlen(remaining_name); - for (uint32_t i = 0; i < trie_node.num_prefixes(); ++i) { - auto prefix_len = trie_node.prefix(i)->namelen; - if (prefix_len > remaining_name_size) continue; - - if (!strncmp(c_string(trie_node.prefix(i)->name_offset), remaining_name, prefix_len)) { - if (context_index != nullptr) *context_index = trie_node.prefix(i)->context_index; - if (schema_index != nullptr) *schema_index = trie_node.prefix(i)->schema_index; - return; - } - } - // Return previously found '.' deliminated prefix match. + CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_schema_index); + // Return previously found prefix match. if (context_index != nullptr) *context_index = return_context_index; if (schema_index != nullptr) *schema_index = return_schema_index; return; diff --git a/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp b/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp index 5fa3463e1..b3fae268e 100644 --- a/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp +++ b/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp @@ -763,5 +763,86 @@ TEST(propertyinfoserializer, RealProperties) { } } +TEST(propertyinfoserializer, GetPropertyInfo_prefix_without_dot) { + auto property_info = std::vector<PropertyInfoEntry>{ + {"persist.radio", "1st", "1st", false}, + {"persist.radio.something.else.here", "2nd", "2nd", false}, + }; + + auto serialized_trie = std::string(); + auto build_trie_error = std::string(); + ASSERT_TRUE(BuildTrie(property_info, "default", "default", &serialized_trie, &build_trie_error)) + << build_trie_error; + + auto property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_trie.data()); + + const char* context; + const char* schema; + property_info_area->GetPropertyInfo("persist.radio", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.subproperty", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radiowords", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.long.long.long.sub.property", &context, + &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here2", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here.after", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.nothere", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); +} + +TEST(propertyinfoserializer, GetPropertyInfo_prefix_with_dot_vs_without) { + auto property_info = std::vector<PropertyInfoEntry>{ + {"persist.", "1st", "1st", false}, + {"persist.radio", "2nd", "2nd", false}, + {"persist.radio.long.property.exact.match", "3rd", "3rd", true}, + }; + + auto serialized_trie = std::string(); + auto build_trie_error = std::string(); + ASSERT_TRUE(BuildTrie(property_info, "default", "default", &serialized_trie, &build_trie_error)) + << build_trie_error; + + auto property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_trie.data()); + + const char* context; + const char* schema; + property_info_area->GetPropertyInfo("persist.notradio", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.subproperty", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radiowords", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.long.property.prefix.match", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.long.property.exact.match", &context, &schema); + EXPECT_STREQ("3rd", context); + EXPECT_STREQ("3rd", schema); +} + } // namespace properties } // namespace android |