Skip to content

Commit

Permalink
Merge pull request #228 from CppMicroServices/227-ldapfilter-match
Browse files Browse the repository at this point in the history
Fix LDAPFilter::Match bug
  • Loading branch information
ksubramz committed Aug 24, 2017
2 parents 5c84a22 + f6c049c commit 90fa8ce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
18 changes: 14 additions & 4 deletions framework/include/cppmicroservices/LDAPFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,18 @@ class US_Framework_EXPORT LDAPFilter {
bool Match(const ServiceReferenceBase& reference) const;

/**
* Filter using a bundle's manifest properties.
* Filter using a bundle's manifest headers.
* <p>
* This <code>LDAPFilter</code> is executed using the keys and values of the
* bundle's manifest properties. The keys are looked up in a case insensitive
* bundle's manifest headers. The keys are looked up in a case insensitive
* manner.
*
* @param bundle The bundle whose properties are used
* @param bundle The bundle whose manifest's headers are used
* in the match.
* @return <code>true</code> if the bundle's properties match this
* @return <code>true</code> if the bundle's manifest headers match this
* <code>LDAPFilter</code> <code>false</code> otherwise.
* @throws std::runtime_error If the number of keys of the bundle's manifest
* headers exceeds the value returned by std::numeric_limits<int>::max().
*/
bool Match(const Bundle& bundle) const;

Expand All @@ -142,6 +144,10 @@ class US_Framework_EXPORT LDAPFilter {
* in the match.
* @return <code>true</code> if the <code>AnyMap</code>'s values match this
* filter; <code>false</code> otherwise.
* @throws std::runtime_error If the number of keys in the <code>dictionary</code>
* exceeds the value returned by std::numeric_limits<int>::max().
* @throws std::runtime_error If the <code>dictionary</code> contains case variants
* of the same key name.
*/
bool Match(const AnyMap& dictionary) const;

Expand All @@ -154,6 +160,10 @@ class US_Framework_EXPORT LDAPFilter {
* in the match.
* @return <code>true</code> if the <code>AnyMap</code>'s values match this
* filter; <code>false</code> otherwise.
* @throws std::runtime_error If the number of keys in the <code>dictionary</code>
* exceeds the value returned by std::numeric_limits<int>::max().
* @throws std::runtime_error If the <code>dictionary</code> contains case variants
* of the same key name.
*/
bool MatchCase(const AnyMap& dictionary) const;

Expand Down
3 changes: 2 additions & 1 deletion framework/src/util/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ int Properties::Find_unlocked(const std::string& key) const
{
for (std::size_t i = 0; i < keys.size(); ++i)
{
if (ci_compare(key.c_str(), keys[i].c_str(), key.size()) == 0)
if (key.size() == keys[i].size() &&
ci_compare(key.c_str(), keys[i].c_str(), key.size()) == 0)
{
return static_cast<int>(i);
}
Expand Down
5 changes: 4 additions & 1 deletion framework/test/bundles/libLQ/resources/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"bundle.symbolic_name" : "TestBundleLQ",
"bundle.testproperty" : "YES",
"bundle.activator" : true
"bundle.activator" : true,
"hosed" : "1",
"hosedd" : "2",
"hose" : "3"
}
31 changes: 31 additions & 0 deletions framework/test/driver/LDAPQueryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ void TestLDAPFilterMatchBundle(const Bundle& bundle)
US_TEST_CONDITION(!ldapValueMismatchCase.Match(bundle), " Evaluating LDAP expr: " + ldapValueMismatchCase.ToString());
}

void TestLDAPFilterMatchNoException(const Bundle& bundle)
{
LDAPFilter ldapMatch( "(hosed=1)" );
AnyMap props(AnyMap::UNORDERED_MAP);
props["hosed"] = std::string("1");
props["hosedd"] = std::string("yum");
props["hose"] = std::string("yum");

// Testing no exception is thrown.
US_TEST_NO_EXCEPTION( ldapMatch.Match(props) );

// Testing key match
US_TEST_CONDITION(ldapMatch.Match(props) == true, "Evaluating LDAP expr: " + ldapMatch.ToString());

// Testing no exception is thrown.
US_TEST_NO_EXCEPTION( ldapMatch.Match(bundle) );

// Testing key match
US_TEST_CONDITION(ldapMatch.Match(bundle) == true, "Evaluating LDAP expr: " + ldapMatch.ToString());

AnyMap props1(AnyMap::UNORDERED_MAP);
props1["hosed"] = std::string("1");
props1["HOSED"] = std::string("yum");

// Testing exception for case variants of the same key.
US_TEST_FOR_EXCEPTION(std::runtime_error, ldapMatch.Match(props1) );
}

void TestLDAPFilterMatchServiceReferenceBase(Bundle bundle)
{
LDAPFilter ldapMatchCase( "(service.testproperty=YES)" );
Expand Down Expand Up @@ -92,6 +120,9 @@ int LDAPQueryTest(int /*argc*/, char* /*argv*/[])
US_TEST_OUTPUT(<< "Testing LDAP query of service properties:")
TestLDAPFilterMatchServiceReferenceBase(bundle);

US_TEST_OUTPUT(<< "Testing LDAP queries that no longer throw exceptions:")
TestLDAPFilterMatchNoException(bundle);

framework.Stop();

US_TEST_END()
Expand Down

0 comments on commit 90fa8ce

Please sign in to comment.