Skip to content

Commit

Permalink
Merge pull request #14 from adammbalogh/master
Browse files Browse the repository at this point in the history
Lightsome key validation
  • Loading branch information
fire015 committed Sep 26, 2014
2 parents 9acbcc0 + 0f17c03 commit c47ccf6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
25 changes: 8 additions & 17 deletions src/Flintstone/FlintstoneDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,24 +512,15 @@ private function preserveLines($data, $reverse) {
* @return boolean
*/
private function isValidKey($key) {
if (!is_string($key)) {
throw new FlintstoneException('Key must be string');
}

// Check key length
$len = strlen($key);
if (strlen($key) > 1024) {
throw new FlintstoneException('Maximum key length is 1024 characters');
}

if ($len < 1) {
throw new FlintstoneException('No key has been set');
}

if ($len > 50) {
throw new FlintstoneException('Maximum key length is 50 characters');
}

// Check valid characters in key
if (!preg_match("/^[A-Za-z0-9_\-]+$/", $key)) {
throw new FlintstoneException('Invalid characters in key');
}

return true;
return true;
}

/**
Expand Down Expand Up @@ -621,4 +612,4 @@ public function getKeys() {
public function getFile() {
return $this->data['file'];
}
}
}
16 changes: 11 additions & 5 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ public function testSetException() {
* @expectedException Flintstone\FlintstoneException
*/
public function testInvalidKey() {
$this->db->get('test!123');
$this->db->get(1);
}

/**
* Test blank key
* @expectedException Flintstone\FlintstoneException
*/
public function testBlankKey() {
$this->db->get('');
$this->assertFalse($this->db->get(''));
}

/**
* Test complex key
*/
public function testComplexKey() {
$this->assertFalse($this->db->get('users:1:name'));
}

/**
* Test huge key
* @expectedException Flintstone\FlintstoneException
*/
public function testHugeKey() {
$this->db->get(str_repeat('a', 100));
$this->db->get(str_repeat('a', 2048));
}

/**
Expand Down Expand Up @@ -101,4 +107,4 @@ public function testGetKeys() {
$this->assertEquals(3, count($keys));
$this->assertContains('a', $keys);
}
}
}

0 comments on commit c47ccf6

Please sign in to comment.