Skip to content

Commit

Permalink
Merge pull request #13 from thijskh/bugfix/shibmd-scope-allow-case-in…
Browse files Browse the repository at this point in the history
…sensitive-match

ShibMd:Scope matching should be case insensitive.
  • Loading branch information
Alex Rothuis committed Dec 9, 2016
2 parents a2fc5bb + def4e12 commit cf0eb85
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
Expand Up @@ -50,7 +50,7 @@ public function __construct($scope, $isRegexp = false)
Assertion::boolean($isRegexp);

if ($isRegexp) {
Assertion::validRegularExpression($scope, 'scope');
Assertion::validRegularExpression('#' . $scope . '#i', 'scope');
}

$this->scope = $scope;
Expand All @@ -66,10 +66,10 @@ public function allows($string)
Assertion::string($string, 'Scope to check should be a string, "%s" given');

if (!$this->isRegexp) {
return $this->scope === $string;
return strcasecmp($this->scope, $string) === 0;
}

$regexp = new RegularExpression($this->scope);
$regexp = new RegularExpression('#' . $this->scope . '#i');
return $regexp->matches($string);
}

Expand All @@ -79,7 +79,7 @@ public function allows($string)
*/
public function equals(ShibbolethMetadataScope $other)
{
return ($this->scope === $other->scope && $this->isRegexp === $other->isRegexp);
return (strcasecmp($this->scope, $other->scope) === 0 && $this->isRegexp === $other->isRegexp);
}

public static function deserialize($data)
Expand Down
Expand Up @@ -18,7 +18,7 @@ public function all_elements_must_be_a_shibboleth_metadata_scope()
{
$elements = array(
ShibbolethMetadataScope::literal('foobar'),
ShibbolethMetadataScope::regexp('/abc/i'),
ShibbolethMetadataScope::regexp('abc'),
new stdClass()
);

Expand All @@ -33,7 +33,7 @@ public function a_string_is_in_scope_if_it_matches_at_least_one_scope_in_the_lis
{
$list = new ShibbolethMetadataScopeList(array(
ShibbolethMetadataScope::literal('first'),
ShibbolethMetadataScope::regexp('/a{3,4}/i')
ShibbolethMetadataScope::regexp('a{3,4}')
));

$this->assertTrue($list->inScope('first'));
Expand All @@ -57,7 +57,7 @@ public function a_list_can_only_determine_if_strings_are_in_scope($invalidScope)
if (!$list) {
$list = new ShibbolethMetadataScopeList(array(
ShibbolethMetadataScope::literal('first'),
ShibbolethMetadataScope::regexp('/a{3,4}/i')
ShibbolethMetadataScope::regexp('a{3,4}')
));
}

Expand All @@ -71,7 +71,7 @@ public function a_list_can_only_determine_if_strings_are_in_scope($invalidScope)
public function adding_scope_to_a_list_creates_a_new_list()
{
$firstScope = ShibbolethMetadataScope::literal('first');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope));
$newList = $list->add($secondScope);
Expand All @@ -92,7 +92,7 @@ public function adding_scope_to_a_list_creates_a_new_list()
public function presence_of_a_scope_can_be_tested()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');
$notInList = ShibbolethMetadataScope::literal('not in list');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));
Expand All @@ -109,7 +109,7 @@ public function presence_of_a_scope_can_be_tested()
public function the_index_of_a_scope_can_be_retrieved()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');
$notInList = ShibbolethMetadataScope::literal('not in list');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));
Expand All @@ -126,7 +126,7 @@ public function the_index_of_a_scope_can_be_retrieved()
public function a_scope_can_be_retrieved_by_index()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -144,7 +144,7 @@ public function a_scope_can_be_retrieved_by_index()
public function index_to_retrieve_the_element_of_must_be_an_integer($invalidArgument)
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -160,7 +160,7 @@ public function index_to_retrieve_the_element_of_must_be_an_integer($invalidArgu
public function an_exception_is_thrown_when_attempting_to_get_an_element_with_a_negative_index()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -176,7 +176,7 @@ public function an_exception_is_thrown_when_attempting_to_get_an_element_with_a_
public function an_exception_is_thrown_when_attempting_to_get_an_element_with_an_index_larger_than_the_list_size()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -190,11 +190,11 @@ public function an_exception_is_thrown_when_attempting_to_get_an_element_with_an
public function a_scope_can_be_searched_for()
{
$predicate = function (ShibbolethMetadataScope $scope) {
return $scope->equals(ShibbolethMetadataScope::regexp('/abc/i'));
return $scope->equals(ShibbolethMetadataScope::regexp('abc'));
};

$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -208,12 +208,12 @@ public function a_scope_can_be_searched_for()
public function find_returns_the_first_matching_element()
{
$predicate = function (ShibbolethMetadataScope $scope) {
return $scope->equals(ShibbolethMetadataScope::regexp('/abc/i'));
return $scope->equals(ShibbolethMetadataScope::regexp('abc'));
};

$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$notReturned = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');
$notReturned = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope, $notReturned));

Expand All @@ -231,7 +231,7 @@ public function null_is_returned_when_no_match_is_found()
};

$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -250,7 +250,7 @@ public function null_is_returned_when_no_match_is_found()
public function find_predicate_must_be_a_callable($notCallable)
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');

$list = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));

Expand All @@ -264,7 +264,7 @@ public function find_predicate_must_be_a_callable($notCallable)
public function lists_are_only_equal_when_containing_the_same_elements_in_the_same_order()
{
$firstScope = ShibbolethMetadataScope::literal('in scope');
$secondScope = ShibbolethMetadataScope::regexp('/abc/i');
$secondScope = ShibbolethMetadataScope::regexp('abc');
$thirdsScope = ShibbolethMetadataScope::literal('also in scope');

$base = new ShibbolethMetadataScopeList(array($firstScope, $secondScope));
Expand All @@ -286,7 +286,7 @@ public function lists_are_only_equal_when_containing_the_same_elements_in_the_sa
public function a_list_can_be_iterated_over()
{
$literal = ShibbolethMetadataScope::literal('foo');
$regexp = ShibbolethMetadataScope::regexp('/a{3,4}/i');
$regexp = ShibbolethMetadataScope::regexp('a{3,4}');

$list = new ShibbolethMetadataScopeList(array($literal, $regexp));

Expand Down Expand Up @@ -315,7 +315,7 @@ public function a_list_can_be_iterated_over()
public function a_list_can_be_counted()
{
$literal = ShibbolethMetadataScope::literal('foo');
$regexp = ShibbolethMetadataScope::regexp('/a{3,4}/i');
$regexp = ShibbolethMetadataScope::regexp('a{3,4}');

$list = new ShibbolethMetadataScopeList(array($literal, $regexp));

Expand All @@ -329,7 +329,7 @@ public function a_list_can_be_counted()
public function a_list_exposes_an_array_containing_its_elements()
{
$literal = ShibbolethMetadataScope::literal('foo');
$regexp = ShibbolethMetadataScope::regexp('/a{3,4}/i');
$regexp = ShibbolethMetadataScope::regexp('a{3,4}');

$list = new ShibbolethMetadataScopeList(array($literal, $regexp));

Expand All @@ -347,7 +347,7 @@ public function a_list_exposes_an_array_containing_its_elements()
public function deserializing_a_serialized_shibboleth_metadata_scope_list_results_in_an_equal_value_object()
{
$literal = ShibbolethMetadataScope::literal('foo');
$regexp = ShibbolethMetadataScope::regexp('/a{3,4}/i');
$regexp = ShibbolethMetadataScope::regexp('a{3,4}');

$original = new ShibbolethMetadataScopeList(array($literal, $regexp));
$deserialized = ShibbolethMetadataScopeList::deserialize($original->serialize());
Expand Down Expand Up @@ -377,7 +377,7 @@ public function deserialization_requires_an_array($notArray)
public function a_list_can_be_cast_to_string()
{
$literal = ShibbolethMetadataScope::literal('foo');
$regexp = ShibbolethMetadataScope::regexp('/a{3,4}/i');
$regexp = ShibbolethMetadataScope::regexp('a{3,4}');

$list = new ShibbolethMetadataScopeList(array($literal, $regexp));

Expand Down
Expand Up @@ -60,7 +60,7 @@ public function a_scope_can_only_be_created_using_a_non_empty_string($invalidVal
*/
public function a_regexp_scope_can_only_be_created_with_a_boolean_argument($notBoolean)
{
new ShibbolethMetadataScope('/scope/', $notBoolean);
new ShibbolethMetadataScope('scope', $notBoolean);
}

/**
Expand Down Expand Up @@ -91,13 +91,26 @@ public function a_literal_scopes_allows_only_exact_matches()
$this->assertFalse($scope->allows('abcdef'));
}

/**
* @test
* @group metadata
*/
public function a_literal_scopes_allows_case_insensitive_matches()
{
$scope = new ShibbolethMetadataScope('abcde');
$this->assertTrue($scope->allows('abCde'));

$scope = new ShibbolethMetadataScope('abcDe');
$this->assertTrue($scope->allows('abcde'));
}

/**
* @test
* @group metadata
*/
public function a_regex_scope_allows_matches()
{
$scope = new ShibbolethMetadataScope('/a{3,4}/i', true);
$scope = new ShibbolethMetadataScope('a{3,4}', true);

$this->assertTrue($scope->allows('aaa'));
$this->assertTrue($scope->allows('aAAa'));
Expand All @@ -111,8 +124,8 @@ public function a_regex_scope_allows_matches()
*/
public function a_literal_scope_is_not_equal_to_a_regexp_scope()
{
$literalScope = new ShibbolethMetadataScope('/notEqual/');
$regexpScope = new ShibbolethMetadataScope('/notEqual/', true);
$literalScope = new ShibbolethMetadataScope('notEqual');
$regexpScope = new ShibbolethMetadataScope('notEqual', true);

$this->assertFalse($literalScope->equals($regexpScope));
}
Expand Down Expand Up @@ -143,9 +156,9 @@ public function literal_scopes_are_compared_based_on_the_literal()
*/
public function regex_scopes_are_compared_based_on_the_regexp_given()
{
$base = new ShibbolethMetadataScope('/[0-9a-z]{8}/', true);
$theSame = new ShibbolethMetadataScope('/[0-9a-z]{8}/', true);
$different = new ShibbolethMetadataScope('/different/', true);
$base = new ShibbolethMetadataScope('[0-9a-z]{8}', true);
$theSame = new ShibbolethMetadataScope('[0-9a-z]{8}', true);
$different = new ShibbolethMetadataScope('different', true);

$this->assertTrue(
$base->equals($theSame),
Expand All @@ -166,7 +179,7 @@ public function regex_scopes_are_compared_based_on_the_regexp_given()
public function deserializing_a_serialized_shibmd_scope_results_in_an_equal_value_object()
{
$literal = new ShibbolethMetadataScope('foobar');
$regexp = new ShibbolethMetadataScope('/abc/i', true);
$regexp = new ShibbolethMetadataScope('abc', true);

$deserializedLiteral = ShibbolethMetadataScope::deserialize($literal->serialize());
$deserializedRegexp = ShibbolethMetadataScope::deserialize($regexp->serialize());
Expand Down

0 comments on commit cf0eb85

Please sign in to comment.