Skip to content

Commit

Permalink
Limit accepted name lengths in identity class
Browse files Browse the repository at this point in the history
  • Loading branch information
rmhdev committed Jan 9, 2016
1 parent f23d890 commit 0a7edba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Identicon/Identity/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

final class Identity
{
const MAX_LENGTH = 255;

private $name;
private $options;
private $hash;
Expand Down Expand Up @@ -43,7 +45,13 @@ private function processLength($length)

private function initializeIdentification($identification)
{
$this->name = mb_convert_case($identification, MB_CASE_LOWER, "UTF-8");
$name = mb_convert_case($identification, MB_CASE_LOWER, "UTF-8");
if (self::MAX_LENGTH < mb_strlen($name)) {
throw new OutOfBoundsException(
sprintf('Identity name is too long (%d), max is %d', mb_strlen($name), self::MAX_LENGTH)
);
}
$this->name = $name;
$this->hash = sha1($this->name);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/Identicon/Tests/Identity/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testIdentityCustomizedLength()
*/
public function testLengthOutOfBoundsException($length)
{
$identity = new Identity("name", array("length" => $length));
new Identity("name", array("length" => $length));
}

public function lengthOutOfBoundsProvider()
Expand Down Expand Up @@ -184,4 +184,11 @@ public function testGetName()
$this->assertEquals("idëntificaçióñ", $identityComplex->getName());
}

/**
* @expectedException Identicon\Exception\OutOfBoundsException
*/
public function testTooLongNameShouldThrowException()
{
new Identity(str_repeat("abcdefghij", 25) . "123456");
}
}

0 comments on commit 0a7edba

Please sign in to comment.